1 # -*- encoding: binary -*-
2 # internal implementation details here, do not rely on them in your code
7 class MogileFS::Socket < Socket
8 include MogileFS::SocketCommon
10 def self.start(host, port)
11 sock = new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
13 sock.connect_nonblock(sockaddr_in(port, host))
14 rescue Errno::EINPROGRESS
16 sock.post_init(host, port)
19 def self.tcp(host, port, timeout = 5)
20 sock = start(host, port)
21 unless IO.select(nil, [ sock ], nil, timeout)
23 raise MogileFS::Timeout, 'socket connect timeout'
28 def timed_read(len, dst = "", timeout = 5)
30 return read_nonblock(len, dst)
32 wait(timeout) or unreadable_socket!
38 def timed_peek(len, dst, timeout = 5)
40 rc = recv_nonblock(len, Socket::MSG_PEEK)
41 return rc.empty? ? nil : dst.replace(rc)
43 wait(timeout) or unreadable_socket!
50 def timed_write(buf, timeout = 5)
54 rc = write_nonblock(buf)
55 return if rc == buf.bytesize
58 if buf.respond_to?(:byteslice)
59 buf = buf.byteslice(rc, buf.bytesize)
61 if buf.respond_to?(:encoding) && buf.encoding != Encoding::BINARY
62 buf = buf.dup.force_encoding(Encoding::BINARY)
64 buf = buf.slice(rc, buf.bytesize)
67 IO.select(nil, [self], nil, timeout) or
68 request_truncated!(written, expect)