~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/tornado/tornado/win32_support.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# NOTE: win32 support is currently experimental, and not recommended
 
2
# for production use.
 
3
 
 
4
import ctypes
 
5
import ctypes.wintypes
 
6
import os
 
7
import socket
 
8
import errno
 
9
 
 
10
 
 
11
# See: http://msdn.microsoft.com/en-us/library/ms738573(VS.85).aspx
 
12
ioctlsocket = ctypes.windll.ws2_32.ioctlsocket
 
13
ioctlsocket.argtypes = (ctypes.wintypes.HANDLE, ctypes.wintypes.LONG, ctypes.wintypes.ULONG)
 
14
ioctlsocket.restype = ctypes.c_int
 
15
 
 
16
# See: http://msdn.microsoft.com/en-us/library/ms724935(VS.85).aspx
 
17
SetHandleInformation = ctypes.windll.kernel32.SetHandleInformation
 
18
SetHandleInformation.argtypes = (ctypes.wintypes.HANDLE, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD)
 
19
SetHandleInformation.restype = ctypes.wintypes.BOOL
 
20
 
 
21
HANDLE_FLAG_INHERIT = 0x00000001
 
22
 
 
23
 
 
24
F_GETFD = 1
 
25
F_SETFD = 2
 
26
F_GETFL = 3
 
27
F_SETFL = 4
 
28
 
 
29
FD_CLOEXEC = 1
 
30
 
 
31
os.O_NONBLOCK = 2048
 
32
 
 
33
FIONBIO = 126
 
34
 
 
35
 
 
36
def fcntl(fd, op, arg=0):
 
37
    if op == F_GETFD or op == F_GETFL:
 
38
        return 0
 
39
    elif op == F_SETFD:
 
40
        # Check that the flag is CLOEXEC and translate
 
41
        if arg == FD_CLOEXEC:
 
42
            success = SetHandleInformation(fd, HANDLE_FLAG_INHERIT, arg)
 
43
            if not success:
 
44
                raise ctypes.GetLastError()
 
45
        else:
 
46
            raise ValueError("Unsupported arg")
 
47
    #elif op == F_SETFL:
 
48
        ## Check that the flag is NONBLOCK and translate
 
49
        #if arg == os.O_NONBLOCK:
 
50
            ##pass
 
51
            #result = ioctlsocket(fd, FIONBIO, 1)
 
52
            #if result != 0:
 
53
                #raise ctypes.GetLastError()
 
54
        #else:
 
55
            #raise ValueError("Unsupported arg")
 
56
    else:
 
57
        raise ValueError("Unsupported op")
 
58
 
 
59
 
 
60
class Pipe(object):
 
61
    """Create an OS independent asynchronous pipe"""
 
62
    def __init__(self):
 
63
        # Based on Zope async.py: http://svn.zope.org/zc.ngi/trunk/src/zc/ngi/async.py
 
64
 
 
65
        self.writer = socket.socket()
 
66
        # Disable buffering -- pulling the trigger sends 1 byte,
 
67
        # and we want that sent immediately, to wake up ASAP.
 
68
        self.writer.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
 
69
 
 
70
        count = 0
 
71
        while 1:
 
72
            count += 1
 
73
            # Bind to a local port; for efficiency, let the OS pick
 
74
            # a free port for us.
 
75
            # Unfortunately, stress tests showed that we may not
 
76
            # be able to connect to that port ("Address already in
 
77
            # use") despite that the OS picked it.  This appears
 
78
            # to be a race bug in the Windows socket implementation.
 
79
            # So we loop until a connect() succeeds (almost always
 
80
            # on the first try).  See the long thread at
 
81
            # http://mail.zope.org/pipermail/zope/2005-July/160433.html
 
82
            # for hideous details.
 
83
            a = socket.socket()
 
84
            a.bind(("127.0.0.1", 0))
 
85
            connect_address = a.getsockname()  # assigned (host, port) pair
 
86
            a.listen(1)
 
87
            try:
 
88
                self.writer.connect(connect_address)
 
89
                break    # success
 
90
            except socket.error, detail:
 
91
                if detail[0] != errno.WSAEADDRINUSE:
 
92
                    # "Address already in use" is the only error
 
93
                    # I've seen on two WinXP Pro SP2 boxes, under
 
94
                    # Pythons 2.3.5 and 2.4.1.
 
95
                    raise
 
96
                # (10048, 'Address already in use')
 
97
                # assert count <= 2 # never triggered in Tim's tests
 
98
                if count >= 10:  # I've never seen it go above 2
 
99
                    a.close()
 
100
                    self.writer.close()
 
101
                    raise socket.error("Cannot bind trigger!")
 
102
                # Close `a` and try again.  Note:  I originally put a short
 
103
                # sleep() here, but it didn't appear to help or hurt.
 
104
                a.close()
 
105
 
 
106
        self.reader, addr = a.accept()
 
107
        self.reader.setblocking(0)
 
108
        self.writer.setblocking(0)
 
109
        a.close()
 
110
        self.reader_fd = self.reader.fileno()
 
111
 
 
112
    def read(self):
 
113
        """Emulate a file descriptors read method"""
 
114
        try:
 
115
            return self.reader.recv(1)
 
116
        except socket.error, ex:
 
117
            if ex.args[0] == errno.EWOULDBLOCK:
 
118
                raise IOError
 
119
            raise
 
120
 
 
121
    def write(self, data):
 
122
        """Emulate a file descriptors write method"""
 
123
        return self.writer.send(data)