예제 #1
0
        public static void ReadBytes(Client client, byte[] buffer, int length)
        {
            MemoryStream stream = new MemoryStream(buffer);
            BinaryReader reader = new BinaryReader(stream);

            while (reader.BaseStream.Position != length)
            {
                byte packetId = reader.ReadByte();
                if (packets.ContainsKey(packetId))
                {
                    try
                    {
                        PacketBase packet = (PacketBase)Activator.CreateInstance(packets[packetId]);
                        packet.SetReader(reader, stream);
                        packet.Read(client);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("No packet with id: " + packetId);
                }
            }
        }
예제 #2
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);
        }
예제 #3
0
        private void OnReceiveTurnData(NetPeer peer, NetEventArgs args)
        {
            PacketTurnData message = PacketBase.Read <PacketTurnData>((NetDataReader)(args.Data));

            //UnityEngine.Debug.Log("Received data from player " + message.Sender);
            lockstep.Insert(message.Commands, message.Turn, message.Sender);
        }
예제 #4
0
        public void HandleMessage(ref Client client, byte[] msgBytes)
        {
            var packetBase = new PacketBase();

            using (var stream = new MemoryStream(msgBytes))
            {
                try
                {
                    var reader = new BinaryReader(stream);

                    packetBase.Read(reader);
                }
                catch (Exception e)
                {
#if DEBUG
                    throw;
#else
                    Trace.WriteLine(string.Format("[IncomingMessageHandler.HandleMessage] - " +
                                                  "Invalid packet from client! Deserialization failed: {0}, Trace: {1}",
                                                  e.Message,
                                                  e.StackTrace));
#endif
                }
            }

            var handler = PacketHandlerFactory.GetPacketHandler(packetBase);

            if (handler != null)
            {
                handler.DoActions(ref client);
            }
        }
예제 #5
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);
        }
예제 #6
0
        public async Task WaitForData()
        {
            var mainPageViewModel = MainPageViewModel.GetInstance();

            using (var dr = new DataReader(ServerProxy.TcpSocket.InputStream))
            {
                while (mainPageViewModel.ConnectionStatus)
                {
                    var stringHeader = await dr.LoadAsync(4);

                    if (stringHeader == 0)
                    {
                        mainPageViewModel.ConnectionStatus = false;
                        return;
                    }

                    int  messageLength = dr.ReadInt32();
                    uint numBytes      = await dr.LoadAsync((uint)messageLength);

                    var packetBaseBuffer = new byte[numBytes];

                    dr.ReadBytes(packetBaseBuffer);

                    var packetBase = new PacketBase();

                    using (var stream = new MemoryStream(packetBaseBuffer))
                    {
                        try
                        {
                            var reader = new BinaryReader(stream);

                            packetBase.Read(reader);
                        }
                        catch (Exception e)
                        {
#if DEBUG
                            throw;
#endif
                        }
                    }


                    var incomingMessage = IncomingMessageFactory.GetMessage(
                        packetBase.Data as PacketV1);

                    incomingMessage.HandleMessage();
                }
            }
        }
예제 #7
0
        private void OnPlayerReady(NetPeer peer, NetEventArgs args)
        {
            if (GameClient.CurrentState == ClientState.Game)
            {
                return;
            }
            PacketPlayerReady message = PacketBase.Read <PacketPlayerReady>((NetDataReader)(args.Data));
            Player            player  = null;

            if (players.TryGetValue(message.Sender, out player))
            {
                player.SetReady(message.Value);
                NetEventManager.Trigger(NetEventType.PlayerReady, args);
            }
        }
예제 #8
0
        /// <summary>
        /// Handler for ready messages
        /// </summary>
        /// <param name="client">the sender</param>
        /// <param name="args">network event arguments</param>
        private void OnClientReady(NetPeer client, NetEventArgs args)
        {
            PacketPlayerReady message = PacketBase.Read <PacketPlayerReady>((NetDataReader)(args.Data));
            Player            p       = null;

            if (server.Players.TryGetValue(message.Sender, out p))
            {
                p.SetReady(message.Value);
                GameServer.SendExcluding(message, client);
            }
            else
            {
                server.Disconnect(client, "Client not authenticated.");
            }
        }
예제 #9
0
        /// <summary>
        /// Handler for "player enter" packets.
        /// </summary>
        /// <param name="peer">server</param>
        /// <param name="args">raw data containing the player packet</param>
        private void OnPlayerEnter(NetPeer peer, NetEventArgs args)
        {
            if (GameClient.CurrentState == ClientState.Game)
            {
                return;
            }

            PacketPlayerEnter message = PacketBase.Read <PacketPlayerEnter>((NetDataReader)(args.Data));

            if (message.Player.ID == identity.ID)
            {
                SetIdentity(message.Player);
            }
            Add(message.Player);
            NetEventManager.Trigger(NetEventType.PlayerEnter, args);
        }
예제 #10
0
        /// <summary>
        /// Handler for 'player leaving' packets.
        /// Removes the player if the game is not started yet, otherwise mark him as 'disconnected'.
        /// </summary>
        /// <param name="peer">server</param>
        /// <param name="args">wrapper around raw data containing the packet</param>
        private void OnPlayerLeave(NetPeer peer, NetEventArgs args)
        {
            PacketPlayerEnter message = PacketBase.Read <PacketPlayerEnter>((NetDataReader)(args.Data));

            if (message.Player.ID == Identity.ID)
            {
                UnityEngine.Debug.LogWarning("Dafuq, I can't leave myself!");
                return;
            }
            if (GameClient.CurrentState != ClientState.Game)
            {
                Remove(message.Player);
            }
            else
            {
                SetDisconnected(message.Player.ID);
                LockstepLogic.Instance.UpdatePlayersCount(activePlayers, message.Player.ID);
            }
            NetEventManager.Trigger(NetEventType.PlayerLeave, args);
        }