we are using Protobuf to communicate between two devices via Ad-hoc WiFi. 
One of the devices is running a Java application and the other a Python 
Here is an example of how we build our messages in python:
    def serialize(self):

        # create outer packet message
        packetMessage = protoPackets_pb2.PacketMessage()
        packetMessage.type = protoPackets_pb2.PacketMessage.ADVERT
        packetMessage.transactionID = self.transactionID
        # add advert-specific content
        packetMessage.advertMessage.finalDestinationIP = 
        packetMessage.advertMessage.ceil = self.ceil
        packetMessage.advertMessage.deadline = self.deadline
        packetMessage.advertMessage.fine = self.fine
        packetMessage.advertMessage.initialBudget = self.initialBudget

        return packetMessage.SerializeToString()

The problem we are having is the following:
1.We build an Object that we want to send to the Java Device on the python 
2.We serialize it as above (with SerializeToString()) 
2.1 We checked that we can successfully deserialize it in python, just to 
make sure that the error is not there
3. We send that over UDP to the Java device
4. The java device now receives the datagram packet and tries to parse the 
byte array into something useful to build an object with:

public Packet buildPacket(byte[] rawdata, Inet4Address sourceIP) throws 
InvalidProtocolBufferException {
        PacketMessage packetMessage = PacketMessage.parseFrom(rawdata);
        int type = packetMessage.getType().getNumber();
        Packet packet = null;
            switch (type) {
            case 0:
                packet = new Advert(packetMessage);
            case 1:

              etc. etc. etc. more code.

4.1 You see that parseFrom(rawdata) is called, rawdata being the byte array 
from the received datagram packet.
5. parseFrom causes an InvalidProtocolBufferException.
6. Upon closer inspection, we saw that the parser seems to be doing 
alright, successfully assembling the packet message. However the message is 
only  a few bytes long, and after that the 
parser just keeps reading the byte array(buffer, which is 128 byte long). 
The byte array is filled with 0's. Upon reading a 0 the exception occurs.
7. We tried to work around it with a dirty hack, but obv. this is not a 
good solution. in CodeInputStream.java, line 700 we changed the function 
isAtEnd() to return true in case of a zero:
  public boolean isAtEnd() throws IOException {
    // EVIL HACK: ToDo: Change BACK!
      return bufferPos == bufferSize && !refillBuffer(false) || 
buffer[bufferPos] == 0;
//    return bufferPos == bufferSize && !refillBuffer(false);
8. This solves our problem for one type of message we have, but in another 
type the byte array does not end with 0's but other numbers that are not 
part of our packetMessage, so it reads them until it reaches a zero and
stores them in some unknownFields variable. 

10. Obviously our solution is bad and we should have asked much sooner, but 
we are out of ideas and hope that you can help us.

If I was unclear with anything please dont hesitate to ask for more info, 
its late and I already typed this up once and deleted it by accident so Im 
not sure how helpful my description is.


You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to