private void ReadPayloadCallback(IAsyncResult ar) { StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; int bytesRead = handler.EndReceive(ar); if (bytesRead == 0) { return; } state.bytesRead += bytesRead; C.log.Debug(string.Format("ReadPayloadCallback: Received {0} bytes: {1}", bytesRead, BitConverter.ToString(state.buffer).Take(C.numberOfBytesToShowFromReceivedMsg).ToString())); if (state.bytesRead < state.buffer.Length) { handler.BeginReceive(state.buffer, state.bytesRead, state.buffer.Length - state.bytesRead, 0, new AsyncCallback(ReadPayloadCallback), state); } else { state.processingDone.Set(); using (var stream = new MemoryStream(state.buffer)) { var formatter = new BinaryFormatter(); stream.Seek(0, SeekOrigin.Begin); object receivedObject = formatter.Deserialize(stream); Interlocked.Increment(ref reception); NewMessageReceivedEvent?.Invoke(receivedObject, handler); } } }
private void OnMessageReceive(object classInstance, Socket client) { NewMessageReceivedEvent?.Invoke(classInstance, client); }
public void NewMessageReceived(User user, Message message) { NewMessageReceivedEvent?.Invoke(this, new NewMessageReceivedEventArgs(user, message)); }