Class: Async::IO::TCPSocket
- Inherits:
-
IPSocket
- Object
- Wrapper
- Generic
- BasicSocket
- IPSocket
- Async::IO::TCPSocket
- Includes:
- Peer
- Defined in:
- lib/async/io/tcp_socket.rb
Overview
Asynchronous TCP socket wrapper.
Direct Known Subclasses
Defined Under Namespace
Classes: StreamWrapper
Constant Summary
Constants inherited from Generic
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
Attributes inherited from Generic
Instance Method Summary collapse
-
#initialize(remote_host, remote_port = nil, local_host = nil, local_port = nil) ⇒ TCPSocket
constructor
A new instance of TCPSocket.
- #read(size) ⇒ Object
- #write ⇒ Object
Methods included from Peer
#connected?, #protocol, #sync, #sync=, #type
Methods inherited from Generic
#connected?, #dup, #nonblock, #nonblock=, #nonblock?, #wait, wrap, wrap_blocking_method, wraps
Constructor Details
#initialize(remote_host, remote_port = nil, local_host = nil, local_port = nil) ⇒ TCPSocket
Returns a new instance of TCPSocket.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/async/io/tcp_socket.rb', line 57 def initialize(remote_host, remote_port = nil, local_host = nil, local_port = nil) if remote_host.is_a? ::TCPSocket super(remote_host) else remote_address = Addrinfo.tcp(remote_host, remote_port) local_address = Addrinfo.tcp(local_host, local_port) if local_host # We do this unusual dance to avoid leaking an "open" socket instance. socket = Socket.connect(remote_address, local_address) fd = socket.fcntl(Fcntl::F_DUPFD) Async.logger.debug(self) {"Connected to #{remote_address.inspect}: #{fd}"} socket.close super(::TCPSocket.for_fd(fd)) # The equivalent blocking operation. Unfortunately there is no trivial way to make this non-blocking. # super(::TCPSocket.new(remote_host, remote_port, local_host, local_port)) end @buffer = Stream.new(StreamWrapper.new(self)) end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
85 86 87 |
# File 'lib/async/io/tcp_socket.rb', line 85 def buffer @buffer end |
Instance Method Details
#read(size) ⇒ Object
95 96 97 |
# File 'lib/async/io/tcp_socket.rb', line 95 def read(size) @buffer.read_partial(size) end |
#write ⇒ Object
89 90 91 92 93 |
# File 'lib/async/io/tcp_socket.rb', line 89 def write(*) @buffer.flush super end |