Class: BasicSocket
- Defined in:
- lib/framework/autocomplete/BasicSocket.rb,
lib/framework/_socket.rb
Overview
It is auto-generated content. Do not do required for this file in your application.
Direct Known Subclasses
Defined Under Namespace
Modules: WaitReadable, WaitWritable
Constant Summary collapse
- SEEK_SET =
0- SEEK_CUR =
1- SEEK_END =
2- LOCK_SH =
1- LOCK_EX =
2- LOCK_UN =
8- LOCK_NB =
4- RDONLY =
0- WRONLY =
1- RDWR =
2- APPEND =
8- CREAT =
256- EXCL =
1024- NONBLOCK =
4- TRUNC =
512- BINARY =
32768- FNM_NOESCAPE =
1- FNM_PATHNAME =
2- FNM_DOTMATCH =
4- FNM_CASEFOLD =
8- FNM_SYSCASE =
8
Class Method Summary collapse
Instance Method Summary collapse
- #close_read ⇒ Object
- #close_write ⇒ Object
-
#connect_address ⇒ Object
Returns an address of the socket suitable for connect in the local machine.
- #do_not_reverse_lookup ⇒ Object
- #do_not_reverse_lookup=(req) ⇒ Object
- #getpeername ⇒ Object
- #getsockname ⇒ Object
- #getsockopt(req, req1) ⇒ Object
- #recv(rest) ⇒ Object
-
#recv_nonblock(rest) ⇒ Object
call-seq: basicsocket.recv_nonblock(maxlen [, flags [, buf [, options ]]]) => mesg.
-
#recvmsg(dlen = nil, flags = 0, clen = nil, scm_rights: false) ⇒ Object
call-seq: basicsocket.recvmsg(maxmesglen=nil, flags=0, maxcontrollen=nil, opts={}) => [mesg, sender_addrinfo, rflags, *controls].
-
#recvmsg_nonblock(dlen = nil, flags = 0, clen = nil, scm_rights: false, exception: true) ⇒ Object
call-seq: basicsocket.recvmsg_nonblock(maxdatalen=nil, flags=0, maxcontrollen=nil, opts={}) => [data, sender_addrinfo, rflags, *controls].
- #send(rest) ⇒ Object
-
#sendmsg(mesg, flags = 0, dest_sockaddr = nil, *controls) ⇒ Object
call-seq: basicsocket.sendmsg(mesg, flags=0, dest_sockaddr=nil, *controls) => numbytes_sent.
-
#sendmsg_nonblock(mesg, flags = 0, dest_sockaddr = nil, *controls, exception: true) ⇒ Object
call-seq: basicsocket.sendmsg_nonblock(mesg, flags=0, dest_sockaddr=nil, *controls, opts={}) => numbytes_sent.
- #setsockopt(req, req1, req2) ⇒ Object
- #shutdown(rest) ⇒ Object
Class Method Details
.do_not_reverse_lookup ⇒ Object
56 57 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 56 def self.do_not_reverse_lookup end |
.do_not_reverse_lookup=(req) ⇒ Object
58 59 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 58 def self.do_not_reverse_lookup=(req) end |
.for_fd(req) ⇒ Object
60 61 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 60 def self.for_fd(req) end |
Instance Method Details
#close_read ⇒ Object
62 63 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 62 def close_read end |
#close_write ⇒ Object
64 65 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 64 def close_write end |
#connect_address ⇒ Object
Returns an address of the socket suitable for connect in the local machine.
This method returns self.local_address, except following condition.
-
IPv4 unspecified address (0.0.0.0) is replaced by IPv4 loopback address (127.0.0.1).
-
IPv6 unspecified address (::) is replaced by IPv6 loopback address (::1).
If the local address is not suitable for connect, SocketError is raised. IPv4 and IPv6 address which port is 0 is not suitable for connect. Unix domain socket which has no path is not suitable for connect.
Addrinfo.tcp("0.0.0.0", 0).listen {|serv|
p serv.connect_address #=> #<Addrinfo: 127.0.0.1:53660 TCP>
serv.connect_address.connect {|c|
s, _ = serv.accept
p [c, s] #=> [#<Socket:fd 4>, #<Socket:fd 6>]
}
}
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/framework/_socket.rb', line 254 def connect_address addr = local_address afamily = addr.afamily if afamily == Socket::AF_INET raise SocketError, "unbound IPv4 socket" if addr.ip_port == 0 if addr.ip_address == "0.0.0.0" addr = Addrinfo.new(["AF_INET", addr.ip_port, nil, "127.0.0.1"], addr.pfamily, addr.socktype, addr.protocol) end elsif defined?(Socket::AF_INET6) && afamily == Socket::AF_INET6 raise SocketError, "unbound IPv6 socket" if addr.ip_port == 0 if addr.ip_address == "::" addr = Addrinfo.new(["AF_INET6", addr.ip_port, nil, "::1"], addr.pfamily, addr.socktype, addr.protocol) elsif addr.ip_address == "0.0.0.0" # MacOS X 10.4 returns "a.b.c.d" for IPv4-mapped IPv6 address. addr = Addrinfo.new(["AF_INET6", addr.ip_port, nil, "::1"], addr.pfamily, addr.socktype, addr.protocol) elsif addr.ip_address == "::ffff:0.0.0.0" # MacOS X 10.6 returns "::ffff:a.b.c.d" for IPv4-mapped IPv6 address. addr = Addrinfo.new(["AF_INET6", addr.ip_port, nil, "::1"], addr.pfamily, addr.socktype, addr.protocol) end elsif defined?(Socket::AF_UNIX) && afamily == Socket::AF_UNIX raise SocketError, "unbound Unix socket" if addr.unix_path == "" end addr end |
#do_not_reverse_lookup ⇒ Object
82 83 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 82 def do_not_reverse_lookup end |
#do_not_reverse_lookup=(req) ⇒ Object
84 85 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 84 def do_not_reverse_lookup=(req) end |
#getpeername ⇒ Object
74 75 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 74 def getpeername end |
#getsockname ⇒ Object
72 73 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 72 def getsockname end |
#getsockopt(req, req1) ⇒ Object
70 71 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 70 def getsockopt(req,req1) end |
#recv(rest) ⇒ Object
78 79 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 78 def recv(rest) end |
#recv_nonblock(rest) ⇒ Object
call-seq: basicsocket.recv_nonblock(maxlen [, flags [, buf [, options ]]]) => mesg
Receives up to maxlen bytes from socket using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. flags is zero or more of the MSG_ options. The result, mesg, is the data received.
When recvfrom(2) returns 0, Socket#recv_nonblock returns an empty string as data. The meaning depends on the socket: EOF on TCP, empty packet on UDP, etc.
Parameters
-
maxlen- the number of bytes to receive from the socket -
flags- zero or more of theMSG_options -
options- keyword hash, supporting ‘exception: false`
Example
serv = TCPServer.new(“127.0.0.1”, 0) af, port, host, addr = serv.addr c = TCPSocket.new(addr, port) s = serv.accept c.send “aaa”, 0 begin # emulate blocking recv. p s.recv_nonblock(10) #=> “aaa” rescue IO::WaitReadable IO.select() retry end
Refer to Socket#recvfrom for the exceptions that may be thrown if the call to recv_nonblock fails.
BasicSocket#recv_nonblock may raise any error corresponding to recvfrom(2) failure, including Errno::EWOULDBLOCK.
If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitReadable. So IO::WaitReadable can be used to rescue the exceptions for retrying recv_nonblock.
By specifying ‘exception: false`, the options hash allows you to indicate that recv_nonblock should not raise an IO::WaitWritable exception, but return the symbol :wait_writable instead.
See
-
Socket#recvfrom
373 374 375 |
# File 'lib/framework/_socket.rb', line 373 def recv_nonblock(len, flag = 0, str = nil, exception: true) __recv_nonblock(len, flag, str, exception) end |
#recvmsg(dlen = nil, flags = 0, clen = nil, scm_rights: false) ⇒ Object
call-seq:
basicsocket.recvmsg(maxmesglen=nil, flags=0, maxcontrollen=nil, opts={}) => [mesg, sender_addrinfo, rflags, *controls]
recvmsg receives a message using recvmsg(2) system call in blocking manner.
maxmesglen is the maximum length of mesg to receive.
flags is bitwise OR of MSG_* constants such as Socket::MSG_PEEK.
maxcontrollen is the maximum length of controls (ancillary data) to receive.
opts is option hash. Currently :scm_rights=>bool is the only option.
:scm_rights option specifies that application expects SCM_RIGHTS control message. If the value is nil or false, application don’t expects SCM_RIGHTS control message. In this case, recvmsg closes the passed file descriptors immediately. This is the default behavior.
If :scm_rights value is neither nil nor false, application expects SCM_RIGHTS control message. In this case, recvmsg creates IO objects for each file descriptors for Socket::AncillaryData#unix_rights method.
The return value is 4-elements array.
mesg is a string of the received message.
sender_addrinfo is a sender socket address for connection-less socket. It is an Addrinfo object. For connection-oriented socket such as TCP, sender_addrinfo is platform dependent.
rflags is a flags on the received message which is bitwise OR of MSG_* constants such as Socket::MSG_TRUNC. It will be nil if the system uses 4.3BSD style old recvmsg system call.
controls is ancillary data which is an array of Socket::AncillaryData objects such as:
#<Socket::AncillaryData: AF_UNIX SOCKET RIGHTS 7>
maxmesglen and maxcontrollen can be nil. In that case, the buffer will be grown until the message is not truncated. Internally, MSG_PEEK is used. Buffer full and MSG_CTRUNC are checked for truncation.
recvmsg can be used to implement recv_io as follows:
mesg, sender_sockaddr, rflags, *controls = sock.recvmsg(:scm_rights=>true)
controls.each {|ancdata|
if ancdata.cmsg_is?(:SOCKET, :RIGHTS)
return ancdata.unix_rights[0]
end
}
428 429 430 |
# File 'lib/framework/_socket.rb', line 428 def recvmsg(dlen = nil, flags = 0, clen = nil, scm_rights: false) __recvmsg(dlen, flags, clen, scm_rights) end |
#recvmsg_nonblock(dlen = nil, flags = 0, clen = nil, scm_rights: false, exception: true) ⇒ Object
call-seq:
basicsocket.recvmsg_nonblock(maxdatalen=nil, flags=0, maxcontrollen=nil, opts={}) => [data, sender_addrinfo, rflags, *controls]
recvmsg receives a message using recvmsg(2) system call in non-blocking manner.
It is similar to BasicSocket#recvmsg but non-blocking flag is set before the system call and it doesn’t retry the system call.
By specifying ‘exception: false`, the opts hash allows you to indicate that recvmsg_nonblock should not raise an IO::WaitWritable exception, but return the symbol :wait_writable instead.
444 445 446 447 |
# File 'lib/framework/_socket.rb', line 444 def recvmsg_nonblock(dlen = nil, flags = 0, clen = nil, scm_rights: false, exception: true) __recvmsg_nonblock(dlen, flags, clen, scm_rights, exception) end |
#send(rest) ⇒ Object
76 77 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 76 def send(rest) end |
#sendmsg(mesg, flags = 0, dest_sockaddr = nil, *controls) ⇒ Object
call-seq:
basicsocket.sendmsg(mesg, flags=0, dest_sockaddr=nil, *controls) => numbytes_sent
sendmsg sends a message using sendmsg(2) system call in blocking manner.
mesg is a string to send.
flags is bitwise OR of MSG_* constants such as Socket::MSG_OOB.
dest_sockaddr is a destination socket address for connection-less socket. It should be a sockaddr such as a result of Socket.sockaddr_in. An Addrinfo object can be used too.
controls is a list of ancillary data. The element of controls should be Socket::AncillaryData or 3-elements array. The 3-element array should contains cmsg_level, cmsg_type and data.
The return value, numbytes_sent is an integer which is the number of bytes sent.
sendmsg can be used to implement send_io as follows:
# use Socket::AncillaryData.
ancdata = Socket::AncillaryData.int(:UNIX, :SOCKET, :RIGHTS, io.fileno)
sock.sendmsg("a", 0, nil, ancdata)
# use 3-element array.
ancdata = [:SOCKET, :RIGHTS, [io.fileno].pack("i!")]
sock.sendmsg("\0", 0, nil, ancdata)
306 307 308 |
# File 'lib/framework/_socket.rb', line 306 def sendmsg(mesg, flags = 0, dest_sockaddr = nil, *controls) __sendmsg(mesg, flags, dest_sockaddr, controls) end |
#sendmsg_nonblock(mesg, flags = 0, dest_sockaddr = nil, *controls, exception: true) ⇒ Object
call-seq:
basicsocket.sendmsg_nonblock(mesg, flags=0, dest_sockaddr=nil, *controls, opts={}) => numbytes_sent
sendmsg_nonblock sends a message using sendmsg(2) system call in non-blocking manner.
It is similar to BasicSocket#sendmsg but the non-blocking flag is set before the system call and it doesn’t retry the system call.
By specifying ‘exception: false`, the opts hash allows you to indicate that sendmsg_nonblock should not raise an IO::WaitWritable exception, but return the symbol :wait_writable instead.
322 323 324 325 |
# File 'lib/framework/_socket.rb', line 322 def sendmsg_nonblock(mesg, flags = 0, dest_sockaddr = nil, *controls, exception: true) __sendmsg_nonblock(mesg, flags, dest_sockaddr, controls, exception) end |
#setsockopt(req, req1, req2) ⇒ Object
68 69 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 68 def setsockopt(req,req1,req2) end |
#shutdown(rest) ⇒ Object
66 67 |
# File 'lib/framework/autocomplete/BasicSocket.rb', line 66 def shutdown(rest) end |