sfml3.rb

Class: SFML::SocketSelector

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

Overview

Watches a set of TcpListener, TcpSocket and UdpSocket instances and blocks until at least one of them is ready to read, so a single thread can multiplex several sockets without polling.

Instance Method Summary collapse

Constructor Details

#new ⇒ SocketSelector

Creates a new, empty socket selector.



55
56
57
# File 'ext/network/socket_selector.c', line 55

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

Instance Method Details

#add(socket) ⇒ self

Adds a TcpListener, TcpSocket or UdpSocket to watch for activity.

Returns:

  • (self)

Returns:

  • (self)

Raises:

  • (TypeError) —

    if socket is none of the above



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'ext/network/socket_selector.c', line 78

static VALUE SocketSelector_add(VALUE self, VALUE rb_socket) {
    sfSocketSelector* selector = Get_SocketSelector_Struct(self);

    if (rb_obj_is_kind_of(rb_socket, Get_Klass_TcpListener())) {
        sfSocketSelector_addTcpListener(selector, Get_TcpListener_Struct(rb_socket));
    } else if (rb_obj_is_kind_of(rb_socket, Get_Klass_TcpSocket())) {
        sfSocketSelector_addTcpSocket(selector, Get_TcpSocket_Struct(rb_socket));
    } else if (rb_obj_is_kind_of(rb_socket, Get_Klass_UdpSocket())) {
        sfSocketSelector_addUdpSocket(selector, Get_UdpSocket_Struct(rb_socket));
    } else {
        rb_raise(rb_eTypeError, "expected an SFML::TcpListener, TcpSocket or UdpSocket");
    }

    return self;
}

#clear ⇒ self

Removes all watched sockets.

Returns:

  • (self)

Returns:

  • (self)


124
125
126
127
# File 'ext/network/socket_selector.c', line 124

static VALUE SocketSelector_clear(VALUE self) {
    sfSocketSelector_clear(Get_SocketSelector_Struct(self));
    return self;
}

#copy ⇒ SocketSelector

Returns an independent copy of the selector.

Returns:

Returns:

  • (SocketSelector) —

    an independent copy with the same watched sockets



65
66
67
68
# File 'ext/network/socket_selector.c', line 65

static VALUE SocketSelector_copy(VALUE self) {
    return SocketSelector_wrap(Get_Klass_SocketSelector(),
                               sfSocketSelector_copy(Get_SocketSelector_Struct(self)));
}

#remove(socket) ⇒ self

Removes a socket previously added with #add.

Returns:

  • (self)

Returns:

  • (self)

Raises:

  • (TypeError) —

    if socket is not a TcpListener, TcpSocket or UdpSocket



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'ext/network/socket_selector.c', line 102

static VALUE SocketSelector_remove(VALUE self, VALUE rb_socket) {
    sfSocketSelector* selector = Get_SocketSelector_Struct(self);

    if (rb_obj_is_kind_of(rb_socket, Get_Klass_TcpListener())) {
        sfSocketSelector_removeTcpListener(selector, Get_TcpListener_Struct(rb_socket));
    } else if (rb_obj_is_kind_of(rb_socket, Get_Klass_TcpSocket())) {
        sfSocketSelector_removeTcpSocket(selector, Get_TcpSocket_Struct(rb_socket));
    } else if (rb_obj_is_kind_of(rb_socket, Get_Klass_UdpSocket())) {
        sfSocketSelector_removeUdpSocket(selector, Get_UdpSocket_Struct(rb_socket));
    } else {
        rb_raise(rb_eTypeError, "expected an SFML::TcpListener, TcpSocket or UdpSocket");
    }

    return self;
}

#tcp_listener_ready?(listener) ⇒ Boolean

Call after #wait returns true to check whether a given watched TcpListener is one of the sockets that became ready.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


158
159
160
161
# File 'ext/network/socket_selector.c', line 158

static VALUE SocketSelector_tcp_listener_ready(VALUE self, VALUE rb_socket) {
    return BOOL2RB(sfSocketSelector_isTcpListenerReady(Get_SocketSelector_Struct(self),
                                                       Get_TcpListener_Struct(rb_socket)));
}

#tcp_socket_ready?(socket) ⇒ Boolean

Call after #wait returns true to check whether a given watched TcpSocket is one of the sockets that became ready.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


171
172
173
174
# File 'ext/network/socket_selector.c', line 171

static VALUE SocketSelector_tcp_socket_ready(VALUE self, VALUE rb_socket) {
    return BOOL2RB(sfSocketSelector_isTcpSocketReady(Get_SocketSelector_Struct(self),
                                                     Get_TcpSocket_Struct(rb_socket)));
}

#udp_socket_ready?(socket) ⇒ Boolean

Call after #wait returns true to check whether a given watched UdpSocket is one of the sockets that became ready.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


184
185
186
187
# File 'ext/network/socket_selector.c', line 184

static VALUE SocketSelector_udp_socket_ready(VALUE self, VALUE rb_socket) {
    return BOOL2RB(sfSocketSelector_isUdpSocketReady(Get_SocketSelector_Struct(self),
                                                     Get_UdpSocket_Struct(rb_socket)));
}

#wait(timeout = Time.zero) ⇒ Boolean

Blocks until one of the watched sockets is ready to read, or timeout elapses. A zero timeout means wait forever.

Returns:

  • (Boolean)

Returns:

  • (Boolean) —

    whether a socket became ready (+false+ on timeout)



137
138
139
140
141
142
143
144
145
146
147
148
# File 'ext/network/socket_selector.c', line 137

static VALUE SocketSelector_wait(int argc, VALUE* argv, VALUE self) {
    VALUE rb_timeout;
    sfTime timeout = sfTime_Zero;

    rb_scan_args(argc, argv, "01", &rb_timeout);

    if (!NIL_P(rb_timeout)) {
        timeout = time_from_rb(rb_timeout);
    }

    return BOOL2RB(sfSocketSelector_wait(Get_SocketSelector_Struct(self), timeout));
}