Re: [protobuf] Java Newbie Question: Attaching a CodedInputStream to an NIO Socket

2011-01-13 Thread Evan Jones

On Jan 13, 2011, at 1:55 , Nader Salehi wrote:

It does help.  However, I seem to have some problem reading messages
that way.  My guess is that it has something to do with the fact that
the channels are non-blocking.  Is there any special thing to consider
when working with such channels?


You need to know the length of the message you are reading, then only  
call the parse method once you have the entire thing buffered. So you  
send the size first, then the message. On the receiving side, you read  
the size, then then you keep reading from the non-blocking socket  
until you have the whole thing buffered, then you parse it. I have  
code that actually does this that is open source, but it is research  
quality so it may not actually be helpful to others. But you may want  
to look at it:


http://people.csail.mit.edu/evanj/hg/index.cgi/javatxn/file/260423aa1c25/src/ca/evanjones/protorpc/ProtoConnection.java#l40

Evan

--
Evan Jones
http://evanjones.ca/

--
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] Java Newbie Question: Attaching a CodedInputStream to an NIO Socket

2011-01-12 Thread Nader Salehi
Hi Guys,

I have a Java-base TCP server which needs some modification.  It has
to accept messages as CodedInputStream from C++ clients that send
CodedOutputStream.  The server uses NIO class
java.nio.channels.SocketChannel to read from the socket.  What would
be the easiest way to attach a CodedInputStream to this?

Thanks,
Nader

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To post to this group, send email to proto...@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] Java Newbie Question: Attaching a CodedInputStream to an NIO Socket

2011-01-12 Thread Evan Jones

On Jan 12, 2011, at 12:57 , Nader Salehi wrote:

I have a Java-base TCP server which needs some modification.  It has
to accept messages as CodedInputStream from C++ clients that send
CodedOutputStream.  The server uses NIO class
java.nio.channels.SocketChannel to read from the socket.  What would
be the easiest way to attach a CodedInputStream to this?


I created a really thin InputStream implementation that wrapped my NIO  
ByteBuffer(s), then use CodedInputStream.newInstance(InputStream  
stream). You really only need to implement the read(byte[]  
destination, int offset, int length) method of this class, so it is  
actually pretty straightforward. There might be a better way but it  
works for me. Hope this helps,


Evan

--
Evan Jones
http://evanjones.ca/

--
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] Java Newbie Question: Attaching a CodedInputStream to an NIO Socket

2011-01-12 Thread Nader Salehi
On 1/13/2011 00:56 Evan Jones writes:
 On Jan 12, 2011, at 12:57 , Nader Salehi wrote:
  I have a Java-base TCP server which needs some modification.  It has
  to accept messages as CodedInputStream from C++ clients that send
  CodedOutputStream.  The server uses NIO class
  java.nio.channels.SocketChannel to read from the socket.  What would
  be the easiest way to attach a CodedInputStream to this?
 
 I created a really thin InputStream implementation that wrapped my NIO  
 ByteBuffer(s), then use CodedInputStream.newInstance(InputStream  
 stream). You really only need to implement the read(byte[]  
 destination, int offset, int length) method of this class, so it is  
 actually pretty straightforward. There might be a better way but it  
 works for me. Hope this helps,

It does help.  However, I seem to have some problem reading messages
that way.  My guess is that it has something to do with the fact that
the channels are non-blocking.  Is there any special thing to consider
when working with such channels?

Nader

 
 Evan
 
 --
 Evan Jones
 http://evanjones.ca/
 

-- 
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.