Class: IO::Endpoint::ConnectedEndpoint

Inherits:
Generic
  • Object
show all
Defined in:
lib/io/endpoint/connected_endpoint.rb

Overview

Represents an endpoint that has been connected to a socket.

Instance Attribute Summary collapse

Attributes inherited from Generic

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#accept, #bind, #bound, #connected, #each, #hostname, #linger, #local_address, parse, #reuse_address?, #reuse_port?, #timeout, #with, #wrapper

Constructor Details

#initialize(endpoint, socket, **options) ⇒ ConnectedEndpoint

Initialize a new connected endpoint.



31
32
33
34
35
36
# File 'lib/io/endpoint/connected_endpoint.rb', line 31

def initialize(endpoint, socket, **options)
  super(**options)
  
  @endpoint = endpoint
  @socket = socket
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



39
40
41
# File 'lib/io/endpoint/connected_endpoint.rb', line 39

def endpoint
  @endpoint
end

#socketObject (readonly)

Returns the value of attribute socket.



41
42
43
# File 'lib/io/endpoint/connected_endpoint.rb', line 41

def socket
  @socket
end

#The original endpoint that was connected.(originalendpointthatwasconnected.) ⇒ Object (readonly)



39
# File 'lib/io/endpoint/connected_endpoint.rb', line 39

attr :endpoint

Class Method Details

.connected(endpoint, close_on_exec: false) ⇒ Object

Create a connected endpoint from an existing endpoint.

Parameters:

  • close_on_exec (Hash) (defaults to: false)

    a customizable set of options

Options Hash (close_on_exec:):

  • Whether (Boolean)

    to close the socket on exec.



19
20
21
22
23
24
25
# File 'lib/io/endpoint/connected_endpoint.rb', line 19

def self.connected(endpoint, close_on_exec: false)
  socket = endpoint.connect
  
  socket.close_on_exec = close_on_exec
  
  return self.new(endpoint, socket, **endpoint.options)
end

Instance Method Details

#closeObject

Close the connected socket.



69
70
71
72
73
74
# File 'lib/io/endpoint/connected_endpoint.rb', line 69

def close
  if @socket
    @socket.close
    @socket = nil
  end
end

#connect(wrapper = self.wrapper, &block) ⇒ Object

Connect using the already connected socket.



60
61
62
63
64
65
66
# File 'lib/io/endpoint/connected_endpoint.rb', line 60

def connect(wrapper = self.wrapper, &block)
  if block_given?
    yield @socket
  else
    return @socket.dup
  end
end

#inspectObject

Get a detailed string representation of the connected endpoint.



84
85
86
# File 'lib/io/endpoint/connected_endpoint.rb', line 84

def inspect
  "\#<#{self.class} #{@socket} connected for #{@endpoint}>"
end

#local_address_endpoint(**options) ⇒ Object

A endpoint for the local end of the bound socket.



45
46
47
# File 'lib/io/endpoint/connected_endpoint.rb', line 45

def local_address_endpoint(**options)
  AddressEndpoint.new(socket.to_io.local_address, **options)
end

#remote_address_endpoint(**options) ⇒ Object

A endpoint for the remote end of the bound socket.



51
52
53
# File 'lib/io/endpoint/connected_endpoint.rb', line 51

def remote_address_endpoint(**options)
  AddressEndpoint.new(socket.to_io.remote_address, **options)
end

#The socket that was connected.=(socketthatwasconnected. = (value)) ⇒ Object



41
# File 'lib/io/endpoint/connected_endpoint.rb', line 41

attr :socket

#to_sObject

Get a string representation of the connected endpoint.



78
79
80
# File 'lib/io/endpoint/connected_endpoint.rb', line 78

def to_s
  "connected:#{@endpoint}"
end