Example #1
0
        public static void CloseSocket(Socket socket)
        {
            if (socket == null)
            {
                return;
            }

            ExceptionUtil.Eat(() => socket.Close(10000));
        }
Example #2
0
        public static void ShutdownSocket(Socket socket)
        {
            if (socket == null)
            {
                return;
            }

            ExceptionUtil.Eat(() => socket.Shutdown(SocketShutdown.Both));
            ExceptionUtil.Eat(() => socket.Close(10000));
        }
Example #3
0
        private void CloseInternal(SocketError socketError, string reason, Exception exception)
        {
            if (Interlocked.CompareExchange(ref _closing, 1, 0) == 0)
            {
                try
                {
                    if (_receiveSocketArgs.Buffer != null)
                    {
                        _receiveDataBufferPool.Return(_receiveSocketArgs.Buffer);
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.Error("Return receiving socket event buffer failed.", ex);
                }

                ExceptionUtil.Eat(() =>
                {
                    if (_sendSocketArgs != null)
                    {
                        _sendSocketArgs.Completed   -= OnSendAsyncCompleted;
                        _sendSocketArgs.AcceptSocket = null;
                        _sendSocketArgs.Dispose();
                    }
                    if (_receiveSocketArgs != null)
                    {
                        _receiveSocketArgs.Completed   -= OnReceiveAsyncCompleted;
                        _receiveSocketArgs.AcceptSocket = null;
                        _receiveSocketArgs.Dispose();
                    }
                });

                SocketUtils.ShutdownSocket(_socket);
                var isDisposedException = exception != null && exception is ObjectDisposedException;
                if (!isDisposedException)
                {
                    LogUtil.InfoFormat("Socket closed, remote endpoint:{0}, socketError:{1}, reason:{2}, ex:{3}", RemotingEndPoint, socketError, reason, exception);
                }
                _socket = null;

                if (_connectionClosedHandler != null)
                {
                    try
                    {
                        _connectionClosedHandler(this, socketError);
                    }
                    catch (Exception ex)
                    {
                        LogUtil.Error("Call connection closed handler failed.", ex);
                    }
                }
            }
        }