[protobuf] Can a message derive from another message?

2011-03-02 Thread ZHOU Xiaobo
Hi:
   My problem is that when I receive a packet I have to check one field( say 
'int32 COMMAND' ) to decide 
the type of the inner packet. For example:

message OutterPkt {
required fiexed32 Length = 1;
required int32 COMMAND = 2;
required string Content = 3;
}

message InnerPktA {
 required int32 UserID;
 required int16 UserGender;
}

message InnerPktB {
required string UserName = 1;
reruired string UserDescripton = 2;
}

my code to process this packet will be:

..
OuuterPkt outp;
outp.ParseFromString(inputbuf);
if (outp.COMMAND == 1) {
InnerPktA pkta;
pkta.ParseFromString(outp.Content());
..
} else if (outp.COMMAND == 2) {
InnerPktB pktb;
pktb.ParseFromString(outp.Content());
..
} else {

}


My question is: I think the pseudo code above is not efficient because it 
parses the buffer twice and there are too many memcpy.
Is there a better way to deal with this situation?
thanks.X

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.



Re: [protobuf] Can a message derive from another message?

2011-03-02 Thread Jason Hsueh
No, message inheritance is not supported. Have a look at
http://code.google.com/apis/protocolbuffers/docs/techniques.html#union and
see if that solves your use case.

On Wed, Mar 2, 2011 at 7:04 AM, ZHOU Xiaobo xb.z...@qq.com wrote:

 Hi:
   My problem is that when I receive a packet I have to check one field( say
 'int32 COMMAND' ) to decide
 the type of the inner packet. For example:

 message OutterPkt {
required fiexed32 Length = 1;
required int32 COMMAND = 2;
required string Content = 3;
 }

 message InnerPktA {
 required int32 UserID;
 required int16 UserGender;
 }

 message InnerPktB {
required string UserName = 1;
reruired string UserDescripton = 2;
 }

 my code to process this packet will be:

 ..
 OuuterPkt outp;
 outp.ParseFromString(inputbuf);
 if (outp.COMMAND == 1) {
InnerPktA pkta;
pkta.ParseFromString(outp.Content());
..
 } else if (outp.COMMAND == 2) {
InnerPktB pktb;
pktb.ParseFromString(outp.Content());
..
 } else {

 }


 My question is: I think the pseudo code above is not efficient because it
 parses the buffer twice and there are too many memcpy.
 Is there a better way to deal with this situation?
 thanks.X

 --
 You received this message because you are subscribed to the Google Groups
 Protocol Buffers group.
 To post to this group, send email to protobuf@googlegroups.com.
 To unsubscribe from this group, send email to
 protobuf+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/protobuf?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.



[protobuf] Re: Strange decode error when reading descriptor set file in python (ver.2.4.0)

2011-03-02 Thread haje01
It's my mistake.

I forgot to open file as 'Binary' mode.

  fds.ParseFromString(open('foo.desc', 'rb').read())

This solved the problem cleary.

Sorry..


On 3월2일, 오전10시28분, haje01 haj...@gmail.com wrote:
 I am using protobuf v2.4.0 in Windows and having problems.

 Say I have following message file. 'foo.proto'

   message School
   {
   optional int32 students = 1;
   }

 from which I generated description set file 'foo.desc' like this:

   protoc -o foo.desc foo.proto

 and, reading description set file..

   from google.protobuf.descriptor_pb2 import FileDescriptorSet

   fds = FileDescriptorSet()
   fds.ParseFromString(open('foo.desc').read())

 generates following error message:

 File build\bdist.win32\egg\google\protobuf\message.py, line 179,
 in ParseFromString
 File build\bdist.win32\egg\google\protobuf\internal
 \python_message.py, line 755, in MergeFromString
 File build\bdist.win32\egg\google\protobuf\internal
 \python_message.py, line 782, in InternalParse
 File build\bdist.win32\egg\google\protobuf\internal\decoder.py,
 line 521, in DecodeRepeatedField
   google.protobuf.message.DecodeError: Truncated message.

 Strangely enough, when I changed field name from 'students' to
 'student', no error occurred.

 Any ideas?

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.