Class: Libuv::TCP

Inherits:
Handle show all
Includes:
Net, Stream
Defined in:
lib/libuv/tcp.rb

Defined Under Namespace

Classes: Socket4, Socket6, SocketBase

Constant Summary collapse

KEEPALIVE_ARGUMENT_ERROR =
"delay must be an Integer".freeze

Constants included from Stream

Stream::BACKLOG_ERROR, Stream::CLOSED_HANDLE_ERROR, Stream::STREAM_CLOSED_ERROR, Stream::WRITE_ERROR

Constants included from Net

Net::IP_ARGUMENT_ERROR, Net::PORT_ARGUMENT_ERROR

Constants included from Assertions

Assertions::MSG_NO_PROC

Constants inherited from Q::Promise

Q::Promise::MAKE_PROMISE

Instance Attribute Summary

Attributes inherited from Handle

#closed, #storage

Instance Method Summary collapse

Methods included from Stream

#listen, #progress, #readable?, #shutdown, #start_read, #stop_read, #writable?, #write

Methods inherited from Handle

#active?, #close, #closing?, #ref, #unref

Methods included from Assertions

#assert_block, #assert_boolean, #assert_type

Methods included from Resource

#check_result, #check_result!, #resolve, #to_ptr

Methods inherited from Q::DeferredPromise

#then

Methods inherited from Q::Promise

#catch, #finally, #progress

Constructor Details

#initialize(loop, acceptor = nil) ⇒ TCP

Returns a new instance of TCP.



12
13
14
15
16
17
18
19
20
# File 'lib/libuv/tcp.rb', line 12

def initialize(loop, acceptor = nil)
    @loop = loop

    tcp_ptr = ::Libuv::Ext.create_handle(:uv_tcp)
    error = check_result(::Libuv::Ext.tcp_init(loop.handle, tcp_ptr))
    error = check_result(::Libuv::Ext.accept(acceptor, tcp_ptr)) if acceptor && error.nil?
    
    super(tcp_ptr, error)
end

Instance Method Details

#accept(callback = nil, &blk) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/libuv/tcp.rb', line 36

def accept(callback = nil, &blk)
    tcp = nil
    begin
        raise RuntimeError, CLOSED_HANDLE_ERROR if @closed
        tcp = TCP.new(loop, handle)
    rescue Exception => e
        @loop.log :info, :tcp_accept_failed, e
    end
    if tcp
        begin
            (callback || blk).call(tcp)
        rescue Exception => e
            @loop.log :error, :tcp_accept_cb, e
        end
    end
    nil
end

#bind(ip, port, callback = nil, &blk) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/libuv/tcp.rb', line 22

def bind(ip, port, callback = nil, &blk)
    return if @closed
    @on_listen = callback || blk
    assert_type(String, ip, IP_ARGUMENT_ERROR)
    assert_type(Integer, port, PORT_ARGUMENT_ERROR)

    begin
        @tcp_socket = create_socket(IPAddr.new(ip), port)
        @tcp_socket.bind
    rescue Exception => e
        reject(e)
    end
end

#connect(ip, port, callback = nil, &blk) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/libuv/tcp.rb', line 54

def connect(ip, port, callback = nil, &blk)
    return if @closed
    @callback = callback || blk
    assert_type(String, ip, IP_ARGUMENT_ERROR)
    assert_type(Integer, port, PORT_ARGUMENT_ERROR)
    
    begin
        @tcp_socket = create_socket(IPAddr.new(ip), port)
        @tcp_socket.connect(callback(:on_connect))
    rescue Exception => e
        reject(e)
    end
end

#disable_keepalive ⇒ Object



98
99
100
101
# File 'lib/libuv/tcp.rb', line 98

def disable_keepalive
    return if @closed
    check_result ::Libuv::Ext.tcp_keepalive(handle, 0, 0)
end

#disable_nodelay ⇒ Object



87
88
89
90
# File 'lib/libuv/tcp.rb', line 87

def disable_nodelay
    return if @closed
    check_result ::Libuv::Ext.tcp_nodelay(handle, 0)
end

#disable_simultaneous_accepts ⇒ Object



108
109
110
111
# File 'lib/libuv/tcp.rb', line 108

def disable_simultaneous_accepts
    return if @closed
    check_result ::Libuv::Ext.tcp_simultaneous_accepts(handle, 0)
end

#enable_keepalive(delay) ⇒ Object



92
93
94
95
96
# File 'lib/libuv/tcp.rb', line 92

def enable_keepalive(delay)
    return if @closed
    assert_type(Integer, delay, KEEPALIVE_ARGUMENT_ERROR)
    check_result ::Libuv::Ext.tcp_keepalive(handle, 1, delay)
end

#enable_nodelay ⇒ Object



82
83
84
85
# File 'lib/libuv/tcp.rb', line 82

def enable_nodelay
    return if @closed
    check_result ::Libuv::Ext.tcp_nodelay(handle, 1)
end

#enable_simultaneous_accepts ⇒ Object



103
104
105
106
# File 'lib/libuv/tcp.rb', line 103

def enable_simultaneous_accepts
    return if @closed
    check_result ::Libuv::Ext.tcp_simultaneous_accepts(handle, 1)
end

#peername ⇒ Object



75
76
77
78
79
80
# File 'lib/libuv/tcp.rb', line 75

def peername
    return [] if @closed
    sockaddr, len = get_sockaddr_and_len
    check_result! ::Libuv::Ext.tcp_getpeername(handle, sockaddr, len)
    get_ip_and_port(::Libuv::Sockaddr.new(sockaddr), len.get_int(0))
end

#sockname ⇒ Object



68
69
70
71
72
73
# File 'lib/libuv/tcp.rb', line 68

def sockname
    return [] if @closed
    sockaddr, len = get_sockaddr_and_len
    check_result! ::Libuv::Ext.tcp_getsockname(handle, sockaddr, len)
    get_ip_and_port(::Libuv::Sockaddr.new(sockaddr), len.get_int(0))
end