public static IObservable <IObservable <ClientCommand> > ClientsCommandsStream( IObservable <ITcpResponse> tcpStream, IObservable <IGroupedObservable <string, ClientState> > clientsStateStream) => clientsStateStream .Select(clientStateStream => tcpStream .OfType <ClientDataReceived>() .Where(response => response.ClientId == clientStateStream.Key) .Select(response => response.Buffer) .Select(buffer => ByteBufferUtil .DecodeString( buffer, 0, buffer.ReadableBytes, Encoding.UTF8)) .WithLatestFrom( clientStateStream, (text, client) => new ClientCommand( client, text ) ) );
private static void DecodeString(string text, Encoding charset) { var buffer = Unpooled.CopiedBuffer(text, charset); Assert.Equal(text, ByteBufferUtil.DecodeString(buffer, 0, buffer.ReadableBytes, charset)); buffer.Release(); }
public void Encode_Test() { var r1 = ByteBufferUtil.EncodeString("hellow"); var sourceBytes1 = Encoding.UTF8.GetBytes("hellow"); var dest1 = new byte[4]; Array.Copy(r1, dest1, dest1.Length); Assert.Equal(sourceBytes1.Length, BitConverter.ToInt32(dest1)); var r2 = ByteBufferUtil.DecodeString(r1, 0, out int len); Assert.Equal("hellow", r2); var short_1 = BitConverter.GetBytes((short)1); var int_1 = BitConverter.GetBytes(2); var long_1 = BitConverter.GetBytes(3L); var combineByte1 = ByteBufferUtil.Combine(short_1, int_1, long_1); var startOffset = 0; var short_2 = ByteBufferUtil.DecodeShort(combineByte1, startOffset, out startOffset); var int_2 = ByteBufferUtil.DecodeInt(combineByte1, startOffset, out startOffset); var long_2 = ByteBufferUtil.DecodeLong(combineByte1, startOffset, out startOffset); Assert.Equal(1, short_2); Assert.Equal(2, int_2); Assert.Equal(3, long_2); var d1 = new DateTime(2019, 1, 1, 1, 1, 1); var b1 = ByteBufferUtil.EncodeDateTime(d1); var d2 = ByteBufferUtil.DecodeDateTime(b1, 0, out int n1); Assert.Equal(d1, d2); var byte1 = new byte[] { 1, 2, 3 }; var b2 = ByteBufferUtil.EncodeBytes(byte1); var byte2 = ByteBufferUtil.DecodeBytes(b2, 0, out int n2); Assert.Equal(byte1, byte2); }
string FormatByteBuffer(string eventName, IByteBuffer msg) { var sbuf = ByteBufferUtil.DecodeString(msg, msg.ReaderIndex, msg.ReadableBytes, Encoding.UTF8); return($"{eventName}: {sbuf}"); }