View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.protocol.networkhandler.packet;
21  
22  import io.netty.buffer.ByteBuf;
23  import io.netty.channel.ChannelHandlerContext;
24  import io.netty.handler.codec.ByteToMessageCodec;
25  import org.waarp.common.utility.WaarpNettyUtil;
26  import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
27  import org.waarp.openr66.protocol.localhandler.packet.KeepAlivePacket;
28  import org.waarp.openr66.protocol.localhandler.packet.LocalPacketCodec;
29  import org.waarp.openr66.protocol.localhandler.packet.LocalPacketFactory;
30  import org.waarp.openr66.protocol.networkhandler.NetworkChannelReference;
31  import org.waarp.openr66.protocol.networkhandler.NetworkServerHandler;
32  import org.waarp.openr66.protocol.networkhandler.NetworkTransaction;
33  import org.waarp.openr66.protocol.utils.ChannelUtils;
34  
35  import java.util.List;
36  
37  /**
38   * Packet Decoder
39   */
40  public class NetworkPacketCodec extends ByteToMessageCodec<NetworkPacket> {
41  
42    @Override
43    protected void decode(final ChannelHandlerContext ctx, final ByteBuf buf,
44                          final List<Object> out) throws Exception {
45      // Make sure if the length field was received.
46      if (buf.readableBytes() < 4) {
47        // The length field was not received yet - return null.
48        // This method will be invoked again when more packets are
49        // received and appended to the buffer.
50        return;
51      }
52      // Mark the current buffer position
53      buf.markReaderIndex();
54      // Read the length field
55      final int length = buf.readInt();
56      if (length < 9) {
57        throw new OpenR66ProtocolPacketException(
58            "Incorrect decode first field in Network Packet: " + length + " < 9");
59      }
60      if (buf.readableBytes() < length) {
61        buf.resetReaderIndex();
62        return;
63      }
64      // Now we can read the two Ids
65      final int localId = buf.readInt();
66      final int remoteId = buf.readInt();
67      final byte code = buf.readByte();
68      final int index = buf.readerIndex();
69      final ByteBuf buffer = buf.retainedSlice(index, length - 9);
70      buf.skipBytes(length - 9);
71      final NetworkPacket networkPacket =
72          new NetworkPacket(localId, remoteId, code, buffer);
73      if (code == LocalPacketFactory.KEEPALIVEPACKET) {
74        final KeepAlivePacket keepAlivePacket =
75            (KeepAlivePacket) LocalPacketCodec.decodeNetworkPacket(
76                networkPacket.getBuffer());
77        WaarpNettyUtil.release(buffer);
78        if (keepAlivePacket.isToValidate()) {
79          keepAlivePacket.validate();
80          final NetworkPacket response =
81              new NetworkPacket(ChannelUtils.NOCHANNEL, ChannelUtils.NOCHANNEL,
82                                keepAlivePacket, null);
83          final NetworkChannelReference nc =
84              NetworkTransaction.getImmediateNetworkChannel(ctx.channel());
85          if (nc != null) {
86            nc.useIfUsed();
87          }
88          ctx.writeAndFlush(response.getNetworkPacket());
89        }
90        final NetworkServerHandler nsh =
91            (NetworkServerHandler) ctx.pipeline().last();
92        nsh.resetKeepAlive();
93        return;
94      }
95      out.add(networkPacket);
96    }
97  
98    @Override
99    protected void encode(final ChannelHandlerContext ctx,
100                         final NetworkPacket msg, final ByteBuf out) {
101     final ByteBuf finalBuf = msg.getNetworkPacket();
102     out.writeBytes(finalBuf);
103     WaarpNettyUtil.releaseCompletely(finalBuf);
104     // DO NOT clear msg
105   }
106 
107 }