Ejemplo n.º 1
0
        /// <summary>
        /// Initializes the networking and starts accepting connections.
        /// </summary>
        public static void Start()
        {
            Assert.IsNull(_server, "The NetworkServer is already initialized.");

            NetworkUtils.GlobalBaseTimestamp = DoubleProtocol.TimeMillis;
            _resettingByteBuffer             = new ResettingBitBuffer(DoubleProtocol.TcpBufferArraySize);
            _clients = new HashSet <NetworkServerClient>();
            _handler = new DoubleServerHandler();
            _server  = new DoubleServer(_handler, NetworkUtils.MaxBotCount,
                                        (NetworkUtils.MaxBotCount * 3 + 1) / 2, NetworkUtils.Port);

            _tickingThread = new TickingThread(NetworkUtils.UdpSendFrequency, () => {
                lock (UdpPayloadLock) {
                    if (_udpPayload != null && _udpPayload.Length != 0)
                    {
                        foreach (NetworkServerClient serverClient in _clients)
                        {
                            _server.SendUdp(serverClient.DoubleClient, buffer => buffer.Write(UdpPayload));
                        }
                    }
                }
            });
        }
Ejemplo n.º 2
0
        public Program(int port)
        {
            _colors.Enqueue(0);
            _colors.Enqueue(1);
            _colors.Enqueue(2);

            _server = new DoubleServer(this, 3, 3, port);

            _senderThread = new Thread(() => {
                try {
                    while (true)
                    {
                        lock (_server) {
                            byte[] data;
                            using (_sendBuffer) {
                                _sendBuffer.WriteBits((ulong)_players.Count, 2);
                                foreach (Player player in _players)
                                {
                                    _sendBuffer.WriteBits(player.Id, 2);
                                    _sendBuffer.WriteBits(player.X, 4);
                                    _sendBuffer.WriteBits(player.Y, 4);
                                }
                                data = _sendBuffer.ReadBytes();
                            }

                            foreach (Player player in _players)
                            {
                                _server.SendUdp(player.ServerClient, buffer => buffer.Write(data));
                            }
                        }
                        Thread.Sleep(1000 / UpdateSendFrequency);
                    }
                } catch (ThreadInterruptedException) {
                }
            });
            _senderThread.Start();
        }