mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
init.c: is_socket
* ext/socket/init.c (is_socket): extract predicate to see if the given fd is a socket. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52605 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
8478b30267
commit
afe142997b
1 changed files with 16 additions and 11 deletions
|
@ -42,24 +42,29 @@ rsock_raise_socket_error(const char *reason, int error)
|
||||||
rb_raise(rb_eSocket, "%s: %s", reason, gai_strerror(error));
|
rb_raise(rb_eSocket, "%s: %s", reason, gai_strerror(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
VALUE
|
#ifdef _WIN32
|
||||||
rsock_init_sock(VALUE sock, int fd)
|
#define is_socket(fd) rb_w32_is_socket(fd)
|
||||||
|
#else
|
||||||
|
static int
|
||||||
|
is_socket(int fd)
|
||||||
{
|
{
|
||||||
rb_io_t *fp;
|
|
||||||
#ifndef _WIN32
|
|
||||||
struct stat sbuf;
|
struct stat sbuf;
|
||||||
|
|
||||||
if (fstat(fd, &sbuf) < 0)
|
if (fstat(fd, &sbuf) < 0)
|
||||||
rb_sys_fail("fstat(2)");
|
rb_sys_fail("fstat(2)");
|
||||||
rb_update_max_fd(fd);
|
return S_ISSOCK(sbuf.st_mode);
|
||||||
if (!S_ISSOCK(sbuf.st_mode))
|
}
|
||||||
rb_raise(rb_eArgError, "not a socket file descriptor");
|
|
||||||
#else
|
|
||||||
rb_update_max_fd(fd);
|
|
||||||
if (!rb_w32_is_socket(fd))
|
|
||||||
rb_raise(rb_eArgError, "not a socket file descriptor");
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rsock_init_sock(VALUE sock, int fd)
|
||||||
|
{
|
||||||
|
rb_io_t *fp;
|
||||||
|
|
||||||
|
rb_update_max_fd(fd);
|
||||||
|
if (!is_socket(fd))
|
||||||
|
rb_raise(rb_eArgError, "not a socket file descriptor");
|
||||||
|
|
||||||
MakeOpenFile(sock, fp);
|
MakeOpenFile(sock, fp);
|
||||||
fp->fd = fd;
|
fp->fd = fd;
|
||||||
fp->mode = FMODE_READWRITE|FMODE_DUPLEX;
|
fp->mode = FMODE_READWRITE|FMODE_DUPLEX;
|
||||||
|
|
Loading…
Add table
Reference in a new issue