Class: Nonnative::SocketPair

Inherits:
Object
  • Object
show all
Defined in:
lib/nonnative/socket_pair.rb

Overview

Base socket-pair implementation used by TCP proxies.

A socket-pair connects an accepted local socket to a remote upstream socket and forwards bytes in both directions until one side closes.

This is used by FaultInjectionProxy to implement pass-through forwarding, and is subclassed to inject failures (close immediately, delay reads, corrupt writes, etc).

The proxy argument is expected to provide host and port for the upstream connection (typically a ConfigurationProxy).

Instance Method Summary collapse

Constructor Details

#initialize(proxy) ⇒ SocketPair



22
23
24
# File 'lib/nonnative/socket_pair.rb', line 22

def initialize(proxy)
  @proxy = proxy
end

Instance Method Details

#connect(local_socket) ⇒ void

This method returns an undefined value.

Connects the given local socket to an upstream socket and pipes data until the connection ends.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nonnative/socket_pair.rb', line 30

def connect(local_socket)
  remote_socket = create_remote_socket

  loop do
    ready = select([local_socket, remote_socket], nil, nil)

    break if pipe?(ready, local_socket, remote_socket)
    break if pipe?(ready, remote_socket, local_socket)
  end
ensure
  Nonnative.logger.info "finished connect for local socket '#{local_socket.inspect}' and '#{remote_socket&.inspect}' for 'socket_pair'"

  local_socket.close
  remote_socket&.close
end