Skip to content

Commit 3785ca9

Browse files
RobertBorgnormanmaurer
authored andcommitted
added support for Protobuf codec nano runtime
Motivation: Netty was missing support for Protobuf nano runtime targeted at weaker systems such as Android devices. Modifications: Added ProtobufDecoderNano and ProtobufDecoderNano in order to provide support for Nano runtime. modified ProtobufVarint32FrameDecoder and ProtobufLengthFieldPrepender in order to remove any on either Nano or Lite runtime by copying the code for handling Protobuf varint32 in from Protobuf library. modified Licenses and NOTICE in order to reflect the changes i made. added Protobuf Nano runtime as optional dependency Result: Netty now supports Protobuf Nano runtime.
1 parent 7b51412 commit 3785ca9

File tree

13 files changed

+431
-66
lines changed

13 files changed

+431
-66
lines changed

NOTICE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ and decompression library written by William Kinney. It can be obtained at:
146146
* HOMEPAGE:
147147
* https://code.google.com/p/jfastlz/
148148

149-
This product optionally depends on 'Protocol Buffers', Google's data
149+
This product contains a modified portion of and optionally depends on 'Protocol Buffers', Google's data
150150
interchange format, which can be obtained at:
151151

152152
* LICENSE:

codec/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<artifactId>protobuf-java</artifactId>
4040
<optional>true</optional>
4141
</dependency>
42+
<dependency>
43+
<groupId>com.google.protobuf.nano</groupId>
44+
<artifactId>protobuf-javanano</artifactId>
45+
<optional>true</optional>
46+
</dependency>
4247
<dependency>
4348
<groupId>org.jboss.marshalling</groupId>
4449
<artifactId>jboss-marshalling</artifactId>

codec/src/main/java/io/netty/handler/codec/protobuf/ProtobufDecoder.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 The Netty Project
2+
* Copyright 2015 The Netty Project
33
*
44
* The Netty Project licenses this file to you under the Apache License,
55
* version 2.0 (the "License"); you may not use this file except in compliance
@@ -33,10 +33,10 @@
3333
/**
3434
* Decodes a received {@link ByteBuf} into a
3535
* <a href="http://code.google.com/p/protobuf/">Google Protocol Buffers</a>
36-
* {@link Message} and {@link MessageLite}. Please note that this decoder must
36+
* {@link Message} and {@link MessageLite}. Please note that this decoder must
3737
* be used with a proper {@link ByteToMessageDecoder} such as {@link ProtobufVarint32FrameDecoder}
3838
* or {@link LengthFieldBasedFrameDecoder} if you are using a stream-based
39-
* transport such as TCP/IP. A typical setup for TCP/IP would be:
39+
* transport such as TCP/IP. A typical setup for TCP/IP would be:
4040
* <pre>
4141
* {@link ChannelPipeline} pipeline = ...;
4242
*
@@ -53,7 +53,8 @@
5353
* and then you can use a {@code MyMessage} instead of a {@link ByteBuf}
5454
* as a message:
5555
* <pre>
56-
* void channelRead({@link ChannelHandlerContext} ctx, MyMessage req) {
56+
* void channelRead({@link ChannelHandlerContext} ctx, Object msg) {
57+
* MyMessage req = (MyMessage) msg;
5758
* MyMessage res = MyMessage.newBuilder().setText(
5859
* "Did you say '" + req.getText() + "'?").build();
5960
* ch.write(res);
@@ -101,7 +102,8 @@ public ProtobufDecoder(MessageLite prototype, ExtensionRegistryLite extensionReg
101102
}
102103

103104
@Override
104-
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
105+
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
106+
throws Exception {
105107
final byte[] array;
106108
final int offset;
107109
final int length = msg.readableBytes();
@@ -122,9 +124,11 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
122124
}
123125
} else {
124126
if (HAS_PARSER) {
125-
out.add(prototype.getParserForType().parseFrom(array, offset, length, extensionRegistry));
127+
out.add(prototype.getParserForType().parseFrom(
128+
array, offset, length, extensionRegistry));
126129
} else {
127-
out.add(prototype.newBuilderForType().mergeFrom(array, offset, length, extensionRegistry).build());
130+
out.add(prototype.newBuilderForType().mergeFrom(
131+
array, offset, length, extensionRegistry).build());
128132
}
129133
}
130134
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2015 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.codec.protobuf;
17+
18+
import com.google.protobuf.nano.MessageNano;
19+
20+
import java.util.List;
21+
22+
import io.netty.buffer.ByteBuf;
23+
import io.netty.channel.ChannelHandler.Sharable;
24+
import io.netty.channel.ChannelHandlerContext;
25+
import io.netty.channel.ChannelPipeline;
26+
import io.netty.handler.codec.ByteToMessageDecoder;
27+
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
28+
import io.netty.handler.codec.MessageToMessageDecoder;
29+
import io.netty.util.internal.ObjectUtil;
30+
31+
/**
32+
* Decodes a received {@link ByteBuf} into a
33+
* <a href="http://code.google.com/p/protobuf/">Google Protocol Buffers</a>
34+
* {@link MessageNano}. Please note that this decoder must
35+
* be used with a proper {@link ByteToMessageDecoder} such as {@link LengthFieldBasedFrameDecoder}
36+
* if you are using a stream-based transport such as TCP/IP. A typical setup for TCP/IP would be:
37+
* <pre>
38+
* {@link ChannelPipeline} pipeline = ...;
39+
*
40+
* // Decoders
41+
* pipeline.addLast("frameDecoder",
42+
* new {@link LengthFieldBasedFrameDecoder}(1048576, 0, 4, 0, 4));
43+
* pipeline.addLast("protobufDecoder",
44+
* new {@link ProtobufDecoderNano}(MyMessage.getDefaultInstance()));
45+
*
46+
* // Encoder
47+
* pipeline.addLast("frameEncoder", new {@link io.netty.handler.codec.LengthFieldPrepender}(4));
48+
* pipeline.addLast("protobufEncoder", new {@link ProtobufEncoderNano}());
49+
* </pre>
50+
* and then you can use a {@code MyMessage} instead of a {@link ByteBuf}
51+
* as a message:
52+
* <pre>
53+
* void channelRead({@link ChannelHandlerContext} ctx, Object msg) {
54+
* MyMessage req = (MyMessage) msg;
55+
* MyMessage res = MyMessage.newBuilder().setText(
56+
* "Did you say '" + req.getText() + "'?").build();
57+
* ch.write(res);
58+
* }
59+
* </pre>
60+
*/
61+
@Sharable
62+
public class ProtobufDecoderNano extends MessageToMessageDecoder<ByteBuf> {
63+
private final Class<? extends MessageNano> clazz;
64+
/**
65+
* Creates a new instance.
66+
*/
67+
public ProtobufDecoderNano(Class<? extends MessageNano> clazz) {
68+
this.clazz = ObjectUtil.checkNotNull(clazz, "You must provide a Class");
69+
}
70+
71+
@Override
72+
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
73+
throws Exception {
74+
final byte[] array;
75+
final int offset;
76+
final int length = msg.readableBytes();
77+
if (msg.hasArray()) {
78+
array = msg.array();
79+
offset = msg.arrayOffset() + msg.readerIndex();
80+
} else {
81+
array = new byte[length];
82+
msg.getBytes(msg.readerIndex(), array, 0, length);
83+
offset = 0;
84+
}
85+
MessageNano prototype = clazz.newInstance();
86+
out.add(MessageNano.mergeFrom(prototype, array, offset, length));
87+
}
88+
}

codec/src/main/java/io/netty/handler/codec/protobuf/ProtobufEncoder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 The Netty Project
2+
* Copyright 2015 The Netty Project
33
*
44
* The Netty Project licenses this file to you under the Apache License,
55
* version 2.0 (the "License"); you may not use this file except in compliance
@@ -50,7 +50,8 @@
5050
* and then you can use a {@code MyMessage} instead of a {@link ByteBuf}
5151
* as a message:
5252
* <pre>
53-
* void channelRead({@link ChannelHandlerContext} ctx, MyMessage req) {
53+
* void channelRead({@link ChannelHandlerContext} ctx, Object msg) {
54+
* MyMessage req = (MyMessage) msg;
5455
* MyMessage res = MyMessage.newBuilder().setText(
5556
* "Did you say '" + req.getText() + "'?").build();
5657
* ch.write(res);
@@ -60,8 +61,8 @@
6061
@Sharable
6162
public class ProtobufEncoder extends MessageToMessageEncoder<MessageLiteOrBuilder> {
6263
@Override
63-
protected void encode(
64-
ChannelHandlerContext ctx, MessageLiteOrBuilder msg, List<Object> out) throws Exception {
64+
protected void encode(ChannelHandlerContext ctx, MessageLiteOrBuilder msg, List<Object> out)
65+
throws Exception {
6566
if (msg instanceof MessageLite) {
6667
out.add(wrappedBuffer(((MessageLite) msg).toByteArray()));
6768
return;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2015 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.codec.protobuf;
17+
18+
import com.google.protobuf.nano.CodedOutputByteBufferNano;
19+
import com.google.protobuf.nano.MessageNano;
20+
21+
import java.util.List;
22+
23+
import io.netty.buffer.ByteBuf;
24+
import io.netty.channel.ChannelHandler;
25+
import io.netty.channel.ChannelHandlerContext;
26+
import io.netty.channel.ChannelPipeline;
27+
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
28+
import io.netty.handler.codec.LengthFieldPrepender;
29+
import io.netty.handler.codec.MessageToMessageEncoder;
30+
31+
/**
32+
* Encodes the requested <a href="http://code.google.com/p/protobuf/">Google
33+
* Protocol Buffers</a> {@link MessageNano} into a
34+
* {@link ByteBuf}. A typical setup for TCP/IP would be:
35+
* <pre>
36+
* {@link ChannelPipeline} pipeline = ...;
37+
*
38+
* // Decoders
39+
* pipeline.addLast("frameDecoder",
40+
* new {@link LengthFieldBasedFrameDecoder}(1048576, 0, 4, 0, 4));
41+
* pipeline.addLast("protobufDecoder",
42+
* new {@link ProtobufDecoderNano}(MyMessage.getDefaultInstance()));
43+
*
44+
* // Encoder
45+
* pipeline.addLast("frameEncoder", new {@link LengthFieldPrepender}(4));
46+
* pipeline.addLast("protobufEncoder", new {@link ProtobufEncoderNano}());
47+
* </pre>
48+
* and then you can use a {@code MyMessage} instead of a {@link ByteBuf}
49+
* as a message:
50+
* <pre>
51+
* void channelRead({@link ChannelHandlerContext} ctx, Object msg) {
52+
* MyMessage req = (MyMessage) msg;
53+
* MyMessage res = MyMessage.newBuilder().setText(
54+
* "Did you say '" + req.getText() + "'?").build();
55+
* ch.write(res);
56+
* }
57+
* </pre>
58+
*/
59+
@ChannelHandler.Sharable
60+
public class ProtobufEncoderNano extends MessageToMessageEncoder<MessageNano> {
61+
@Override
62+
protected void encode(
63+
ChannelHandlerContext ctx, MessageNano msg, List<Object> out) throws Exception {
64+
final int size = msg.getSerializedSize();
65+
final ByteBuf buffer = ctx.alloc().heapBuffer(size, size);
66+
final byte[] array = buffer.array();
67+
CodedOutputByteBufferNano cobbn = CodedOutputByteBufferNano.newInstance(array,
68+
buffer.arrayOffset(), buffer.capacity());
69+
msg.writeTo(cobbn);
70+
buffer.writerIndex(size);
71+
out.add(buffer);
72+
}
73+
}

0 commit comments

Comments
 (0)