public void Append_Hex_String_Appropriately() { // arrange var builder = new ByteArrayBuilder(); var expected = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44 }; // act builder.AppdendHexString("0x001122"); builder.AppdendHexString("3344"); builder.AppdendHexString(""); var bytes = builder.Build(); // assert bytes.Should().Equal(expected); }
public byte[] ToBytes() { var fmt = ClientsInfo.Select(_ => String.Concat("i,b", MaxNameLength)); string fmtString = String.Join(",", fmt); var bufferBuilder = new ByteArrayBuilder(String.Concat("b,i,", fmtString)) .Append(MessageType.ClientConnected.Value()) .Append(ClientsInfo.Count); foreach (var client in ClientsInfo) { bufferBuilder.Append(client.Id); string paddedName = client.Name.PadRight(MaxNameLength); var nameBytes = Encoding.UTF8.GetBytes(paddedName); bufferBuilder.Append(nameBytes); } return(bufferBuilder.Build()); }
/// <summary> /// Get byte representation /// </summary> /// <returns>Message bytes</returns> public byte[] ToBytes() { int dataLength = Entities.Count * 5; string format = String.Concat("b,i,i", dataLength); var bufferBuilder = new ByteArrayBuilder(format) .Append(MessageType.Entities.Value()) .Append(Entities.Count); foreach (var entity in Entities) { bufferBuilder.Append(entity.Id); bufferBuilder.Append(entity.Position.X); bufferBuilder.Append(entity.Position.Y); bufferBuilder.Append(entity.Velocity.X); bufferBuilder.Append(entity.Velocity.Y); } return(bufferBuilder.Build()); }
/// <summary> /// Get message bytes /// </summary> public byte[] ToBytes() { int obstaclesSize = Obstacles.Count * 4; string fmt = String.Concat("b,i,i,i,i,i", obstaclesSize); var bufferBuilder = new ByteArrayBuilder(fmt) .Append(MessageType.FieldParams.Value()) .Append(Size) .Append(Position.X) .Append(Position.Y) .Append(Obstacles.Count); foreach (var box in Obstacles) { bufferBuilder.Append(box.X).Append(box.Y); bufferBuilder.Append(box.Width).Append(box.Height); } return(bufferBuilder.Build()); }