1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.protocol.networkhandler.packet;
21
22 import io.netty.buffer.ByteBuf;
23 import io.netty.buffer.ByteBufAllocator;
24 import org.waarp.common.utility.WaarpNettyUtil;
25 import org.waarp.common.utility.WaarpStringUtils;
26 import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
27 import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
28 import org.waarp.openr66.protocol.localhandler.packet.AbstractLocalPacket;
29 import org.waarp.openr66.protocol.localhandler.packet.LocalPacketFactory;
30
31
32
33
34
35
36
37
38 public class NetworkPacket {
39 public static final int NETWORK_HEADER_SIZE = 4 * 3 + 1;
40 private ByteBuf buffer;
41
42 private final int remoteId;
43
44 private final int localId;
45
46 private final byte code;
47
48 private boolean uniqueBuffer;
49
50
51
52
53
54
55
56 public NetworkPacket(final int localId, final int remoteId, final byte code,
57 final ByteBuf buffer) {
58 this.remoteId = remoteId;
59 this.localId = localId;
60 this.code = code;
61 this.buffer = buffer;
62 this.uniqueBuffer = false;
63 }
64
65
66
67
68
69
70
71
72
73 public NetworkPacket(final int localId, final int remoteId,
74 final AbstractLocalPacket packet,
75 final LocalChannelReference lcr)
76 throws OpenR66ProtocolPacketException {
77 this.remoteId = remoteId;
78 this.localId = localId;
79 code = packet.getType();
80 uniqueBuffer = true;
81 buffer = packet.getLocalPacketForNetworkPacket(lcr, this);
82 }
83
84
85
86
87 public final ByteBuf getBuffer() {
88 return buffer;
89 }
90
91
92
93
94 public final int getRemoteId() {
95 return remoteId;
96 }
97
98
99
100
101 public final int getLocalId() {
102 return localId;
103 }
104
105
106
107
108 public final byte getCode() {
109 return code;
110 }
111
112 public final void writeNetworkHeader(final ByteBuf buf, final int capacity) {
113 buf.writerIndex(0);
114 buf.writeInt(capacity + NETWORK_HEADER_SIZE - 4);
115 buf.writeInt(remoteId);
116 buf.writeInt(localId);
117 buf.writeByte(code);
118 }
119
120
121
122
123 public ByteBuf getNetworkPacket() {
124 if (uniqueBuffer) {
125 writeNetworkHeader(buffer, buffer.capacity() - NETWORK_HEADER_SIZE);
126 buffer.writerIndex(buffer.capacity());
127 return buffer;
128 }
129 final ByteBuf buf = ByteBufAllocator.DEFAULT.ioBuffer(NETWORK_HEADER_SIZE,
130 NETWORK_HEADER_SIZE);
131 writeNetworkHeader(buf, buffer.capacity());
132 buffer = ByteBufAllocator.DEFAULT.compositeDirectBuffer(2)
133 .addComponents(buf, buffer);
134 uniqueBuffer = true;
135 return buffer;
136 }
137
138 @Override
139 public String toString() {
140 return "RId: " + remoteId + " LId: " + localId + " Code: " + code +
141 " Length: " + (buffer != null? (buffer.readableBytes() + (code ==
142 LocalPacketFactory.REQUESTPACKET?
143 buffer.toString(buffer.readerIndex(), buffer.readableBytes(),
144 WaarpStringUtils.UTF8) : "")) : "no buffer");
145 }
146
147 public final void clear() {
148 if (WaarpNettyUtil.release(buffer)) {
149 buffer = null;
150 }
151 }
152 }