예제 #1
0
        public ServerConnection([NotNull] Listener listener, [NotNull] IBufferManager bufferManager, [NotNull] ClientConnectionInfo connectionInfo, int id)
        {
            if (connectionInfo == null)
            {
                throw new ArgumentNullException("connectionInfo");
            }
            if (listener == null)
            {
                throw new ArgumentNullException("listener");
            }
            if (bufferManager == null)
            {
                throw new ArgumentNullException("bufferManager");
            }

            this._connectionInfo = connectionInfo;
            this._clientSocket   = connectionInfo.Socket;
            this._id             = id;
            this._listener       = listener;
            this._bufferManager  = bufferManager;

            StartTime     = DateTime.Now;
            ThreadId      = Thread.CurrentThread.ManagedThreadId;
            ClientAddress = ((IPEndPoint)this._clientSocket.RemoteEndPoint).Address;
        }
예제 #2
0
        private async Task ProcessConnection(ClientConnectionInfo connectionInfo)
        {
            int id = Interlocked.Increment(ref this._connectionCounter);
            ServerConnection connection = new ServerConnection(this, _compressionBufferManager, connectionInfo, id);

            await connection.Start();
        }
예제 #3
0
        internal void CloseClientSocket(ClientConnectionInfo connectionInfo)
        {
            // close the socket associated with the client
            try
            {
                if (!connectionInfo.IsInited)
                {
                    this._clientConnectionInfoPool.Put(connectionInfo);
                    return;
                }

                SocketAsyncEventArgs args = this._disconnectSocketArgsPool.Get();
                args.UserToken = connectionInfo;

                connectionInfo.Socket.Shutdown(SocketShutdown.Send);
                bool willRaiseEvent = connectionInfo.Socket.DisconnectAsync(args);
                if (!willRaiseEvent)
                {
                    DisconnectSocketCompletedHandler(this, args);
                }
            }
            // throws if client process has already closed
            catch (Exception)
            {
            }
        }
예제 #4
0
        private async void ProcessAccept(SocketAsyncEventArgs socketAsyncEventArgs)
        {
            Interlocked.Increment(ref _accepts);
            // Get the socket for the accepted client connection and put it into the object user token
            ClientConnectionInfo connectionInfo = this._clientConnectionInfoPool.Get();

            try
            {
                Socket acceptSocket = socketAsyncEventArgs.AcceptSocket;

                // Accept the next connection request
                AcceptAsync(socketAsyncEventArgs);

                connectionInfo.Init(acceptSocket, new NetworkStream(acceptSocket));

                await ProcessConnection(connectionInfo);
            }
            catch (SocketException)
            {
                CloseClientSocket(connectionInfo);
            }
            catch (IOException)
            {
                CloseClientSocket(connectionInfo);
            }
        }
예제 #5
0
        private void DisconnectSocketCompletedHandler(object sender, SocketAsyncEventArgs e)
        {
            Debug.Assert(e.SocketError == SocketError.Success);

            ClientConnectionInfo connectionInfo = (ClientConnectionInfo)e.UserToken;

            this._socketPool.Put(connectionInfo.Socket);
            connectionInfo.Stream.Dispose();
            connectionInfo.Clean();

            e.UserToken = null;
            this._disconnectSocketArgsPool.Put(e);

            this._clientConnectionInfoPool.Put(connectionInfo);
        }