|
| 1 | +# Implements nonbocking and timeout handling routines for BER parsing. |
| 2 | +module Net::BER::BERParserNonblock |
| 3 | + # Internal: Returns the BER message ID or nil. |
| 4 | + def read_ber_id |
| 5 | + ber_timeout_getbyte |
| 6 | + end |
| 7 | + private :read_ber_id |
| 8 | + |
| 9 | + # Internal: specify the BER socket read timeouts, nil by default (no timeout). |
| 10 | + attr_accessor :ber_io_deadline |
| 11 | + private :ber_io_deadline |
| 12 | + |
| 13 | + ## |
| 14 | + # sets a timeout of timeout seconds for read_ber and ber_timeout_write operations in the provided block the proin the future for if there is not already a earlier deadline set |
| 15 | + def with_timeout(timeout) |
| 16 | + timeout = timeout.to_f |
| 17 | + # don't change deadline if run without timeout |
| 18 | + return yield if timeout <= 0 |
| 19 | + # clear deadline if it is not in the future |
| 20 | + self.ber_io_deadline = nil unless ber_io_timeout.to_f > 0 |
| 21 | + new_deadline = Time.now + timeout |
| 22 | + # don't add deadline if current deadline is shorter |
| 23 | + return yield if ber_io_deadline && ber_io_deadline < new_deadline |
| 24 | + old_deadline = ber_io_deadline |
| 25 | + begin |
| 26 | + self.ber_io_deadline = new_deadline |
| 27 | + yield |
| 28 | + ensure |
| 29 | + self.ber_io_deadline = old_deadline |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + # seconds until ber_io_deadline |
| 34 | + def ber_io_timeout |
| 35 | + ber_io_deadline ? ber_io_deadline - Time.now : nil |
| 36 | + end |
| 37 | + private :ber_io_timeout |
| 38 | + |
| 39 | + def read_select! |
| 40 | + return if IO.select([self], nil, nil, ber_io_timeout) |
| 41 | + raise Errno::ETIMEDOUT, "Timed out reading from the socket" |
| 42 | + end |
| 43 | + private :read_select! |
| 44 | + |
| 45 | + def write_select! |
| 46 | + return if IO.select(nil, [self], nil, ber_io_timeout) |
| 47 | + raise Errno::ETIMEDOUT, "Timed out reading from the socket" |
| 48 | + end |
| 49 | + private :write_select! |
| 50 | + |
| 51 | + # Internal: Replaces `getbyte` with nonblocking implementation. |
| 52 | + def ber_timeout_getbyte |
| 53 | + read_nonblock(1).ord |
| 54 | + rescue IO::WaitReadable |
| 55 | + read_select! |
| 56 | + retry |
| 57 | + rescue IO::WaitWritable |
| 58 | + write_select! |
| 59 | + retry |
| 60 | + rescue EOFError |
| 61 | + # nothing to read on the socket (StringIO) |
| 62 | + nil |
| 63 | + end |
| 64 | + private :ber_timeout_getbyte |
| 65 | + |
| 66 | + # Internal: Read `len` bytes, respecting timeout. |
| 67 | + def ber_timeout_read(len) |
| 68 | + buffer ||= ''.force_encoding(Encoding::ASCII_8BIT) |
| 69 | + begin |
| 70 | + read_nonblock(len, buffer) |
| 71 | + return buffer if buffer.bytesize >= len |
| 72 | + rescue IO::WaitReadable, IO::WaitWritable |
| 73 | + buffer.clear |
| 74 | + rescue EOFError |
| 75 | + # nothing to read on the socket (StringIO) |
| 76 | + nil |
| 77 | + end |
| 78 | + block ||= ''.force_encoding(Encoding::ASCII_8BIT) |
| 79 | + len -= buffer.bytesize |
| 80 | + loop do |
| 81 | + begin |
| 82 | + read_nonblock(len, block) |
| 83 | + rescue IO::WaitReadable |
| 84 | + read_select! |
| 85 | + retry |
| 86 | + rescue IO::WaitWritable |
| 87 | + write_select! |
| 88 | + retry |
| 89 | + rescue EOFError |
| 90 | + return buffer.empty? ? nil : buffer |
| 91 | + end |
| 92 | + buffer << block |
| 93 | + len -= block.bytesize |
| 94 | + return buffer if len <= 0 |
| 95 | + end |
| 96 | + end |
| 97 | + private :ber_timeout_read |
| 98 | + |
| 99 | + ## |
| 100 | + # Writes val as a plain write would, but respecting the dealine set by with_timeout |
| 101 | + def ber_timeout_write(val) |
| 102 | + total_written = 0 |
| 103 | + while val.bytesize > 0 |
| 104 | + begin |
| 105 | + written = write_nonblock(val) |
| 106 | + rescue IO::WaitReadable |
| 107 | + read_select! |
| 108 | + retry |
| 109 | + rescue IO::WaitWritable |
| 110 | + write_select! |
| 111 | + retry |
| 112 | + end |
| 113 | + total_written += written |
| 114 | + val = val.byteslice(written..-1) |
| 115 | + end |
| 116 | + total_written |
| 117 | + end |
| 118 | +end |
0 commit comments