Пример #1
0
        public void SendPacketsThreadFunc()
        {
            while (Running)
            {
                Packet           packet = _packetsToSend.Take();
                NetClientService client = null;
                try
                {
                    ClientsLock.AcquireReaderLock(15 * 1000);
                    client = Clients[packet.ReceiverId];
                    ClientsLock.ReleaseReaderLock();
                }
                catch (ApplicationException exception)
                {
                    Console.WriteLine("=============================================================================");
                    Console.WriteLine("NetServerService::SendPacketsThreadFunc was not able to lock in 15 sec");
                    Console.WriteLine("=============================================================================");

                    if (ClientsLock.IsReaderLockHeld) // Should always be true...
                    {
                        ClientsLock.ReleaseReaderLock();
                    }
                    continue;
                }

                // SendPacket outside synchronization block, because it may take a while
                // And we don't care if at that time the user has just disconnected and removed
                // from Clients Dictionary, we handle that scenario in the Net client class
                client?.SendPacket(packet);
            }
        }
Пример #2
0
 private void SendPacket(Client client, Packet packet)
 {
     try
     {
         ClientsLock.AcquireReaderLock(15 * 1000); /* We could also use Timeout.Infinite */
         NetClientService netClient = Clients[client.Id];
         netClient?.SendPacket(packet);
     }
     catch (ApplicationException ex)
     {
         Console.WriteLine("=============================================================================");
         Console.WriteLine("NetServerService::SendPacket was not able to lock in 15 sec");
         Console.WriteLine("=============================================================================");
     }
     finally
     {
         ClientsLock.ReleaseReaderLock();
     }
 }
Пример #3
0
        public void Start()
        {
            _listener = new TcpListener(IPAddress.Loopback, 3002);
            _listener.Start();

            Running             = true;
            _packetSenderThread = new Thread(SendPacketsThreadFunc);
            _packetSenderThread.Start();

            while (Running)
            {
                TcpClient client = _listener.AcceptTcpClient();

                int clientId = (int)client.Client.Handle;
                NetClientService netClientService = new NetClientService(this, client);
                bool             added            = false;
                try
                {
                    ClientsLock.AcquireWriterLock(15 * 1000); /* We could also use Timeout.Infinite */
                    Clients.Add(clientId, netClientService);
                    ClientsLock.ReleaseWriterLock();
                    added = true;
                }
                catch (ApplicationException exception)
                {
                    Console.WriteLine(
                        "======================================================================================");
                    Console.WriteLine(
                        "Deadlock detected? 30 seconds waiting for ClientsClock in NetServerService::Start()");
                    Console.WriteLine(
                        "======================================================================================");
                }

                if (added)
                {
                    netClientService.InteractAsync();
                }
                else
                {
                    netClientService.ShutdownConnection();
                }
            }
        }
Пример #4
0
        private void OnConnectionPending(IAsyncResult result)
        {
            Socket serverSocket = ((Socket)result.AsyncState);
            Socket clientSocket = serverSocket.EndAccept(result);


            AcceptOneClient(serverSocket);

            NetClientService netClientService = new NetClientService(this, clientSocket);
            bool             added            = false;

            try
            {
                int clientId = (int)clientSocket.Handle;
                ClientsLock.AcquireWriterLock(15 * 1000); /* We could also use Timeout.Infinite */
                Clients.Add(clientId, netClientService);
                ClientsLock.ReleaseWriterLock();
                added = true;
            }
            catch (ApplicationException exception)
            {
                Console.WriteLine(
                    "======================================================================================");
                Console.WriteLine(
                    "Deadlock detected? 30 seconds waiting for ClientsClock in NetServerService::Start()");
                Console.WriteLine(
                    "======================================================================================");
            }

            if (added)
            {
                netClientService.InteractAsync();
            }
            else
            {
                netClientService.ShutdownConnection();
            }
        }