sfml3.rb

Class: SFML::TcpListener

Inherits:
Object
  • Object
show all
Defined in:
ext/network/tcp_listener.c,
ext/network/tcp_listener.c

Overview

Listens for and accepts incoming TCP connections, producing connected TcpSocket instances.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new ⇒ TcpListener

Creates a new TCP listener that is not yet listening.



56
57
58
# File 'ext/network/tcp_listener.c', line 56

static VALUE TcpListener_initialize(VALUE self) {
    return self;
}

Class Method Details

.any_port ⇒ Integer

Returns the port value that makes #listen bind to an OS-chosen port.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    a port value that #listen interprets as "let the OS pick an available port"



68
69
70
# File 'ext/network/tcp_listener.c', line 68

static VALUE TcpListener_any_port(VALUE klass) {
    return UINT2NUM(sfTcpListener_anyPort());
}

Instance Method Details

#accept ⇒ Array, Symbol

Blocks (unless #blocking? is false) waiting for a client to connect.

Returns ].

Returns:

  • (Array, Symbol) —

    ]

Returns:

  • (Array(TcpSocket, Symbol)) —

    the connected socket (or nil if the status isn't :done), and a SocketStatus name



141
142
143
144
145
146
147
148
149
150
# File 'ext/network/tcp_listener.c', line 141

static VALUE TcpListener_accept(VALUE self) {
    sfTcpSocket* socket = NULL;
    sfSocketStatus status = sfTcpListener_accept(Get_TcpListener_Struct(self), &socket);
    VALUE rb_result = rb_ary_new_capa(2);

    rb_ary_push(rb_result, status == sfSocketDone ? tcp_socket_from_handle(socket) : Qnil);
    rb_ary_push(rb_result, ID2SYM(rb_intern(socket_status_name(status))));

    return rb_result;
}

#blocking=(value) ⇒ Boolean

Enables or disables blocking mode according to value.

Returns:

  • (Boolean) —

    value



89
90
91
92
# File 'ext/network/tcp_listener.c', line 89

static VALUE TcpListener_set_blocking(VALUE self, VALUE rb_value) {
    sfTcpListener_setBlocking(Get_TcpListener_Struct(self), RTEST(rb_value));
    return rb_value;
}

#blocking? ⇒ Boolean

Returns true if the listener is in blocking mode.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


78
79
80
# File 'ext/network/tcp_listener.c', line 78

static VALUE TcpListener_blocking(VALUE self) {
    return BOOL2RB(sfTcpListener_isBlocking(Get_TcpListener_Struct(self)));
}

#close ⇒ self

Stops listening and closes the underlying socket.

Returns:

  • (self)

Returns:

  • (self)


129
130
131
132
# File 'ext/network/tcp_listener.c', line 129

static VALUE TcpListener_close(VALUE self) {
    sfTcpListener_close(Get_TcpListener_Struct(self));
    return self;
}

#listen(port, address = IpAddress::ANY) ⇒ Symbol

Starts listening for incoming connections on port, optionally restricted to a specific local network interface via address.

Returns:

  • (Symbol)

Returns:

  • (Symbol) —

    a SocketStatus name, :done on success



113
114
115
116
117
118
119
120
121
# File 'ext/network/tcp_listener.c', line 113

static VALUE TcpListener_listen(int argc, VALUE* argv, VALUE self) {
    VALUE rb_port, rb_address;

    rb_scan_args(argc, argv, "11", &rb_port, &rb_address);

    return ID2SYM(rb_intern(socket_status_name(
        sfTcpListener_listen(Get_TcpListener_Struct(self), (unsigned short)NUM2INT(rb_port),
                             ip_address_from_rb(rb_address, sfIpAddress_Any)))));
}

#local_port ⇒ Integer

Returns the port the listener is bound to, or 0 if it isn't listening.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    the port the listener is bound to, or 0 if it isn't listening



101
102
103
# File 'ext/network/tcp_listener.c', line 101

static VALUE TcpListener_local_port(VALUE self) {
    return UINT2NUM(sfTcpListener_getLocalPort(Get_TcpListener_Struct(self)));
}