public ServerPacket(byte[] binaryServerPacket)
 {
     this.packetIdField = new PacketIdCounter( BitConverter.ToUInt16(binaryServerPacket, 2));
     int playerNumber = BitConverter.ToUInt16(binaryServerPacket, 4);
     this.timestampField = DateTime.FromBinary(BitConverter.ToInt64(binaryServerPacket, 6));
     this.playerInfoListField = new List<PlayerInfo>();
     int positionIn = 14;
     for (int i = 0; i < playerNumber; i++)
     {
         PlayerInfo info = new PlayerInfo(binaryServerPacket, positionIn);
         positionIn += info.ByteCount;
         playerInfoListField.Add(info);
     }
 }
Beispiel #2
0
 public static PlayerInfo TranslateBetweenClientPacketAndPlayerInfo(ClientPacket from)
 {
     PlayerInfo to = new PlayerInfo();
     to.PlayerCarringWeponOne = from.PlayerCarringWeponOne;
     to.PlayerCarringWeponTwo = from.PlayerCarringWeponTwo;
     to.PlayerDucking = from.PlayerDucking;
     to.PlayerId = from.PlayerId;
     to.PlayerJumping = from.PlayerJumping;
     to.PlayerLookingDirection = from.PlayerLookingDirection;
     to.PlayerMovementDirection = from.PlayerMovementDirection;
     to.PlayerPosition = from.PlayerPosition;
     //to.PlayerRunning = from.PlayerRunning;
     to.PlayerShooting = from.PlayerShooting;
     //to.PlayerWalking = from.PlayerWalking;
     to.Timestamp = from.TimeStamp;
     return to;
 }
Beispiel #3
0
        /// <summary>
        /// Starts local game, it also starts broadcasting game info all over network
        /// </summary>
        /// <param name="gameOptions"> game parameters</param>
        /// <param name="dedicatedServer">is server going to be dedicated</param>
        /// <param name="Me">if server is not dedicated - playerData is needed</param>
        /// <returns>Server Adress or null if error occured</returns>
        public IPEndPoint StartLANServer(GameOptions gameOptions,bool dedicatedServer,Interfaces.IPlayerMe Me)
        {
            try
            {
                if(lanBroadcast == null)
                lanBroadcast = new ServerLanBroadcast();
                Usefull.PlayerMe me = (Usefull.PlayerMe)Me;
                PlayerStatus ps = new PlayerStatus();
                lock (gameInfoPacketLock)
                {
                    Random rand = new Random(gameOptions.GetHashCode());
                    gameInfoPacket.GameId = (ushort)rand.Next(1000, 9999);
                    gameInfoPacket.GameType = gameOptions.GameType;
                    gameInfoPacket.Limit = gameOptions.GameLimit;
                    gameInfoPacket.PlayerStatusList = new List<PlayerStatus>();
                    gameInfoPacket.RoundNumber = 1;
                    if (!dedicatedServer && me != null)
                    {
                        ps.PlayerId = (ushort)rand.Next(1000, 9999);
                        ps.PlayerName = (string) me.PlayerName.Clone();
                        if (me.PlayerTeam == gameOptions.TeamOneName)
                            ps.PlayerTeam = 13;
                        else
                            ps.PlayerTeam = 39;

                        if (clientForServer == null)
                        {
                            clientForServer = new ClientForServer();
                            clientForServer.AddMessageToServer += new ClientForServer.AddMessageDelegate(clientForServer_AddMessageToServer);
                            clientForServer.EndGameEvent += new EventHandler(clientForServer_EndGameEvent);
                        }
                        clientForServer.JoinGame(ps.PlayerId, gameInfoPacket.GameId, ps.PlayerName, ps.PlayerTeam);
                        gameInfoPacket.PlayerStatusList.Add(ps);
                    }

                    gameInfoPacket.TeamScoreList = new List<TeamData>();
                    TeamData ts1 = new TeamData(13, gameOptions.TeamOneName);
                    TeamData ts2 = new TeamData(39, gameOptions.TeamTwoName);
                    gameInfoPacket.ServerAddress = base.ServerIpAdress;
                    gameInfoPacket.TeamScoreList.Add(ts1);
                    gameInfoPacket.TeamScoreList.Add(ts2);
                    gameInfoPacketSendCounter = 0;
                }
                if (!dedicatedServer && me != null)
                {
                    lock (serverPacketLock)
                    {
                        PlayerInfo pi = new PlayerInfo();
                        pi.PlayerId = ps.PlayerId;
                        serverPacket.PlayerInfoList.Add(pi);
                    }
                }

                timer.Change(0, _TimerTickPeriod);
                return base.Start();
            }
            catch (Exception ex)
            {
                Diagnostic.NetworkingDiagnostics.Logging.Fatal("StartLanServer S", ex);
            }
            return null;
        }
Beispiel #4
0
        private void myUdpServer_MessageWasReceivedEvent(object sender, EventArgs e)
        {
            UDPPacketBuffer buff = sender as UDPPacketBuffer;

            if (buff != null)
            {
                try
                {
                    Interfaces.IPacket packet = PacketTypeChecker.GetPacket(buff.Data);
                    if (packet != null)
                    {
                        switch (packet.PacketType)
                        {
                            case PacketTypeEnumeration.PingResponse :
                                DateTime received = DateTime.Now;
                                TimeSpan span = received.Subtract(packet.TimeStamp);
                                Packets.PingResponsePacket pingResponsePacket = (Packets.PingResponsePacket)packet;
                                lock (gameInfoPacketLock)
                                {
                                    for (int i = 0; i < gameInfoPacket.PlayerStatusList.Count; i++)
                                    {
                                        if (gameInfoPacket.PlayerStatusList[i].PlayerId == pingResponsePacket.PlayerId)
                                        {
                                            gameInfoPacket.PlayerStatusList[i].PlayerPing = (ushort)span.TotalMilliseconds;
                                            break;
                                        }
                                    }
                                }
                                break;
                            case (PacketTypeEnumeration.JoinPacket):
                                JoinPacket joinPacket = (JoinPacket)packet;
                                ushort id = 0;
                                lock (gameInfoPacketLock)
                                {
                                    // quit if this is join for other game
                                    if (gameInfoPacket.GameId != joinPacket.GameId)
                                        return;

                                    bool newPlayer = true;
                                    // id smaller then 1000 means that new player without id
                                    if (joinPacket.PlayerId < 1000)
                                        for (int i = 0; i < gameInfoPacket.PlayerStatusList.Count; i++)
                                        {
                                            if (gameInfoPacket.PlayerStatusList[i].PlayerId == joinPacket.PlayerId)
                                            {
                                                // we already have this player
                                                newPlayer = false;
                                                // we update playerName
                                                gameInfoPacket.PlayerStatusList[i].PlayerName = joinPacket.PlayerName;
                                                // change team request
                                                gameInfoPacket.PlayerStatusList[i].PlayerTeam = joinPacket.TeamId;
                                                break;
                                            }
                                        }
                                    // we need to add new player to gameInformation
                                    if (newPlayer)
                                    {
                                        PlayerStatus ps = new PlayerStatus();
                                        Random rand = new Random(joinPacket.PlayerName.GetHashCode());
                                        id = (ushort)rand.Next(1000, 9999);

                                        bool newPlayerOk = true;
                                        List<ushort> idList = new List<ushort>();

                                        for (int i = 0; i < gameInfoPacket.PlayerStatusList.Count; i++)
                                        {
                                            idList.Add(gameInfoPacket.PlayerStatusList[i].PlayerId);
                                            if (gameInfoPacket.PlayerStatusList[i].PlayerName == joinPacket.PlayerName)
                                                newPlayerOk = false;
                                        }
                                        if (!newPlayerOk)
                                            return;

                                        // ensure that id is uniqe
                                        while (idList.Contains(id))
                                        {
                                            id = (ushort)rand.Next(1000, 9999);
                                        }
                                        ps.PlayerId = id;
                                        ps.PlayerName = joinPacket.PlayerName;
                                        ps.PlayerTeam = joinPacket.TeamId;
                                        gameInfoPacket.PlayerStatusList.Add(ps);
                                    }
                                }
                                lock (serverPacketLock)
                                {
                                    if (!cliendAdressList.Contains(buff.RemoteEndPoint))
                                    {
                                        cliendAdressList.Add(buff.RemoteEndPoint);

                                        PlayerInfo playerInfo = new PlayerInfo();
                                        playerInfo.PlayerId = id;
                                        serverPacket.PlayerInfoList.Add(playerInfo);
                                    }
                                }
                                break;
                            case (PacketTypeEnumeration.StandardClientPacket):
                                ClientPacket clientPacket = (ClientPacket)packet;
                                bool packetOk = false;
                                bool getOnlyEvents = false;
                                lock (clientPackagesDictionaryLock)
                                {
                                    if (!clientPackagesDictionary.ContainsKey(clientPacket.PlayerId))
                                    {
                                        clientPackagesDictionary.Add(clientPacket.PlayerId, new UDPClientServerCommons.Usefull.Last10Packages());
                                        clientPackagesDictionary[clientPacket.PlayerId].AddPacket(clientPacket);
                                        packetOk = true;
                                    }
                                    else
                                        if (clientPackagesDictionary[clientPacket.PlayerId].LastPacket != null &&
                                            clientPacket.PacketId == clientPackagesDictionary[clientPacket.PlayerId].LastPacket.PacketId)
                                        {
                                            Diagnostic.NetworkingDiagnostics.Logging.Error("Old ClientPacket was received and ignored");
                                            return;
                                        }
                                        else if (clientPackagesDictionary[clientPacket.PlayerId].LastPacket != null &&
                                            clientPacket.PacketId < clientPackagesDictionary[clientPacket.PlayerId].LastPacket.PacketId)
                                        {
                                            //clientPackagesDictionary[clientPacket.PlayerId].AddPacket(clientPacket);
                                            packetOk = true;
                                            getOnlyEvents = true;
                                        }
                                        else
                                        {
                                            //clientPacket.PacketId > clientPackagesDictionary[clientPacket.PlayerId].LastPacket.PacketId
                                            clientPackagesDictionary[clientPacket.PlayerId].AddPacket(clientPacket);
                                            packetOk = true;
                                        }
                                }
                                if(packetOk)
                                lock (serverPacketLock)
                                {
                                    for (int i = 0; i < serverPacket.PlayerInfoList.Count; i++)
                                    {
                                        if (clientPacket.PlayerId == serverPacket.PlayerInfoList[i].PlayerId)
                                        {
                                            //if (clientPacket.PlayerJumping || clientPacket.PlayerShooting)
                                            //    Diagnostic.NetworkingDiagnostics.Logging.Warn(" Packet with events received " + clientPacket.ToString());
                                            // here we need to validate what Client send us !!!!!!
                                            // todo: validation
                                            if (getOnlyEvents)
                                            {
                                                if (clientPacket.PlayerJumping)
                                                    serverPacket.PlayerInfoList[i].PlayerJumping = clientPacket.PlayerJumping;
                                                if (clientPacket.PlayerShooting)
                                                    serverPacket.PlayerInfoList[i].PlayerShooting = clientPacket.PlayerShooting;
                                            }
                                            else
                                            {
                                                bool jmp = serverPacket.PlayerInfoList[i].PlayerJumping;
                                                bool sho = serverPacket.PlayerInfoList[i].PlayerShooting;
                                                serverPacket.PlayerInfoList[i] = UDPClientServerCommons.Translator.TranslateBetweenClientPacketAndPlayerInfo(clientPacket);
                                                if (jmp)
                                                    serverPacket.PlayerInfoList[i].PlayerJumping = true;
                                                if (sho)
                                                    serverPacket.PlayerInfoList[i].PlayerShooting = true;
                                            }
                                            break;
                                        }
                                    }
                                }
                                break;
                            case (PacketTypeEnumeration.QuitPacket):
                                LeaveGamePacket leaveGamePacket = (LeaveGamePacket)packet;
                                lock (gameInfoPacketLock)
                                {
                                    // do nothing if its not this game
                                    if (gameInfoPacket.GameId != leaveGamePacket.GameId)
                                        return;

                                    for (int i = 0; i < gameInfoPacket.PlayerStatusList.Count; i++)
                                    {
                                        if (leaveGamePacket.PlayerId == gameInfoPacket.PlayerStatusList[i].PlayerId)
                                        {
                                            gameInfoPacket.PlayerStatusList.RemoveAt(i);
                                            break;
                                        }
                                    }
                                }
                                lock (serverPacketLock)
                                {
                                    cliendAdressList.Remove(buff.RemoteEndPoint);

                                    for (int i = 0; i < serverPacket.PlayerInfoList.Count; i++)
                                    {
                                        if (serverPacket.PlayerInfoList[i].PlayerId == leaveGamePacket.PlayerId)
                                        {
                                            serverPacket.PlayerInfoList.RemoveAt(i);
                                            break;
                                        }
                                    }
                                }
                                break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    UDPClientServerCommons.Diagnostic.NetworkingDiagnostics.Logging.Fatal("myUdpServer_MessageWasReceivedEvent", ex);
                }
            }
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            UDPClientServerCommons.Usefull.PlayerData pd = new UDPClientServerCommons.Usefull.PlayerData();
            pd.Duck = true;
            bool tst = pd.Duck;
            byte[] binary = null;

            Vector vector = new Vector(34.35f, 12.89f, 31.98f);
            Console.WriteLine(vector);
            binary = vector.ToMinimalByte();

            Vector newVector = new Vector(binary,0);

            Console.WriteLine(newVector);
            Console.ReadLine();
            Console.WriteLine("========================================");
               // ClientPacket testClientPacket = new ClientPacket();

            ClientPacket testClientPacket = new ClientPacket();

            //testClientPacket.PacketId = 10;
            testClientPacket.PlayerCarringWeponOne = true;
            testClientPacket.PlayerCarringWeponTwo = false;
            testClientPacket.PlayerDucking = true;
            testClientPacket.PlayerId = 67;
            testClientPacket.PlayerJumping = true;
            testClientPacket.PlayerLookingDirection = new Vector(45.56f, 34.67f, 87.45f);
            testClientPacket.PlayerMovementDirection = new Vector(43.54f, 87.34f, 56.21f);
            testClientPacket.PlayerPosition = new Vector(1f, 5f, 67.65f);
            //testClientPacket.PlayerRunning = true;
            testClientPacket.PlayerShooting = true;
            //testClientPacket.PlayerWalking = false;
            testClientPacket.TimeStamp = DateTime.Now;
            testClientPacket.PacketId = 10;

            Console.WriteLine(testClientPacket);

            ClientPacket tmp = new ClientPacket(testClientPacket.ToByte());
            Console.WriteLine("========================================");
            Console.WriteLine(tmp);
            Console.WriteLine("========================================");
            Console.ReadLine();

            GameInfoPacket gameInfoPacket = new GameInfoPacket();
            gameInfoPacket.GameType = GameTypeEnumeration.Objective;
            gameInfoPacket.Limit = 10;
            gameInfoPacket.PacketId = 6889;
            gameInfoPacket.GameId = 10;
            gameInfoPacket.ServerAddress = new IPEndPoint(IPAddress.Parse("90.156.78.90"), 1234);
            PlayerStatus ps1 = new PlayerStatus();
            ps1.PlayerHealth = 100;
            ps1.PlayerId = 8;
            ps1.PlayerName = "Karpik";
            ps1.PlayerPing = 23;
            ps1.PlayerScore = 3;
            ps1.PlayerTeam = 1;
            PlayerStatus ps2 = new PlayerStatus();
            ps2.PlayerHealth = 78;
            ps2.PlayerId = 2;
            ps2.PlayerName = "Ziomek";
            ps2.PlayerPing = 34;
            ps2.PlayerScore = 7;
            ps2.PlayerTeam = 2;
            gameInfoPacket.PlayerStatusList.Add(ps1);
            gameInfoPacket.PlayerStatusList.Add(ps2);
            TeamData ts1 = new TeamData();
            ts1.TeamId = 1;
            ts1.TeamScore = 89;
            ts1.TeamName = "the crazy killers";
            TeamData ts2 = new TeamData();
            ts2.TeamId = 2;
            ts2.TeamScore = 45;
            ts2.TeamName = "what the fuckers";
            gameInfoPacket.TeamScoreList.Add(ts1);
            gameInfoPacket.TeamScoreList.Add(ts2);
            gameInfoPacket.TimeStamp = DateTime.Now;

            Console.WriteLine(gameInfoPacket);

            GameInfoPacket tmp2 = new GameInfoPacket(gameInfoPacket.ToByte());
            Console.WriteLine("========================================");
            Console.WriteLine(tmp2);
            Console.WriteLine("========================================");
            Console.ReadLine();

            JoinPacket joinPacket = new JoinPacket();
            joinPacket.GameId = 1;
            joinPacket.PacketId = 6543;
            joinPacket.PlayerId = 34;
            joinPacket.PlayerName = "Ziomek2";
            joinPacket.TimeStamp = DateTime.Now;
            joinPacket.TeamId = 67;

            Console.WriteLine(joinPacket);
            JoinPacket tmp3 = new JoinPacket(joinPacket.ToByte());
            Console.WriteLine("========================================");
            Console.WriteLine(tmp3);
            Console.WriteLine("========================================");
            Console.ReadLine();

            LeaveGamePacket leaveGamePacket = new LeaveGamePacket();
            leaveGamePacket.GameId = 56;
            leaveGamePacket.PacketId = 876;
            leaveGamePacket.PlayerId = 89;
            leaveGamePacket.TimeStamp = DateTime.Now;

            Console.WriteLine(leaveGamePacket);
            LeaveGamePacket tmp4 = new LeaveGamePacket(leaveGamePacket.ToByte());
            Console.WriteLine("========================================");
            Console.WriteLine(tmp4);
            Console.WriteLine("========================================");
            Console.ReadLine();

            PlayerInfo testClientPacket2 = new PlayerInfo();

            //testClientPacket.PacketId = 10;
            testClientPacket2.PlayerCarringWeponOne = false;
            testClientPacket2.PlayerCarringWeponTwo = true;
            testClientPacket2.PlayerDucking = true;
            testClientPacket2.PlayerId = 647;
            testClientPacket2.PlayerJumping = true;
            testClientPacket2.PlayerLookingDirection = new Vector(45.4f, 334.617f, 187.425f);
            testClientPacket2.PlayerMovementDirection = new Vector(473.547f, 837.343f, 546.231f);
            testClientPacket2.PlayerPosition = new Vector(17f, 57f, 677.675f);
            //testClientPacket2.PlayerRunning = true;
            testClientPacket2.PlayerShooting = true;
            //testClientPacket2.PlayerWalking = false;

            testClientPacket2.Health = 100;
            testClientPacket2.AckIds.Add(412);
            testClientPacket2.AckIds.Add(456);
            testClientPacket2.Timestamp = DateTime.Now;
            Console.WriteLine(testClientPacket2);
            PlayerInfo tmp5 = new PlayerInfo(testClientPacket2.ToByte());
            Console.WriteLine("========================================");
            Console.WriteLine(tmp5);
            Console.WriteLine("========================================");
            Console.ReadLine();

            ServerPacket server = new ServerPacket();

            //server.NumberOfPlayers = 2;
            server.PacketId = 346;
            server.PlayerInfoList.Add(testClientPacket2);
            server.PlayerInfoList.Add(testClientPacket2);
            server.TimeStamp = DateTime.Now;

            Console.WriteLine(server);
            ServerPacket tmp6 = new ServerPacket(server.ToByte());
            Console.WriteLine("========================================");
            Console.WriteLine(tmp6);
            Console.WriteLine("========================================");
            Console.ReadLine();
        }