protected TotMessageBase(TotMessageType messageType, TotMessageId messageId, TotPurpose purpose, TotContent content) { Version = TotVersion.Version1; MessageType = Guard.NotNull(nameof(messageType), messageType); MessageId = Guard.NotNull(nameof(messageId), messageId); Purpose = purpose ?? TotPurpose.Empty; Content = content ?? TotContent.Empty; }
public async Task RespondSuccessAsync(TotMessageId messageId, TotContent content) { Guard.NotNull(nameof(messageId), messageId); Guard.NotNull(nameof(content), content); var response = new TotResponse(TotPurpose.Success, content, messageId); await SendAsync(response.ToBytes()).ConfigureAwait(false); }
/// <param name="purpose">Success, BadRequest, VersionMismatch, UnsuccessfulRequest</param> public TotResponse(TotPurpose purpose, TotContent content, TotMessageId messageId) : base(TotMessageType.Response, messageId, purpose, content) { Guard.NotNull(nameof(purpose), purpose); if (purpose != TotPurpose.Success && purpose != TotPurpose.BadRequest && purpose != TotPurpose.VersionMismatch && purpose != TotPurpose.UnsuccessfulRequest) { throw new ArgumentException($"{nameof(purpose)} of {nameof(TotResponse)} can only be {TotPurpose.Success}, {TotPurpose.BadRequest}, {TotPurpose.VersionMismatch} or {TotPurpose.UnsuccessfulRequest}. Actual: {purpose}."); } }
public void TotContentTest() { var x = TotContent.CantRequestSubscribeNotifyChannel; var x2 = TotContent.Empty; var x3 = new TotContent(); x3.FromHex("0003040506070909"); var x4 = new TotContent(); x4.FromBytes(new byte[] { 0, 3, 4, 5, 6, 7, 9, 9 }); Assert.NotEqual(x, x2); Assert.Equal(x3, x4); Assert.Equal(new TotContent("Cannot send Request to a SubscribeNotify channel."), TotContent.CantRequestSubscribeNotifyChannel); Assert.Equal(new TotContent("Cannot send SubscribeRequest to a RequestResponse channel."), TotContent.CantSubscribeRequestRequestResponseChannel); Assert.Equal(new byte[] { }, TotContent.Empty.ToBytes()); Assert.Equal(new TotContent(""), TotContent.Empty); Assert.Equal(new TotContent(new byte[] { }), TotContent.Empty); Assert.NotEqual(new TotContent(), TotContent.Empty); Assert.Equal("", TotContent.Empty.ToString()); Assert.Equal("foo", new TotContent("foo").ToString()); Assert.Equal(3, new TotContent("foo").Length); // This test crashes Travis CI, so keep it commented out. //var bigStringBuilder = new StringBuilder(); //for (int i = 0; i < 536870912; i++) //{ // bigStringBuilder.Append("0"); //} //new TotContent(bigStringBuilder.ToString()); //bigStringBuilder.Append("0"); //Assert.Throws<ArgumentOutOfRangeException>(() => new TotContent(bigStringBuilder.ToString())); var x5 = new TotContent(); x5.FromBytes(new byte[] { 3, 0, 0, 0, 1, 2, 3 }, startsWithLength: true); x5.FromBytes(new byte[] { 0, 0, 0, 0 }, startsWithLength: true); Assert.Throws <FormatException>(() => x5.FromBytes(new byte[] { 3, 0, 0, 0, 1, 2 }, startsWithLength: true)); Assert.Throws <FormatException>(() => x5.FromBytes(new byte[] { 3, 0, 0 }, startsWithLength: true)); }
public override void FromBytes(byte[] bytes) { Guard.NotNullOrEmpty(nameof(bytes), bytes); Guard.InRangeAndNotNull($"{nameof(bytes)}.{nameof(bytes.Length)}", bytes.Length, 7, 536870912 + 3 + 4 + 255); Version = new TotVersion(); Version.FromByte(bytes[0]); MessageType = new TotMessageType(); MessageType.FromByte(bytes[1]); MessageId = new TotMessageId(); MessageId.FromBytes(bytes.Skip(2).Take(2).ToArray()); int purposeLength = bytes[4]; Purpose = new TotPurpose(); Purpose.FromBytes(bytes.Skip(4).Take(purposeLength + 1).ToArray(), startsWithLength: true); int contentLength = BitConverter.ToInt32(bytes.Skip(5 + purposeLength).Take(4).ToArray(), 0); Content = new TotContent(); Content.FromBytes(bytes.Skip(5 + purposeLength).ToArray(), startsWithLength: true); }
public TotPong(TotContent content, TotMessageId messageId) : base(TotMessageType.Pong, messageId, TotPurpose.Pong, content) { }
/// <param name="purpose">The Purpose of Request is arbitrary.</param> public TotRequest(string purpose, TotContent content) : base(TotMessageType.Request, TotMessageId.Random, new TotPurpose(purpose), content) { }
public TotPing(TotContent content) : base(TotMessageType.Ping, TotMessageId.Random, TotPurpose.Ping, content) { }
/// <param name="purpose">The Purpose of SubscribeRequest and Notification is arbitrary, but clients and servers MUST implement the same Purpose for all three.</param> public TotNotification(string purpose, TotContent content) : base(TotMessageType.Notification, new TotMessageId(0), new TotPurpose(purpose), content) { }