Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

tcpserver.c
Go to the documentation of this file.
1 /************************************************
2
3 tcpserver.c -
4
5 created at: Thu Mar 31 12:21:29 JST 1994
6
7 Copyright (C) 1993-2007 Yukihiro Matsumoto
8
9 ************************************************/
10
11 #include "rubysocket.h"
12
13 /*
14 * call-seq:
15 * TCPServer.new([hostname,] port) => tcpserver
16 *
17 * Creates a new server socket bound to _port_.
18 *
19 * If _hostname_ is given, the socket is bound to it.
20 *
21 * serv = TCPServer.new("127.0.0.1", 28561)
22 * s = serv.accept
23 * s.puts Time.now
24 * s.close
25 *
26 * Internally, TCPServer.new calls getaddrinfo() function to
27 * obtain addresses.
28 * If getaddrinfo() returns multiple addresses,
29 * TCPServer.new tries to create a server socket for each address
30 * and returns first one that is successful.
31 *
32 */
33 static VALUE
34 tcp_svr_init(int argc, VALUE *argv, VALUE sock)
35 {
36 VALUE hostname, port;
37
38 rb_scan_args(argc, argv, "011", &hostname, &port);
39 return rsock_init_inetsock(sock, hostname, port, Qnil, Qnil, INET_SERVER);
40 }
41
42 /*
43 * call-seq:
44 * tcpserver.accept => tcpsocket
45 *
46 * Accepts an incoming connection. It returns a new TCPSocket object.
47 *
48 * TCPServer.open("127.0.0.1", 14641) {|serv|
49 * s = serv.accept
50 * s.puts Time.now
51 * s.close
52 * }
53 *
54 */
55 static VALUE
56 tcp_accept(VALUE sock)
57 {
58 rb_io_t *fptr;
59 union_sockaddr from;
60 socklen_t fromlen;
61
62 GetOpenFile(sock, fptr);
63 fromlen = (socklen_t)sizeof(from);
64 return rsock_s_accept(rb_cTCPSocket, fptr->fd, &from.addr, &fromlen);
65 }
66
67 /* :nodoc: */
68 static VALUE
69 tcp_accept_nonblock(VALUE sock, VALUE ex)
70 {
71 rb_io_t *fptr;
72 union_sockaddr from;
73 socklen_t len = (socklen_t)sizeof(from);
74
75 GetOpenFile(sock, fptr);
76 return rsock_s_accept_nonblock(rb_cTCPSocket, ex, fptr, &from.addr, &len);
77 }
78
79 /*
80 * call-seq:
81 * tcpserver.sysaccept => file_descriptor
82 *
83 * Returns a file descriptor of a accepted connection.
84 *
85 * TCPServer.open("127.0.0.1", 28561) {|serv|
86 * fd = serv.sysaccept
87 * s = IO.for_fd(fd)
88 * s.puts Time.now
89 * s.close
90 * }
91 *
92 */
93 static VALUE
94 tcp_sysaccept(VALUE sock)
95 {
96 rb_io_t *fptr;
97 union_sockaddr from;
98 socklen_t fromlen;
99
100 GetOpenFile(sock, fptr);
101 fromlen = (socklen_t)sizeof(from);
102 return rsock_s_accept(0, fptr->fd, &from.addr, &fromlen);
103 }
104
105 void
106 rsock_init_tcpserver(void)
107 {
108 /*
109 * Document-class: TCPServer < TCPSocket
110 *
111 * TCPServer represents a TCP/IP server socket.
112 *
113 * A simple TCP server may look like:
114 *
115 * require 'socket'
116 *
117 * server = TCPServer.new 2000 # Server bind to port 2000
118 * loop do
119 * client = server.accept # Wait for a client to connect
120 * client.puts "Hello !"
121 * client.puts "Time is #{Time.now}"
122 * client.close
123 * end
124 *
125 * A more usable server (serving multiple clients):
126 *
127 * require 'socket'
128 *
129 * server = TCPServer.new 2000
130 * loop do
131 * Thread.start(server.accept) do |client|
132 * client.puts "Hello !"
133 * client.puts "Time is #{Time.now}"
134 * client.close
135 * end
136 * end
137 *
138 */
139 rb_cTCPServer = rb_define_class("TCPServer", rb_cTCPSocket);
140 rb_define_method(rb_cTCPServer, "accept", tcp_accept, 0);
141 rb_define_private_method(rb_cTCPServer,
142 "__accept_nonblock", tcp_accept_nonblock, 1);
143 rb_define_method(rb_cTCPServer, "sysaccept", tcp_sysaccept, 0);
144 rb_define_method(rb_cTCPServer, "initialize", tcp_svr_init, -1);
145 rb_define_method(rb_cTCPServer, "listen", rsock_sock_listen, 1); /* in socket.c */
146 }
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:649
rb_scan_args
#define rb_scan_args(argc, argvp, fmt,...)
Definition: rb_mjit_min_header-2.7.0.h:6372
rb_cTCPSocket
VALUE rb_cTCPSocket
Definition: init.c:19
VALUE
unsigned long VALUE
Definition: ruby.h:102
union_sockaddr
Definition: rubysocket.h:192
rsock_s_accept_nonblock
VALUE rsock_s_accept_nonblock(VALUE klass, VALUE ex, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len)
Definition: init.c:710
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1551
rb_io_t::fd
int fd
Definition: io.h:68
rsock_s_accept
VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
Definition: init.c:752
socklen_t
int socklen_t
Definition: getaddrinfo.c:83
rsock_init_tcpserver
void rsock_init_tcpserver(void)
Definition: tcpserver.c:106
rb_cTCPServer
VALUE rb_cTCPServer
Definition: init.c:20
INET_SERVER
#define INET_SERVER
Definition: rubysocket.h:228
argv
char ** argv
Definition: ruby.c:223
argc
int argc
Definition: ruby.c:222
GetOpenFile
#define GetOpenFile(obj, fp)
Definition: io.h:127
rsock_sock_listen
VALUE rsock_sock_listen(VALUE sock, VALUE log)
Definition: socket.c:653
len
uint8_t len
Definition: escape.c:17
rubysocket.h
Qnil
#define Qnil
Definition: ruby.h:469
rb_io_t
Definition: io.h:66
union_sockaddr::addr
struct sockaddr addr
Definition: rubysocket.h:193
rb_define_private_method
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1569
rsock_init_inetsock
VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type)
Definition: ipsocket.c:159