Class: Socket
- Inherits:
-
Object
- Object
- Socket
- Extended by:
- MogileFS::Util
- Defined in:
- lib/mogilefs/util.rb
Constant Summary collapse
- TCP_CORK =
3
Constants included from MogileFS::Util
Instance Attribute Summary collapse
-
#mogilefs_addr ⇒ Object
Returns the value of attribute mogilefs_addr.
-
#mogilefs_connected ⇒ Object
Returns the value of attribute mogilefs_connected.
Class Method Summary collapse
-
.mogilefs_new(host, port, timeout = 5.0) ⇒ Object
Like TCPSocket.new(host, port), but with an explicit timeout (and we don’t care for local address/port we’re binding to).
-
.mogilefs_new_nonblock(host, port) ⇒ Object
Creates a new (TCP) Socket and initiates (but does not wait for) the connection.
-
.mogilefs_new_request(host, port, request, timeout = 5.0) ⇒ Object
Makes a request on a new TCP Socket and returns with a readble socket within the given timeout.
Instance Method Summary collapse
- #mogilefs_init(host = nil, port = nil) ⇒ Object
-
#mogilefs_peername ⇒ Object
Socket lacks peeraddr method of the IPSocket/TCPSocket classes.
- #mogilefs_tcp_cork=(set) ⇒ Object
Methods included from MogileFS::Util
Instance Attribute Details
#mogilefs_addr ⇒ Object
Returns the value of attribute mogilefs_addr.
90 91 92 |
# File 'lib/mogilefs/util.rb', line 90 def mogilefs_addr @mogilefs_addr end |
#mogilefs_connected ⇒ Object
Returns the value of attribute mogilefs_connected.
90 91 92 |
# File 'lib/mogilefs/util.rb', line 90 def mogilefs_connected @mogilefs_connected end |
Class Method Details
.mogilefs_new(host, port, timeout = 5.0) ⇒ Object
Like TCPSocket.new(host, port), but with an explicit timeout (and we don’t care for local address/port we’re binding to). This raises MogileFS::Timeout if timeout expires
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mogilefs/util.rb', line 138 def mogilefs_new(host, port, timeout = 5.0) sock = mogilefs_new_nonblock(host, port) or return sock while timeout > 0 t0 = Time.now r = IO.select(nil, [sock], nil, timeout) return sock if r && r[1] && sock.mogilefs_init timeout -= (Time.now - t0) end sock.close rescue nil raise MogileFS::Timeout, 'socket write timeout' end |
.mogilefs_new_nonblock(host, port) ⇒ Object
Creates a new (TCP) Socket and initiates (but does not wait for) the connection
125 126 127 128 129 130 131 132 133 |
# File 'lib/mogilefs/util.rb', line 125 def mogilefs_new_nonblock(host, port) sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) sock.sync = true if defined?(Socket::TCP_NODELAY) sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) end sock.mogilefs_init(host, port) sock end |
.mogilefs_new_request(host, port, request, timeout = 5.0) ⇒ Object
Makes a request on a new TCP Socket and returns with a readble socket within the given timeout. This raises MogileFS::Timeout if timeout expires
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/mogilefs/util.rb', line 157 def mogilefs_new_request(host, port, request, timeout = 5.0) t0 = Time.now sock = mogilefs_new(host, port, timeout) syswrite_full(sock, request, timeout) timeout -= (Time.now - t0) raise MogileFS::Timeout, 'socket read timeout' if timeout < 0 r = IO.select([sock], nil, nil, timeout) return sock if r && r[0] raise MogileFS::Timeout, 'socket read timeout' end |
Instance Method Details
#mogilefs_init(host = nil, port = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/mogilefs/util.rb', line 106 def mogilefs_init(host = nil, port = nil) return true if defined?(@mogilefs_connected) @mogilefs_addr = Socket.sockaddr_in(port, host).freeze if port && host begin connect_nonblock(@mogilefs_addr) @mogilefs_connected = true rescue Errno::EINPROGRESS nil rescue Errno::EISCONN @mogilefs_connected = true end end |
#mogilefs_peername ⇒ Object
Socket lacks peeraddr method of the IPSocket/TCPSocket classes
102 103 104 |
# File 'lib/mogilefs/util.rb', line 102 def mogilefs_peername Socket.unpack_sockaddr_in(getpeername).reverse.map {|x| x.to_s }.join(':') end |