Ejemplo n.º 1
0
        public void SendPlayerUpdate(TcpClient client, Packet p)
        {
            PacketReader pr        = new PacketReader(p);
            int          id        = pr.GetInt();
            float        x         = pr.GetFloat();
            float        y         = pr.GetFloat();
            Player       netplayer = Players[id];

            netplayer.Position = new Vector2(x, y);
            PacketBuilder pb = new PacketBuilder(PacketType.UPDATE_OTHER_POS);

            pb.Add(id);
            pb.Add(netplayer.Position.X);
            pb.Add(netplayer.Position.Y);
            Packet packet = pb.Build();

            foreach (TcpClient c in connections.Keys)
            {
                if (c != client)
                {
                    lock (c)
                    {
                        SendData(packet, c.GetStream());
                    }
                }
            }
        }
        public void SendPlayerUpdate()
        {
            PacketBuilder pb = new PacketBuilder(PacketType.UPDATE_MY_POS);

            pb.Add(connectionId);
            pb.Add(player.Position.X);
            pb.Add(player.Position.Y);
            Packet packet = pb.Build();
            networkManager.SendData(packet);
        }
Ejemplo n.º 3
0
        public void SendEnemyHealth(int id, int health)
        {
            Console.WriteLine("Packet sent with enemy id: " + id + " and health " + health);
            PacketBuilder pb = new PacketBuilder(PacketType.UPDATE_ENEMY_HEALTH);

            pb.Add(id);
            pb.Add(health);
            Packet packet = pb.Build();

            SendData(packet);
        }
Ejemplo n.º 4
0
        public void UpdatePlayerHealth(int playerId)
        {
            Player player = Players[playerId];

            foreach (TcpClient c in connections.Keys)
            {
                PacketBuilder pb = new PacketBuilder(PacketType.UPDATE_PLAYER_HEALTH);
                pb.Add(playerId);
                pb.Add(player.Health);
                Packet p = pb.Build();
                //Console.WriteLine("player: " + playerId + " health " + player.Health);
                lock (c)
                {
                    SendData(p, c.GetStream());
                }
            }
        }
Ejemplo n.º 5
0
        public void SendEnemyUpdate()
        {
            PacketBuilder pb          = new PacketBuilder(PacketType.UPDATE_ENEMY);
            int           enemieCount = Server.world.enemies.Count;

            pb.Add(Server.world.enemies.Count);
            foreach (Entity enemy in Server.world.enemies)
            {
                pb.Add(enemy.Id);
                pb.Add(enemy.Position.X);
                pb.Add(enemy.Position.Y);
                pb.Add(enemy.Health);
                pb.Add(enemy.chunk.chunkPos.X);
                pb.Add(enemy.chunk.chunkPos.Y);
                //Console.WriteLine("send enemy with: " + enemy.Id + " with health " + enemy.Health);
            }
            Packet packet = pb.Build();

            foreach (TcpClient client in connections.Keys)
            {
                lock (client)
                {
                    SendData(packet, client.GetStream());
                }
            }
        }
Ejemplo n.º 6
0
        public void SendItemUpdate(int id)
        {
            PacketBuilder pb = new PacketBuilder(PacketType.UPDATE_ITEM);

            pb.Add(id);
            Packet packet = pb.Build();

            SendData(packet);
            //Console.WriteLine("PAcket sent with item: " + id);
        }
Ejemplo n.º 7
0
        public void SendMyPlayerHealth(int health)
        {
            //Console.WriteLine("Packet sent with enemy id: " + id + " and health " + health);
            PacketBuilder pb = new PacketBuilder(PacketType.UPDATE_PLAYER_HEALTH);

            pb.Add(health);
            Packet packet = pb.Build();

            SendData(packet);
        }
Ejemplo n.º 8
0
        public void SendNewPlayerJoined(TcpClient client, int newClientId)
        {
            PacketBuilder pb = new PacketBuilder(PacketType.NEW_PLAYER);

            foreach (Player p in Players.Values)
            {
                if (p.ConnId == newClientId)
                {
                    pb.Add(p.ConnId);
                    pb.Add(p.Position.X);
                    pb.Add(p.Position.Y);
                    pb.Add(p.Health);
                }
            }
            Packet packet = pb.Build();

            lock (client)
            {
                SendData(packet, client.GetStream());
            }
        }
Ejemplo n.º 9
0
        public void UpdatePlayerHealth(TcpClient client, Packet packet)
        {
            int          id     = connections[client];
            PacketReader pr     = new PacketReader(packet);
            int          health = pr.GetInt();

            Players[id].Health = health;

            foreach (TcpClient c in connections.Keys)
            {
                if (c != client)
                {
                    PacketBuilder pb = new PacketBuilder(PacketType.UPDATE_PLAYER_HEALTH);
                    pb.Add(id);
                    pb.Add(health);
                    Packet p = pb.Build();
                    lock (c)
                    {
                        SendData(p, c.GetStream());
                    }
                }
            }
        }
Ejemplo n.º 10
0
        public void SendEnemyHealth(TcpClient client, Packet packet)
        {
            PacketReader pr     = new PacketReader(packet);
            int          id     = pr.GetInt();
            int          health = pr.GetInt();

            //Console.WriteLine("received enemy with: " + id + " with health " + health);
            foreach (Entity enemy in Server.world.enemies)
            {
                if (enemy.Id == id)
                {
                    enemy.Health = health;
                    if (enemy.Health <= 0)
                    {
                        enemy.HasRecentlyDied = true;
                    }
                    break;
                }
            }
            PacketBuilder pb = new PacketBuilder(PacketType.UPDATE_ENEMY_HEALTH);

            pb.Add(id);
            pb.Add(health);
            Packet p = pb.Build();

            //Console.WriteLine("send enemy update");
            foreach (TcpClient c in connections.Keys)
            {
                if (client != c)
                {
                    lock (c)
                    {
                        SendData(p, c.GetStream());
                    }
                }
            }
        }
Ejemplo n.º 11
0
        public void SendDeadEnemy(int id)
        {
            PacketBuilder pb = new PacketBuilder(PacketType.ENEMY_DIED);

            pb.Add(id);
            Packet p = pb.Build();

            foreach (TcpClient c in connections.Keys)
            {
                lock (c)
                {
                    SendData(p, c.GetStream());
                }
            }
        }
Ejemplo n.º 12
0
        public void SendItemUpdate(TcpClient client, Packet p)
        {
            PacketReader pr = new PacketReader(p);
            int          id = pr.GetInt();
            //Console.WriteLine("received update on item: " + id);
            PacketBuilder pb = new PacketBuilder(PacketType.UPDATE_ITEM);

            pb.Add(id);
            Packet packet = pb.Build();

            itemsToDestroy.Add(id);
            //Console.WriteLine("sent update on item: " + id);
            foreach (TcpClient c in connections.Keys)
            {
                if (c != client)
                {
                    lock (c)
                    {
                        SendData(packet, c.GetStream());
                    }
                }
            }
        }
Ejemplo n.º 13
0
        public void ClientThreadLoop(TcpClient client)
        {
            while (client.Connected)
            {
                Packet p = ReadData(client.GetStream());
                switch (p.PacketType)
                {
                case PacketType.PING:
                    //Console.WriteLine("ping packet received");
                    PacketBuilder pb = new PacketBuilder(0);
                    pb.Add(14);
                    Packet packet = pb.Build();     //ping packet
                    SendData(packet, client.GetStream());
                    //Console.WriteLine("Send pong");
                    break;

                case PacketType.UPDATE_MY_POS:
                    SendPlayerUpdate(client, p);
                    break;

                case PacketType.UPDATE_ITEM:
                    SendItemUpdate(client, p);
                    break;

                case PacketType.UPDATE_ENEMY_HEALTH:
                    SendEnemyHealth(client, p);
                    break;

                case PacketType.UPDATE_PLAYER_HEALTH:
                    UpdatePlayerHealth(client, p);
                    break;

                default:
                    break;
                }
            }
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            //string name = "Nicky";


            //CMSG_LOGIN_CONNECT cmsg = new CMSG_LOGIN_CONNECT("Nicky");

            //Console.WriteLine($"Name of client is {cmsg.ClientName}");
            //Console.WriteLine($"PacketType is: {cmsg.PacketType}");


            //Client nicky = new Client
            //{
            //    GUID = Guid.NewGuid(),
            //    PublicKey = "",
            //    Username = "******",
            //    Password = "******"
            //};

            //Console.WriteLine(nicky);
            //Console.WriteLine($"Current session state is: {nicky.Session.State}");


            //nicky.Session.State = Session.SessionState.Initializing;

            //Console.WriteLine($"Current session state is: {nicky.Session.State}");

            ///* Simulate epic code */
            //System.Threading.Thread.Sleep(399);
            //nicky.Session.State = Session.SessionState.Prooving;
            //Console.WriteLine($"Current session state is: {nicky.Session.State}");

            ///* Simulate epic code */
            //System.Threading.Thread.Sleep(299);
            //nicky.Session.State = Session.SessionState.WaitingForApproval;
            //Console.WriteLine($"Current session state is: {nicky.Session.State}");

            ///* Simulate epic code */
            //System.Threading.Thread.Sleep(177);
            //nicky.Session.State = Session.SessionState.Authorized;
            //Console.WriteLine($"Current session state is: {nicky.Session.State}");


            //var packet = new Packet(12, Packet.Type.CMSG_LOGIN_CONNECT);


            //int x = int.MaxValue;

            //byte[] ba = BitConverter.GetBytes(x);
            //byte[] ba2 = BitConverter.GetBytes(x);
            //byte[] ba3 = BitConverter.GetBytes(x);


            //packet.RawData = new[]
            //{
            //    ba[0],ba[1],ba[2],ba[3],
            //    ba2[0], ba2[1], ba2[2], ba2[3],
            //    ba3[0],ba3[1],ba3[2],ba3[3]
            //};
            //// Should be int.MaxValue:



            //PacketReader pr = new PacketReader(packet);

            //Console.WriteLine($"pr.ReadInt: {pr.ReadInt()}");
            //Console.WriteLine($"int.MaxValue: {int.MaxValue}");

            //while (pr.Peek(1))
            //{
            //    Console.WriteLine($"I read this: {pr.ReadByte()}");
            //}

            //if (pr.Peek(sizeof(int)))
            //{
            //    Console.WriteLine($"I read this: {pr.ReadInt()}");
            //}


            //Console.ReadLine();
            //Console.ReadLine();
            //Console.ReadLine();
            //Console.ReadLine();


            //HashVerifier hv = new HashVerifier();
            //HashVerifier hv2 = new HashVerifier(new MD5CryptoServiceProvider(), new SHA256CryptoServiceProvider());

            //byte[] a = new[] { (byte)255, (byte)255, (byte)255 };
            //byte[] b = new[] { (byte)255, (byte)255, (byte)255 };
            //byte[] c = new[] { (byte)255, (byte)255, (byte)254 };

            ////Reference equality.
            //if (a == b) Console.WriteLine("TRUE");
            //Console.WriteLine();


            //Random rnd = new Random(DateTime.Now.Millisecond);


            //byte[] hashme = new[]
            //{
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next()

            //    ,(byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(),
            //    (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next(), (byte) rnd.Next()

            //    ,(byte) rnd.Next()
            //    ,(byte) rnd.Next()
            //    ,(byte) rnd.Next()
            //    ,(byte) rnd.Next()
            //    ,(byte) rnd.Next()
            //    ,(byte) rnd.Next()
            //    ,(byte) rnd.Next()
            //    ,(byte) rnd.Next()
            //    ,(byte) rnd.Next()
            //    ,(byte) rnd.Next()


            //};

            //MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            //SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider();


            //Console.WriteLine(Encoding.ASCII.GetString(md5.ComputeHash(hashme)));
            //Console.WriteLine($"Length is {md5.ComputeHash(hashme).Length}");

            //Console.WriteLine(Encoding.ASCII.GetString(sha256.ComputeHash(hashme)));
            //Console.WriteLine($"Length is {sha256.ComputeHash(hashme).Length}");



            //var crypto = new Cryptography();
            //var keys = crypto.GenerateKeys();



            //int m = 25;



            //// Need a client to connect to:
            ////IP: 192.168.10.13
            ////User: nicky
            ////Password: 1q2w3e4r5t
            //TcpClient tcp = new TcpClient("192.168.10.13", 1337);
            //NetworkStream ns = tcp.GetStream();

            //Random rnd = new Random();


            //// Inspect in WireShark:

            //for (int i = 0; i < 100; i++)
            //{
            //                            //1500 > should be a Jumbo frame....
            //                            //1460 is TCP max for 1 packet.
            //    byte[] buffer = new byte[1600];//Encoding.ASCII.GetBytes(rnd.Next().ToString());
            //    ns.Write(buffer, 0, buffer.Length);

            //    int x = 25;
            //}

            ////kahoot...



            //string hi = "hey";
            //Console.WriteLine("hi");


            PacketBuilder pb = new PacketBuilder(5);

            pb.Type = PacketType.CMSG_LOGIN_AUTHENTICATION_DETAILS;
            pb.Add(int.MaxValue);
            pb.Add("HelloAsdfjgijigjigjgijg");
        }
Ejemplo n.º 15
0
        public void SendAcceptPacket(TcpClient client)
        {
            Vector2 spawnPoint = Server.world.GetSpawnPoint(Server.rand);
            int     clientId   = connections[client];
            Player  pl         = new Player(spawnPoint.X, spawnPoint.Y, clientId);

            Players.TryAdd(clientId, pl);
            PacketBuilder pb = new PacketBuilder(PacketType.INIT);

            pb.Add(clientId);                   //connection id
            pb.Add(seed);                       //seed
            pb.Add(spawnPoint.X);               //spawn posX
            pb.Add(spawnPoint.Y);               //spawn posY
            pb.Add(pl.Health);                  //spawn posY
            pb.Add(Server.world.enemies.Count); //count of enemies
            foreach (Entity enemy in Server.world.enemies)
            {
                pb.Add(enemy.Id);
                pb.Add(enemy.Position.X); //position is float
                pb.Add(enemy.Position.Y);
                pb.Add(enemy.Health);
            }
            pb.Add(itemsToDestroy.Count);
            foreach (int item in itemsToDestroy)
            {
                pb.Add(item);
            }

            Packet packet = pb.Build();

            //Console.WriteLine("Init packet is built");
            lock (client)
            {
                SendData(packet, client.GetStream());
            }

            foreach (int id in Players.Keys)
            {
                if (id != clientId)
                {
                    SendNewPlayerJoined(client, id);
                }
            }
        }