Class: OpenSSL::SSL::SSLSocket
- Defined in:
- lib/polyphony/extensions/openssl.rb
Overview
OpenSSL socket helper methods (to make it compatible with Socket API) and overrides
Instance Method Summary collapse
-
#dont_linger ⇒ Object
Sets DONT_LINGER option.
-
#initialize(socket, context = nil) ⇒ SSLSocket
constructor
Initializese a new SSL socket.
-
#no_delay ⇒ Object
Sets NO_DELAY option.
-
#read(maxlen = nil, buf = nil, buffer_pos = 0) ⇒ String
Reads from the socket.
-
#read_loop(maxlen = 8192) {|String| ... } ⇒ OpenSSL::SSL::SSLSocket
(also: #recv_loop)
Receives up to
maxlen
bytes at a time in an infinite loop. -
#readpartial(maxlen, buf = +'',, buffer_pos = 0, raise_on_eof = true) ⇒ String?
Reads up to
maxlen
from the socket. -
#reuse_addr ⇒ Object
Sets REUSE_ADDR option.
Constructor Details
#initialize(socket, context = nil) ⇒ SSLSocket
Initializese a new SSL socket
20 21 22 23 |
# File 'lib/polyphony/extensions/openssl.rb', line 20 def initialize(socket, context = nil) socket = socket.respond_to?(:io) ? socket.io || socket : socket context ? orig_initialize(socket, context) : orig_initialize(socket) end |
Instance Method Details
#dont_linger ⇒ Object
Sets DONT_LINGER option
26 27 28 |
# File 'lib/polyphony/extensions/openssl.rb', line 26 def dont_linger io.dont_linger end |
#no_delay ⇒ Object
Sets NO_DELAY option
31 32 33 |
# File 'lib/polyphony/extensions/openssl.rb', line 31 def no_delay io.no_delay end |
#read(maxlen = nil, buf = nil, buffer_pos = 0) ⇒ String
Reads from the socket. If maxlen
is given, reads up to maxlen
bytes from
the socket, otherwise reads to EOF
. If buf
is given, it is used as the
buffer to read into, otherwise a new string is allocated. If buffer_pos
is
given, reads into the given offset (in bytes) in the given buffer. If the
given buffer offset is negative, it is calculated from the current end of
the buffer (-1
means the read data will be appended to the end of the
buffer).
If no bytes are available and EOF
is not hit, this method will block until
the socket is ready to read from.
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/polyphony/extensions/openssl.rb', line 106 def read(maxlen = nil, buf = nil, buffer_pos = 0) return readpartial(maxlen, buf, buffer_pos) if buf buf = +'' return readpartial(maxlen, buf) if maxlen readpartial(4096, buf, -1) while true rescue EOFError buf end |
#read_loop(maxlen = 8192) {|String| ... } ⇒ OpenSSL::SSL::SSLSocket Also known as: recv_loop
Receives up to maxlen
bytes at a time in an infinite loop. Read buffers
will be passed to the given block.
156 157 158 159 160 |
# File 'lib/polyphony/extensions/openssl.rb', line 156 def read_loop(maxlen = 8192) while (data = sysread(maxlen)) yield data end end |
#readpartial(maxlen, buf = +'',, buffer_pos = 0, raise_on_eof = true) ⇒ String?
Reads up to maxlen
from the socket. If buf
is given, it is used as the
buffer to read into, otherwise a new string is allocated. If buffer_pos
is
given, reads into the given offset (in bytes) in the given buffer. If the
given buffer offset is negative, it is calculated from the current end of
the buffer (-1
means the read data will be appended to the end of the
buffer). If raise_on_eof
is true
(the default,) an EOFError
will be
raised on EOF
, otherwise nil
will be returned.
If no bytes are available and EOF
is not hit, this method will block until
the socket is ready to read from.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/polyphony/extensions/openssl.rb', line 133 def readpartial(maxlen, buf = +'', buffer_pos = 0, raise_on_eof = true) if buffer_pos != 0 if (result = sysread(maxlen, +'')) if buffer_pos == -1 result = buf + result else result = buf[0...buffer_pos] + result end end else result = sysread(maxlen, buf) end raise EOFError if !result && raise_on_eof result end |
#reuse_addr ⇒ Object
Sets REUSE_ADDR option
36 37 38 |
# File 'lib/polyphony/extensions/openssl.rb', line 36 def reuse_addr io.reuse_addr end |