private void ProcessMessage() { try { var message = EventStreamMessage.FromBuffer(_workingMessage, 0, _currentMessageLength); MessageReceived?.Invoke(this, new EventStreamMessageReceivedEventArgs(message, MessageReceivedContext)); _state = DecoderState.Start; } catch (Exception) { _state = DecoderState.Error; throw; } }
public void TestNegativeDecode() { foreach (var entry in _negativeEncodedTestCases) { try { var message = EventStreamMessage.FromBuffer(entry.Value, 0, entry.Value.Length); Assert.Fail("Invalid message should have failed"); } catch (Exception) { } } }
/// <summary> /// Initialize this with message and object conetext /// </summary> public EventStreamMessageReceivedEventArgs(EventStreamMessage message, Object context) { Message = message; Context = context; }
/// <summary> /// Initialize this with message /// </summary> public EventStreamMessageReceivedEventArgs(EventStreamMessage message) { Message = message; }
public void TestPositiveDecode() { foreach (var entry in _positiveEncodedTestCases) { //verify decode doesn't throw any errors. var message = EventStreamMessage.FromBuffer(entry.Value, 0, entry.Value.Length); //reencode and make sure the buffers are identical var serializedMessage = message.ToByteArray(); CollectionAssert.AreEqual(entry.Value, serializedMessage); //now loop over the decoded document and make sure the fields are match. var decodedDoc = _positiveDecodedTestCases[entry.Key]; var jsonStr = Encoding.UTF8.GetString(decodedDoc); var data = JsonMapper.ToObject(jsonStr); if (data[PayloadField] != null) { var base64Payload = (String)data[PayloadField]; var payload = Convert.FromBase64String(base64Payload); CollectionAssert.AreEqual(payload, message.Payload); } if (data[HeaderField] != null) { var headersCollection = data[HeaderField]; Assert.AreEqual(headersCollection.Count, message.Headers.Count); foreach (var header in headersCollection.OfType <JsonData>()) { string headerName = (string)header[HeaderNameField]; var headerValue = message.Headers[headerName]; Assert.IsNotNull(headerValue); var type = (EventStreamHeaderType)(int)header[HeaderTypeField]; Assert.AreEqual(type, headerValue.HeaderType); switch (type) { case EventStreamHeaderType.String: var strVal = Encoding.UTF8.GetString(Convert.FromBase64String((string)header[HeaderValueField])); Assert.AreEqual(strVal, headerValue.AsString()); break; case EventStreamHeaderType.UUID: var uuidVal = Convert.FromBase64String((string)header[HeaderValueField]); CollectionAssert.AreEqual(uuidVal, headerValue.AsUUID().ToByteArray()); break; case EventStreamHeaderType.ByteBuf: var byteBuf = Convert.FromBase64String((string)header[HeaderValueField]); CollectionAssert.AreEqual(byteBuf, headerValue.AsByteBuf()); break; case EventStreamHeaderType.BoolFalse: case EventStreamHeaderType.BoolTrue: var boolVal = (bool)header[HeaderValueField]; Assert.AreEqual(boolVal, headerValue.AsBool()); break; case EventStreamHeaderType.Byte: var byteVal = (byte)(int)header[HeaderValueField]; Assert.AreEqual(byteVal, headerValue.AsByte()); break; case EventStreamHeaderType.Int16: var int16Val = (short)header[HeaderValueField]; Assert.AreEqual(int16Val, headerValue.AsInt16()); break; case EventStreamHeaderType.Int32: var int32Val = (int)header[HeaderValueField]; Assert.AreEqual(int32Val, headerValue.AsInt32()); break; case EventStreamHeaderType.Int64: var intVal = (long)header[HeaderValueField]; Assert.AreEqual(intVal, headerValue.AsInt64()); break; case EventStreamHeaderType.Timestamp: var dateVal = (long)header[HeaderValueField]; /* we only do this in this spot because we're setting it from the unix epoch directly. * normal API usage, you can use DateTime as a first class citizen. */ Assert.AreEqual(dateVal, headerValue.AsTimestamp().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds); break; default: break; } } } } }
public void TestPositiveEncode() { foreach (var entry in _positiveDecodedTestCases) { byte[] payload = null; var headersList = new List <IEventStreamHeader>(); //read the data to encode, then add it to payload and headers list. var decodedDoc = entry.Value; var jsonStr = Encoding.UTF8.GetString(decodedDoc); var data = JsonMapper.ToObject(jsonStr); if (data[PayloadField] != null) { var base64Payload = (String)data[PayloadField]; payload = Convert.FromBase64String(base64Payload); } if (data[HeaderField] != null) { var headersCollection = data[HeaderField]; foreach (var header in headersCollection.OfType <JsonData>()) { var headerName = (string)header[HeaderNameField]; var headerValue = new EventStreamHeader(headerName); var type = (EventStreamHeaderType)(int)header[HeaderTypeField]; switch (type) { case EventStreamHeaderType.String: var strVal = Encoding.UTF8.GetString(Convert.FromBase64String((string)header[HeaderValueField])); headerValue.SetString(strVal); break; case EventStreamHeaderType.UUID: var uuidVal = Convert.FromBase64String((string)header[HeaderValueField]); headerValue.SetUUID(new Guid(uuidVal)); break; case EventStreamHeaderType.ByteBuf: var byteBuf = Convert.FromBase64String((string)header[HeaderValueField]); headerValue.SetByteBuf(byteBuf); break; case EventStreamHeaderType.BoolFalse: case EventStreamHeaderType.BoolTrue: var boolVal = (bool)header[HeaderValueField]; headerValue.SetBool(boolVal); break; case EventStreamHeaderType.Byte: var byteVal = (byte)(int)header[HeaderValueField]; headerValue.SetByte(byteVal); break; case EventStreamHeaderType.Int16: var int16Val = (short)header[HeaderValueField]; headerValue.SetInt16(int16Val); break; case EventStreamHeaderType.Int32: var int32Val = (int)header[HeaderValueField]; headerValue.SetInt32(int32Val); break; case EventStreamHeaderType.Int64: var intVal = (long)header[HeaderValueField]; headerValue.SetInt64(intVal); break; case EventStreamHeaderType.Timestamp: var dateVal = (long)header[HeaderValueField]; /* we only do this in this spot because we're setting it from the unix epoch directly. * normal API usage, you can use DateTime as a first class citizen. */ headerValue.SetTimestamp(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(dateVal)); break; default: break; } headersList.Add(headerValue); } } //create the message, and serialize it, it should equal the original encoded data. var message = new EventStreamMessage(headersList, payload); CollectionAssert.Equals(_positiveEncodedTestCases[entry.Key], message.ToByteArray()); } }
/// <summary> /// Converts an EventStreamMessage to an event. /// </summary> /// <param name="eventStreamMessage">The event stream message to be converted.</param> /// <returns>The event</returns> protected T ConvertMessageToEvent(EventStreamMessage eventStreamMessage) { var eventStreamMessageHeaders = eventStreamMessage.Headers; string eventStreamMessageType; try { // Message type can be an event, an exception, or an error. This information is stored in the :message-type header. eventStreamMessageType = eventStreamMessageHeaders[HeaderMessageType].AsString(); } catch (KeyNotFoundException ex) { throw new EventStreamValidationException("Message type missing from event stream message.", ex); } switch (eventStreamMessageType) { case EventHeaderMessageTypeValue: string eventTypeKey; try { eventTypeKey = eventStreamMessageHeaders[HeaderEventType].AsString(); } catch (KeyNotFoundException ex) { throw new EventStreamValidationException("Event Type not defined for event.", ex); } try { return(EventMapping[eventTypeKey](eventStreamMessage)); } catch (KeyNotFoundException) { return(EventMapping[UnknownEventKey](eventStreamMessage)); } case ExceptionHeaderMessageTypeValue: string exceptionTypeKey; try { exceptionTypeKey = eventStreamMessageHeaders[HeaderExceptionType].AsString(); } catch (KeyNotFoundException ex) { throw new EventStreamValidationException("Exception Type not defined for exception.", ex); } try { throw ExceptionMapping[exceptionTypeKey](eventStreamMessage); } catch (KeyNotFoundException) { throw new UnknownEventStreamException(exceptionTypeKey); } case ErrorHeaderMessageTypeValue: int errorCode; try { errorCode = eventStreamMessageHeaders[HeaderErrorCode].AsInt32(); } catch (KeyNotFoundException ex) { throw new EventStreamValidationException("Error Code not defined for error.", ex); } // Error message is not required for errors. Errors do not have payloads. IEventStreamHeader errorMessage = null; var hasErrorMessage = eventStreamMessageHeaders.TryGetValue(HeaderErrorMessage, out errorMessage); throw new EventStreamErrorCodeException(errorCode, hasErrorMessage ? errorMessage.AsString() : string.Empty); default: // Unknown message type. Swallow the message to enable future message types without breaking existing clients. throw new UnknownEventStreamMessageTypeException(); } }