/// <summary> /// Close socket /// </summary> public virtual bool Close() { if (Interlocked.CompareExchange(ref isInClosing, ActiveSentinel, NoneSentinel) == NoneSentinel) { try { WorkSocket.Shutdown(SocketShutdown.Both); } catch (SocketException) { } catch (Exception ex) { var stackFrameString = StackTrace.GetStackFrameString(); logger.Error("ExSocket EndPoint:{0} Close error:{1} stack:{2}", RemoteEndPoint, ex, stackFrameString); } finally { WorkSocket.Close(); } return(true); } return(false); }
internal void Close() { if (_Listener != null) { Server.SendMessageTask task = _Listener.GetTask(this); int queueCount; lock (_QueueLockObj) { _Closed = true; queueCount = _Queue.Count; } task.IncTotalQueueCount(0 - queueCount); } try { WorkSocket.Close(); } catch { } }
public void CloseSocket() { if (WorkSocket != null) { WorkSocket.Shutdown(SocketShutdown.Both); WorkSocket.Close(); } }
/// <summary> /// /// </summary> public void Close() { try { WorkSocket.Shutdown(SocketShutdown.Both); } catch { } WorkSocket.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; } }
/// <summary> /// send message from queue. only server side use it. /// </summary> /// <returns>how many messages has been send</returns> internal int SendFromQueue() { lock (_QueueLockObj) { if (_Closed) { return(0); } int queueCount = _Queue.Count; try { Server.SendMessageTask task = _Listener.GetTask(this); task.IncTotalQueueCount(0 - queueCount); while (_Queue.Count > 0) { Message message = _Queue.Dequeue(); message.WriteToStream(_MStream); IncBufferLength(0 - message.Data.Length); if (_MStream.Length > MStreamCapacity / 2) { try { AsyncSend(_MStream.GetBuffer(), 0, (int)_MStream.Length); } catch (Exception e) { _Queue.Clear(); try { if (WorkSocket.Connected) { WorkSocket.Close(); } } catch { } finally { //Server.NTcpListener.RemoteSCB(this); } WriteError("SendFromQueue", e); return(queueCount); } if (_MStream.Length > MStreamCapacity) { _MStream = new System.IO.MemoryStream(MStreamCapacity); } else { _MStream.SetLength(0); _MStream.Position = 0; } } } if (_MStream.Length > 0) { try { AsyncSend(_MStream.GetBuffer(), 0, (int)_MStream.Length); } catch (Exception e) { _Queue.Clear(); try { if (WorkSocket.Connected) { WorkSocket.Close(); } } catch { } finally { //Server.NTcpListener.RemoteSCB(this); } WriteError("SendFromQueue", e); return(queueCount); } if (_MStream.Length > MStreamCapacity) { _MStream = new System.IO.MemoryStream(MStreamCapacity); } else { _MStream.SetLength(0); _MStream.Position = 0; } } } catch (Exception e) { WriteError("SendFromQueue", e); } return(queueCount); } }
private void Async_Receive(IAsyncResult ar) { Socket s = (Socket)ar.AsyncState; int read = 0; try { read = s.EndReceive(ar); if (read == 0) { //Remote connection call shut down. if (!SocketConnected) { //Disconnected OnDisconnect(this); } else { try { WorkSocket.Close(); } catch { } OnDisconnect(this); WriteError("EndReceive", new Exception("Remote shut down")); } return; } } catch (Exception e) { if (!SocketConnected) { //Disconnected OnDisconnect(this); } else { WriteError("EndReceive", e); } return; } int offset = 0; List <Event.ReceiveEventArgs> recvArgsList = null; if (OnBatchReceive != null) { recvArgsList = new List <NTCPMSG.Event.ReceiveEventArgs>(); } while (offset < read) { if (_CurState == State.Data) { int copyLen = Math.Min(read - offset, _CurLength - _CurDataOffset); Array.Copy(_Buffer, offset, _CurData, _CurDataOffset, copyLen); offset += copyLen; _CurDataOffset += copyLen; if (_CurDataOffset >= _CurData.Length) { if (OnBatchReceive != null) { recvArgsList.Add(new NTCPMSG.Event.ReceiveEventArgs( this.Id, this.RemoteIPEndPoint, _CurFlag, _CurEvent, _CurCableId, _CurChannel, _CurData)); } if (OnReceive != null) { try { OnReceive(this, _CurFlag, _CurEvent, _CurCableId, _CurChannel, _CurData); } catch (Exception e) { WriteError("OnReceive", e); } } _CurState = State.Sync0; } if (offset >= read) { break; } } _CurState = NextState(_Buffer[offset++]); } if (_CurState == State.Data && _CurLength == 0) { if (_CurDataOffset >= _CurData.Length) { if (OnBatchReceive != null) { recvArgsList.Add(new NTCPMSG.Event.ReceiveEventArgs( this.Id, this.RemoteIPEndPoint, _CurFlag, _CurEvent, _CurCableId, _CurChannel, _CurData)); } if (OnReceive != null) { try { OnReceive(this, _CurFlag, _CurEvent, _CurCableId, _CurChannel, new byte[0]); } catch (Exception e) { WriteError("OnReceive", e); } } _CurState = State.Sync0; } } if (OnBatchReceive != null) { OnBatchReceive(this, recvArgsList); } if (read > 0) { try { s.BeginReceive(_Buffer, 0, _Buffer.Length, SocketFlags.None, new AsyncCallback(Async_Receive), WorkSocket); } catch (Exception e) { if (!SocketConnected) { //Disconnected OnDisconnect(this); } else { WriteError("BeginReceive", e); } } } }