Class: Celluloid::IO::TCPSocket

Inherits:
Stream show all
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

Socket::Constants

Instance Attribute Summary

Attributes inherited from Stream

#sync

Class Method Summary collapse

Instance Method Summary collapse

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

new, #to_io, try_convert

Constructor Details

#initialize(remote_host, remote_port = nil, local_host = nil, local_port = nil) ⇒ TCPSocket #initialize(socket) ⇒ TCPSocket

Returns a new instance of TCPSocket.

Overloads:

  • #initialize(remote_host, remote_port = nil, local_host = nil, local_port = nil) ⇒ TCPSocket

    Opens a TCP connection to remote_host on remote_port. If local_host and local_port are specified, then those parameters are used on the local end to establish the connection.

    Parameters:

    • remote_host (String, Resolv::IPv4, Resolv::IPv6)
    • remote_port (Numeric) (defaults to: nil)
    • local_host (String) (defaults to: nil)
    • local_port (Numeric) (defaults to: nil)
  • #initialize(socket) ⇒ TCPSocket

    Wraps an already existing tcp socket.

    Parameters:

    • socket (::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

#addrResolv::IPv4, Resolv::IPv6

Returns:

  • (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