private void OnClientReceive(IAsyncResult ar) { try { int readBytes = _socket.EndReceive(ar); if (readBytes > 0) { _buffer.Cursor += readBytes; int localCursor = 0; for (int i = 0; i < _buffer.Cursor; i++) { if (_buffer.Buffer[i] == '\0') // match { StompMessage message = null; try { message = new StompMessage(_buffer.Buffer, localCursor, i - localCursor); StompStatistics.CountIncomingMessage(); if (OnMessageReceived != null) { try { OnMessageReceived(this, message); } catch (Exception) { } } } catch (Exception ex) { if (StompLogger.CanLogException) { StompLogger.LogException("Failed to parse stomp packet", ex); } } localCursor = i + 1; } } if (localCursor > 0) { _buffer.Remove(localCursor); } BeginReceive(); } else { throw new Exception("Connection closed."); } } catch (Exception) { OnInternalDisconnect(); } }