rbs 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,177 @@
1
+ # TCPServer represents a TCP/IP server socket.
2
+ #
3
+ # A simple TCP server may look like:
4
+ #
5
+ # require 'socket'
6
+ #
7
+ # server = TCPServer.new 2000 # Server bind to port 2000
8
+ # loop do
9
+ # client = server.accept # Wait for a client to connect
10
+ # client.puts "Hello !"
11
+ # client.puts "Time is #{Time.now}"
12
+ # client.close
13
+ # end
14
+ #
15
+ # A more usable server (serving multiple clients):
16
+ #
17
+ # require 'socket'
18
+ #
19
+ # server = TCPServer.new 2000
20
+ # loop do
21
+ # Thread.start(server.accept) do |client|
22
+ # client.puts "Hello !"
23
+ # client.puts "Time is #{Time.now}"
24
+ # client.close
25
+ # end
26
+ # end
27
+ class TCPServer < TCPSocket
28
+ public
29
+
30
+ # Accepts an incoming connection. It returns a new TCPSocket object.
31
+ #
32
+ # TCPServer.open("127.0.0.1", 14641) {|serv|
33
+ # s = serv.accept
34
+ # s.puts Time.now
35
+ # s.close
36
+ # }
37
+ #
38
+ def accept: () -> TCPSocket
39
+
40
+ # Accepts an incoming connection using accept(2) after O_NONBLOCK is set for the
41
+ # underlying file descriptor. It returns an accepted TCPSocket for the incoming
42
+ # connection.
43
+ #
44
+ # ### Example
45
+ # require 'socket'
46
+ # serv = TCPServer.new(2202)
47
+ # begin # emulate blocking accept
48
+ # sock = serv.accept_nonblock
49
+ # rescue IO::WaitReadable, Errno::EINTR
50
+ # IO.select([serv])
51
+ # retry
52
+ # end
53
+ # # sock is an accepted socket.
54
+ #
55
+ # Refer to Socket#accept for the exceptions that may be thrown if the call to
56
+ # TCPServer#accept_nonblock fails.
57
+ #
58
+ # TCPServer#accept_nonblock may raise any error corresponding to accept(2)
59
+ # failure, including Errno::EWOULDBLOCK.
60
+ #
61
+ # If the exception is Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::ECONNABORTED,
62
+ # Errno::EPROTO, it is extended by IO::WaitReadable. So IO::WaitReadable can be
63
+ # used to rescue the exceptions for retrying accept_nonblock.
64
+ #
65
+ # By specifying a keyword argument *exception* to `false`, you can indicate that
66
+ # accept_nonblock should not raise an IO::WaitReadable exception, but return the
67
+ # symbol `:wait_readable` instead.
68
+ #
69
+ # ### See
70
+ # * TCPServer#accept
71
+ # * Socket#accept
72
+ #
73
+ #
74
+ def accept_nonblock: (?exception: boolish) -> (TCPSocket | :wait_readable)
75
+
76
+ # Listens for connections, using the specified `int` as the backlog. A call to
77
+ # *listen* only applies if the `socket` is of type SOCK_STREAM or
78
+ # SOCK_SEQPACKET.
79
+ #
80
+ # ### Parameter
81
+ # * `backlog` - the maximum length of the queue for pending connections.
82
+ #
83
+ #
84
+ # ### Example 1
85
+ # require 'socket'
86
+ # include Socket::Constants
87
+ # socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
88
+ # sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
89
+ # socket.bind( sockaddr )
90
+ # socket.listen( 5 )
91
+ #
92
+ # ### Example 2 (listening on an arbitrary port, unix-based systems only):
93
+ # require 'socket'
94
+ # include Socket::Constants
95
+ # socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
96
+ # socket.listen( 1 )
97
+ #
98
+ # ### Unix-based Exceptions
99
+ # On unix based systems the above will work because a new `sockaddr` struct is
100
+ # created on the address ADDR_ANY, for an arbitrary port number as handed off by
101
+ # the kernel. It will not work on Windows, because Windows requires that the
102
+ # `socket` is bound by calling *bind* before it can *listen*.
103
+ #
104
+ # If the *backlog* amount exceeds the implementation-dependent maximum queue
105
+ # length, the implementation's maximum queue length will be used.
106
+ #
107
+ # On unix-based based systems the following system exceptions may be raised if
108
+ # the call to *listen* fails:
109
+ # * Errno::EBADF - the *socket* argument is not a valid file descriptor
110
+ # * Errno::EDESTADDRREQ - the *socket* is not bound to a local address, and
111
+ # the protocol does not support listening on an unbound socket
112
+ # * Errno::EINVAL - the *socket* is already connected
113
+ # * Errno::ENOTSOCK - the *socket* argument does not refer to a socket
114
+ # * Errno::EOPNOTSUPP - the *socket* protocol does not support listen
115
+ # * Errno::EACCES - the calling process does not have appropriate privileges
116
+ # * Errno::EINVAL - the *socket* has been shut down
117
+ # * Errno::ENOBUFS - insufficient resources are available in the system to
118
+ # complete the call
119
+ #
120
+ #
121
+ # ### Windows Exceptions
122
+ # On Windows systems the following system exceptions may be raised if the call
123
+ # to *listen* fails:
124
+ # * Errno::ENETDOWN - the network is down
125
+ # * Errno::EADDRINUSE - the socket's local address is already in use. This
126
+ # usually occurs during the execution of *bind* but could be delayed if the
127
+ # call to *bind* was to a partially wildcard address (involving ADDR_ANY)
128
+ # and if a specific address needs to be committed at the time of the call to
129
+ # *listen*
130
+ # * Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the
131
+ # service provider is still processing a callback function
132
+ # * Errno::EINVAL - the `socket` has not been bound with a call to *bind*.
133
+ # * Errno::EISCONN - the `socket` is already connected
134
+ # * Errno::EMFILE - no more socket descriptors are available
135
+ # * Errno::ENOBUFS - no buffer space is available
136
+ # * Errno::ENOTSOC - `socket` is not a socket
137
+ # * Errno::EOPNOTSUPP - the referenced `socket` is not a type that supports
138
+ # the *listen* method
139
+ #
140
+ #
141
+ # ### See
142
+ # * listen manual pages on unix-based systems
143
+ # * listen function in Microsoft's Winsock functions reference
144
+ #
145
+ #
146
+ def listen: (Integer backlog) -> void
147
+
148
+ # Returns a file descriptor of a accepted connection.
149
+ #
150
+ # TCPServer.open("127.0.0.1", 28561) {|serv|
151
+ # fd = serv.sysaccept
152
+ # s = IO.for_fd(fd)
153
+ # s.puts Time.now
154
+ # s.close
155
+ # }
156
+ #
157
+ def sysaccept: () -> Integer
158
+
159
+ private
160
+
161
+ def __accept_nonblock: (untyped) -> untyped
162
+
163
+ # Creates a new server socket bound to *port*.
164
+ #
165
+ # If *hostname* is given, the socket is bound to it.
166
+ #
167
+ # serv = TCPServer.new("127.0.0.1", 28561)
168
+ # s = serv.accept
169
+ # s.puts Time.now
170
+ # s.close
171
+ #
172
+ # Internally, TCPServer.new calls getaddrinfo() function to obtain addresses. If
173
+ # getaddrinfo() returns multiple addresses, TCPServer.new tries to create a
174
+ # server socket for each address and returns first one that is successful.
175
+ #
176
+ def initialize: (?String host, Integer port) -> untyped
177
+ end
@@ -0,0 +1,35 @@
1
+ # TCPSocket represents a TCP/IP client socket.
2
+ #
3
+ # A simple client may look like:
4
+ #
5
+ # require 'socket'
6
+ #
7
+ # s = TCPSocket.new 'localhost', 2000
8
+ #
9
+ # while line = s.gets # Read lines from socket
10
+ # puts line # and print them
11
+ # end
12
+ #
13
+ # s.close # close socket when done
14
+ class TCPSocket < IPSocket
15
+ # Use Addrinfo.getaddrinfo instead. This method is deprecated for the following
16
+ # reasons:
17
+ #
18
+ # * The 3rd element of the result is the address family of the first address.
19
+ # The address families of the rest of the addresses are not returned.
20
+ # * gethostbyname() may take a long time and it may block other threads. (GVL
21
+ # cannot be released since gethostbyname() is not thread safe.)
22
+ # * This method uses gethostbyname() function already removed from POSIX.
23
+ #
24
+ #
25
+ # This method lookups host information by *hostname*.
26
+ #
27
+ # TCPSocket.gethostbyname("localhost")
28
+ # #=> ["localhost", ["hal"], 2, "127.0.0.1"]
29
+ #
30
+ def self.gethostbyname: (String host) -> [String, Array[String], Integer, String]
31
+
32
+ private
33
+
34
+ def initialize: (String remote_host, Integer remote_port, ?String local_host, ?Integer local_port) -> untyped
35
+ end
@@ -0,0 +1,111 @@
1
+ # UDPSocket represents a UDP/IP socket.
2
+ class UDPSocket < IPSocket
3
+ public
4
+
5
+ # Binds *udpsocket* to *host*:*port*.
6
+ #
7
+ # u1 = UDPSocket.new
8
+ # u1.bind("127.0.0.1", 4913)
9
+ # u1.send "message-to-self", 0, "127.0.0.1", 4913
10
+ # p u1.recvfrom(10) #=> ["message-to", ["AF_INET", 4913, "localhost", "127.0.0.1"]]
11
+ #
12
+ def bind: (String host, Integer port) -> void
13
+
14
+ # Connects *udpsocket* to *host*:*port*.
15
+ #
16
+ # This makes possible to send without destination address.
17
+ #
18
+ # u1 = UDPSocket.new
19
+ # u1.bind("127.0.0.1", 4913)
20
+ # u2 = UDPSocket.new
21
+ # u2.connect("127.0.0.1", 4913)
22
+ # u2.send "uuuu", 0
23
+ # p u1.recvfrom(10) #=> ["uuuu", ["AF_INET", 33230, "localhost", "127.0.0.1"]]
24
+ #
25
+ def connect: (String host, Integer port) -> void
26
+
27
+ # Receives up to *maxlen* bytes from `udpsocket` using recvfrom(2) after
28
+ # O_NONBLOCK is set for the underlying file descriptor. *flags* is zero or more
29
+ # of the `MSG_` options. The first element of the results, *mesg*, is the data
30
+ # received. The second element, *sender_inet_addr*, is an array to represent the
31
+ # sender address.
32
+ #
33
+ # When recvfrom(2) returns 0, Socket#recvfrom_nonblock returns an empty string
34
+ # as data. It means an empty packet.
35
+ #
36
+ # ### Parameters
37
+ # * `maxlen` - the number of bytes to receive from the socket
38
+ # * `flags` - zero or more of the `MSG_` options
39
+ # * `outbuf` - destination String buffer
40
+ # * `options` - keyword hash, supporting `exception: false`
41
+ #
42
+ #
43
+ # ### Example
44
+ # require 'socket'
45
+ # s1 = UDPSocket.new
46
+ # s1.bind("127.0.0.1", 0)
47
+ # s2 = UDPSocket.new
48
+ # s2.bind("127.0.0.1", 0)
49
+ # s2.connect(*s1.addr.values_at(3,1))
50
+ # s1.connect(*s2.addr.values_at(3,1))
51
+ # s1.send "aaa", 0
52
+ # begin # emulate blocking recvfrom
53
+ # p s2.recvfrom_nonblock(10) #=> ["aaa", ["AF_INET", 33302, "localhost.localdomain", "127.0.0.1"]]
54
+ # rescue IO::WaitReadable
55
+ # IO.select([s2])
56
+ # retry
57
+ # end
58
+ #
59
+ # Refer to Socket#recvfrom for the exceptions that may be thrown if the call to
60
+ # *recvfrom_nonblock* fails.
61
+ #
62
+ # UDPSocket#recvfrom_nonblock may raise any error corresponding to recvfrom(2)
63
+ # failure, including Errno::EWOULDBLOCK.
64
+ #
65
+ # If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by
66
+ # IO::WaitReadable. So IO::WaitReadable can be used to rescue the exceptions for
67
+ # retrying recvfrom_nonblock.
68
+ #
69
+ # By specifying a keyword argument *exception* to `false`, you can indicate that
70
+ # recvfrom_nonblock should not raise an IO::WaitReadable exception, but return
71
+ # the symbol `:wait_readable` instead.
72
+ #
73
+ # ### See
74
+ # * Socket#recvfrom
75
+ #
76
+ #
77
+ def recvfrom_nonblock: (Integer len, ?Integer flag, ?String outbuf, ?exception: boolish) -> [String, [String, Integer, String, String]]
78
+
79
+ # Sends *mesg* via *udpsocket*.
80
+ #
81
+ # *flags* should be a bitwise OR of Socket::MSG_* constants.
82
+ #
83
+ # u1 = UDPSocket.new
84
+ # u1.bind("127.0.0.1", 4913)
85
+ #
86
+ # u2 = UDPSocket.new
87
+ # u2.send "hi", 0, "127.0.0.1", 4913
88
+ #
89
+ # mesg, addr = u1.recvfrom(10)
90
+ # u1.send mesg, 0, addr[3], addr[1]
91
+ #
92
+ # p u2.recv(100) #=> "hi"
93
+ #
94
+ def send: (String msg, ?Integer flags, ?String host, ?Integer port) -> Integer
95
+
96
+ private
97
+
98
+ def __recvfrom_nonblock: (untyped, untyped, untyped, untyped) -> untyped
99
+
100
+ # Creates a new UDPSocket object.
101
+ #
102
+ # *address_family* should be an integer, a string or a symbol: Socket::AF_INET,
103
+ # "AF_INET", :INET, etc.
104
+ #
105
+ # require 'socket'
106
+ #
107
+ # UDPSocket.new #=> #<UDPSocket:fd 3>
108
+ # UDPSocket.new(Socket::AF_INET6) #=> #<UDPSocket:fd 4>
109
+ #
110
+ def initialize: (?Integer family) -> untyped
111
+ end
@@ -0,0 +1,154 @@
1
+ # UNIXServer represents a UNIX domain stream server socket.
2
+ class UNIXServer < UNIXSocket
3
+ public
4
+
5
+ # Accepts an incoming connection. It returns a new UNIXSocket object.
6
+ #
7
+ # UNIXServer.open("/tmp/sock") {|serv|
8
+ # UNIXSocket.open("/tmp/sock") {|c|
9
+ # s = serv.accept
10
+ # s.puts "hi"
11
+ # s.close
12
+ # p c.read #=> "hi\n"
13
+ # }
14
+ # }
15
+ #
16
+ def accept: () -> UNIXSocket
17
+
18
+ # Accepts an incoming connection using accept(2) after O_NONBLOCK is set for the
19
+ # underlying file descriptor. It returns an accepted UNIXSocket for the incoming
20
+ # connection.
21
+ #
22
+ # ### Example
23
+ # require 'socket'
24
+ # serv = UNIXServer.new("/tmp/sock")
25
+ # begin # emulate blocking accept
26
+ # sock = serv.accept_nonblock
27
+ # rescue IO::WaitReadable, Errno::EINTR
28
+ # IO.select([serv])
29
+ # retry
30
+ # end
31
+ # # sock is an accepted socket.
32
+ #
33
+ # Refer to Socket#accept for the exceptions that may be thrown if the call to
34
+ # UNIXServer#accept_nonblock fails.
35
+ #
36
+ # UNIXServer#accept_nonblock may raise any error corresponding to accept(2)
37
+ # failure, including Errno::EWOULDBLOCK.
38
+ #
39
+ # If the exception is Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::ECONNABORTED or
40
+ # Errno::EPROTO, it is extended by IO::WaitReadable. So IO::WaitReadable can be
41
+ # used to rescue the exceptions for retrying accept_nonblock.
42
+ #
43
+ # By specifying a keyword argument *exception* to `false`, you can indicate that
44
+ # accept_nonblock should not raise an IO::WaitReadable exception, but return the
45
+ # symbol `:wait_readable` instead.
46
+ #
47
+ # ### See
48
+ # * UNIXServer#accept
49
+ # * Socket#accept
50
+ #
51
+ #
52
+ def accept_nonblock: (?exception: boolish) -> (UNIXSocket | :wait_readable)
53
+
54
+ # Listens for connections, using the specified `int` as the backlog. A call to
55
+ # *listen* only applies if the `socket` is of type SOCK_STREAM or
56
+ # SOCK_SEQPACKET.
57
+ #
58
+ # ### Parameter
59
+ # * `backlog` - the maximum length of the queue for pending connections.
60
+ #
61
+ #
62
+ # ### Example 1
63
+ # require 'socket'
64
+ # include Socket::Constants
65
+ # socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
66
+ # sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
67
+ # socket.bind( sockaddr )
68
+ # socket.listen( 5 )
69
+ #
70
+ # ### Example 2 (listening on an arbitrary port, unix-based systems only):
71
+ # require 'socket'
72
+ # include Socket::Constants
73
+ # socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
74
+ # socket.listen( 1 )
75
+ #
76
+ # ### Unix-based Exceptions
77
+ # On unix based systems the above will work because a new `sockaddr` struct is
78
+ # created on the address ADDR_ANY, for an arbitrary port number as handed off by
79
+ # the kernel. It will not work on Windows, because Windows requires that the
80
+ # `socket` is bound by calling *bind* before it can *listen*.
81
+ #
82
+ # If the *backlog* amount exceeds the implementation-dependent maximum queue
83
+ # length, the implementation's maximum queue length will be used.
84
+ #
85
+ # On unix-based based systems the following system exceptions may be raised if
86
+ # the call to *listen* fails:
87
+ # * Errno::EBADF - the *socket* argument is not a valid file descriptor
88
+ # * Errno::EDESTADDRREQ - the *socket* is not bound to a local address, and
89
+ # the protocol does not support listening on an unbound socket
90
+ # * Errno::EINVAL - the *socket* is already connected
91
+ # * Errno::ENOTSOCK - the *socket* argument does not refer to a socket
92
+ # * Errno::EOPNOTSUPP - the *socket* protocol does not support listen
93
+ # * Errno::EACCES - the calling process does not have appropriate privileges
94
+ # * Errno::EINVAL - the *socket* has been shut down
95
+ # * Errno::ENOBUFS - insufficient resources are available in the system to
96
+ # complete the call
97
+ #
98
+ #
99
+ # ### Windows Exceptions
100
+ # On Windows systems the following system exceptions may be raised if the call
101
+ # to *listen* fails:
102
+ # * Errno::ENETDOWN - the network is down
103
+ # * Errno::EADDRINUSE - the socket's local address is already in use. This
104
+ # usually occurs during the execution of *bind* but could be delayed if the
105
+ # call to *bind* was to a partially wildcard address (involving ADDR_ANY)
106
+ # and if a specific address needs to be committed at the time of the call to
107
+ # *listen*
108
+ # * Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the
109
+ # service provider is still processing a callback function
110
+ # * Errno::EINVAL - the `socket` has not been bound with a call to *bind*.
111
+ # * Errno::EISCONN - the `socket` is already connected
112
+ # * Errno::EMFILE - no more socket descriptors are available
113
+ # * Errno::ENOBUFS - no buffer space is available
114
+ # * Errno::ENOTSOC - `socket` is not a socket
115
+ # * Errno::EOPNOTSUPP - the referenced `socket` is not a type that supports
116
+ # the *listen* method
117
+ #
118
+ #
119
+ # ### See
120
+ # * listen manual pages on unix-based systems
121
+ # * listen function in Microsoft's Winsock functions reference
122
+ #
123
+ #
124
+ def listen: (Integer backlog) -> void
125
+
126
+ # Accepts a new connection. It returns the new file descriptor which is an
127
+ # integer.
128
+ #
129
+ # UNIXServer.open("/tmp/sock") {|serv|
130
+ # UNIXSocket.open("/tmp/sock") {|c|
131
+ # fd = serv.sysaccept
132
+ # s = IO.new(fd)
133
+ # s.puts "hi"
134
+ # s.close
135
+ # p c.read #=> "hi\n"
136
+ # }
137
+ # }
138
+ #
139
+ def sysaccept: () -> Integer
140
+
141
+ private
142
+
143
+ def __accept_nonblock: (untyped) -> untyped
144
+
145
+ # Creates a new UNIX server socket bound to *path*.
146
+ #
147
+ # require 'socket'
148
+ #
149
+ # serv = UNIXServer.new("/tmp/sock")
150
+ # s = serv.accept
151
+ # p s.read
152
+ #
153
+ def initialize: (untyped) -> untyped
154
+ end