1

CocoaAsyncSocket/GCDAsyncSocket が使用されている tcp 経由で netty サーバーに protobuf メッセージを送信する必要がある小さなアプリを作成しています。

これを行うには、次のコードを使用します。

Message_LoginMessageRequest_Builder *msg_tmp_login_build=[Message_LoginMessageRequest builder];
[msg_tmp_login_build setPhoneNumber:input_phone.text];
[msg_tmp_login_build setEmail:input_email.text];
[msg_tmp_login_build setPassword:input_password.text];

Message_Builder *msg_tmp_build=[Message builder];
[msg_tmp_build setType:Message_MessageTypeLoginReq];
[msg_tmp_build setLoginRequest:msg_tmp_login_build.build];

// send the message to the socket: we need to code the stream first
Message *msg=[msg_tmp_build build];
NSMutableData *msg_data=[[NSMutableData alloc] initWithCapacity:[[msg data] length]];
PBCodedOutputStream *coded_output_stream=[PBCodedOutputStream streamWithData:msg_data];
[msg writeToCodedOutputStream:coded_output_stream];
[socket writeData:msg_data withTimeout:-1 tag:Message_MessageTypeLoginReq];

ただし、コードは writeToCodedOutputStream で常に「領域不足」エラーを受け取りました。詳細なトレース情報は、writeToCodedOutputStream/writeEnum/writeTag/writeRawVariant32/writeRawByte/flush です。

何か助けはありますか?ありがとう!

4

1 に答える 1

0

netty/codec/src/main/java/io/netty/handler/codec/protobuf/ にある protobuf の netty コーデックのコードを読んで、自分で答えを見つけました。

netty protobuf では、protobuf メッセージの長さを説明するメッセージ ヘッダー (後でメッセージ本文を参照) をメッセージ本文の前に挿入する必要があります。メッセージ本文の長さは、RawVariant32 タイプでコーディングする必要があります。詳細については、以下に引用されている ProtobufVariant32FrameDecoder.java の例を参照してください。

/**
 * A decoder that splits the received {@link ByteBuf}s dynamically by the
 * value of the Google Protocol Buffers
 * <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html#varints">Base
 * 128 Varints</a> integer length field in the message.  For example:
 * <pre>
 * BEFORE DECODE (302 bytes)       AFTER DECODE (300 bytes)
 * +--------+---------------+      +---------------+
 * | Length | Protobuf Data |----->| Protobuf Data |
 * | 0xAC02 |  (300 bytes)  |      |  (300 bytes)  |
 * +--------+---------------+      +---------------+
 * </pre>
 *
 * @see CodedInputStream
 */
public class ProtobufVarint32FrameDecoder extends ByteToMessageDecoder {

    // TODO maxFrameLength + safe skip + fail-fast option
    //      (just like LengthFieldBasedFrameDecoder)

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        in.markReaderIndex();
        final byte[] buf = new byte[5];
        for (int i = 0; i < buf.length; i ++) {
            if (!in.isReadable()) {
                in.resetReaderIndex();
                return;
            }

            buf[i] = in.readByte();
            if (buf[i] >= 0) {
                int length = CodedInputStream.newInstance(buf, 0, i + 1).readRawVarint32();
                if (length < 0) {
                    throw new CorruptedFrameException("negative length: " + length);
                }

                if (in.readableBytes() < length) {
                    in.resetReaderIndex();
                    return;
                } else {
                    out.add(in.readBytes(length));
                    return;
                }
            }
        }

        // Couldn't find the byte whose MSB is off.
        throw new CorruptedFrameException("length wider than 32-bit");
    }
}

詳細については、私の個人ページをご覧ください: http://167.88.47.13/?p=254

于 2015-03-17T04:59:06.620 に答える