Class: SFML::UdpSocket
- Inherits:
-
Object
- Object
- SFML::UdpSocket
- Defined in:
- ext/network/udp_socket.c,
ext/network/udp_socket.c
Overview
A connectionless, unreliable, datagram-oriented socket for UDP communication. Datagrams may be lost, duplicated or arrive out of order, and are capped at .max_datagram_size bytes.
Class Method Summary collapse
-
.any_port ⇒ Integer
Returns the port value that makes #bind bind to an OS-chosen port.
-
.max_datagram_size ⇒ Integer
Returns the maximum size, in bytes, of a single UDP datagram.
Instance Method Summary collapse
-
#bind(port, address = IpAddress::ANY) ⇒ Symbol
Binds the socket to a local port, optionally restricted to a specific local network interface via
address, so it can receive datagrams. -
#blocking=(value) ⇒ Boolean
Enables or disables blocking mode according to
value. -
#blocking? ⇒ Boolean
Returns
trueif the socket is in blocking mode. -
#new ⇒ UdpSocket
constructor
Creates a new, unbound UDP socket.
-
#local_port ⇒ Integer
Returns the local port the socket is bound to.
-
#receive(max_length = 1024) ⇒ Array, ...
Returns [data, IpAddress, port, status]; the address and port are those of the sender.
-
#receive_packet(packet) ⇒ Array, ...
Fills
packet(replacing its previous contents) with the next datagram received. -
#send(data, address, port) ⇒ Symbol
Sends a single datagram to a remote peer.
-
#send_packet(packet, address, port) ⇒ Symbol
Sends
packetas a datagram to the givenaddressandport. -
#unbind ⇒ self
Unbinds the socket from its local port.
Constructor Details
#new ⇒ UdpSocket
Creates a new, unbound UDP socket.
56 57 58 |
# File 'ext/network/udp_socket.c', line 56 static VALUE UdpSocket_initialize(VALUE self) { return self; } |
Class Method Details
.any_port ⇒ Integer
Returns the port value that makes #bind bind to an OS-chosen port.
68 69 70 |
# File 'ext/network/udp_socket.c', line 68 static VALUE UdpSocket_any_port(VALUE klass) { return UINT2NUM(sfUdpSocket_anyPort()); } |
.max_datagram_size ⇒ Integer
Returns the maximum size, in bytes, of a single UDP datagram.
80 81 82 |
# File 'ext/network/udp_socket.c', line 80 static VALUE UdpSocket_max_datagram_size(VALUE klass) { return UINT2NUM(sfUdpSocket_maxDatagramSize()); } |
Instance Method Details
#bind(port, address = IpAddress::ANY) ⇒ Symbol
Binds the socket to a local port, optionally restricted to a specific
local network interface via address, so it can receive datagrams.
124 125 126 127 128 129 130 131 132 |
# File 'ext/network/udp_socket.c', line 124
static VALUE UdpSocket_bind(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(
sfUdpSocket_bind(Get_UdpSocket_Struct(self), (unsigned short)NUM2INT(rb_port),
ip_address_from_rb(rb_address, sfIpAddress_Any)))));
}
|
#blocking=(value) ⇒ Boolean
Enables or disables blocking mode according to value.
101 102 103 104 |
# File 'ext/network/udp_socket.c', line 101
static VALUE UdpSocket_set_blocking(VALUE self, VALUE rb_value) {
sfUdpSocket_setBlocking(Get_UdpSocket_Struct(self), RTEST(rb_value));
return rb_value;
}
|
#blocking? ⇒ Boolean
Returns true if the socket is in blocking mode.
90 91 92 |
# File 'ext/network/udp_socket.c', line 90 static VALUE UdpSocket_blocking(VALUE self) { return BOOL2RB(sfUdpSocket_isBlocking(Get_UdpSocket_Struct(self))); } |
#local_port ⇒ Integer
Returns the local port the socket is bound to.
112 113 114 |
# File 'ext/network/udp_socket.c', line 112 static VALUE UdpSocket_local_port(VALUE self) { return UINT2NUM(sfUdpSocket_getLocalPort(Get_UdpSocket_Struct(self))); } |
#receive(max_length = 1024) ⇒ Array, ...
Returns [data, IpAddress, port, status]; the address and port are those of the sender.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'ext/network/udp_socket.c', line 170
static VALUE UdpSocket_receive(int argc, VALUE* argv, VALUE self) {
VALUE rb_max_length;
long max_length = 1024;
char* buffer;
size_t received = 0;
sfIpAddress remote_address = sfIpAddress_None;
unsigned short remote_port = 0;
sfSocketStatus status;
VALUE rb_result;
rb_scan_args(argc, argv, "01", &rb_max_length);
if (!NIL_P(rb_max_length)) {
max_length = NUM2LONG(rb_max_length);
}
if (max_length <= 0) {
rb_raise(rb_eArgError, "maximum length must be positive");
}
buffer = malloc((size_t)max_length);
if (buffer == NULL) {
rb_raise(rb_eNoMemError, "failed to allocate receive buffer");
}
status = sfUdpSocket_receive(Get_UdpSocket_Struct(self), buffer, (size_t)max_length, &received,
&remote_address, &remote_port);
rb_result = rb_ary_new_capa(4);
rb_ary_push(rb_result, rb_str_new(buffer, (long)received));
rb_ary_push(rb_result, ip_address_to_rb(remote_address));
rb_ary_push(rb_result, UINT2NUM(remote_port));
rb_ary_push(rb_result, ID2SYM(rb_intern(socket_status_name(status))));
free(buffer);
return rb_result;
}
|
#receive_packet(packet) ⇒ Array, ...
Fills packet (replacing its previous contents) with the next datagram
received. Returns [IpAddress, port, status], the address and port of the
sender.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'ext/network/udp_socket.c', line 238
static VALUE UdpSocket_receive_packet(VALUE self, VALUE rb_packet) {
sfIpAddress remote_address = sfIpAddress_None;
unsigned short remote_port = 0;
sfSocketStatus status;
VALUE rb_result;
if (!rb_obj_is_kind_of(rb_packet, Get_Klass_Packet())) {
rb_raise(rb_eTypeError, "expected an SFML::Packet");
}
status = sfUdpSocket_receivePacket(Get_UdpSocket_Struct(self), Get_Packet_Struct(rb_packet),
&remote_address, &remote_port);
rb_result = rb_ary_new_capa(3);
rb_ary_push(rb_result, ip_address_to_rb(remote_address));
rb_ary_push(rb_result, UINT2NUM(remote_port));
rb_ary_push(rb_result, ID2SYM(rb_intern(socket_status_name(status))));
return rb_result;
}
|
#send(data, address, port) ⇒ Symbol
Sends a single datagram to a remote peer. data must be no larger than
.max_datagram_size bytes.
153 154 155 156 157 158 159 |
# File 'ext/network/udp_socket.c', line 153
static VALUE UdpSocket_send(VALUE self, VALUE rb_data, VALUE rb_address, VALUE rb_port) {
StringValue(rb_data);
return ID2SYM(rb_intern(socket_status_name(sfUdpSocket_send(
Get_UdpSocket_Struct(self), RSTRING_PTR(rb_data), (size_t)RSTRING_LEN(rb_data),
ip_address_from_rb(rb_address, sfIpAddress_None), (unsigned short)NUM2INT(rb_port)))));
}
|
#send_packet(packet, address, port) ⇒ Symbol
Sends packet as a datagram to the given address and port.
218 219 220 221 222 223 224 225 226 |
# File 'ext/network/udp_socket.c', line 218
static VALUE UdpSocket_send_packet(VALUE self, VALUE rb_packet, VALUE rb_address, VALUE rb_port) {
if (!rb_obj_is_kind_of(rb_packet, Get_Klass_Packet())) {
rb_raise(rb_eTypeError, "expected an SFML::Packet");
}
return ID2SYM(rb_intern(socket_status_name(sfUdpSocket_sendPacket(
Get_UdpSocket_Struct(self), Get_Packet_Struct(rb_packet),
ip_address_from_rb(rb_address, sfIpAddress_None), (unsigned short)NUM2INT(rb_port)))));
}
|
#unbind ⇒ self
Unbinds the socket from its local port.
140 141 142 143 |
# File 'ext/network/udp_socket.c', line 140 static VALUE UdpSocket_unbind(VALUE self) { sfUdpSocket_unbind(Get_UdpSocket_Struct(self)); return self; } |