public override void OnReceiveData(ChoConnectionState state) { while (state.AvailableData > 0) { int readBytes = state.Read(state.Buffer, 0, state.Buffer.Length); byte[] buffer = state.Buffer.Take(readBytes).ToArray(); if (_inbuffer == null) { _inbuffer = buffer; _headerFound = false; } else { _inbuffer = ChoByteArrayEx.Combine(_inbuffer, buffer); } try { if (!_headerFound) { _msgBodySize = ProcessHeader(out _headerFound); } if (_headerFound) { ProcessBody(state, _msgBodySize); } } catch (Exception ex) { ChoTrace.Write(ex); } } }
private bool ProcessBody(ChoConnectionState state, int msgBodySize) { if (_inbuffer.Length < msgBodySize) { return(false); } try { MemoryStream instream = new MemoryStream(); instream.Write(_inbuffer, 0, msgBodySize); _inbuffer = ChoByteArrayEx.SubArray(_inbuffer, msgBodySize); instream.Flush(); instream.Position = 0; InvokeObjectReceived(state, instream.GetBuffer()); } catch (Exception ex) { ChoTrace.Write(ex); } finally { //_inbuffer = null; _headerFound = false; } return(true); }
private bool ProcessBody() { if (_inbuffer.Length < _msgBodySize) { return(false); } MemoryStream _instream = new MemoryStream(); _instream.Write(_inbuffer, 0, _msgBodySize); _inbuffer = ChoByteArrayEx.SubArray(_inbuffer, _msgBodySize); _instream.Flush(); _instream.Position = 0; try { var packet = _formatter.Deserialize(_instream); OnObject(packet); } finally { _headerFound = false; } return(true); }
public override void OnReceiveData(ChoConnectionState state) { while (state.AvailableData > 0) { int readBytes = state.Read(state._buffer, 0, state._buffer.Length); byte[] buffer = state._buffer.Take(readBytes).ToArray(); if (_inbuffer == null) { _inbuffer = buffer; } else { _inbuffer = ChoByteArrayEx.Combine(_inbuffer, buffer); } if (!_headerFound) { _msgBodySize = ProcessHeader(); if (_headerFound) { ProcessBody(); } } else { ProcessBody(); } } }
public override byte[] Marshal(byte[] buffer, out int length) { var header = new byte[8]; byte[] streamLength = BitConverter.GetBytes((Int32)buffer.Length); Buffer.BlockCopy(_version, 0, header, 0, _version.Length); Buffer.BlockCopy(streamLength, 0, header, _version.Length, streamLength.Length); byte[] ret = ChoByteArrayEx.Combine(header, buffer.Take((Int32)buffer.Length).ToArray()); length = ret != null ? ret.Length : 0; return(ret); }
private int ProcessHeader() { if (_inbuffer.Length < 8) { return(-1); } _headerFound = true; int version = BitConverter.ToInt32(_inbuffer, 0); int bodySize = BitConverter.ToInt32(_inbuffer, 4); _inbuffer = ChoByteArrayEx.SubArray(_inbuffer, 8); return(bodySize); }
/// <summary> /// Send an object. /// </summary> /// <param name="value">Object to send</param> /// <remarks> /// Must be marked as serializable and connection must be open. /// </remarks> public override byte[] ToByteArray(object value) { var stream = new MemoryStream(); stream.Position = 0; _formatter.Serialize(stream, value); byte[] body = stream.GetBuffer(); var header = new byte[8]; byte[] streamLength = BitConverter.GetBytes((Int32)stream.Length); Buffer.BlockCopy(_version, 0, header, 0, _version.Length); Buffer.BlockCopy(streamLength, 0, header, _version.Length, streamLength.Length); return(ChoByteArrayEx.Combine(header, body.Take((Int32)stream.Length).ToArray())); }