summaryrefslogtreecommitdiff
path: root/M2Crypto/SSL/timeout.py
blob: 42b029141ab33f62938f1a80ac9e08d610d28ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Support for SSL socket timeouts.

Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved.

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


class timeout(object):

    def __init__(self, sec=DEFAULT_TIMEOUT, microsec=0):
        # type: (int, int) -> None
        self.sec = sec
        self.microsec = microsec

    def pack(self):
        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
    if sys.platform == 'win32':
        millisec = struct.unpack('l', binstr)[0]
        # On py3, int/int performs exact division and returns float. We want
        # the whole number portion of the exact division result:
        sec = int(millisec / 1000)
        microsec = (millisec % 1000) * 1000
    else:
        (sec, microsec) = struct.unpack('ll', binstr)
    return timeout(sec, microsec)


def struct_size():
    # type: () -> int
    if sys.platform == 'win32':
        return struct.calcsize('l')
    else:
        return struct.calcsize('ll')