Class: BasicSocket
- Inherits:
-
IO
- Object
- IO
- BasicSocket
- Defined in:
- lib/rubysl/socket/socket.rb
Direct Known Subclasses
Constant Summary collapse
- FFI =
Rubinius::FFI
Class Method Summary collapse
- .do_not_reverse_lookup ⇒ Object
- .do_not_reverse_lookup=(setting) ⇒ Object
- .for_fd ⇒ Object
- .from_descriptor(fixnum) ⇒ Object
Instance Method Summary collapse
- #close_read ⇒ Object
- #close_write ⇒ Object
- #from_descriptor(fixnum) ⇒ Object
-
#getpeername ⇒ Object
Obtain peername information for this socket.
- #getsockname ⇒ Object
- #getsockopt(level, optname) ⇒ Object
- #recv(bytes_to_read, flags = 0) ⇒ Object
-
#recv_nonblock(bytes_to_read, flags = 0) ⇒ Object
Sets socket nonblocking and reads up to given number of bytes.
- #recvfrom(bytes_to_read, flags = 0) ⇒ Object
- #send(message, flags, to = nil) ⇒ Object
- #setsockopt(level, optname, optval) ⇒ Object
- #shutdown(how = 2) ⇒ Object
Class Method Details
.do_not_reverse_lookup ⇒ Object
32 33 34 |
# File 'lib/rubysl/socket/socket.rb', line 32 def self.do_not_reverse_lookup @no_reverse_lookup ? true : false end |
.do_not_reverse_lookup=(setting) ⇒ Object
28 29 30 |
# File 'lib/rubysl/socket/socket.rb', line 28 def self.do_not_reverse_lookup=(setting) @no_reverse_lookup = setting end |
.for_fd ⇒ Object
20 21 22 23 24 |
# File 'lib/rubysl/socket/socket.rb', line 20 def from_descriptor(fixnum) sock = allocate() sock.from_descriptor(fixnum) return sock end |
.from_descriptor(fixnum) ⇒ Object
14 15 16 17 18 |
# File 'lib/rubysl/socket/socket.rb', line 14 def from_descriptor(fixnum) sock = allocate() sock.from_descriptor(fixnum) return sock end |
Instance Method Details
#close_read ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rubysl/socket/socket.rb', line 111 def close_read ensure_open # If we were only in readonly mode, close it all together if @mode & ACCMODE == RDONLY return close end # MRI doesn't check if shutdown worked, so we don't. Socket::Foreign.shutdown @descriptor, 0 @mode = WRONLY nil end |
#close_write ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rubysl/socket/socket.rb', line 127 def close_write ensure_open # If we were only in writeonly mode, close it all together if @mode & ACCMODE == WRONLY return close end Socket::Foreign.shutdown @descriptor, 1 # Mark it as read only @mode = RDONLY nil end |
#from_descriptor(fixnum) ⇒ Object
23 24 25 26 |
# File 'lib/rubysl/socket/socket.rb', line 23 def from_descriptor(fixnum) IO.setup self, fixnum, nil, true return self end |
#getpeername ⇒ Object
Obtain peername information for this socket.
79 80 81 |
# File 'lib/rubysl/socket/socket.rb', line 79 def getpeername() Socket::Foreign.getpeername @descriptor end |
#getsockname ⇒ Object
70 71 72 |
# File 'lib/rubysl/socket/socket.rb', line 70 def getsockname() return Socket::Foreign.getsockname(descriptor) end |
#getsockopt(level, optname) ⇒ Object
36 37 38 |
# File 'lib/rubysl/socket/socket.rb', line 36 def getsockopt(level, optname) Socket::Foreign.getsockopt descriptor, level, optname end |
#recv(bytes_to_read, flags = 0) ⇒ Object
106 107 108 109 |
# File 'lib/rubysl/socket/socket.rb', line 106 def recv(bytes_to_read, flags = 0) # FIXME 0 is knowledge from io.cpp return socket_recv(bytes_to_read, flags, 0) end |
#recv_nonblock(bytes_to_read, flags = 0) ⇒ Object
TODO:
Should EWOULDBLOCK be passed unchanged? –rue
Sets socket nonblocking and reads up to given number of bytes.
148 149 150 151 152 153 |
# File 'lib/rubysl/socket/socket.rb', line 148 def recv_nonblock(bytes_to_read, flags = 0) fcntl Fcntl::F_SETFL, Fcntl::O_NONBLOCK socket_recv bytes_to_read, flags, 0 rescue Errno::EWOULDBLOCK raise Errno::EAGAIN end |
#recvfrom(bytes_to_read, flags = 0) ⇒ Object
101 102 103 104 |
# File 'lib/rubysl/socket/socket.rb', line 101 def recvfrom(bytes_to_read, flags = 0) # FIXME 0 is knowledge from io.cpp return socket_recv(bytes_to_read, flags, 0) end |
#send(message, flags, to = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rubysl/socket/socket.rb', line 86 def send(, flags, to = nil) connect to if to bytes = .length bytes_sent = 0 FFI::MemoryPointer.new :char, bytes + 1 do |buffer| buffer.write_string bytes_sent = Socket::Foreign.send(descriptor, buffer, bytes, flags) Errno.handle 'send(2)' if bytes_sent < 0 end bytes_sent end |
#setsockopt(level, optname, optval) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rubysl/socket/socket.rb', line 40 def setsockopt(level, optname, optval) optval = 1 if optval == true optval = 0 if optval == false error = 0 case optval when Fixnum then FFI::MemoryPointer.new :socklen_t do |val| val.write_int optval error = Socket::Foreign.setsockopt(descriptor, level, optname, val, val.total) end when String then FFI::MemoryPointer.new optval.size do |val| val.write_string optval error = Socket::Foreign.setsockopt(descriptor, level, optname, val, optval.size) end else raise TypeError, "socket option should be a String, a Fixnum, true, or false" end Errno.handle "Unable to set socket option" unless error == 0 return 0 end |
#shutdown(how = 2) ⇒ Object
155 156 157 158 |
# File 'lib/rubysl/socket/socket.rb', line 155 def shutdown(how = 2) err = Socket::Foreign.shutdown @descriptor, how Errno.handle "shutdown" unless err == 0 end |