Ejemplo n.º 1
0
        private byte[] GetBytes(FieldPositions fieldPositions)
        {
            var bytes = new List <byte>();

            foreach (var fieldPosition in fieldPositions)
            {
                bytes.AddRange(GetBytes(fieldPosition));
            }

            return(bytes.ToArray());
        }
Ejemplo n.º 2
0
        public ClientManager(PortManager portManager)
        {
            var random = new Random();

            while (_food.Count < 50)
            {
                var foodPos = new Vector2(random.Next(-20, 20), random.Next(-20, 20));
                if (_food.Contains(foodPos))
                {
                    continue;
                }
                _food.Add(foodPos);
            }

            _registerThread = new Thread(() => {
                IPEndPoint remoteEp = null;

                while (true)
                {
                    var rMessage = new Message(_udp.Receive(ref remoteEp));
                    if (rMessage.Type == MessageType.Register)
                    {
                        var port = portManager.Get();

                        var client = Create(new IPEndPoint(IPAddress.Any, port), remoteEp);

                        var sMessage = new Message(MessageType.ConnectData, new ConnectData(client.Id, port));

                        _udp.Send(sMessage.Bytes, sMessage.Bytes.Length, remoteEp);
                    }

                    remoteEp = null;

                    Thread.Sleep(200);
                }
            });
            _registerThread.Start();

            _positionsThread = new Thread(() => {
                while (true)
                {
                    var positions = new FieldPositions();

                    foreach (var client in Clients)
                    {
                        positions.Add(new FieldPosition(FieldPositionType.Player, client.Position, client.Id));
                    }

                    foreach (var position in Food)
                    {
                        positions.Add(new FieldPosition(FieldPositionType.Food, position));
                    }

                    var message = new Message(MessageType.FieldPositions, positions);
                    Broadcast(message);
                    Thread.Sleep(100);
                }
            });
            _positionsThread.Start();

            _serverInfoThread = new Thread(() => {
                while (true)
                {
                    var message = new Message(MessageType.Info, new ServerInfo(_clients.Count, _food.Count));
                    Broadcast(message);
                    Thread.Sleep(200);
                }
            });
            _serverInfoThread.Start();

            _checkConnThread = new Thread(() => {
                while (true)
                {
                    lock (_clients) {
                        foreach (var client in Clients)
                        {
                            if (client.Connection.Delay.Seconds > 10)
                            {
                                _clients.Remove(client.Id);
                                client.Close();
                            }
                        }
                    }
                    Thread.Sleep(1000);
                }
            });
            _checkConnThread.Start();
        }