private static void PrepareContext(HeaderMessageContext <THeader> context, int size, ParsingState nextState) { // Reset context for next state context.CurrentIndex = 0; context.ReadSize = size; if (context.ReadBuffer.Length < size) // Only increase read buffer if it is too small { context.ReadBuffer = new byte[size]; } context.State = nextState; }
/// <summary> /// Create incoming message from transmission /// </summary> private static BinaryMessage ToMessage(HeaderMessageContext <THeader> headerContext) { var includesPayload = headerContext.State == ParsingState.ReadingPayload; var payloadLength = includesPayload ? headerContext.Header.PayloadLength : 0; var payload = includesPayload ? new byte[payloadLength] : BinaryMessage.EmptyBytes; if (includesPayload) { Buffer.BlockCopy(headerContext.ReadBuffer, 0, payload, 0, payloadLength); } return(new BinaryMessage <THeader>(headerContext.Header, payload)); }