|
| 1 | +import functools |
| 2 | +import io |
| 3 | + |
| 4 | +import win32file |
| 5 | +import win32pipe |
| 6 | + |
| 7 | +cSECURITY_SQOS_PRESENT = 0x100000 |
| 8 | +cSECURITY_ANONYMOUS = 0 |
| 9 | +cPIPE_READMODE_MESSAGE = 2 |
| 10 | + |
| 11 | + |
| 12 | +def check_closed(f): |
| 13 | + @functools.wraps(f) |
| 14 | + def wrapped(self, *args, **kwargs): |
| 15 | + if self._closed: |
| 16 | + raise RuntimeError( |
| 17 | + 'Can not reuse socket after connection was closed.' |
| 18 | + ) |
| 19 | + return f(self, *args, **kwargs) |
| 20 | + return wrapped |
| 21 | + |
| 22 | + |
| 23 | +class NpipeSocket(object): |
| 24 | + """ Partial implementation of the socket API over windows named pipes. |
| 25 | + This implementation is only designed to be used as a client socket, |
| 26 | + and server-specific methods (bind, listen, accept...) are not |
| 27 | + implemented. |
| 28 | + """ |
| 29 | + def __init__(self, handle=None): |
| 30 | + self._timeout = win32pipe.NMPWAIT_USE_DEFAULT_WAIT |
| 31 | + self._handle = handle |
| 32 | + self._closed = False |
| 33 | + |
| 34 | + def accept(self): |
| 35 | + raise NotImplementedError() |
| 36 | + |
| 37 | + def bind(self, address): |
| 38 | + raise NotImplementedError() |
| 39 | + |
| 40 | + def close(self): |
| 41 | + self._handle.Close() |
| 42 | + self._closed = True |
| 43 | + |
| 44 | + @check_closed |
| 45 | + def connect(self, address): |
| 46 | + win32pipe.WaitNamedPipe(address, self._timeout) |
| 47 | + handle = win32file.CreateFile( |
| 48 | + address, |
| 49 | + win32file.GENERIC_READ | win32file.GENERIC_WRITE, |
| 50 | + 0, |
| 51 | + None, |
| 52 | + win32file.OPEN_EXISTING, |
| 53 | + cSECURITY_ANONYMOUS | cSECURITY_SQOS_PRESENT, |
| 54 | + 0 |
| 55 | + ) |
| 56 | + self.flags = win32pipe.GetNamedPipeInfo(handle)[0] |
| 57 | + # self.state = win32pipe.GetNamedPipeHandleState(handle)[0] |
| 58 | + |
| 59 | + # if self.state & cPIPE_READMODE_MESSAGE != 0: |
| 60 | + # raise RuntimeError("message readmode pipes not supported") |
| 61 | + self._handle = handle |
| 62 | + self._address = address |
| 63 | + |
| 64 | + @check_closed |
| 65 | + def connect_ex(self, address): |
| 66 | + return self.connect(address) |
| 67 | + |
| 68 | + @check_closed |
| 69 | + def detach(self): |
| 70 | + self._closed = True |
| 71 | + return self._handle |
| 72 | + |
| 73 | + @check_closed |
| 74 | + def dup(self): |
| 75 | + return NpipeSocket(self._handle) |
| 76 | + |
| 77 | + @check_closed |
| 78 | + def fileno(self): |
| 79 | + return int(self._handle) |
| 80 | + |
| 81 | + def getpeername(self): |
| 82 | + return self._address |
| 83 | + |
| 84 | + def getsockname(self): |
| 85 | + return self._address |
| 86 | + |
| 87 | + def getsockopt(self, level, optname, buflen=None): |
| 88 | + raise NotImplementedError() |
| 89 | + |
| 90 | + def ioctl(self, control, option): |
| 91 | + raise NotImplementedError() |
| 92 | + |
| 93 | + def listen(self, backlog): |
| 94 | + raise NotImplementedError() |
| 95 | + |
| 96 | + def makefile(self, mode=None, bufsize=None): |
| 97 | + if mode.strip('b') != 'r': |
| 98 | + raise NotImplementedError() |
| 99 | + rawio = NpipeFileIOBase(self) |
| 100 | + if bufsize is None: |
| 101 | + bufsize = io.DEFAULT_BUFFER_SIZE |
| 102 | + return io.BufferedReader(rawio, buffer_size=bufsize) |
| 103 | + |
| 104 | + @check_closed |
| 105 | + def recv(self, bufsize, flags=0): |
| 106 | + err, data = win32file.ReadFile(self._handle, bufsize) |
| 107 | + return data |
| 108 | + |
| 109 | + @check_closed |
| 110 | + def recvfrom(self, bufsize, flags=0): |
| 111 | + data = self.recv(bufsize, flags) |
| 112 | + return (data, self._address) |
| 113 | + |
| 114 | + @check_closed |
| 115 | + def recvfrom_into(self, buf, nbytes=0, flags=0): |
| 116 | + return self.recv_into(buf, nbytes, flags), self._address |
| 117 | + |
| 118 | + @check_closed |
| 119 | + def recv_into(self, buf, nbytes=0): |
| 120 | + readbuf = buf |
| 121 | + if not isinstance(buf, memoryview): |
| 122 | + readbuf = memoryview(buf) |
| 123 | + |
| 124 | + err, data = win32file.ReadFile( |
| 125 | + self._handle, |
| 126 | + readbuf[:nbytes] if nbytes else readbuf |
| 127 | + ) |
| 128 | + return len(data) |
| 129 | + |
| 130 | + @check_closed |
| 131 | + def send(self, string, flags=0): |
| 132 | + err, nbytes = win32file.WriteFile(self._handle, string) |
| 133 | + return nbytes |
| 134 | + |
| 135 | + @check_closed |
| 136 | + def sendall(self, string, flags=0): |
| 137 | + return self.send(string, flags) |
| 138 | + |
| 139 | + @check_closed |
| 140 | + def sendto(self, string, address): |
| 141 | + self.connect(address) |
| 142 | + return self.send(string) |
| 143 | + |
| 144 | + def setblocking(self, flag): |
| 145 | + if flag: |
| 146 | + return self.settimeout(None) |
| 147 | + return self.settimeout(0) |
| 148 | + |
| 149 | + def settimeout(self, value): |
| 150 | + if value is None: |
| 151 | + self._timeout = win32pipe.NMPWAIT_NOWAIT |
| 152 | + elif not isinstance(value, (float, int)) or value < 0: |
| 153 | + raise ValueError('Timeout value out of range') |
| 154 | + elif value == 0: |
| 155 | + self._timeout = win32pipe.NMPWAIT_USE_DEFAULT_WAIT |
| 156 | + else: |
| 157 | + self._timeout = value |
| 158 | + |
| 159 | + def gettimeout(self): |
| 160 | + return self._timeout |
| 161 | + |
| 162 | + def setsockopt(self, level, optname, value): |
| 163 | + raise NotImplementedError() |
| 164 | + |
| 165 | + @check_closed |
| 166 | + def shutdown(self, how): |
| 167 | + return self.close() |
| 168 | + |
| 169 | + |
| 170 | +class NpipeFileIOBase(io.RawIOBase): |
| 171 | + def __init__(self, npipe_socket): |
| 172 | + self.sock = npipe_socket |
| 173 | + |
| 174 | + def close(self): |
| 175 | + super(NpipeFileIOBase, self).close() |
| 176 | + self.sock = None |
| 177 | + |
| 178 | + def fileno(self): |
| 179 | + return self.sock.fileno() |
| 180 | + |
| 181 | + def isatty(self): |
| 182 | + return False |
| 183 | + |
| 184 | + def readable(self): |
| 185 | + return True |
| 186 | + |
| 187 | + def readinto(self, buf): |
| 188 | + return self.sock.recv_into(buf) |
| 189 | + |
| 190 | + def seekable(self): |
| 191 | + return False |
| 192 | + |
| 193 | + def writable(self): |
| 194 | + return False |
0 commit comments