Class: Celluloid::IO::TCPSocket
- Extended by:
- Forwardable
- Defined in:
- lib/celluloid/io/tcp_socket.rb
Overview
TCPSocket with combined blocking and evented support
Constant Summary
Constants inherited from Socket
Instance Attribute Summary
Attributes inherited from Stream
Class Method Summary collapse
-
.from_ruby_socket(ruby_socket) ⇒ Object
deprecated
Deprecated.
Use #new instead.
-
.open(*args, &_block) ⇒ Object
Open a TCP socket, yielding it to the given block and closing it automatically when done (if a block is given).
Instance Method Summary collapse
- #addr ⇒ Resolv::IPv4, Resolv::IPv6
-
#initialize(*args) ⇒ TCPSocket
constructor
A new instance of TCPSocket.
-
#recv(maxlen, flags = nil) ⇒ Object
Receives a message.
-
#send(msg, flags, dest_sockaddr = nil) ⇒ Object
Send a message.
Methods inherited from Stream
#<<, #close, #each, #each_byte, #eof?, #flush, #getc, #gets, #print, #printf, #puts, #read, #readchar, #readline, #readlines, #readpartial, #sysread, #syswrite, #ungetc, #wait_readable, #wait_writable, #write
Methods inherited from Socket
Constructor Details
#initialize(remote_host, remote_port = nil, local_host = nil, local_port = nil) ⇒ TCPSocket #initialize(socket) ⇒ TCPSocket
Returns a new instance of TCPSocket.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/celluloid/io/tcp_socket.rb', line 45 def initialize(*args) if args.first.kind_of? ::BasicSocket # socket socket = args.first fail ArgumentError, "wrong number of arguments (#{args.size} for 1)" if args.size != 1 fail ArgumentError, "wrong kind of socket (#{socket.class} for TCPSocket)" unless socket.kind_of? ::TCPSocket super(socket) else super(create_socket(*args)) end end |
Class Method Details
.from_ruby_socket(ruby_socket) ⇒ Object
Deprecated.
Use #new instead.
Convert a Ruby TCPSocket into a Celluloid::IO::TCPSocket DEPRECATED: to be removed in a future release
28 29 30 |
# File 'lib/celluloid/io/tcp_socket.rb', line 28 def self.from_ruby_socket(ruby_socket) new(ruby_socket) end |
.open(*args, &_block) ⇒ Object
Open a TCP socket, yielding it to the given block and closing it automatically when done (if a block is given)
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/celluloid/io/tcp_socket.rb', line 14 def self.open(*args, &_block) sock = new(*args) return sock unless block_given? begin yield(sock) ensure sock.close end end |
Instance Method Details
#addr ⇒ Resolv::IPv4, Resolv::IPv6
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/celluloid/io/tcp_socket.rb', line 71 def addr socket = to_io ra = socket.remote_address if ra.ipv4? return Resolv::IPv4.create(ra.ip_address) elsif ra.ipv6? return Resolv::IPv6.create(ra.ip_address) else raise ArgumentError, "not an ip socket: #{socket.inspect}" end end |
#recv(maxlen, flags = nil) ⇒ Object
Receives a message
58 59 60 61 |
# File 'lib/celluloid/io/tcp_socket.rb', line 58 def recv(maxlen, flags = nil) fail NotImplementedError, "flags not supported" if flags && !flags.zero? readpartial(maxlen) end |
#send(msg, flags, dest_sockaddr = nil) ⇒ Object
Send a message
64 65 66 67 68 |
# File 'lib/celluloid/io/tcp_socket.rb', line 64 def send(msg, flags, dest_sockaddr = nil) fail NotImplementedError, "dest_sockaddr not supported" if dest_sockaddr fail NotImplementedError, "flags not supported" unless flags.zero? write(msg) end |