3

I would like to consume a stream in a thrift service, for example, in a service method to have a stream or something similar as an argument to the method (for example, to be able to serialize the result from an IDataReader to a stream, and then deserialize the reference to the data on the other server side).

I don't think that this is explicitly possible, but I was wondering if there was another way to achieve something similar.

Thanks.

Ian
  • 275
  • 6
  • 17
  • possible duplicate of [How to stream an image from Python to C++ using Apache Thrift](http://stackoverflow.com/questions/26739520/how-to-stream-an-image-from-python-to-c-using-apache-thrift) – JensG Jan 04 '15 at 17:54

1 Answers1

3

Apache thrift does not support sending streams. The closest you can get, is send a byte array.

To achieve stream-like experience using thrift, you can create an interface that returns the next part of the stream in a form of byte array.

In C# syntax it would look like

interface MyService
{     
   int OpenStream(string path);

   byte[] ReadNextBlock(int openedStreamId, long maxBlockSize);

}

OpenStream returns "stream id" that is passed to ReadNextBlock on each call. On your server-side you may hold a Dictionary(key - openStreamID, value - Stream) that will be used to keep the source stream open and read next blocks from it.

You can also create a helper class on client side that will be Stream descendant and will use OpenStream and ReadNextBlock to get the actual data.

alex
  • 12,464
  • 3
  • 46
  • 67
  • Thanks for that - my only reservation is that I would like the server to request more data when it needs it (similar to dataReader.Next(), or Stream.GetBytes()) - is there any way to do that? – Ian Apr 23 '13 at 14:28
  • Server already has a stream and it can do what he wants with it. Client can call ReadNextBlock repeatedly until empty array is returned. – alex Apr 23 '13 at 14:55