// async public void StartSend(byte[] data, SocketSendEventHandler externalCallback = null, object externalCallbackState = null) { SendStateObject state = new SendStateObject(); state.workSocket = _socket; state.externalCallback = externalCallback; state.externalCallbackState = externalCallbackState; _socket.BeginSend(data, 0, data.Length, SocketFlags.None, new System.AsyncCallback(SendCallback), state); }
private void SendCallback(IAsyncResult ar) { try { SendStateObject state = (SendStateObject)ar.AsyncState; Socket handler = state.workSocket; handler.EndSend(ar); // BUG: returns before data sent SocketSendEvent?.Invoke(this, new SocketSendEventArgs(state, this)); if (state.externalCallback != null) { state.externalCallback(this, new SocketSendEventArgs(state, this)); } } catch (ObjectDisposedException) { } // socket closed catch (SocketException) { } // socket closed by peer }
public SocketSendEventArgs(SendStateObject state, SockBase handler) { State = state; Handler = handler; }