1 # -*- encoding: binary -*-
4 # Revactor Sockets do not implement readpartial, so we emulate just
5 # enough to avoid mucking with TeeInput internals. Fortunately
6 # this code is not heavily used so we can usually avoid the overhead
7 # of adding a userspace buffer.
8 class Rainbows::Revactor::Client::TeeSocket
10 # IO::Buffer is used internally by Rev which Revactor is based on
11 # so we'll always have it available
12 @socket, @rbuf = socket, IO::Buffer.new
19 # Revactor socket reads always return an unspecified amount,
21 def kgio_read(length, dst = "")
22 return dst.replace("") if length == 0
24 # always check and return from the userspace buffer first
25 @rbuf.size > 0 and return dst.replace(@rbuf.read(length))
27 # read off the socket since there was nothing in rbuf
30 # we didn't read too much, good, just return it straight back
31 # to avoid needlessly wasting memory bandwidth
32 tmp.size <= length and return dst.replace(tmp)
34 # ugh, read returned too much
35 @rbuf << tmp[length, tmp.size]
36 dst.replace(tmp[0, length])
40 # just proxy any remaining methods TeeInput may use