Ticket #1150: named-pipes-mess.patch
File named-pipes-mess.patch, 6.9 KB (added by , 5 years ago) |
---|
-
xpra/net/bytestreams.py
52 52 import winerror #@UnresolvedImport 53 53 #on win32, we have to deal with a few more odd error codes: 54 54 #(it would be nicer if those were wrapped using errno instead..) 55 #maybe we should just inject those from xpra/platform/win32 ? 55 56 WSAEWOULDBLOCK = 10035 56 57 CONTINUE[WSAEWOULDBLOCK] = "WSAEWOULDBLOCK" 57 58 … … 75 76 WSAETIMEDOUT : "WSAETIMEDOUT", 76 77 WSAEHOSTUNREACH : "WSAEHOSTUNREACH", 77 78 WSAEDISCON : "WSAEDISCON", 78 winerror.ERROR_BROKEN_PIPE : "BROKENPIPE", 79 winerror.ERROR_NO_DATA : "NO_DATA", 80 winerror.ERROR_BROKEN_PIPE : "BROKENPIPE", 81 winerror.ERROR_PIPE_NOT_CONNECTED : "PIPE_NOT_CONNECTED", 79 82 }) 80 83 #on win32, we want to wait just a little while, 81 84 #to prevent servers spinning wildly on non-blocking sockets: -
xpra/platform/win32/namedpipes/connection.py
6 6 7 7 #@PydevCodeAnalysisIgnore 8 8 9 import os 9 10 import binascii 10 import win32api #@UnresolvedImport 11 from xpra.net import ConnectionClosedException 11 12 from xpra.net.bytestreams import Connection 13 import win32api #@UnresolvedImport 12 14 from win32pipe import DisconnectNamedPipe #@UnresolvedImport 13 15 from win32file import ReadFile, WriteFile, CloseHandle, FlushFileBuffers #@UnresolvedImport 14 16 from pywintypes import error #@UnresolvedImport … … 17 19 from xpra.log import Logger 18 20 log = Logger("network", "win32") 19 21 22 PIPE_DEBUG = os.environ.get("XPRA_WIN32_PIPE_DEBUG", "0")=="1" 23 PIPE_PATH = "\\\\.\\pipe\\" 20 24 25 IO_ERRORS = { 26 winerror.ERROR_NO_DATA : "NO_DATA", 27 winerror.ERROR_BROKEN_PIPE : "BROKENPIPE", 28 winerror.ERROR_PIPE_NOT_CONNECTED : "PIPE_NOT_CONNECTED", 29 } 30 31 21 32 class NamedPipeConnection(Connection): 22 33 def __init__(self, name, pipe_handle): 23 34 Connection.__init__(self, name, "named-pipe") 24 35 self.pipe_handle = pipe_handle 36 self.target = name 37 if name.startswith(PIPE_PATH): 38 self.target = name[len(PIPE_PATH):] 25 39 26 40 def untilConcludes(self, *args): 27 41 try: 28 42 return Connection.untilConcludes(self, *args) 29 43 except error as e: 30 code = e[0] 31 raise IOError("%s: %s" % (code, win32api.FormatMessage(code))) 44 #convert IOErrors into ConnectionClosedException 45 if e[0] in IO_ERRORS: 46 self.close() 47 raise ConnectionClosedException(e[2]) 48 log("windows error: %s", e) 49 raise 32 50 33 51 def read(self, n): 34 52 return self._read(self._pipe_read, n) 35 53 36 def _pipe_read(self, buf):37 d ata = []38 hr = winerror.ERROR_MORE_DATA39 while hr==winerror.ERROR_MORE_DATA:40 hr, d = ReadFile(self.pipe_handle, 65536)41 data.append(d)42 s = b"".join(data)43 log("pipe_read: %i / %s", hr, binascii.hexlify(s))44 return s54 def _pipe_read(self, n): 55 d = None 56 while not d: 57 err, d = ReadFile(self.pipe_handle, n) 58 if err!=0: 59 raise ConnectionClosedException(win32api.FormatMessage(err)) 60 if PIPE_DEBUG: 61 log("pipe_read: %i bytes: %s", len(d), binascii.hexlify(d[:32])) 62 return d 45 63 46 64 def write(self, buf): 47 65 return self._write(self._pipe_write, buf) 48 66 49 67 def _pipe_write(self, buf): 50 log("pipe_write: %s", binascii.hexlify(buf)) 51 WriteFile(self.pipe_handle, buf) 68 if PIPE_DEBUG: 69 log("pipe_write: %s", binascii.hexlify(buf)) 70 if not WriteFile(self.pipe_handle, buf): 71 err = win32api.GetLastError() 72 raise ConnectionClosedException(win32api.FormatMessage(err)) 52 73 FlushFileBuffers(self.pipe_handle) 53 #SetFilePointer(self.pipe_handle, 0, FILE_BEGIN)54 74 return len(buf) 55 75 56 76 def close(self): 77 log("close() named pipe %s", self.target) 78 self.set_active(False) 79 ph = self.pipe_handle 80 if ph is None: 81 return 82 self.pipe_handle = None 57 83 try: 58 DisconnectNamedPipe( self.pipe_handle)84 DisconnectNamedPipe(ph) 59 85 except Exception as e: 60 log .error("Error: DisconnectNamedPipe(%s) %s", self.pipe_handle, e)86 log("DisconnectNamedPipe(%s) %s", ph, e) 61 87 try: 62 CloseHandle( self.pipe_handle)88 CloseHandle(ph) 63 89 except Exception as e: 64 log .error("Error: CloseHandle(%s) %s", self.pipe_handle, e)90 log("CloseHandle(%s) %s", ph, e) 65 91 66 92 def __repr__(self): 67 93 return "%s named-pipe" % self.target -
xpra/platform/win32/namedpipes/listener.py
14 14 15 15 import pywintypes, winerror #@UnresolvedImport 16 16 from win32file import CloseHandle, FILE_GENERIC_READ, FILE_GENERIC_WRITE, FILE_FLAG_OVERLAPPED, FILE_ALL_ACCESS #@UnresolvedImport 17 from win32pipe import CreateNamedPipe, ConnectNamedPipe, PIPE_ACCESS_DUPLEX, PIPE_ READMODE_BYTE, PIPE_UNLIMITED_INSTANCES #@UnresolvedImport17 from win32pipe import CreateNamedPipe, ConnectNamedPipe, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE, PIPE_READMODE_MESSAGE, PIPE_WAIT, PIPE_READMODE_BYTE, PIPE_UNLIMITED_INSTANCES #@UnresolvedImport 18 18 from win32api import error #@UnresolvedImport 19 19 from ntsecuritycon import SECURITY_CREATOR_SID_AUTHORITY, SECURITY_WORLD_SID_AUTHORITY, SECURITY_WORLD_RID, SECURITY_CREATOR_OWNER_RID #@UnresolvedImport 20 20 … … 47 47 def do_run(self): 48 48 while not self.exit_loop: 49 49 pipe_handle = self.CreatePipeHandle() 50 log("CreatePipeHandle()=%s (waiting for connections)", pipe_handle) 50 51 try: 51 52 hr = ConnectNamedPipe(pipe_handle) 52 53 assert hr in (0, winerror.ERROR_PIPE_CONNECTED), "ConnectNamedPipe returned %i" % hr … … 67 68 sa = self.CreatePipeSecurityObject() 68 69 try: 69 70 return CreateNamedPipe(self.pipe_name, 70 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,71 PIPE_ READMODE_BYTE,71 PIPE_ACCESS_DUPLEX, # | FILE_FLAG_OVERLAPPED, 72 PIPE_TYPE_MESSAGE | PIPE_WAIT, #PIPE_READMODE_BYTE, 72 73 MAX_INSTANCES, 73 74 0, 0, TIMEOUT, sa) 74 75 except Exception: