Ejemplo n.º 1
0
// ConnectHandler method for accept new connection from socket
func ConnectHandler(buffers *core.Buffers, app Application, c net.Conn) {
	defer core.ErrorNetworkHandler(c)

	var (
		buffer      *bytes.Buffer      = bytes.NewBuffer([]byte{})
		player      *entitie.Player    = entitie.NewPlayer(buffers)
		database    *database.Database = database.NewDatabase(&app.DatabaseInfo)
		packetAlloc *packet.Packet     = packet.NewPacket(player, database)
	)

	// Once send first a packet
	packet, err := packetAlloc.Encode(support.OP_SERVER_CHAPSTR)
	if err != nil {
		log.WithError(err).Error("Error in packet encode")
	}

	buffers.GetWC() <- packet

	for getBytes := range buffers.GetRC() {
		buffer.Reset()
		buffer.Write(getBytes)

		log.WithField("bytes", fmt.Sprintf("% x", buffer.Bytes())).Info("Print message from client")

		// Ping <-> pong
		if buffer.Len() <= 2 {
			buffers.GetWC() <- []byte{0x00, 0x02}
			continue
		}

		opcodes, err := packetAlloc.Decode(buffer.Bytes())
		if err != nil {
			log.WithError(err).Error("Error in packet decode")
			return
		}

		if len(opcodes) == 0 {
			continue
		}

		for _, opcode := range opcodes {
			response, err := packetAlloc.Encode(opcode)
			if err != nil {
				log.WithError(err).Error("Error in packet encode")
				break
			}

			buffers.GetWC() <- response
		}

		if player.Error != nil {
			// Before disconnect
			time.Sleep(time.Second)

			log.WithError(player.Error).Error("Client was rejected by God!")
			return
		}
	}
}
Ejemplo n.º 2
0
func (i *IncomingAuth) Packet(db *database.Database) (func(netes network.Netes), func(player *entitie.Player) []int) {
	handler := func(netes network.Netes) {
		netes.ReadString(&i.Key)
		netes.ReadString(&i.Login)
		netes.ReadString(&i.Password)
		netes.ReadString(&i.MAC)
		netes.ReadUint16(&i.IsCheat)
		netes.ReadUint16(&i.ClientVersion)

		return
	}

	process := func(player *entitie.Player) []int {
		if player.Error != nil {
			// Here was gopher...
		}

		user, err := db.User().GetByName(i.Login)
		if err != nil {
			player.Error = &support.PlayerIsNotFound

			return []int{support.OP_SERVER_LOGIN}
		}

		encryptPassword, err := crypt.EncryptPassword(strings.ToUpper(user.Password[:24]), player.Time)
		if err != nil {
			player.Error = &support.CustomError{0, err}

			return []int{support.OP_SERVER_LOGIN}
		}

		if encryptPassword != i.Password {
			player.Error = &support.PasswordIncorrect

			return []int{support.OP_SERVER_LOGIN}
		}

		if user.IsActive {
			player.Error = &support.PlayerInGame

			return []int{support.OP_SERVER_LOGIN}
		}

		user.IsActive = true
		err = user.Update(user.ID)
		if err != nil {
			player.Error = &support.CustomError{0, err}

			return []int{support.OP_SERVER_LOGIN}
		}

		player.ID = user.ID

		return []int{support.OP_SERVER_LOGIN}
	}

	return handler, process
}
Ejemplo n.º 3
0
func (i *IncomingExit) Packet(db *database.Database) (func(netes network.Netes), func(player *entitie.Player) []int) {
	handler := func(netes network.Netes) {
		return
	}

	process := func(player *entitie.Player) []int {
		user := db.User()
		user.IsActive = false
		err := user.Update(player.ID)
		if err != nil {
			player.Error = &support.CustomError{0, err}
		}

		player.Buffers.GetEC() <- struct{}{}

		return nil
	}

	return handler, process
}