Пример #1
0
        internal void CloseClient(Connector connector, NetworkCloseMode mode)
        {
            if (connector == null)
            {
                Log.Warn("ServerNetwork.CloseClient socket is null");
                return;
            }

            lock (toRemoveClientConnectors.Lock)
            {
                toRemoveClientConnectors.In.Enqueue(connector);
                Log.Debug("ServerNetwork.CloseClient connectId={0}", mode, connector.Id);
            }
        }
Пример #2
0
        public void Poll()
        {
            try
            {
                if (defferedConnected != null)
                {
                    connector = defferedConnected.Conn;
                    connector.BeginReceive();

                    // notify
                    if (ConnectorConnected != null)
                    {
                        ConnectorConnected(defferedConnected.Conn, defferedConnected.Ex);
                    }

                    defferedConnected = null;
                }

                RefreshMessageQueue();
                RefreshClient();
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Пример #3
0
        // io thread schedules this entrance
        private void OnAcceptedCallback(IAsyncResult ar)
        {
            try
            {
                var clientSocket = listenSocket.EndAccept(ar);

                clientSocket.SendTimeout = 500;
                clientSocket.ReceiveTimeout = 500;
                clientSocket.NoDelay = true;

                var id = nextClientConnectorId;

                // to ensure atomic increment of nextClientConnectorId 
                // todo handle wraparound
                // Interlocked.Exchange is not supported on specific platforms, unity iOS full-AOT 
                Interlocked.Exchange(ref nextClientConnectorId, nextClientConnectorId + 1 < int.MaxValue ? nextClientConnectorId + 1 : 1);
                while (clientConnectorsDict.ContainsKey(nextClientConnectorId))
                {
                    Interlocked.Exchange(ref nextClientConnectorId, nextClientConnectorId + 1 < int.MaxValue ? nextClientConnectorId + 1 : 1);
                }

                var clientConnector = new Connector(clientSocket, id);

                lock (toAddClientConnectors.Lock)
                {
                    toAddClientConnectors.In.Enqueue(clientConnector);
                }

                // continue to accept
                listenSocket.BeginAccept(OnAcceptedCallback, null);
            }
            catch (Exception)
            {
                //if (OnAccepted != null) OnAccepted(acceptedResult.SetValue(null, -1, null, e));
            }
        }