Ejemplo n.º 1
0
        // This function is meant to be run in its own thread
        // and will populate the _incomingMessages queue
        private void _networkRun()
        {
            while (_running.Value)
            {
                bool canRead    = _udpClient.Available > 0;
                int  numToWrite = _outgoingMessages.Count;

                // Get data if there is some
                if (canRead)
                {
                    // Read in one datagram
                    IPEndPoint ep   = new IPEndPoint(IPAddress.Any, 0);
                    byte[]     data = _udpClient.Receive(ref ep);          // Blocks

                    // Enque a new message
                    NetworkMessage nm = new NetworkMessage();
                    nm.Sender      = ep;
                    nm.Packet      = new Packet(data);
                    nm.ReceiveTime = DateTime.Now;

                    _incomingMessages.Enqueue(nm);

                    //Console.WriteLine("RCVD: {0}", nm.Packet);
                }

                // Write out queued
                for (int i = 0; i < numToWrite; i++)
                {
                    // Send some data
                    Packet packet;
                    bool   have = _outgoingMessages.TryDequeue(out packet);
                    if (have)
                    {
                        packet.Send(_udpClient);
                    }

                    //Console.WriteLine("SENT: {0}", packet);
                }

                // If Nothing happened, take a nap
                if (!canRead && (numToWrite == 0))
                {
                    Thread.Sleep(1);
                }
            }

            // Check to see if a bye was requested, one last operation
            if (_sendBye.Value)
            {
                ByePacket bp = new ByePacket();
                bp.Send(_udpClient);
                Thread.Sleep(1000);     // Needs some time to send through
            }
        }
Ejemplo n.º 2
0
        // This function is meant to be run in its own thread
        // Is writes and reads Packets to/from the UdpClient
        private void _networkRun()
        {
            if (!_running.Value)
            {
                return;
            }

            Console.WriteLine("[Server] Waiting for UDP datagrams on port {0}", Port);

            while (_running.Value)
            {
                bool canRead         = _udpClient.Available > 0;
                int  numToWrite      = _outgoingMessages.Count;
                int  numToDisconnect = _sendByePacketTo.Count;

                // Get data if there is some
                if (canRead)
                {
                    // Read in one datagram
                    IPEndPoint ep   = new IPEndPoint(IPAddress.Any, 0);
                    byte[]     data = _udpClient.Receive(ref ep);          // Blocks

                    // Enque a new message
                    NetworkMessage nm = new NetworkMessage();
                    nm.Sender      = ep;
                    nm.Packet      = new Packet(data);
                    nm.ReceiveTime = DateTime.Now;

                    _incomingMessages.Enqueue(nm);

                    //Console.WriteLine("RCVD: {0}", nm.Packet);
                }

                // Write out queued
                for (int i = 0; i < numToWrite; i++)
                {
                    // Send some data
                    Tuple <Packet, IPEndPoint> msg;
                    bool have = _outgoingMessages.TryDequeue(out msg);
                    if (have)
                    {
                        msg.Item1.Send(_udpClient, msg.Item2);
                    }

                    //Console.WriteLine("SENT: {0}", msg.Item1);
                }

                // Notify clients of Bye
                for (int i = 0; i < numToDisconnect; i++)
                {
                    IPEndPoint to;
                    bool       have = _sendByePacketTo.TryDequeue(out to);
                    if (have)
                    {
                        ByePacket bp = new ByePacket();
                        bp.Send(_udpClient, to);
                    }
                }

                // If Nothing happened, take a nap
                if (!canRead && (numToWrite == 0) && (numToDisconnect == 0))
                {
                    Thread.Sleep(1);
                }
            }

            Console.WriteLine("[Server] Done listening for UDP datagrams");

            // Wait for all arena's thread to join
            Queue <Arena> arenas = new Queue <Arena>(_activeArenas.Keys);

            if (arenas.Count > 0)
            {
                Console.WriteLine("[Server] Waiting for active Areans to finish...");
                foreach (Arena arena in arenas)
                {
                    arena.JoinThread();
                }
            }

            // See which clients are left to notify of Bye
            if (_sendByePacketTo.Count > 0)
            {
                Console.WriteLine("[Server] Notifying remaining clients of shutdown...");

                // run in a loop until we've told everyone else
                IPEndPoint to;
                bool       have = _sendByePacketTo.TryDequeue(out to);
                while (have)
                {
                    ByePacket bp = new ByePacket();
                    bp.Send(_udpClient, to);
                    have = _sendByePacketTo.TryDequeue(out to);
                }
            }
        }