Feature #11242 ยป 0001-socket-allow-explicit-buffer-for-recv-and-recv_nonbl.patch
ext/socket/basicsocket.c | ||
---|---|---|
/*
|
||
* call-seq:
|
||
* basicsocket.recv(maxlen) => mesg
|
||
* basicsocket.recv(maxlen, flags) => mesg
|
||
* basicsocket.recv(maxlen[, flags[, outbuf]]) => mesg
|
||
*
|
||
* Receives a message.
|
||
*
|
||
... | ... | |
*
|
||
* _flags_ should be a bitwise OR of Socket::MSG_* constants.
|
||
*
|
||
* _outbuf_ will contain only the received data after the method call
|
||
* even if it is not empty at the beginning.
|
||
*
|
||
* UNIXSocket.pair {|s1, s2|
|
||
* s1.puts "Hello World"
|
||
* p s2.recv(4) #=> "Hell"
|
ext/socket/init.c | ||
---|---|---|
return (VALUE)ret;
|
||
}
|
||
static VALUE
|
||
rsock_strbuf(VALUE str, long buflen)
|
||
{
|
||
long len;
|
||
if (NIL_P(str)) return rb_tainted_str_new(0, buflen);
|
||
StringValue(str);
|
||
len = RSTRING_LEN(str);
|
||
if (len >= buflen) {
|
||
rb_str_modify(str);
|
||
} else {
|
||
rb_str_modify_expand(str, buflen - len);
|
||
}
|
||
rb_str_set_len(str, buflen);
|
||
return str;
|
||
}
|
||
static VALUE
|
||
recvfrom_locktmp(VALUE v)
|
||
{
|
||
struct recvfrom_arg *arg = (struct recvfrom_arg *)v;
|
||
return rb_thread_io_blocking_region(recvfrom_blocking, arg, arg->fd);
|
||
}
|
||
VALUE
|
||
rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
|
||
{
|
||
rb_io_t *fptr;
|
||
VALUE str, klass;
|
||
VALUE str;
|
||
struct recvfrom_arg arg;
|
||
VALUE len, flg;
|
||
long buflen;
|
||
long slen;
|
||
rb_scan_args(argc, argv, "11", &len, &flg);
|
||
rb_scan_args(argc, argv, "12", &len, &flg, &str);
|
||
if (flg == Qnil) arg.flags = 0;
|
||
else arg.flags = NUM2INT(flg);
|
||
buflen = NUM2INT(len);
|
||
str = rsock_strbuf(str, buflen);
|
||
GetOpenFile(sock, fptr);
|
||
if (rb_io_read_pending(fptr)) {
|
||
... | ... | |
}
|
||
arg.fd = fptr->fd;
|
||
arg.alen = (socklen_t)sizeof(arg.buf);
|
||
arg.str = str = rb_tainted_str_new(0, buflen);
|
||
klass = RBASIC(str)->klass;
|
||
rb_obj_hide(str);
|
||
arg.str = str;
|
||
while (rb_io_check_closed(fptr),
|
||
rsock_maybe_wait_fd(arg.fd),
|
||
(slen = BLOCKING_REGION_FD(recvfrom_blocking, &arg)) < 0) {
|
||
(slen = (long)rb_str_locktmp_ensure(str, recvfrom_locktmp,
|
||
(VALUE)&arg)) < 0) {
|
||
if (!rb_io_wait_readable(fptr->fd)) {
|
||
rb_sys_fail("recvfrom(2)");
|
||
}
|
||
if (RBASIC(str)->klass || RSTRING_LEN(str) != buflen) {
|
||
rb_raise(rb_eRuntimeError, "buffer string modified");
|
||
}
|
||
}
|
||
rb_obj_reveal(str, klass);
|
||
if (slen < RSTRING_LEN(str)) {
|
||
if (slen != RSTRING_LEN(str)) {
|
||
rb_str_set_len(str, slen);
|
||
}
|
||
rb_obj_taint(str);
|
||
... | ... | |
VALUE opts = Qnil;
|
||
socklen_t len0;
|
||
rb_scan_args(argc, argv, "11:", &len, &flg, &opts);
|
||
rb_scan_args(argc, argv, "12:", &len, &flg, &str, &opts);
|
||
if (flg == Qnil) flags = 0;
|
||
else flags = NUM2INT(flg);
|
||
buflen = NUM2INT(len);
|
||
str = rsock_strbuf(str, buflen);
|
||
#ifdef MSG_DONTWAIT
|
||
/* MSG_DONTWAIT avoids the race condition between fcntl and recvfrom.
|
||
... | ... | |
}
|
||
fd = fptr->fd;
|
||
str = rb_tainted_str_new(0, buflen);
|
||
rb_io_check_closed(fptr);
|
||
if (!MSG_DONTWAIT_RELIABLE)
|
||
... | ... | |
}
|
||
rb_sys_fail("recvfrom(2)");
|
||
}
|
||
if (slen < RSTRING_LEN(str)) {
|
||
if (slen != RSTRING_LEN(str)) {
|
||
rb_str_set_len(str, slen);
|
||
}
|
||
rb_obj_taint(str);
|
ext/socket/unixsocket.c | ||
---|---|---|
/*
|
||
* call-seq:
|
||
* unixsocket.recvfrom(maxlen [, flags]) => [mesg, unixaddress]
|
||
* unixsocket.recvfrom(maxlen [, flags[, outbuf]) => [mesg, unixaddress]
|
||
*
|
||
* Receives a message via _unixsocket_.
|
||
*
|
||
... | ... | |
*
|
||
* _flags_ should be a bitwise OR of Socket::MSG_* constants.
|
||
*
|
||
* _outbuf_ will contain only the received data after the method call
|
||
* even if it is not empty at the beginning.
|
||
*
|
||
* s1 = Socket.new(:UNIX, :DGRAM, 0)
|
||
* s1_ai = Addrinfo.unix("/tmp/sock1")
|
||
* s1.bind(s1_ai)
|
string.c | ||
---|---|---|
return str;
|
||
}
|
||
VALUE
|
||
RUBY_FUNC_EXPORTED VALUE
|
||
rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg)
|
||
{
|
||
rb_str_locktmp(str);
|
test/socket/test_nonblock.rb | ||
---|---|---|
}
|
||
mesg = u1.recv_nonblock(100)
|
||
assert_equal("", mesg)
|
||
buf = "short"
|
||
out = "hello world" * 4
|
||
out.freeze
|
||
u2.send(out, 0, u1.getsockname)
|
||
IO.select [u1]
|
||
rv = u1.recv_nonblock(100, 0, buf)
|
||
assert_equal rv.object_id, buf.object_id
|
||
assert_equal out, rv
|
||
assert_equal out, buf
|
||
ensure
|
||
u1.close if u1
|
||
u2.close if u2
|
test/socket/test_unix.rb | ||
---|---|---|
assert_equal("", s1.recv(10))
|
||
assert_equal("", s1.recv(10))
|
||
assert_raise(IO::EAGAINWaitReadable) { s1.recv_nonblock(10) }
|
||
buf = ""
|
||
s2.send("BBBBBB", 0)
|
||
sleep 0.1
|
||
rv = s1.recv(100, 0, buf)
|
||
assert_equal buf.object_id, rv.object_id
|
||
assert_equal "BBBBBB", rv
|
||
ensure
|
||
s1.close if s1
|
||
s2.close if s2
|
||
-
|