private void SendCallback (IAsyncResult ar) { SendStateObject sendState = (SendStateObject)ar.AsyncState; try { //Socket client = (Socket)ar.AsyncState; Socket client = sendState.workSocket; int bytesSent = client.EndSend (ar); if (bytesSent < sendState.buffer.Length - sendState.offset) { sendState.offset += bytesSent; client.BeginSend (sendState.buffer, sendState.offset, sendState.buffer.Length - sendState.offset, SocketFlags.None, asyncSendCallback, sendState); return; } if (sendState.disconnectAfterSend && !keepAlive) { Disconnect (); } } catch (SocketException ex) { Logger.Write (LogLevel.Error, "SendCallback. Socket error while sending data: {0}", ex.Message); OnDisconnected (); Interlocked.Exchange (ref sendProcessing, 0); return; } catch (ObjectDisposedException) { Logger.Write (LogLevel.Error, "SendCallback. Socket has already been disposed"); OnDisconnected (); Interlocked.Exchange (ref sendProcessing, 0); return; //??? should we return or try to process other messages in queue. } catch (Exception ex) { Logger.Write (LogLevel.Error, "Unexpected exception in SendCallback", ex); Interlocked.Exchange (ref sendProcessing, 0); throw; } finally { //set sendState properties to null //workaround for fixing huge memory leak with sendstate if (sendState != null) { sendState.buffer = null; sendState.workSocket = null; sendState = null; } } bool continueSend = false; lock (sendQueue) { if (sendQueue.Count > 0) { //get next element if we have continueSend = true; } else { Interlocked.Exchange (ref sendProcessing, 0); } } if (continueSend) { StartSendPackets (); } }
private void SendCallback(IAsyncResult ar) { SendStateObject sendState = (SendStateObject)ar.AsyncState; try { //Socket client = (Socket)ar.AsyncState; Socket client = sendState.workSocket; int bytesSent = client.EndSend(ar); if (bytesSent < sendState.buffer.Length - sendState.offset) { sendState.offset += bytesSent; client.BeginSend(sendState.buffer, sendState.offset, sendState.buffer.Length - sendState.offset, SocketFlags.None, asyncSendCallback, sendState); return; } if (sendState.disconnectAfterSend && !keepAlive) { Disconnect(); } } catch (SocketException ex) { Logger.Write(LogLevel.Error, "SendCallback. Socket error while sending data: {0}", ex.Message); OnDisconnected(); Interlocked.Exchange(ref sendProcessing, 0); return; } catch (ObjectDisposedException) { Logger.Write(LogLevel.Error, "SendCallback. Socket has already been disposed"); OnDisconnected(); Interlocked.Exchange(ref sendProcessing, 0); return; //??? should we return or try to process other messages in queue. } catch (Exception ex) { Logger.Write(LogLevel.Error, "Unexpected exception in SendCallback", ex); Interlocked.Exchange(ref sendProcessing, 0); throw; } finally { //set sendState properties to null //workaround for fixing huge memory leak with sendstate if (sendState != null) { sendState.buffer = null; sendState.workSocket = null; sendState = null; } } bool continueSend = false; lock (sendQueue) { if (sendQueue.Count > 0) { //get next element if we have continueSend = true; } else { Interlocked.Exchange(ref sendProcessing, 0); } } if (continueSend) { StartSendPackets(); } }