3 # Copyright (c) 2006- Facebook
4 # Distributed under the Thrift Software License
6 # See accompanying file LICENSE or visit the Thrift site at:
7 # http://developers.facebook.com/thrift/
9 # Author: Mark Slee <mcslee@facebook.com>
12 require 'thrift/thrift'
14 class TTransportException < TException
24 def initialize(type=UNKNOWN, message=nil)
32 def isOpen(); nil; end
38 def read(sz); nil; end
44 chunk = read(sz - have)
51 def write(buf); nil; end
57 class TServerTransport
58 def listen(); nil; end
60 def accept(); nil; end
66 class TTransportFactory
67 def getTransport(trans)
72 class TBufferedTransport < TTransport
73 def initialize(transport)
74 @transport = transport
79 return @transport.isOpen()
91 return @transport.read(sz)
99 @transport.write(@wbuf)
105 class TBufferedTransportFactory < TTransportFactory
106 def getTransport(transport)
107 return TBufferedTransport.new(transport)
112 class TFramedTransport < TTransport
113 def initialize(transport, read=true, write=true)
114 @transport = transport
122 return @transport.isOpen
135 return @transport.read(sz)
142 if (@rbuf.length == 0)
147 if (sz > @rbuf.length)
154 out = @rbuf.slice(0, sz)
155 @rbuf = @rbuf.slice(sz, @rbuf.length)
160 def write(buf,sz=nil)
163 return @transport.write(buf);
166 if (sz != nil && sz < buf.length)
167 buf = buf.slice(0,sz)
175 # Writes the output buffer to the stream in the format of a 4-byte length
176 # followed by the actual data.
180 return @transport.flush
183 out = [@wbuf.length].pack('N')
185 @transport.write(out)
193 buf = @transport.readAll(4)
194 val = buf.unpack('N')
197 @rbuf = @transport.readAll(sz)
203 class TFramedTransportFactory < TTransportFactory
204 def getTransport(transport)
205 return TFramedTransport.new(transport)
209 class TMemoryBuffer < TTransport
210 def initialize(sz=1024)
235 def resetBuffer(new_buf = '')
238 @wpos = new_buf.length
249 return '' if avail == 0
253 give = avail if avail < len
255 ret = @buf.slice(@rpos,give)
264 @wpos += wbuf.length()