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,503 @@
1
+ # BasicSocket is the super class for all the Socket classes.
2
+ class BasicSocket < IO
3
+ # Gets the global do_not_reverse_lookup flag.
4
+ #
5
+ # BasicSocket.do_not_reverse_lookup #=> false
6
+ #
7
+ def self.do_not_reverse_lookup: () -> bool
8
+
9
+ # Sets the global do_not_reverse_lookup flag.
10
+ #
11
+ # The flag is used for initial value of do_not_reverse_lookup for each socket.
12
+ #
13
+ # s1 = TCPSocket.new("localhost", 80)
14
+ # p s1.do_not_reverse_lookup #=> true
15
+ # BasicSocket.do_not_reverse_lookup = false
16
+ # s2 = TCPSocket.new("localhost", 80)
17
+ # p s2.do_not_reverse_lookup #=> false
18
+ # p s1.do_not_reverse_lookup #=> true
19
+ #
20
+ def self.do_not_reverse_lookup=: (boolish) -> void
21
+
22
+ # Returns a socket object which contains the file descriptor, *fd*.
23
+ #
24
+ # # If invoked by inetd, STDIN/STDOUT/STDERR is a socket.
25
+ # STDIN_SOCK = Socket.for_fd(STDIN.fileno)
26
+ # p STDIN_SOCK.remote_address
27
+ #
28
+ def self.for_fd: (Integer fileno) -> BasicSocket
29
+
30
+ public
31
+
32
+ # Disallows further read using shutdown system call.
33
+ #
34
+ # s1, s2 = UNIXSocket.pair
35
+ # s1.close_read
36
+ # s2.puts #=> Broken pipe (Errno::EPIPE)
37
+ #
38
+ def close_read: () -> void
39
+
40
+ # Disallows further write using shutdown system call.
41
+ #
42
+ # UNIXSocket.pair {|s1, s2|
43
+ # s1.print "ping"
44
+ # s1.close_write
45
+ # p s2.read #=> "ping"
46
+ # s2.print "pong"
47
+ # s2.close
48
+ # p s1.read #=> "pong"
49
+ # }
50
+ #
51
+ def close_write: () -> void
52
+
53
+ # Returns an address of the socket suitable for connect in the local machine.
54
+ #
55
+ # This method returns *self*.local_address, except following condition.
56
+ #
57
+ # * IPv4 unspecified address (0.0.0.0) is replaced by IPv4 loopback address
58
+ # (127.0.0.1).
59
+ # * IPv6 unspecified address (::) is replaced by IPv6 loopback address (::1).
60
+ #
61
+ #
62
+ # If the local address is not suitable for connect, SocketError is raised. IPv4
63
+ # and IPv6 address which port is 0 is not suitable for connect. Unix domain
64
+ # socket which has no path is not suitable for connect.
65
+ #
66
+ # Addrinfo.tcp("0.0.0.0", 0).listen {|serv|
67
+ # p serv.connect_address #=> #<Addrinfo: 127.0.0.1:53660 TCP>
68
+ # serv.connect_address.connect {|c|
69
+ # s, _ = serv.accept
70
+ # p [c, s] #=> [#<Socket:fd 4>, #<Socket:fd 6>]
71
+ # }
72
+ # }
73
+ #
74
+ def connect_address: () -> Addrinfo
75
+
76
+ # Gets the do_not_reverse_lookup flag of *basicsocket*.
77
+ #
78
+ # require 'socket'
79
+ #
80
+ # BasicSocket.do_not_reverse_lookup = false
81
+ # TCPSocket.open("www.ruby-lang.org", 80) {|sock|
82
+ # p sock.do_not_reverse_lookup #=> false
83
+ # }
84
+ # BasicSocket.do_not_reverse_lookup = true
85
+ # TCPSocket.open("www.ruby-lang.org", 80) {|sock|
86
+ # p sock.do_not_reverse_lookup #=> true
87
+ # }
88
+ #
89
+ def do_not_reverse_lookup: () -> bool
90
+
91
+ # Sets the do_not_reverse_lookup flag of *basicsocket*.
92
+ #
93
+ # TCPSocket.open("www.ruby-lang.org", 80) {|sock|
94
+ # p sock.do_not_reverse_lookup #=> true
95
+ # p sock.peeraddr #=> ["AF_INET", 80, "221.186.184.68", "221.186.184.68"]
96
+ # sock.do_not_reverse_lookup = false
97
+ # p sock.peeraddr #=> ["AF_INET", 80, "carbon.ruby-lang.org", "54.163.249.195"]
98
+ # }
99
+ #
100
+ def do_not_reverse_lookup=: (boolish) -> void
101
+
102
+ # Returns the user and group on the peer of the UNIX socket. The result is a two
103
+ # element array which contains the effective uid and the effective gid.
104
+ #
105
+ # Socket.unix_server_loop("/tmp/sock") {|s|
106
+ # begin
107
+ # euid, egid = s.getpeereid
108
+ #
109
+ # # Check the connected client is myself or not.
110
+ # next if euid != Process.uid
111
+ #
112
+ # # do something about my resource.
113
+ #
114
+ # ensure
115
+ # s.close
116
+ # end
117
+ # }
118
+ #
119
+ def getpeereid: () -> [Integer, Integer]
120
+
121
+ # Returns the remote address of the socket as a sockaddr string.
122
+ #
123
+ # TCPServer.open("127.0.0.1", 1440) {|serv|
124
+ # c = TCPSocket.new("127.0.0.1", 1440)
125
+ # s = serv.accept
126
+ # p s.getpeername #=> "\x02\x00\x82u\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
127
+ # }
128
+ #
129
+ # If Addrinfo object is preferred over the binary string, use
130
+ # BasicSocket#remote_address.
131
+ #
132
+ def getpeername: () -> String
133
+
134
+ # Returns the local address of the socket as a sockaddr string.
135
+ #
136
+ # TCPServer.open("127.0.0.1", 15120) {|serv|
137
+ # p serv.getsockname #=> "\x02\x00;\x10\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
138
+ # }
139
+ #
140
+ # If Addrinfo object is preferred over the binary string, use
141
+ # BasicSocket#local_address.
142
+ #
143
+ def getsockname: () -> String
144
+
145
+ # Gets a socket option. These are protocol and system specific, see your local
146
+ # system documentation for details. The option is returned as a Socket::Option
147
+ # object.
148
+ #
149
+ # ### Parameters
150
+ # * `level` is an integer, usually one of the SOL_ constants such as
151
+ # Socket::SOL_SOCKET, or a protocol level. A string or symbol of the name,
152
+ # possibly without prefix, is also accepted.
153
+ # * `optname` is an integer, usually one of the SO_ constants, such as
154
+ # Socket::SO_REUSEADDR. A string or symbol of the name, possibly without
155
+ # prefix, is also accepted.
156
+ #
157
+ #
158
+ # ### Examples
159
+ #
160
+ # Some socket options are integers with boolean values, in this case #getsockopt
161
+ # could be called like this:
162
+ #
163
+ # reuseaddr = sock.getsockopt(:SOCKET, :REUSEADDR).bool
164
+ #
165
+ # optval = sock.getsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR)
166
+ # optval = optval.unpack "i"
167
+ # reuseaddr = optval[0] == 0 ? false : true
168
+ #
169
+ # Some socket options are integers with numeric values, in this case #getsockopt
170
+ # could be called like this:
171
+ #
172
+ # ipttl = sock.getsockopt(:IP, :TTL).int
173
+ #
174
+ # optval = sock.getsockopt(Socket::IPPROTO_IP, Socket::IP_TTL)
175
+ # ipttl = optval.unpack("i")[0]
176
+ #
177
+ # Option values may be structs. Decoding them can be complex as it involves
178
+ # examining your system headers to determine the correct definition. An example
179
+ # is a +struct linger+, which may be defined in your system headers as:
180
+ # struct linger {
181
+ # int l_onoff;
182
+ # int l_linger;
183
+ # };
184
+ #
185
+ # In this case #getsockopt could be called like this:
186
+ #
187
+ # # Socket::Option knows linger structure.
188
+ # onoff, linger = sock.getsockopt(:SOCKET, :LINGER).linger
189
+ #
190
+ # optval = sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER)
191
+ # onoff, linger = optval.unpack "ii"
192
+ # onoff = onoff == 0 ? false : true
193
+ #
194
+ def getsockopt: (Symbol | Integer, Symbol | Integer) -> (Integer | boolish | String)
195
+
196
+ # Returns an Addrinfo object for local address obtained by getsockname.
197
+ #
198
+ # Note that addrinfo.protocol is filled by 0.
199
+ #
200
+ # TCPSocket.open("www.ruby-lang.org", 80) {|s|
201
+ # p s.local_address #=> #<Addrinfo: 192.168.0.129:36873 TCP>
202
+ # }
203
+ #
204
+ # TCPServer.open("127.0.0.1", 1512) {|serv|
205
+ # p serv.local_address #=> #<Addrinfo: 127.0.0.1:1512 TCP>
206
+ # }
207
+ #
208
+ def local_address: () -> Addrinfo
209
+
210
+ # Receives a message.
211
+ #
212
+ # *maxlen* is the maximum number of bytes to receive.
213
+ #
214
+ # *flags* should be a bitwise OR of Socket::MSG_* constants.
215
+ #
216
+ # *outbuf* will contain only the received data after the method call even if it
217
+ # is not empty at the beginning.
218
+ #
219
+ # UNIXSocket.pair {|s1, s2|
220
+ # s1.puts "Hello World"
221
+ # p s2.recv(4) #=> "Hell"
222
+ # p s2.recv(4, Socket::MSG_PEEK) #=> "o Wo"
223
+ # p s2.recv(4) #=> "o Wo"
224
+ # p s2.recv(10) #=> "rld\n"
225
+ # }
226
+ #
227
+ def recv: (Integer maxlen, ?Integer flags, ?String outbuf) -> String
228
+ | (Integer maxlen, ?String outbuf) -> String
229
+
230
+ # Receives up to *maxlen* bytes from `socket` using recvfrom(2) after O_NONBLOCK
231
+ # is set for the underlying file descriptor. *flags* is zero or more of the
232
+ # `MSG_` options. The result, *mesg*, is the data received.
233
+ #
234
+ # When recvfrom(2) returns 0, Socket#recv_nonblock returns an empty string as
235
+ # data. The meaning depends on the socket: EOF on TCP, empty packet on UDP, etc.
236
+ #
237
+ # ### Parameters
238
+ # * `maxlen` - the number of bytes to receive from the socket
239
+ # * `flags` - zero or more of the `MSG_` options
240
+ # * `buf` - destination String buffer
241
+ # * `options` - keyword hash, supporting `exception: false`
242
+ #
243
+ #
244
+ # ### Example
245
+ # serv = TCPServer.new("127.0.0.1", 0)
246
+ # af, port, host, addr = serv.addr
247
+ # c = TCPSocket.new(addr, port)
248
+ # s = serv.accept
249
+ # c.send "aaa", 0
250
+ # begin # emulate blocking recv.
251
+ # p s.recv_nonblock(10) #=> "aaa"
252
+ # rescue IO::WaitReadable
253
+ # IO.select([s])
254
+ # retry
255
+ # end
256
+ #
257
+ # Refer to Socket#recvfrom for the exceptions that may be thrown if the call to
258
+ # *recv_nonblock* fails.
259
+ #
260
+ # BasicSocket#recv_nonblock may raise any error corresponding to recvfrom(2)
261
+ # failure, including Errno::EWOULDBLOCK.
262
+ #
263
+ # If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by
264
+ # IO::WaitReadable. So IO::WaitReadable can be used to rescue the exceptions for
265
+ # retrying recv_nonblock.
266
+ #
267
+ # By specifying a keyword argument *exception* to `false`, you can indicate that
268
+ # recv_nonblock should not raise an IO::WaitReadable exception, but return the
269
+ # symbol `:wait_readable` instead.
270
+ #
271
+ # ### See
272
+ # * Socket#recvfrom
273
+ #
274
+ #
275
+ def recv_nonblock: (Integer maxlen, ?Integer flags, ?String buf, ?exception: boolish) -> (String | :wait_readable)
276
+
277
+ # recvmsg receives a message using recvmsg(2) system call in blocking manner.
278
+ #
279
+ # *maxmesglen* is the maximum length of mesg to receive.
280
+ #
281
+ # *flags* is bitwise OR of MSG_* constants such as Socket::MSG_PEEK.
282
+ #
283
+ # *maxcontrollen* is the maximum length of controls (ancillary data) to receive.
284
+ #
285
+ # *opts* is option hash. Currently :scm_rights=>bool is the only option.
286
+ #
287
+ # :scm_rights option specifies that application expects SCM_RIGHTS control
288
+ # message. If the value is nil or false, application don't expects SCM_RIGHTS
289
+ # control message. In this case, recvmsg closes the passed file descriptors
290
+ # immediately. This is the default behavior.
291
+ #
292
+ # If :scm_rights value is neither nil nor false, application expects SCM_RIGHTS
293
+ # control message. In this case, recvmsg creates IO objects for each file
294
+ # descriptors for Socket::AncillaryData#unix_rights method.
295
+ #
296
+ # The return value is 4-elements array.
297
+ #
298
+ # *mesg* is a string of the received message.
299
+ #
300
+ # *sender_addrinfo* is a sender socket address for connection-less socket. It is
301
+ # an Addrinfo object. For connection-oriented socket such as TCP,
302
+ # sender_addrinfo is platform dependent.
303
+ #
304
+ # *rflags* is a flags on the received message which is bitwise OR of MSG_*
305
+ # constants such as Socket::MSG_TRUNC. It will be nil if the system uses 4.3BSD
306
+ # style old recvmsg system call.
307
+ #
308
+ # *controls* is ancillary data which is an array of Socket::AncillaryData
309
+ # objects such as:
310
+ #
311
+ # #<Socket::AncillaryData: AF_UNIX SOCKET RIGHTS 7>
312
+ #
313
+ # *maxmesglen* and *maxcontrollen* can be nil. In that case, the buffer will be
314
+ # grown until the message is not truncated. Internally, MSG_PEEK is used. Buffer
315
+ # full and MSG_CTRUNC are checked for truncation.
316
+ #
317
+ # recvmsg can be used to implement recv_io as follows:
318
+ #
319
+ # mesg, sender_sockaddr, rflags, *controls = sock.recvmsg(:scm_rights=>true)
320
+ # controls.each {|ancdata|
321
+ # if ancdata.cmsg_is?(:SOCKET, :RIGHTS)
322
+ # return ancdata.unix_rights[0]
323
+ # end
324
+ # }
325
+ #
326
+ def recvmsg: (?Integer dlen, ?Integer flags, ?Integer clen, ?scm_rights: boolish) -> [String, Addrinfo, Integer?, Array[Socket::AncillaryData]]
327
+
328
+ # recvmsg receives a message using recvmsg(2) system call in non-blocking
329
+ # manner.
330
+ #
331
+ # It is similar to BasicSocket#recvmsg but non-blocking flag is set before the
332
+ # system call and it doesn't retry the system call.
333
+ #
334
+ # By specifying a keyword argument *exception* to `false`, you can indicate that
335
+ # recvmsg_nonblock should not raise an IO::WaitReadable exception, but return
336
+ # the symbol `:wait_readable` instead.
337
+ #
338
+ def recvmsg_nonblock: (?Integer dlen, ?Integer flags, ?Integer clen, ?exception: boolish, ?scm_rights: boolish) -> ([String, Addrinfo, Integer?, Array[Socket::AncillaryData]] | :wait_readable)
339
+
340
+ # Returns an Addrinfo object for remote address obtained by getpeername.
341
+ #
342
+ # Note that addrinfo.protocol is filled by 0.
343
+ #
344
+ # TCPSocket.open("www.ruby-lang.org", 80) {|s|
345
+ # p s.remote_address #=> #<Addrinfo: 221.186.184.68:80 TCP>
346
+ # }
347
+ #
348
+ # TCPServer.open("127.0.0.1", 1728) {|serv|
349
+ # c = TCPSocket.new("127.0.0.1", 1728)
350
+ # s = serv.accept
351
+ # p s.remote_address #=> #<Addrinfo: 127.0.0.1:36504 TCP>
352
+ # }
353
+ #
354
+ def remote_address: () -> Addrinfo
355
+
356
+ # send *mesg* via *basicsocket*.
357
+ #
358
+ # *mesg* should be a string.
359
+ #
360
+ # *flags* should be a bitwise OR of Socket::MSG_* constants.
361
+ #
362
+ # *dest_sockaddr* should be a packed sockaddr string or an addrinfo.
363
+ #
364
+ # TCPSocket.open("localhost", 80) {|s|
365
+ # s.send "GET / HTTP/1.0\r\n\r\n", 0
366
+ # p s.read
367
+ # }
368
+ #
369
+ def send: (String msg, ?Integer flags, ?(Addrinfo | String) dest_sockaddr) -> void
370
+
371
+ # sendmsg sends a message using sendmsg(2) system call in blocking manner.
372
+ #
373
+ # *mesg* is a string to send.
374
+ #
375
+ # *flags* is bitwise OR of MSG_* constants such as Socket::MSG_OOB.
376
+ #
377
+ # *dest_sockaddr* is a destination socket address for connection-less socket. It
378
+ # should be a sockaddr such as a result of Socket.sockaddr_in. An Addrinfo
379
+ # object can be used too.
380
+ #
381
+ # *controls* is a list of ancillary data. The element of *controls* should be
382
+ # Socket::AncillaryData or 3-elements array. The 3-element array should contains
383
+ # cmsg_level, cmsg_type and data.
384
+ #
385
+ # The return value, *numbytes_sent* is an integer which is the number of bytes
386
+ # sent.
387
+ #
388
+ # sendmsg can be used to implement send_io as follows:
389
+ #
390
+ # # use Socket::AncillaryData.
391
+ # ancdata = Socket::AncillaryData.int(:UNIX, :SOCKET, :RIGHTS, io.fileno)
392
+ # sock.sendmsg("a", 0, nil, ancdata)
393
+ #
394
+ # # use 3-element array.
395
+ # ancdata = [:SOCKET, :RIGHTS, [io.fileno].pack("i!")]
396
+ # sock.sendmsg("\0", 0, nil, ancdata)
397
+ #
398
+ def sendmsg: (String mesg, ?Integer flags, ?(Addrinfo | String) dest_sockaddr, *Socket::AncillaryData controls) -> Integer
399
+
400
+ # sendmsg_nonblock sends a message using sendmsg(2) system call in non-blocking
401
+ # manner.
402
+ #
403
+ # It is similar to BasicSocket#sendmsg but the non-blocking flag is set before
404
+ # the system call and it doesn't retry the system call.
405
+ #
406
+ # By specifying a keyword argument *exception* to `false`, you can indicate that
407
+ # sendmsg_nonblock should not raise an IO::WaitWritable exception, but return
408
+ # the symbol `:wait_writable` instead.
409
+ #
410
+ def sendmsg_nonblock: (String mesg, ?Integer flags, ?(Addrinfo | String) dest_sockaddr, *Socket::AncillaryData controls, ?exception: boolish) -> (Integer | :wait_writable)
411
+
412
+ # Sets a socket option. These are protocol and system specific, see your local
413
+ # system documentation for details.
414
+ #
415
+ # ### Parameters
416
+ # * `level` is an integer, usually one of the SOL_ constants such as
417
+ # Socket::SOL_SOCKET, or a protocol level. A string or symbol of the name,
418
+ # possibly without prefix, is also accepted.
419
+ # * `optname` is an integer, usually one of the SO_ constants, such as
420
+ # Socket::SO_REUSEADDR. A string or symbol of the name, possibly without
421
+ # prefix, is also accepted.
422
+ # * `optval` is the value of the option, it is passed to the underlying
423
+ # setsockopt() as a pointer to a certain number of bytes. How this is done
424
+ # depends on the type:
425
+ # * Integer: value is assigned to an int, and a pointer to the int is
426
+ # passed, with length of sizeof(int).
427
+ # * true or false: 1 or 0 (respectively) is assigned to an int, and the
428
+ # int is passed as for an Integer. Note that `false` must be passed, not
429
+ # `nil`.
430
+ # * String: the string's data and length is passed to the socket.
431
+ #
432
+ # * `socketoption` is an instance of Socket::Option
433
+ #
434
+ #
435
+ # ### Examples
436
+ #
437
+ # Some socket options are integers with boolean values, in this case #setsockopt
438
+ # could be called like this:
439
+ # sock.setsockopt(:SOCKET, :REUSEADDR, true)
440
+ # sock.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
441
+ # sock.setsockopt(Socket::Option.bool(:INET, :SOCKET, :REUSEADDR, true))
442
+ #
443
+ # Some socket options are integers with numeric values, in this case #setsockopt
444
+ # could be called like this:
445
+ # sock.setsockopt(:IP, :TTL, 255)
446
+ # sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, 255)
447
+ # sock.setsockopt(Socket::Option.int(:INET, :IP, :TTL, 255))
448
+ #
449
+ # Option values may be structs. Passing them can be complex as it involves
450
+ # examining your system headers to determine the correct definition. An example
451
+ # is an `ip_mreq`, which may be defined in your system headers as:
452
+ # struct ip_mreq {
453
+ # struct in_addr imr_multiaddr;
454
+ # struct in_addr imr_interface;
455
+ # };
456
+ #
457
+ # In this case #setsockopt could be called like this:
458
+ # optval = IPAddr.new("224.0.0.251").hton +
459
+ # IPAddr.new(Socket::INADDR_ANY, Socket::AF_INET).hton
460
+ # sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, optval)
461
+ #
462
+ #
463
+ def setsockopt: (*(Symbol | Integer), boolish | Integer | String) -> void
464
+
465
+ # Calls shutdown(2) system call.
466
+ #
467
+ # s.shutdown(Socket::SHUT_RD) disallows further read.
468
+ #
469
+ # s.shutdown(Socket::SHUT_WR) disallows further write.
470
+ #
471
+ # s.shutdown(Socket::SHUT_RDWR) disallows further read and write.
472
+ #
473
+ # *how* can be symbol or string:
474
+ # * :RD, :SHUT_RD, "RD" and "SHUT_RD" are accepted as Socket::SHUT_RD.
475
+ # * :WR, :SHUT_WR, "WR" and "SHUT_WR" are accepted as Socket::SHUT_WR.
476
+ # * :RDWR, :SHUT_RDWR, "RDWR" and "SHUT_RDWR" are accepted as
477
+ # Socket::SHUT_RDWR.
478
+ #
479
+ # UNIXSocket.pair {|s1, s2|
480
+ # s1.puts "ping"
481
+ # s1.shutdown(:WR)
482
+ # p s2.read #=> "ping\n"
483
+ # s2.puts "pong"
484
+ # s2.close
485
+ # p s1.read #=> "pong\n"
486
+ #
487
+ # }
488
+ #
489
+ #
490
+ def shutdown: (Symbol | String | Integer flags) -> void
491
+
492
+ private
493
+
494
+ def __recv_nonblock: (untyped, untyped, untyped, untyped) -> untyped
495
+
496
+ def __recvmsg: (untyped, untyped, untyped, untyped) -> untyped
497
+
498
+ def __recvmsg_nonblock: (untyped, untyped, untyped, untyped, untyped) -> untyped
499
+
500
+ def __sendmsg: (untyped, untyped, untyped, untyped) -> untyped
501
+
502
+ def __sendmsg_nonblock: (untyped, untyped, untyped, untyped, untyped) -> untyped
503
+ end