Class: Socket

Inherits:
Object
  • Object
show all
Extended by:
MogileFS::Util
Defined in:
lib/mogilefs/util.rb

Constant Summary collapse

TCP_CORK =
3

Constants included from MogileFS::Util

MogileFS::Util::CHUNK_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MogileFS::Util

sysread_full, sysrwloop, syswrite_full

Instance Attribute Details

#mogilefs_addrObject

Returns the value of attribute mogilefs_addr.



113
114
115
# File 'lib/mogilefs/util.rb', line 113

def mogilefs_addr
  @mogilefs_addr
end

#mogilefs_connectedObject

Returns the value of attribute mogilefs_connected.



113
114
115
# File 'lib/mogilefs/util.rb', line 113

def mogilefs_connected
  @mogilefs_connected
end

#mogilefs_sizeObject

Returns the value of attribute mogilefs_size.



113
114
115
# File 'lib/mogilefs/util.rb', line 113

def mogilefs_size
  @mogilefs_size
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

Raises:



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/mogilefs/util.rb', line 161

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



148
149
150
151
152
153
154
155
156
# File 'lib/mogilefs/util.rb', line 148

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

Raises:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/mogilefs/util.rb', line 180

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)
  if timeout < 0
    sock.close rescue nil
    raise MogileFS::Timeout, 'socket read timeout'
  end
  r = IO.select([sock], nil, nil, timeout)
  return sock if r && r[0]

  sock.close rescue nil
  raise MogileFS::Timeout, 'socket read timeout'
end

Instance Method Details

#mogilefs_init(host = nil, port = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/mogilefs/util.rb', line 129

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_peernameObject

Socket lacks peeraddr method of the IPSocket/TCPSocket classes



125
126
127
# File 'lib/mogilefs/util.rb', line 125

def mogilefs_peername
  Socket.unpack_sockaddr_in(getpeername).reverse.map {|x| x.to_s }.join(':')
end

#mogilefs_tcp_cork=(set) ⇒ Object



117
118
119
120
121
122
# File 'lib/mogilefs/util.rb', line 117

def mogilefs_tcp_cork=(set)
  if defined?(TCP_CORK)
    self.setsockopt(SOL_TCP, TCP_CORK, set ? 1 : 0) rescue nil
  end
  set
end