1 # -*- encoding: binary -*-
2 # internal implementation details here, do not rely on them in your code
4 class MogileFS::Socket < Socket
5 include MogileFS::SocketCommon
7 def self.start(host, port)
8 sock = new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
10 sock.connect_nonblock(sockaddr_in(port, host))
11 rescue Errno::EINPROGRESS
13 sock.post_init(host, port)
16 def self.tcp(host, port, timeout = 5)
17 sock = start(host, port)
18 unless IO.select(nil, [ sock ], nil, timeout)
20 raise MogileFS::Timeout, 'socket connect timeout'
25 def timed_read(len, dst = "", timeout = 5)
27 IO.select([self], nil, nil, timeout) or unreadable_socket!
28 return read_nonblock(len, dst)
35 def timed_peek(len, dst, timeout = 5)
37 IO.select([self], nil, nil, timeout) or unreadable_socket!
38 rc = recv_nonblock(len, Socket::MSG_PEEK)
39 return rc.empty? ? nil : dst.replace(rc)
47 def timed_write(buf, timeout = 5)
51 rc = write_nonblock(buf)
52 return expect if rc == buf.bytesize
55 if buf.respond_to?(:byteslice)
56 buf = buf.byteslice(rc, buf.bytesize)
58 if buf.respond_to?(:encoding) && buf.encoding != Encoding::BINARY
59 buf = buf.dup.force_encoding(Encoding::BINARY)
61 buf = buf.slice(rc, buf.bytesize)
64 IO.select(nil, [self], nil, timeout) or
65 request_truncated!(written, expect)