public void TestCodec() { Person person = new Person { Name = "张宏伟", Age = 18 }; TransportMessage transportMessage = new TransportMessage { Id = "1", ContentType = "Person", Content = person }; MessagePackTransportMessageCodecFactory factory = new MessagePackTransportMessageCodecFactory(); ITransportMessageEncoder encoder = factory.GetEncoder(); ITransportMessageDecoder decoder = factory.GetDecoder(); byte[] vs = encoder.Encode(transportMessage); TransportMessage message = decoder.Decode(vs); Assert.AreEqual(message.Id, "1"); Assert.AreEqual(message.ContentType, "Person"); Assert.AreEqual(((object[])message.Content)[0].ToString(), "张宏伟"); Assert.AreEqual(((object[])message.Content)[1].ToString(), "18"); }
public override void ChannelRead(IChannelHandlerContext context, object message) { BytesHolder buffer = message as BytesHolder; var data = buffer.Bytes; var transportMessage = _transportMessageDecoder.Decode(data); context.FireChannelRead(transportMessage); }
public override void ChannelRead(IChannelHandlerContext context, object message) { var buffer = (IByteBuffer)message; var data = buffer.ToArray(); var transportMessage = _transportMessageDecoder.Decode(data); context.FireChannelRead(transportMessage); }
public async Task ChannelRead(TProtocol context) { var msg = await context.ReadMessageBeginAsync(); var message = _transportMessageDecoder.Decode(await context.ReadBinaryAsync()); await context.ReadMessageEndAsync(); await _messageListener.OnReceived(_messageSender, message); }
public override void ChannelRead(IChannelHandlerContext context, object message) { var buffer = (IByteBuffer)message; var data = new byte[buffer.ReadableBytes]; buffer.ReadBytes(data); var transportMessage = _transportMessageDecoder.Decode(data); context.FireChannelRead(transportMessage); }
public override void ChannelRead(IChannelHandlerContext context, object message) { var buffer = (IByteBuffer)message; var data = new byte[buffer.ReadableBytes]; buffer.ReadBytes(data); if (!DataEquals(data, Encoding.UTF8.GetBytes(DotNettyConstants.HeartBeatPacket))) { var transportMessage = _transportMessageDecoder.Decode(data); context.FireChannelRead(transportMessage); } ReferenceCountUtil.Release(buffer); //ReferenceCountUtil.Release(buffer); }
public Task OnSessionDataReceived(TcpSocketSaeaSession session, byte[] data, int offset, int count) { if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("接收到数据包。"); } var message = _transportMessageDecoder.Decode(data.Skip(offset).Take(count).ToArray()); if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("接收到消息:" + message.Id); } _readAction(session, message); #if NET return(Task.FromResult(1)); #else return(Task.CompletedTask); #endif }
public override void ChannelRead(IChannelHandlerContext context, object message) { var buffer = (IByteBuffer)message; try { try { var data = new byte[buffer.ReadableBytes]; buffer.ReadBytes(data); var transportMessage = _transportMessageDecoder.Decode(data); context.FireChannelRead(transportMessage); } catch (Exception ex) { throw ex; } } finally { buffer.Release(); } }
public async Task OnServerDataReceived(TcpSocketSaeaClient client, byte[] data, int offset, int count) { if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("接收到数据包。"); } var message = _transportMessageDecoder.Decode(data.Skip(offset).Take(count).ToArray()); if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("接收到消息:" + message.Id); } _messageListener.OnReceived(new SimpleClientMessageSender(_transportMessageEncoder, client), message); #if NET await Task.FromResult(1); #else await Task.CompletedTask; #endif }
public async Task StartAsync(EndPoint endPoint) { _server = new TcpSocketSaeaServer((IPEndPoint)endPoint, async(session, data, offset, count) => { if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("接收到数据包。"); } var message = _transportMessageDecoder.Decode(data.Skip(offset).Take(count).ToArray()); if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("接收到消息:" + message.Id); } var sender = new SimpleServerMessageSender(_transportMessageEncoder, session, _logger); await OnReceived(sender, message); }); _server.Listen(); #if NET await Task.FromResult(1); #else await Task.CompletedTask; #endif }