Class: Rendezvous::Socket::CustomSocket

Inherits:
Socket
  • Object
show all
Defined in:
lib/rendezvous/socket/custom_socket.rb

Instance Method Summary collapse

Constructor Details

#initializeCustomSocket

Returns a new instance of CustomSocket.



6
7
8
9
10
11
12
# File 'lib/rendezvous/socket/custom_socket.rb', line 6

def initialize
  super(AF_INET, SOCK_STREAM, 0)
  setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  if defined?(SO_REUSEPORT)
    setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
  end
end

Instance Method Details

#acceptObject



38
39
40
41
# File 'lib/rendezvous/socket/custom_socket.rb', line 38

def accept
  $stderr.puts "attempting to accept" if $DEBUG
  super[0]
end

#accept_nonblockObject



43
44
45
46
# File 'lib/rendezvous/socket/custom_socket.rb', line 43

def accept_nonblock
  $stderr.puts "attempting to accept_nonblock" if $DEBUG
  super[0]
end

#addrObject



48
49
50
# File 'lib/rendezvous/socket/custom_socket.rb', line 48

def addr
  ::Socket.unpack_sockaddr_in(getsockname)
end

#bind(port = 0) ⇒ Object



19
20
21
22
23
24
# File 'lib/rendezvous/socket/custom_socket.rb', line 19

def bind(port = 0)
  ip = ::Socket.ip_address_list.detect{|a| a.ipv4_private? }.ip_address
  $stderr.puts "attempting to bind to #{ip}:#{port}" if $DEBUG
  addr_local = ::Socket.pack_sockaddr_in(port, ip)
  super(addr_local)
end

#connect(ip, port) ⇒ Object



26
27
28
29
30
# File 'lib/rendezvous/socket/custom_socket.rb', line 26

def connect(ip, port)
  $stderr.puts "attempting to connect to #{ip}:#{port}" if $DEBUG
  addr_remote = ::Socket.pack_sockaddr_in(port, ip)
  super(addr_remote)
end

#connect_nonblock(ip, port) ⇒ Object



32
33
34
35
36
# File 'lib/rendezvous/socket/custom_socket.rb', line 32

def connect_nonblock(ip, port)
  $stderr.puts "attempting to connect_nonblock to #{ip}:#{port}" if $DEBUG
  addr_remote = ::Socket.pack_sockaddr_in(port, ip)
  super(addr_remote)
end

#listen(buf) ⇒ Object



14
15
16
17
# File 'lib/rendezvous/socket/custom_socket.rb', line 14

def listen(buf)
  $stderr.puts "attempting to listen" if $DEBUG
  super(buf)
end

#local_portObject



52
53
54
# File 'lib/rendezvous/socket/custom_socket.rb', line 52

def local_port
  addr[0]
end