2009-01-16 23:11:27 -05:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
udpsocket.c -
|
|
|
|
|
|
|
|
created at: Thu Mar 31 12:21:29 JST 1994
|
|
|
|
|
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "rubysocket.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* UDPSocket.new([address_family]) => socket
|
|
|
|
*
|
|
|
|
* Creates a new UDPSocket object.
|
|
|
|
*
|
|
|
|
* _address_family_ should be an integer, a string or a symbol:
|
|
|
|
* Socket::AF_INET, "AF_INET", :INET, etc.
|
|
|
|
*
|
2016-10-04 23:57:32 -04:00
|
|
|
* require 'socket'
|
|
|
|
*
|
2009-01-16 23:11:27 -05:00
|
|
|
* UDPSocket.new #=> #<UDPSocket:fd 3>
|
|
|
|
* UDPSocket.new(Socket::AF_INET6) #=> #<UDPSocket:fd 4>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
udp_init(int argc, VALUE *argv, VALUE sock)
|
|
|
|
{
|
|
|
|
VALUE arg;
|
|
|
|
int family = AF_INET;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "01", &arg) == 1) {
|
2009-03-01 01:30:41 -05:00
|
|
|
family = rsock_family_arg(arg);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
2009-03-01 01:48:22 -05:00
|
|
|
fd = rsock_socket(family, SOCK_DGRAM, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (fd < 0) {
|
|
|
|
rb_sys_fail("socket(2) - udp");
|
|
|
|
}
|
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_init_sock(sock, fd);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct udp_arg
|
|
|
|
{
|
2014-02-19 04:38:24 -05:00
|
|
|
struct rb_addrinfo *res;
|
2015-10-11 20:11:37 -04:00
|
|
|
rb_io_t *fptr;
|
2009-01-16 23:11:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
2019-08-26 02:20:15 -04:00
|
|
|
udp_connect_internal(VALUE v)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2019-08-26 02:20:15 -04:00
|
|
|
struct udp_arg *arg = (void *)v;
|
2015-10-10 06:04:44 -04:00
|
|
|
rb_io_t *fptr;
|
|
|
|
int fd;
|
2009-01-16 23:11:27 -05:00
|
|
|
struct addrinfo *res;
|
|
|
|
|
2015-10-11 20:11:37 -04:00
|
|
|
rb_io_check_closed(fptr = arg->fptr);
|
2015-10-10 06:04:44 -04:00
|
|
|
fd = fptr->fd;
|
2014-02-19 04:38:24 -05:00
|
|
|
for (res = arg->res->ai; res; res = res->ai_next) {
|
2020-09-25 03:20:18 -04:00
|
|
|
if (rsock_connect(fd, res->ai_addr, res->ai_addrlen, 0, NULL) >= 0) {
|
2009-01-16 23:11:27 -05:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* udpsocket.connect(host, port) => 0
|
|
|
|
*
|
|
|
|
* Connects _udpsocket_ to _host_:_port_.
|
|
|
|
*
|
|
|
|
* This makes possible to send without destination address.
|
|
|
|
*
|
|
|
|
* u1 = UDPSocket.new
|
|
|
|
* u1.bind("127.0.0.1", 4913)
|
|
|
|
* u2 = UDPSocket.new
|
|
|
|
* u2.connect("127.0.0.1", 4913)
|
|
|
|
* u2.send "uuuu", 0
|
|
|
|
* p u1.recvfrom(10) #=> ["uuuu", ["AF_INET", 33230, "localhost", "127.0.0.1"]]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
udp_connect(VALUE sock, VALUE host, VALUE port)
|
|
|
|
{
|
|
|
|
struct udp_arg arg;
|
|
|
|
VALUE ret;
|
|
|
|
|
2015-10-11 20:11:37 -04:00
|
|
|
GetOpenFile(sock, arg.fptr);
|
2015-10-12 23:14:13 -04:00
|
|
|
arg.res = rsock_addrinfo(host, port, rsock_fd_family(arg.fptr->fd), SOCK_DGRAM, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
ret = rb_ensure(udp_connect_internal, (VALUE)&arg,
|
2010-02-26 07:06:17 -05:00
|
|
|
rsock_freeaddrinfo, (VALUE)arg.res);
|
2013-04-05 22:39:44 -04:00
|
|
|
if (!ret) rsock_sys_fail_host_port("connect(2)", host, port);
|
2009-01-16 23:11:27 -05:00
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
|
2015-10-10 06:04:44 -04:00
|
|
|
static VALUE
|
2019-08-26 02:20:15 -04:00
|
|
|
udp_bind_internal(VALUE v)
|
2015-10-10 06:04:44 -04:00
|
|
|
{
|
2019-08-26 02:20:15 -04:00
|
|
|
struct udp_arg *arg = (void *)v;
|
2015-10-10 06:04:44 -04:00
|
|
|
rb_io_t *fptr;
|
|
|
|
int fd;
|
|
|
|
struct addrinfo *res;
|
|
|
|
|
2015-10-11 20:11:37 -04:00
|
|
|
rb_io_check_closed(fptr = arg->fptr);
|
2015-10-10 06:04:44 -04:00
|
|
|
fd = fptr->fd;
|
|
|
|
for (res = arg->res->ai; res; res = res->ai_next) {
|
|
|
|
if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* udpsocket.bind(host, port) #=> 0
|
|
|
|
*
|
|
|
|
* Binds _udpsocket_ to _host_:_port_.
|
|
|
|
*
|
|
|
|
* u1 = UDPSocket.new
|
|
|
|
* u1.bind("127.0.0.1", 4913)
|
|
|
|
* u1.send "message-to-self", 0, "127.0.0.1", 4913
|
|
|
|
* p u1.recvfrom(10) #=> ["message-to", ["AF_INET", 4913, "localhost", "127.0.0.1"]]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
udp_bind(VALUE sock, VALUE host, VALUE port)
|
|
|
|
{
|
2015-10-10 06:04:44 -04:00
|
|
|
struct udp_arg arg;
|
|
|
|
VALUE ret;
|
2013-04-05 22:39:44 -04:00
|
|
|
|
2015-10-11 20:11:37 -04:00
|
|
|
GetOpenFile(sock, arg.fptr);
|
2015-10-12 23:14:13 -04:00
|
|
|
arg.res = rsock_addrinfo(host, port, rsock_fd_family(arg.fptr->fd), SOCK_DGRAM, 0);
|
2015-10-10 06:04:44 -04:00
|
|
|
ret = rb_ensure(udp_bind_internal, (VALUE)&arg,
|
|
|
|
rsock_freeaddrinfo, (VALUE)arg.res);
|
|
|
|
if (!ret) rsock_sys_fail_host_port("bind(2)", host, port);
|
2009-01-16 23:11:27 -05:00
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
|
2015-10-11 20:11:54 -04:00
|
|
|
struct udp_send_arg {
|
|
|
|
struct rb_addrinfo *res;
|
|
|
|
rb_io_t *fptr;
|
|
|
|
struct rsock_send_arg sarg;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
2019-08-26 02:20:15 -04:00
|
|
|
udp_send_internal(VALUE v)
|
2015-10-11 20:11:54 -04:00
|
|
|
{
|
2019-08-26 02:20:15 -04:00
|
|
|
struct udp_send_arg *arg = (void *)v;
|
2015-10-11 20:11:54 -04:00
|
|
|
rb_io_t *fptr;
|
|
|
|
int n;
|
|
|
|
struct addrinfo *res;
|
|
|
|
|
|
|
|
rb_io_check_closed(fptr = arg->fptr);
|
|
|
|
for (res = arg->res->ai; res; res = res->ai_next) {
|
|
|
|
retry:
|
|
|
|
arg->sarg.fd = fptr->fd;
|
|
|
|
arg->sarg.to = res->ai_addr;
|
|
|
|
arg->sarg.tolen = res->ai_addrlen;
|
|
|
|
rsock_maybe_fd_writable(arg->sarg.fd);
|
|
|
|
n = (int)BLOCKING_REGION_FD(rsock_sendto_blocking, &arg->sarg);
|
|
|
|
if (n >= 0) {
|
|
|
|
return INT2FIX(n);
|
|
|
|
}
|
|
|
|
if (rb_io_wait_writable(fptr->fd)) {
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* udpsocket.send(mesg, flags, host, port) => numbytes_sent
|
|
|
|
* udpsocket.send(mesg, flags, sockaddr_to) => numbytes_sent
|
|
|
|
* udpsocket.send(mesg, flags) => numbytes_sent
|
|
|
|
*
|
|
|
|
* Sends _mesg_ via _udpsocket_.
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2009-01-16 23:11:27 -05:00
|
|
|
* _flags_ should be a bitwise OR of Socket::MSG_* constants.
|
|
|
|
*
|
|
|
|
* u1 = UDPSocket.new
|
|
|
|
* u1.bind("127.0.0.1", 4913)
|
|
|
|
*
|
|
|
|
* u2 = UDPSocket.new
|
|
|
|
* u2.send "hi", 0, "127.0.0.1", 4913
|
|
|
|
*
|
|
|
|
* mesg, addr = u1.recvfrom(10)
|
|
|
|
* u1.send mesg, 0, addr[3], addr[1]
|
|
|
|
*
|
|
|
|
* p u2.recv(100) #=> "hi"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
udp_send(int argc, VALUE *argv, VALUE sock)
|
|
|
|
{
|
|
|
|
VALUE flags, host, port;
|
2015-10-11 20:11:54 -04:00
|
|
|
struct udp_send_arg arg;
|
|
|
|
VALUE ret;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
if (argc == 2 || argc == 3) {
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_bsock_send(argc, argv, sock);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
2015-10-11 20:11:54 -04:00
|
|
|
rb_scan_args(argc, argv, "4", &arg.sarg.mesg, &flags, &host, &port);
|
|
|
|
|
|
|
|
StringValue(arg.sarg.mesg);
|
|
|
|
GetOpenFile(sock, arg.fptr);
|
|
|
|
arg.sarg.fd = arg.fptr->fd;
|
|
|
|
arg.sarg.flags = NUM2INT(flags);
|
2015-10-12 23:14:13 -04:00
|
|
|
arg.res = rsock_addrinfo(host, port, rsock_fd_family(arg.fptr->fd), SOCK_DGRAM, 0);
|
2015-10-11 20:11:54 -04:00
|
|
|
ret = rb_ensure(udp_send_internal, (VALUE)&arg,
|
|
|
|
rsock_freeaddrinfo, (VALUE)arg.res);
|
|
|
|
if (!ret) rsock_sys_fail_host_port("sendto(2)", host, port);
|
|
|
|
return ret;
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
socket: avoid arg parsing in rsock_s_recvfrom_nonblock
* ext/socket/init.c (rsock_s_recvfrom_nonblock):
avoid arg parsing with C API
[ruby-core:71439] [Feature #11339]
* ext/socket/basicsocket.c (bsock_recv_nonblock):
adjust for above change, make private
* ext/socket/socket.c (sock_recvfrom_nonblock): ditto
* ext/socket/udpsocket.c (udp_recvfrom_nonblock): ditto
* ext/socket/lib/socket.rb (BasicSocket#recv_nonblock):
new wrapper for private method, move RDoc
(Socket#recvfrom_nonblock): ditto
(UDPSocket#recvfrom_nonblock): ditto
Note, not adding bm_recv_nonblock.rb to benchmark/ directory
since it is non-portable. It is only in this commit message.
Benchmark results + code
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52540) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52540) [x86_64-linux]
-----------------------------------------------------------
recv_nonblock
require 'socket'
nr = 1000000
msg = 'hello world'
buf = ''
size = msg.bytesize
UNIXSocket.pair(:SEQPACKET) do |a, b|
nr.times do
a.sendmsg(msg)
b.recv_nonblock(size, 0, buf, exception: false)
end
end
-----------------------------------------------------------
raw data:
[["recv_nonblock",
[[1.83511221408844,
1.8703329525887966,
1.8448856547474861,
1.859263762831688,
1.8331583738327026],
[1.5637447573244572,
1.4062932096421719,
1.4247371144592762,
1.4108827747404575,
1.4802536629140377]]]]
Elapsed time: 16.530452496 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recv_nonblock 1.833 1.406
Speedup ratio: compare with the result of `a' (greater is better)
name b
recv_nonblock 1.304
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52598 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-16 18:25:03 -05:00
|
|
|
/* :nodoc: */
|
2009-01-16 23:11:27 -05:00
|
|
|
static VALUE
|
socket: avoid arg parsing in rsock_s_recvfrom_nonblock
* ext/socket/init.c (rsock_s_recvfrom_nonblock):
avoid arg parsing with C API
[ruby-core:71439] [Feature #11339]
* ext/socket/basicsocket.c (bsock_recv_nonblock):
adjust for above change, make private
* ext/socket/socket.c (sock_recvfrom_nonblock): ditto
* ext/socket/udpsocket.c (udp_recvfrom_nonblock): ditto
* ext/socket/lib/socket.rb (BasicSocket#recv_nonblock):
new wrapper for private method, move RDoc
(Socket#recvfrom_nonblock): ditto
(UDPSocket#recvfrom_nonblock): ditto
Note, not adding bm_recv_nonblock.rb to benchmark/ directory
since it is non-portable. It is only in this commit message.
Benchmark results + code
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52540) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52540) [x86_64-linux]
-----------------------------------------------------------
recv_nonblock
require 'socket'
nr = 1000000
msg = 'hello world'
buf = ''
size = msg.bytesize
UNIXSocket.pair(:SEQPACKET) do |a, b|
nr.times do
a.sendmsg(msg)
b.recv_nonblock(size, 0, buf, exception: false)
end
end
-----------------------------------------------------------
raw data:
[["recv_nonblock",
[[1.83511221408844,
1.8703329525887966,
1.8448856547474861,
1.859263762831688,
1.8331583738327026],
[1.5637447573244572,
1.4062932096421719,
1.4247371144592762,
1.4108827747404575,
1.4802536629140377]]]]
Elapsed time: 16.530452496 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recv_nonblock 1.833 1.406
Speedup ratio: compare with the result of `a' (greater is better)
name b
recv_nonblock 1.304
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52598 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-16 18:25:03 -05:00
|
|
|
udp_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str, VALUE ex)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
socket: avoid arg parsing in rsock_s_recvfrom_nonblock
* ext/socket/init.c (rsock_s_recvfrom_nonblock):
avoid arg parsing with C API
[ruby-core:71439] [Feature #11339]
* ext/socket/basicsocket.c (bsock_recv_nonblock):
adjust for above change, make private
* ext/socket/socket.c (sock_recvfrom_nonblock): ditto
* ext/socket/udpsocket.c (udp_recvfrom_nonblock): ditto
* ext/socket/lib/socket.rb (BasicSocket#recv_nonblock):
new wrapper for private method, move RDoc
(Socket#recvfrom_nonblock): ditto
(UDPSocket#recvfrom_nonblock): ditto
Note, not adding bm_recv_nonblock.rb to benchmark/ directory
since it is non-portable. It is only in this commit message.
Benchmark results + code
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52540) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52540) [x86_64-linux]
-----------------------------------------------------------
recv_nonblock
require 'socket'
nr = 1000000
msg = 'hello world'
buf = ''
size = msg.bytesize
UNIXSocket.pair(:SEQPACKET) do |a, b|
nr.times do
a.sendmsg(msg)
b.recv_nonblock(size, 0, buf, exception: false)
end
end
-----------------------------------------------------------
raw data:
[["recv_nonblock",
[[1.83511221408844,
1.8703329525887966,
1.8448856547474861,
1.859263762831688,
1.8331583738327026],
[1.5637447573244572,
1.4062932096421719,
1.4247371144592762,
1.4108827747404575,
1.4802536629140377]]]]
Elapsed time: 16.530452496 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recv_nonblock 1.833 1.406
Speedup ratio: compare with the result of `a' (greater is better)
name b
recv_nonblock 1.304
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52598 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-16 18:25:03 -05:00
|
|
|
return rsock_s_recvfrom_nonblock(sock, len, flg, str, ex, RECV_IP);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-03-22 12:15:21 -04:00
|
|
|
rsock_init_udpsocket(void)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2011-08-15 19:08:39 -04:00
|
|
|
/*
|
|
|
|
* Document-class: UDPSocket < IPSocket
|
|
|
|
*
|
|
|
|
* UDPSocket represents a UDP/IP socket.
|
|
|
|
*
|
|
|
|
*/
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_cUDPSocket = rb_define_class("UDPSocket", rb_cIPSocket);
|
|
|
|
rb_define_method(rb_cUDPSocket, "initialize", udp_init, -1);
|
|
|
|
rb_define_method(rb_cUDPSocket, "connect", udp_connect, 2);
|
|
|
|
rb_define_method(rb_cUDPSocket, "bind", udp_bind, 2);
|
|
|
|
rb_define_method(rb_cUDPSocket, "send", udp_send, -1);
|
socket: avoid arg parsing in rsock_s_recvfrom_nonblock
* ext/socket/init.c (rsock_s_recvfrom_nonblock):
avoid arg parsing with C API
[ruby-core:71439] [Feature #11339]
* ext/socket/basicsocket.c (bsock_recv_nonblock):
adjust for above change, make private
* ext/socket/socket.c (sock_recvfrom_nonblock): ditto
* ext/socket/udpsocket.c (udp_recvfrom_nonblock): ditto
* ext/socket/lib/socket.rb (BasicSocket#recv_nonblock):
new wrapper for private method, move RDoc
(Socket#recvfrom_nonblock): ditto
(UDPSocket#recvfrom_nonblock): ditto
Note, not adding bm_recv_nonblock.rb to benchmark/ directory
since it is non-portable. It is only in this commit message.
Benchmark results + code
target 0: a (ruby 2.3.0dev (2015-11-12 trunk 52540) [x86_64-linux])
target 1: b (ruby 2.3.0dev (2015-11-12 avoid-kwarg-capi 52540) [x86_64-linux]
-----------------------------------------------------------
recv_nonblock
require 'socket'
nr = 1000000
msg = 'hello world'
buf = ''
size = msg.bytesize
UNIXSocket.pair(:SEQPACKET) do |a, b|
nr.times do
a.sendmsg(msg)
b.recv_nonblock(size, 0, buf, exception: false)
end
end
-----------------------------------------------------------
raw data:
[["recv_nonblock",
[[1.83511221408844,
1.8703329525887966,
1.8448856547474861,
1.859263762831688,
1.8331583738327026],
[1.5637447573244572,
1.4062932096421719,
1.4247371144592762,
1.4108827747404575,
1.4802536629140377]]]]
Elapsed time: 16.530452496 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name a b
recv_nonblock 1.833 1.406
Speedup ratio: compare with the result of `a' (greater is better)
name b
recv_nonblock 1.304
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52598 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-16 18:25:03 -05:00
|
|
|
|
|
|
|
/* for ext/socket/lib/socket.rb use only: */
|
|
|
|
rb_define_private_method(rb_cUDPSocket,
|
|
|
|
"__recvfrom_nonblock", udp_recvfrom_nonblock, 4);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|