Пример #1
0
        private void OnClientAuthenticated(NetPeer peer, NetEventArgs args)
        {
            PacketAuth message = PacketBase.Read <PacketAuth>((NetDataReader)(args.Data));

            PlayerManager.SetIdentity(new Player(message.ID, username));
            NetEventManager.Trigger(NetEventType.Authenticated, null);
        }
Пример #2
0
        private void OnConnect(NetPeer peer, NetEventArgs args)
        {
            NetEventManager.Trigger(NetEventType.Connected, args);
            PacketBase message = new PacketAuth(0, username, 0);

            GameClient.CurrentState = ClientState.Connected;
            GameClient.Send(message);
        }
Пример #3
0
        /// <summary>
        /// Handler for authentication requests
        /// </summary>
        /// <param name="client">client the requested to be authenticated</param>
        /// <param name="args">arguments containing raw data</param>
        private void OnClientAuthRequest(NetPeer client, NetEventArgs args)
        {
            PacketAuth packet = PacketBase.Read <PacketAuth>((NetDataReader)(args.Data));

            //refuse if ID not 0
            if (packet.Sender != 0)
            {
                UnityEngine.Debug.Log("[SERVER] Client already has an ID");
                server.Disconnect(client, "Authentication failed.");
                return;
            }
            PlayerBuffer players = server.Players;

            //refuse if there's no available slot
            if (players.IsFull())
            {
                UnityEngine.Debug.Log("[SERVER] players list full");
                server.Disconnect(client, "Server full.");
                return;
            }

            //refuse if the ID getter fails (it shouldn't though)
            int id;

            if (!players.FirstEmptyID(out id))
            {
                UnityEngine.Debug.Log("[SERVER] no available id");
                server.Disconnect(client, "Authentication failed.");
                return;
            }
            //otherwise welcome new player
            Player     newPlayer = new Player(id, packet.Message);
            PacketAuth response  = new PacketAuth(server.ID, "WLCM", newPlayer.ID);

            GameServer.Send(response, client);

            //send already connected players to the new client
            foreach (Player p in players)
            {
                PacketBase playerPacket = new PacketPlayerEnter(server.ID, p);
                GameServer.Send(playerPacket, client);
            }

            //send game info
            PacketGameInfo gameInfo = new PacketGameInfo(server.ID, 0);

            GameServer.Send(gameInfo, client);

            //send new player to everybody, add him to the buffer
            GameServer.Send(new PacketPlayerEnter(server.ID, newPlayer), null);
            server.AddPlayer(client, newPlayer);
        }