Пример #1
0
        protected void InitConnectionBase(Socket socket)
        {
            Ensure.NotNull(socket, "socket");

            _socket        = socket;
            _localEndPoint = Eat.Exception(() => (IPEndPoint)socket.LocalEndPoint);
        }
Пример #2
0
 private void HandleBadAccept(SocketAsyncEventArgs socketArgs)
 {
     Eat.Exception(() =>
     {
         if (socketArgs.AcceptSocket != null) // avoid annoying exceptions
         {
             socketArgs.AcceptSocket.Close(SocketCloseTimeoutMs);
         }
     });
     socketArgs.AcceptSocket = null;
     _acceptSocketArgsPool.Return(socketArgs);
 }
Пример #3
0
 private void TimerCallback(object state)
 {
     foreach (var pendingConnection in _pendingConections.Values)
     {
         if (DateTime.UtcNow >= pendingConnection.WhenToKill && RemoveFromConnecting(pendingConnection))
         {
             // ReSharper disable AccessToForEachVariableInClosure
             Eat.Exception(() => pendingConnection.Connection.Close("Connection establishment timeout."));
             // ReSharper restore AccessToForEachVariableInClosure
         }
     }
     _timer.Change(CheckPeriodMs, Timeout.Infinite);
 }
Пример #4
0
        private void HandleBadConnect(SocketAsyncEventArgs socketArgs)
        {
            var serverEndPoint     = socketArgs.RemoteEndPoint;
            var socketError        = socketArgs.SocketError;
            var callbacks          = (CallbacksStateToken)socketArgs.UserToken;
            var onConnectionFailed = callbacks.OnConnectionFailed;
            var pendingConnection  = callbacks.PendingConnection;

            Eat.Exception(() => socketArgs.AcceptSocket.Close(TcpConfiguration.SocketCloseTimeoutMs));
            socketArgs.AcceptSocket = null;
            callbacks.Reset();
            _connectSocketArgsPool.Return(socketArgs);

            if (RemoveFromConnecting(pendingConnection))
            {
                onConnectionFailed((IPEndPoint)serverEndPoint, socketError);
            }
        }
Пример #5
0
        private void CloseInternal(SocketError socketError, string reason)
        {
#pragma warning disable 420
            if (Interlocked.CompareExchange(ref _closed, 1, 0) != 0)
            {
                return;
            }
#pragma warning restore 420

            NotifyClosed();

            if (_verbose)
            {
                this.Log().Info("{12} closed [{0:HH:mm:ss.fff}: N{1}, L{2}, {3:B}]:\nReceived bytes: {4}, Sent bytes: {5}\n"
                                + "Send calls: {6}, callbacks: {7}\nReceive calls: {8}, callbacks: {9}\nClose reason: [{10}] {11}\n",
                                DateTime.UtcNow, RemoteEndPoint, LocalEndPoint, _connectionId,
                                TotalBytesReceived, TotalBytesSent,
                                SendCalls, SendCallbacks,
                                ReceiveCalls, ReceiveCallbacks,
                                socketError, reason, GetType().Name);
            }

            if (_socket != null)
            {
                Eat.Exception(() => _socket.Shutdown(SocketShutdown.Both));
                Eat.Exception(() => _socket.Close(TcpConfiguration.SocketCloseTimeoutMs));
                _socket = null;
            }

            using (_sendingLock.Acquire())
            {
                if (!_isSending)
                {
                    ReturnSendingSocketArgs();
                }
            }

            var handler = ConnectionClosed;
            if (handler != null)
            {
                handler(this, socketError);
            }
        }
Пример #6
0
        public void Start(Action <IPEndPoint, Socket> onSocketAccepted)
        {
            this.Log().Info("Starting listening on TCP endpoint: {0}.", _serverEndPoint);
            _onSocketAccepted = onSocketAccepted;
            try
            {
                _listeningSocket.Bind(_serverEndPoint);
                _listeningSocket.Listen(ConcurrentAccepts);
            }
            catch (Exception)
            {
                this.Log().Info("Failed to listen on TCP endpoint: {0}.", _serverEndPoint);
                Eat.Exception(() => _listeningSocket.Close(SocketCloseTimeoutMs));
                throw;
            }

            for (var i = 0; i < ConcurrentAccepts; ++i)
            {
                StartAccepting();
            }
        }