//add end private void HandleCloseSocket() { if (Connected) { lock (syncRoot) { if (Connected) { mState = NetState.Disconnect; try { socket.Shutdown(SocketShutdown.Both); try { if (CloseConnect != null) { CloseConnect(this, null, 0); } } catch (Exception ex) { Trace.TraceError("OnDisconnected: {0}", ex); } } catch { } socket.Close(); } } } }
internal void Close() { if (mIOSendEventArgs == null || mIOSendEventArgs == null) { return; } try { var socket = WorkSocket; if (socket != null) { WorkSocket.Shutdown(SocketShutdown.Both); WorkSocket.Close(); } var dataToken0 = (AsyncUserToken)mIOSendEventArgs.UserToken; if (dataToken0 != null) { dataToken0.Reset(true); dataToken0.tcpConn = null; } var dataToken1 = (AsyncUserToken)mIOReceiveEventArgs.UserToken; if (dataToken1 != null) { dataToken1.Reset(true); dataToken1.tcpConn = null; } } catch (Exception) { } finally { mIOSendEventArgs.AcceptSocket = null; server.GetIOArgPool().Push(mIOSendEventArgs); mIOSendEventArgs = null; mIOReceiveEventArgs.AcceptSocket = null; server.GetIOArgPool().Push(mIOReceiveEventArgs); mIOReceiveEventArgs = null; this.server = null; mState = NetState.Disconnect; } }
public TcpConnect(TcpServer server, SocketAsyncEventArgs acceptEventArgs) { HashCode = Guid.NewGuid(); this.server = server; server.GetIOArgPool().TryPop(out mIOReceiveEventArgs); mIOReceiveEventArgs.AcceptSocket = acceptEventArgs.AcceptSocket; var dataToken1 = (AsyncUserToken)mIOReceiveEventArgs.UserToken; mIOReceiveEventArgs.SetBuffer(dataToken1.bufferOffset, server.socketSettings.BufferSize); dataToken1.tcpConn = this; server.GetIOArgPool().TryPop(out mIOSendEventArgs); mIOSendEventArgs.AcceptSocket = acceptEventArgs.AcceptSocket; var dataToken2 = (AsyncUserToken)mIOSendEventArgs.UserToken; mIOSendEventArgs.SetBuffer(dataToken2.bufferOffset, server.socketSettings.BufferSize); dataToken2.tcpConn = this; mSendQueue = new ThreadSafeQueue <byte[]>(); mState = NetState.Invalid; }