quiet mismatched indentation warnings
[rainbows.git] / lib / rainbows / revactor / client / tee_socket.rb
blob31b8dec1038acdefb870a47bc514ffb5b216e7ed
1 # -*- encoding: binary -*-
2 # :enddoc:
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
9   def initialize(socket)
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
13   end
15   def leftover
16     @rbuf.read
17   end
19   # Revactor socket reads always return an unspecified amount,
20   # sometimes too much
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
28     tmp = @socket.read
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])
37   rescue EOFError
38   end
40   # just proxy any remaining methods TeeInput may use
41   def close
42     @socket.close
43   end
44 end