Exemple #1
0
        private void InitializeClientConnection(object data)
        {
            CyberFuck.Logger.Log("Network", "initializing new client");
            Connection    conn   = (Connection)data;
            NetworkStream stream = conn.stream;

            try
            {
                //get the player name
                byte[] length = { 0 };
                stream.Read(length, 0, 1);
                byte[] buffer = new byte[length[0]];
                stream.Read(buffer, 0, length[0]);
                char[] chars = new char[length[0]];

                System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                int    charLen        = d.GetChars(buffer, 0, length[0], chars, 0);
                string name           = new System.String(chars);


                //send the world bitmap to the client:
                Tile[,] worldMap = world.Map.tileMap;
                BinaryFormatter formatter = new BinaryFormatter();
                using (MemoryStream memmory = new MemoryStream())
                {
                    formatter.Serialize(memmory, worldMap);
                    long size = memmory.Length;
                    Console.WriteLine(size);
                    byte[] bsize = BitConverter.GetBytes(size);
                    byte[] image = memmory.ToArray();
                    stream.Write(bsize, 0, bsize.Length);
                    stream.Write(image, 0, image.Length);
                }
                //send entities data (not players):
                byte[][]       entitiesBytes = new byte[world.Entities.Count][];
                List <IEntity> entities      = world.Entities;
                for (int i = 0; i < entities.Count; i++)
                {
                    entitiesBytes[i] = new EntityData(entities[i]).Encode();
                }
                byte[] entitiesData = ByteManipulation.ConcatByteArrays(entitiesBytes);
                // send the length of the entities data, send the number of entities and then send the entities themselves
                byte[] entitiesMsg = ByteManipulation.ConcatByteArrays(BitConverter.GetBytes(entitiesData.Length), BitConverter.GetBytes(entities.Count), entitiesData);
                stream.Write(entitiesMsg, 0, entitiesMsg.Length);

                //send all players data:
                List <Player> players = world.Players.Values.ToList();

                byte[][] playersBytes = new byte[world.Players.Count][];
                for (int i = 0; i < players.Count; i++)
                {
                    byte[] byData = System.Text.Encoding.ASCII.GetBytes(players[i].Name);
                    var    l      = new byte[] { (byte)byData.Length };
                    playersBytes[i] = ByteManipulation.ConcatByteArrays(new PlayerData(players[i]).Encode(), l, byData);
                }
                byte[] playersData = ByteManipulation.ConcatByteArrays(playersBytes);
                // send the length of the players data, send the number of players and then send the players themselves
                byte[] playersMsg = ByteManipulation.ConcatByteArrays(BitConverter.GetBytes(playersData.Length), BitConverter.GetBytes(players.Count), playersData);
                stream.Write(playersMsg, 0, playersMsg.Length);
                // after aproval of the client, send it its position
                // the client will send 0xFFFFFFFF to approve the connection
                CyberFuck.Logger.Log("Network", "waiting for client's approval...");
                byte[] approval = new byte[4];
                stream.Read(approval, 0, 4);
                if (!approval.All((b) => b == 0xFF))
                {
                    CyberFuck.Logger.Log("Network", "Client connection failed - bad approval message");
                    return;
                }
                // create a player for the client and
                // send the client its position
                Player clientPlayer       = new Player(world, conn.id, name);
                byte[] clientsPlayerBytes = new PlayerData(clientPlayer).Encode();
                stream.Write(BitConverter.GetBytes(clientsPlayerBytes.Length), 0, sizeof(int));
                stream.Write(clientsPlayerBytes, 0, clientsPlayerBytes.Length);
                CyberFuck.Logger.Log("Network", "player with id " + conn.id + " added");
                world.LoadPlayer(clientPlayer);
                conn.State = ConnectionState.Connected;
            }
            catch (Exception e) {
                conn.Close();
            }
        }