Skip to content

Commit aec21d9

Browse files
mtoddJohn Wedoff
authored andcommitted
Implement read_ber_id with nonblocking read
1 parent c16ae11 commit aec21d9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

lib/net/ber/ber_parser.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ module Net::BER::BERParser
2525
BuiltinSyntax = Net::BER.compile_syntax(:universal => universal,
2626
:context_specific => context)
2727

28+
# Public: specify the BER socket read timeouts, nil by default (no timeout).
29+
attr_accessor :read_ber_timeout
30+
2831
##
2932
# This is an extract of our BER object parsing to simplify our
3033
# understanding of how we parse basic BER object types.
@@ -182,6 +185,17 @@ def read_ber(syntax = nil)
182185

183186
# Internal: Returns the BER message ID or nil.
184187
def read_ber_id
185-
getbyte
188+
begin
189+
read_nonblock(1).ord
190+
rescue IO::WaitReadable
191+
if IO.select([self], nil, nil, read_ber_timeout)
192+
read_nonblock(1).ord
193+
else
194+
raise Net::LDAP::LdapError, "Timed out reading from the socket"
195+
end
196+
end
197+
rescue EOFError
198+
# nothing to read on the socket (StringIO)
199+
nil
186200
end
187201
end

0 commit comments

Comments
 (0)