Ejemplo n.º 1
0
        public void RegisterPlayer(Socket s, bool loadTexture = true)
        {
            int id = Players;

            Players++;

            byte[] playerJoinInfo = BitConverter.GetBytes(id);

            ObjectNetID objectNetID = new ObjectNetID(Objects++, id);

            NetworkObjects.Add(objectNetID, game.RegisterSnake(objectNetID, loadTexture));

            SocketAsyncEventArgs args = new SocketAsyncEventArgs();

            args.SetBuffer(playerJoinInfo, 0, playerJoinInfo.Count());

            if (s != null)
            {
                Clients.Add(id, s);
                s.SendAsync(args);
            }

            foreach (var o in NetworkObjects)
            {
                o.Value.setNetworkClean(false);
            }
        }
Ejemplo n.º 2
0
        public Fruit RegisterFruit(ObjectNetID objectNetID, bool loadTexture = true)
        {
            Fruit f = new Fruit(_gameField.RandomPointInField(), _gameField, loadTexture);

            _gameField.Fruits.Add(f);

            return(f);
        }
Ejemplo n.º 3
0
        public Snake RegisterSnake(ObjectNetID ID, bool loadTexture = true)
        {
            Snake snake = new Snake(_gameField.RandomPointInField(), _gameField, loadTexture)
            {
                objectNetID = ID
            };

            Random r = new Random();

            snake.SetColor((byte)r.Next(256), (byte)r.Next(256), (byte)r.Next(256));

            _gameField.Snakes.Add(snake);
            return(snake);
        }
Ejemplo n.º 4
0
        public void RegisterFruit(bool loadTexture = true)
        {
            int id = 0;

            byte[] playerJoinInfo = BitConverter.GetBytes(id);

            ObjectNetID objectNetID = new ObjectNetID(Objects++, id);

            NetworkObjects.Add(objectNetID, game.RegisterFruit(objectNetID, loadTexture));


            foreach (var o in NetworkObjects)
            {
                o.Value.setNetworkClean(false);
            }
        }
Ejemplo n.º 5
0
        public void NetworkTick()
        {
            if (isServer)
            {
                foreach (KeyValuePair <int, Socket> pair in Clients)
                {
                    Socket s = pair.Value;

                    if (s.Available > 0)
                    {
                        byte[] buffer = new byte[256];

                        s.Receive(buffer);

                        string r = Encoding.ASCII.GetString(buffer);

                        string command = r.Split(':')[0];

                        switch (command)
                        {
                        case "Turn":

                            Direction d = (Direction)BitConverter.ToInt16(buffer, 5);

                            Snake snake = null;

                            foreach (var netobj in NetworkObjects)
                            {
                                if (netobj.Key.OwnerID == pair.Key)
                                {
                                    snake = (Snake)netobj.Value;
                                }
                            }
                            if (snake != null)
                            {
                                snake.SetDirection(d);
                            }
                            break;
                        }
                    }
                }
            }
            else
            {
                if (Host.Available > 0)
                {
                    int Available = Host.Available;

                    byte[] Message = new byte[2048];
                    Host.Receive(Message);

                    Console.WriteLine("Bytes: " + Message.Count());

                    int bytesRead = 0;

                    while (bytesRead < Available)
                    {
                        int objClass = BitConverter.ToInt32(Message, bytesRead);
                        bytesRead += sizeof(Int32);
                        int ownerID = BitConverter.ToInt32(Message, bytesRead);
                        bytesRead += sizeof(Int32);
                        int objectID = BitConverter.ToInt32(Message, bytesRead);
                        bytesRead += sizeof(Int32);
                        int objLength = BitConverter.ToInt32(Message, bytesRead);
                        bytesRead += sizeof(Int32);

                        byte[] obj = new byte[objLength];
                        Buffer.BlockCopy(Message, bytesRead, obj, 0, objLength);

                        bytesRead += objLength;

                        ObjectNetID objectNetID = new ObjectNetID(objectID, ownerID);
                        if (NetworkObjects.ContainsKey(objectNetID))
                        {
                            NetworkObjects[objectNetID].Deserialize(obj);
                        }
                        else
                        {
                            Console.WriteLine("adding network object: " + objectID + " " + ownerID);
                            Console.WriteLine(Encoding.ASCII.GetChars(obj));

                            switch (objClass)
                            {
                            case 1:


                                Snake s = new Snake(new Point(2, 2), game.gameField);
                                game.gameField.Snakes.Add(s);
                                s.Deserialize(obj);
                                s.objectNetID = new ObjectNetID(objectID, ownerID);
                                NetworkObjects.Add(s.objectNetID, s);
                                break;

                            case 2:

                                Fruit f = new Fruit(new Point(0, 0), game.gameField);
                                game.gameField.Fruits.Add(f);
                                f.Deserialize(obj);
                                f.objectNetID = new ObjectNetID(objectID, ownerID);
                                NetworkObjects.Add(f.objectNetID, f);

                                break;
                            }
                        }
                    }
                }
            }

            if (TickCount < TickDelay)
            {
                TickCount++;
            }
            else
            {
                //Console.WriteLine("Network tick server");

                TickCount = 0;
                //Network Tick
                if (isServer)
                {
                    IEnumerable <byte> Replication = new byte[0];

                    foreach (KeyValuePair <ObjectNetID, ISerializable> pair in NetworkObjects)
                    {
                        if (!pair.Value.isNetworkDirty())
                        {
                            continue;
                        }
                        pair.Value.setNetworkClean(true);

                        int cid = 1;

                        if (pair.Value is Fruit)
                        {
                            cid = 2;
                        }

                        byte[] classID = BitConverter.GetBytes(cid);

                        byte[] objectBytes = pair.Value.Serialize();

                        byte[] objLength = BitConverter.GetBytes(objectBytes.Count());

                        byte[] ownerID = BitConverter.GetBytes(pair.Key.OwnerID);

                        byte[] objectID = BitConverter.GetBytes(pair.Key.ObjectID);

                        Replication = Replication.Concat(classID).Concat(ownerID).Concat(objectID).Concat(objLength).Concat(objectBytes);
                    }

                    foreach (KeyValuePair <int, Socket> s in Clients)
                    {
                        SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                        byte[] message            = Replication.ToArray();
                        args.SetBuffer(message, 0, message.Count());

                        Console.WriteLine("Sending " + message.Count() + " bytes.");

                        s.Value.SendAsync(args);
                    }
                }
            }
        }