func (c *Connection) ProcessPacket(_packet *pnet.Packet) { header := _packet.ReadUint8() switch header { case pnet.HEADER_LOGIN: c.SendPlayerData() case pnet.HEADER_WALK: c.ReceivePlayerWalk(_packet) case pnet.HEADER_TURN: c.ReceivePlayerTurn(_packet) case pnet.HEADER_REFRESHWORLD: c.ReceiveRefreshWorld() case pnet.HEADER_FRIENDUPDATE: c.ReceiveFriendUpdate(_packet) case pnet.HEADER_CHAT: c.ReceiveChat(_packet) case pnet.HEADER_DIALOG: c.ReceiveDialogAnswer(_packet) case pnet.HEADER_QUESTUPDATE: c.ReceiveQuestUpdate(_packet) } }
// Check login credentials before creating a player object func (s *Server) ParseFirstMessage(conn *websocket.Conn, packet *pnet.Packet) { // Read packet header header := packet.ReadUint8() if header != pnet.HEADER_LOGIN { return } // Make new Connection object to handle net.Conn connection := NewConnection(conn) // Parse packet // We can use the same packet for sending the return status firstMessage := &pnetmsg.LoginMessage{} firstMessage.ReadPacket(packet) if g_game.State == GAME_STATE_CLOSING || g_game.State == GAME_STATE_CLOSED { firstMessage.Status = pnetmsg.LOGINSTATUS_SERVERCLOSED } else if firstMessage.ClientVersion < s.ClientVersion { firstMessage.Status = pnetmsg.LOGINSTATUS_WRONGVERSION } else { // Load account info ret, playerId := s.CheckAccountInfo(firstMessage.Username, firstMessage.Password) if !ret { firstMessage.Status = pnetmsg.LOGINSTATUS_WRONGACCOUNT logger.Printf("[LOGIN] %s - Wrong Account - %s", firstMessage.Username, firstMessage.Password) } else { // Account exists and password is correct logger.Println("LoadPlayerProfile: 0") ret, player := s.LoadPlayerProfile(playerId) if !ret || player == nil { firstMessage.Status = pnetmsg.LOGINSTATUS_FAILPROFILELOAD logger.Printf("[LOGIN] %s - Failed to load profile", firstMessage.Username) } else if player.Conn != nil { firstMessage.Status = pnetmsg.LOGINSTATUS_ALREADYLOGGEDIN logger.Println("[LOGIN] %s - Already logged in", firstMessage.Username) } else { firstMessage.Status = pnetmsg.LOGINSTATUS_READY logger.Printf("[LOGIN] %d - %v logged in", player.GetUID(), player.GetName()) // Assign Connection to Player object player.SetConnection(connection) // AddCreature sends few messages to the player so, // quickly sending the status message before adding the player. connection.SendMessage(firstMessage) g_game.AddCreature(player) player.Conn.HandleConnection() return } } } connection.SendMessage(firstMessage) }
func (s *Server) ParseMessage(_socket net.Conn, _packet *pnet.Packet) { // Read packet header header := _packet.ReadUint8() if header != pnet.HEADER_LOGIN { return } firstMessage := NewLoginMessage() firstMessage.ReadPacket(_packet) ret, iduser := s.checkCredentials(firstMessage.Username, firstMessage.Password) if !ret { firstMessage.Status = LOGINSTATUS_WRONGACCOUNT } else { s.loadCharacters(firstMessage, iduser) } // Send packet back to user packet, _ := firstMessage.WritePacket() packet.SetHeader() _socket.Write(packet.Buffer[0:packet.MsgSize]) }