diff options
author | Andreas Maier <andreas.r.maier@gmx.de> | 2018-06-03 21:10:27 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2018-06-04 09:51:15 +0200 |
commit | 780c5ad481a11eb09124cd2f7b751fcaaffaa588 (patch) | |
tree | fda24b728e5300181c4b4c7c47f5ca5173cd6208 | |
parent | 94ecb0af0152f53b9173e43af7a039db925258d5 (diff) | |
download | m2crypto-780c5ad481a11eb09124cd2f7b751fcaaffaa588.tar.gz |
Fixed socket timeout on Windows
Details:
The Connection.set_socket_read_timeout() and set_socket_write_timeout()
methods set the socket timeout viy respective socket options. The
current code provided a struct of two long values (seconds, microseconds).
That is valid only on Linux, UNIX, and (probably) OS-X. On Windows however,
the value must be a single DWORD in milliseconds.
This change supplies the values correctly when on Windows, by converting between
sec,microsec and millisec (rounding as needed). When not on Windows, the code
is unchanged.
Signed-off-by: Andreas Maier <andreas.r.maier@gmx.de>
-rw-r--r-- | M2Crypto/SSL/timeout.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/M2Crypto/SSL/timeout.py b/M2Crypto/SSL/timeout.py index c8611e3..3c82684 100644 --- a/M2Crypto/SSL/timeout.py +++ b/M2Crypto/SSL/timeout.py @@ -7,6 +7,7 @@ Copyright 2008 Heikki Toivonen. All rights reserved. __all__ = ['DEFAULT_TIMEOUT', 'timeout', 'struct_to_timeout', 'struct_size'] +import sys import struct DEFAULT_TIMEOUT = 600 # type: int @@ -20,15 +21,28 @@ class timeout(object): self.microsec = microsec def pack(self): - return struct.pack('ll', self.sec, self.microsec) + if sys.platform == 'win32': + millisec = int(self.sec * 1000 + round(float(self.microsec) / 1000)) + binstr = struct.pack('l', millisec) + else: + binstr = struct.pack('ll', self.sec, self.microsec) + return binstr def struct_to_timeout(binstr): # type: (bytes) -> timeout - (s, ms) = struct.unpack('ll', binstr) - return timeout(s, ms) + if sys.platform == 'win32': + millisec = struct.unpack('l', binstr)[0] + sec = int(round(float(millisec) / 1000)) + microsec = int(round((float(millisec) % 1000) * 1000)) + else: + (sec, microsec) = struct.unpack('ll', binstr) + return timeout(sec, microsec) def struct_size(): # type: () -> int - return struct.calcsize('ll') + if sys.platform == 'win32': + return struct.calcsize('l') + else: + return struct.calcsize('ll') |