/// <summary> /// .NET-specific decoding handler for incoming UDP messages. /// </summary> public override void Read(ChannelHandlerContext ctx, object msg) { var dgram = msg as DatagramPacket; if (dgram == null) { ctx.FireRead(msg); return; } var buf = dgram.Content; var sender = dgram.Sender; var recipient = dgram.Recipient; try { var decoder = new Decoder(_signatureFactory); bool finished = decoder.Decode(ctx, buf, recipient, sender); if (finished) { // prepare finish ctx.FireRead(decoder.PrepareFinish()); } else { Logger.Warn("Did not get the complete packet!"); } } catch (Exception ex) { Logger.Error("Error in UDP decoding.", ex); throw; } }
private void Decoding(ChannelHandlerContext ctx, IPEndPoint sender, IPEndPoint receiver) { bool finished = true; bool moreData = true; while (finished && moreData) { finished = _decoder.Decode(ctx, _cumulation, receiver, sender); if (finished) { _lastId = _decoder.Message.MessageId; moreData = _cumulation.ReadableBytes > 0; ctx.FireRead(_decoder.PrepareFinish()); } else { if (_decoder.Message == null) { // Wait for more data. This may happen if we don't get the first // 58 bytes, which is the size of the header. return; } if (_lastId == _decoder.Message.MessageId) { // This ID was the same as the last and the last message already // finished the parsing. So this message is finished as well, although // it may send only partial content. finished = true; moreData = _cumulation.ReadableBytes > 0; ctx.FireRead(_decoder.PrepareFinish()); } else if (_decoder.Message.IsStreaming()) { ctx.FireRead(_decoder.Message); } // arriving here, the next handler must not be called since another // current architecture -> skip rest of handlers // TCP packet needs to be cumulated ctx.SkipRestRead(); } } }