Python win32file.AllocateReadBuffer() Examples
The following are 22
code examples of win32file.AllocateReadBuffer().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
win32file
, or try the search function
.
Example #1
Source File: _win32serialport.py From BitTorrent with GNU General Public License v3.0 | 6 votes |
def serialReadEvent(self): #get that character we set up n = win32file.GetOverlappedResult(self._serial.hComPort, self._overlappedRead, 0) if n: first = str(self.read_buf[:n]) #now we should get everything that is already in the buffer flags, comstat = win32file.ClearCommError(self._serial.hComPort) if comstat.cbInQue: win32event.ResetEvent(self._overlappedRead.hEvent) rc, buf = win32file.ReadFile(self._serial.hComPort, win32file.AllocateReadBuffer(comstat.cbInQue), self._overlappedRead) n = win32file.GetOverlappedResult(self._serial.hComPort, self._overlappedRead, 1) #handle all the received data: self.protocol.dataReceived(first + str(buf[:n])) else: #handle all the received data: self.protocol.dataReceived(first) #set up next one win32event.ResetEvent(self._overlappedRead.hEvent) rc, self.read_buf = win32file.ReadFile(self._serial.hComPort, win32file.AllocateReadBuffer(1), self._overlappedRead)
Example #2
Source File: test_win32pipe.py From ironpython2 with Apache License 2.0 | 6 votes |
def testTransactNamedPipeBlockingBuffer(self): # Like testTransactNamedPipeBlocking, but a pre-allocated buffer is # passed (not really that useful, but it exercises the code path) event = threading.Event() self.startPipeServer(event) open_mode = win32con.GENERIC_READ | win32con.GENERIC_WRITE hpipe = win32file.CreateFile(self.pipename, open_mode, 0, # no sharing None, # default security win32con.OPEN_EXISTING, 0, # win32con.FILE_FLAG_OVERLAPPED, None) # set to message mode. win32pipe.SetNamedPipeHandleState( hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None) buffer = win32file.AllocateReadBuffer(1024) hr, got = win32pipe.TransactNamedPipe(hpipe, str2bytes("foo\0bar"), buffer, None) self.failUnlessEqual(got, str2bytes("bar\0foo")) event.wait(5) self.failUnless(event.isSet(), "Pipe server thread didn't terminate")
Example #3
Source File: _win32serialport.py From python-for-android with Apache License 2.0 | 6 votes |
def serialReadEvent(self): #get that character we set up n = win32file.GetOverlappedResult(self._serial.hComPort, self._overlappedRead, 0) if n: first = str(self.read_buf[:n]) #now we should get everything that is already in the buffer flags, comstat = win32file.ClearCommError(self._serial.hComPort) if comstat.cbInQue: win32event.ResetEvent(self._overlappedRead.hEvent) rc, buf = win32file.ReadFile(self._serial.hComPort, win32file.AllocateReadBuffer(comstat.cbInQue), self._overlappedRead) n = win32file.GetOverlappedResult(self._serial.hComPort, self._overlappedRead, 1) #handle all the received data: self.protocol.dataReceived(first + str(buf[:n])) else: #handle all the received data: self.protocol.dataReceived(first) #set up next one win32event.ResetEvent(self._overlappedRead.hEvent) rc, self.read_buf = win32file.ReadFile(self._serial.hComPort, win32file.AllocateReadBuffer(1), self._overlappedRead)
Example #4
Source File: _win32serialport.py From learn_python3_spider with MIT License | 6 votes |
def serialReadEvent(self): #get that character we set up n = win32file.GetOverlappedResult(self._serial._port_handle, self._overlappedRead, 0) first = to_bytes(self.read_buf[:n]) #now we should get everything that is already in the buffer flags, comstat = self._clearCommError() if comstat.cbInQue: win32event.ResetEvent(self._overlappedRead.hEvent) rc, buf = win32file.ReadFile(self._serial._port_handle, win32file.AllocateReadBuffer(comstat.cbInQue), self._overlappedRead) n = win32file.GetOverlappedResult(self._serial._port_handle, self._overlappedRead, 1) #handle all the received data: self.protocol.dataReceived(first + to_bytes(buf[:n])) else: #handle all the received data: self.protocol.dataReceived(first) #set up next one win32event.ResetEvent(self._overlappedRead.hEvent) rc, self.read_buf = win32file.ReadFile(self._serial._port_handle, win32file.AllocateReadBuffer(1), self._overlappedRead)
Example #5
Source File: _win32serialport.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def serialReadEvent(self): #get that character we set up n = win32file.GetOverlappedResult(self._serial.hComPort, self._overlappedRead, 0) if n: first = str(self.read_buf[:n]) #now we should get everything that is already in the buffer flags, comstat = win32file.ClearCommError(self._serial.hComPort) if comstat.cbInQue: win32event.ResetEvent(self._overlappedRead.hEvent) rc, buf = win32file.ReadFile(self._serial.hComPort, win32file.AllocateReadBuffer(comstat.cbInQue), self._overlappedRead) n = win32file.GetOverlappedResult(self._serial.hComPort, self._overlappedRead, 1) #handle all the received data: self.protocol.dataReceived(first + str(buf[:n])) else: #handle all the received data: self.protocol.dataReceived(first) #set up next one win32event.ResetEvent(self._overlappedRead.hEvent) rc, self.read_buf = win32file.ReadFile(self._serial.hComPort, win32file.AllocateReadBuffer(1), self._overlappedRead)
Example #6
Source File: test_win32file.py From ironpython2 with Apache License 2.0 | 5 votes |
def connect_thread_runner(self, expect_payload, giveup_event): # As Windows 2000 doesn't do ConnectEx, we need to use a non-blocking # accept, as our test connection may never come. May as well use # AcceptEx for this... listener = socket.socket() self.addr = ('localhost', random.randint(10000,64000)) listener.bind(self.addr) listener.listen(1) # create accept socket accepter = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # An overlapped overlapped = pywintypes.OVERLAPPED() overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None) # accept the connection. if expect_payload: buf_size = 1024 else: # when we don't expect data we must be careful to only pass the # exact number of bytes for the endpoint data... buf_size = win32file.CalculateSocketEndPointSize(listener) buffer = win32file.AllocateReadBuffer(buf_size) win32file.AcceptEx(listener, accepter, buffer, overlapped) # wait for the connection or our test to fail. events = giveup_event, overlapped.hEvent rc = win32event.WaitForMultipleObjects(events, False, 2000) if rc == win32event.WAIT_TIMEOUT: self.fail("timed out waiting for a connection") if rc == win32event.WAIT_OBJECT_0: # Our main thread running the test failed and will never connect. return # must be a connection. nbytes = win32file.GetOverlappedResult(listener.fileno(), overlapped, False) if expect_payload: self.request = buffer[:nbytes] accepter.send(str2bytes('some expected response'))
Example #7
Source File: _win32serialport.py From BitTorrent with GNU General Public License v3.0 | 5 votes |
def __init__(self, protocol, deviceNameOrPortNumber, reactor, baudrate = 9600, bytesize = EIGHTBITS, parity = PARITY_NONE, stopbits = STOPBITS_ONE, xonxoff = 0, rtscts = 0): self._serial = serial.Serial(deviceNameOrPortNumber, baudrate=baudrate, bytesize=bytesize, parity=parity, stopbits=stopbits, timeout=None, xonxoff=xonxoff, rtscts=rtscts) self.flushInput() self.flushOutput() self.reactor = reactor self.protocol = protocol self.outQueue = [] self.closed = 0 self.closedNotifies = 0 self.writeInProgress = 0 self.protocol = protocol self._overlappedRead = win32file.OVERLAPPED() self._overlappedRead.hEvent = win32event.CreateEvent(None, 1, 0, None) self._overlappedWrite = win32file.OVERLAPPED() self._overlappedWrite.hEvent = win32event.CreateEvent(None, 0, 0, None) self.reactor.addEvent(self._overlappedRead.hEvent, self, 'serialReadEvent') self.reactor.addEvent(self._overlappedWrite.hEvent, self, 'serialWriteEvent') self.protocol.makeConnection(self) flags, comstat = win32file.ClearCommError(self._serial.hComPort) rc, self.read_buf = win32file.ReadFile(self._serial.hComPort, win32file.AllocateReadBuffer(1), self._overlappedRead)
Example #8
Source File: _win32serialport.py From python-for-android with Apache License 2.0 | 5 votes |
def __init__(self, protocol, deviceNameOrPortNumber, reactor, baudrate = 9600, bytesize = EIGHTBITS, parity = PARITY_NONE, stopbits = STOPBITS_ONE, xonxoff = 0, rtscts = 0): self._serial = serial.Serial(deviceNameOrPortNumber, baudrate=baudrate, bytesize=bytesize, parity=parity, stopbits=stopbits, timeout=None, xonxoff=xonxoff, rtscts=rtscts) self.flushInput() self.flushOutput() self.reactor = reactor self.protocol = protocol self.outQueue = [] self.closed = 0 self.closedNotifies = 0 self.writeInProgress = 0 self.protocol = protocol self._overlappedRead = win32file.OVERLAPPED() self._overlappedRead.hEvent = win32event.CreateEvent(None, 1, 0, None) self._overlappedWrite = win32file.OVERLAPPED() self._overlappedWrite.hEvent = win32event.CreateEvent(None, 0, 0, None) self.reactor.addEvent(self._overlappedRead.hEvent, self, 'serialReadEvent') self.reactor.addEvent(self._overlappedWrite.hEvent, self, 'serialWriteEvent') self.protocol.makeConnection(self) flags, comstat = win32file.ClearCommError(self._serial.hComPort) rc, self.read_buf = win32file.ReadFile(self._serial.hComPort, win32file.AllocateReadBuffer(1), self._overlappedRead)
Example #9
Source File: _win32serialport.py From learn_python3_spider with MIT License | 5 votes |
def _finishPortSetup(self): """ Finish setting up the serial port. This is a separate method to facilitate testing. """ flags, comstat = self._clearCommError() rc, self.read_buf = win32file.ReadFile(self._serial._port_handle, win32file.AllocateReadBuffer(1), self._overlappedRead)
Example #10
Source File: dirmon.py From dragonfly with GNU Lesser General Public License v3.0 | 5 votes |
def __init__(self): self._buffer = win32file.AllocateReadBuffer(8192) self._directories = []
Example #11
Source File: _win32serialport.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def _finishPortSetup(self): """ Finish setting up the serial port. This is a separate method to facilitate testing. """ flags, comstat = win32file.ClearCommError(self._serial.hComPort) rc, self.read_buf = win32file.ReadFile(self._serial.hComPort, win32file.AllocateReadBuffer(1), self._overlappedRead)
Example #12
Source File: test_win32file.py From ironpython2 with Apache License 2.0 | 5 votes |
def _watcherThreadOverlapped(self, dn, dh, changes): flags = win32con.FILE_NOTIFY_CHANGE_FILE_NAME buf = win32file.AllocateReadBuffer(8192) overlapped = pywintypes.OVERLAPPED() overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None) while 1: win32file.ReadDirectoryChangesW(dh, buf, False, #sub-tree flags, overlapped) # Wait for our event, or for 5 seconds. rc = win32event.WaitForSingleObject(overlapped.hEvent, 5000) if rc == win32event.WAIT_OBJECT_0: # got some data! Must use GetOverlappedResult to find out # how much is valid! 0 generally means the handle has # been closed. Blocking is OK here, as the event has # already been set. nbytes = win32file.GetOverlappedResult(dh, overlapped, True) if nbytes: bits = win32file.FILE_NOTIFY_INFORMATION(buf, nbytes) changes.extend(bits) else: # This is "normal" exit - our 'tearDown' closes the # handle. # print "looks like dir handle was closed!" return else: print "ERROR: Watcher thread timed-out!" return # kill the thread!
Example #13
Source File: test_win32file.py From ironpython2 with Apache License 2.0 | 5 votes |
def testAcceptEx(self): port = 4680 running = threading.Event() stopped = threading.Event() t = threading.Thread(target=self.acceptWorker, args=(port, running,stopped)) t.start() running.wait(2) if not running.isSet(): self.fail("AcceptEx Worker thread failed to start") s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('127.0.0.1', port)) win32file.WSASend(s, str2bytes("hello"), None) overlapped = pywintypes.OVERLAPPED() overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None) # Like above - WSARecv used to allow strings as the receive buffer!! buffer = " " * 10 self.assertRaises(TypeError, win32file.WSARecv, s, buffer, overlapped) # This one should work :) buffer = win32file.AllocateReadBuffer(10) win32file.WSARecv(s, buffer, overlapped) nbytes = win32file.GetOverlappedResult(s.fileno(), overlapped, True) got = buffer[:nbytes] self.failUnlessEqual(got, str2bytes("hello")) # thread should have stopped stopped.wait(2) if not stopped.isSet(): self.fail("AcceptEx Worker thread failed to successfully stop")
Example #14
Source File: test_win32file.py From ironpython2 with Apache License 2.0 | 5 votes |
def acceptWorker(self, port, running_event, stopped_event): listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listener.bind(('', port)) listener.listen(200) # create accept socket accepter = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # An overlapped overlapped = pywintypes.OVERLAPPED() overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None) # accept the connection. # We used to allow strings etc to be passed here, and they would be # modified! Obviously this is evil :) buffer = " " * 1024 # EVIL - SHOULD NOT BE ALLOWED. self.assertRaises(TypeError, win32file.AcceptEx, listener, accepter, buffer, overlapped) # This is the correct way to allocate the buffer... buffer = win32file.AllocateReadBuffer(1024) rc = win32file.AcceptEx(listener, accepter, buffer, overlapped) self.failUnlessEqual(rc, winerror.ERROR_IO_PENDING) # Set the event to say we are all ready running_event.set() # and wait for the connection. rc = win32event.WaitForSingleObject(overlapped.hEvent, 2000) if rc == win32event.WAIT_TIMEOUT: self.fail("timed out waiting for a connection") nbytes = win32file.GetOverlappedResult(listener.fileno(), overlapped, False) #fam, loc, rem = win32file.GetAcceptExSockaddrs(accepter, buffer) accepter.send(buffer[:nbytes]) # NOT set in a finally - this means *successfully* stopped! stopped_event.set()
Example #15
Source File: test_win32file.py From ironpython2 with Apache License 2.0 | 5 votes |
def testSimpleSlice(self): buffer = win32file.AllocateReadBuffer(2) val = str2bytes('\0\0') buffer[:2] = val self.failUnlessEqual(buffer[0:2], val)
Example #16
Source File: test_win32file.py From ironpython2 with Apache License 2.0 | 5 votes |
def testSimpleIndex(self): val = str2bytes('\xFF') buffer = win32file.AllocateReadBuffer(1) buffer[0] = val self.failUnlessEqual(buffer[0], val)
Example #17
Source File: test_win32file.py From ironpython2 with Apache License 2.0 | 5 votes |
def testLen(self): buffer = win32file.AllocateReadBuffer(1) self.failUnlessEqual(len(buffer), 1)
Example #18
Source File: test_win32pipe.py From ironpython2 with Apache License 2.0 | 5 votes |
def testTransactNamedPipeAsync(self): event = threading.Event() overlapped = pywintypes.OVERLAPPED() overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None) self.startPipeServer(event, 0.5) open_mode = win32con.GENERIC_READ | win32con.GENERIC_WRITE hpipe = win32file.CreateFile(self.pipename, open_mode, 0, # no sharing None, # default security win32con.OPEN_EXISTING, win32con.FILE_FLAG_OVERLAPPED, None) # set to message mode. win32pipe.SetNamedPipeHandleState( hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None) buffer = win32file.AllocateReadBuffer(1024) hr, got = win32pipe.TransactNamedPipe(hpipe, str2bytes("foo\0bar"), buffer, overlapped) self.failUnlessEqual(hr, winerror.ERROR_IO_PENDING) nbytes = win32file.GetOverlappedResult(hpipe, overlapped, True) got = buffer[:nbytes] self.failUnlessEqual(got, str2bytes("bar\0foo")) event.wait(5) self.failUnless(event.isSet(), "Pipe server thread didn't terminate")
Example #19
Source File: Stateless_module.py From XFLTReaT with MIT License | 5 votes |
def post_authentication_server(self, control_message, additional_data): c = self.lookup_client_pub(additional_data) if c.get_initiated(): c.set_authenticated(True) self.packetselector.add_client(c) if c.get_pipe_r() not in self.rlist: self.rlist.append(c.get_pipe_r()) if self.os_type == common.OS_WINDOWS: # creating objects and adding to corresponding lists import win32event import win32file import pywintypes hEvent_pipe = win32event.CreateEvent(None, 0, 0, None) # for reading from the pipe overlapped_pipe = pywintypes.OVERLAPPED() overlapped_pipe.hEvent = hEvent_pipe message_buffer = win32file.AllocateReadBuffer(4096) self.olist.append(overlapped_pipe) self.elist.append(hEvent_pipe) self.mlist.append(message_buffer) self.ulist.append(len(self.elist)-1) return True return False # PLACEHOLDER: prolog for the communication # What comes here: anything that should be set up before the actual # communication
Example #20
Source File: __init__.py From EventGhost with GNU General Public License v2.0 | 4 votes |
def ReceiveThread(self): from win32event import ( ResetEvent, MsgWaitForMultipleObjects, QS_ALLINPUT, WAIT_OBJECT_0, WAIT_TIMEOUT, ) from win32file import ReadFile, AllocateReadBuffer, GetOverlappedResult from win32api import GetLastError continueLoop = True overlapped = self.serial._overlappedRead hComPort = self.serial.hComPort hEvent = overlapped.hEvent stopEvent = self.stopEvent n = 1 waitingOnRead = False buf = AllocateReadBuffer(n) while continueLoop: if not waitingOnRead: ResetEvent(hEvent) hr, _ = ReadFile(hComPort, buf, overlapped) if hr == 997: waitingOnRead = True elif hr == 0: pass #n = GetOverlappedResult(hComPort, overlapped, 1) #self.HandleChar(str(buf)) else: self.PrintError("error") raise rc = MsgWaitForMultipleObjects( (hEvent, stopEvent), 0, 1000, QS_ALLINPUT ) if rc == WAIT_OBJECT_0: n = GetOverlappedResult(hComPort, overlapped, 1) if n: self.HandleChar(str(buf)) #else: # print "WAIT_OBJECT_0", n, str(buf[:n]) waitingOnRead = False elif rc == WAIT_OBJECT_0+1: continueLoop = False elif rc == WAIT_TIMEOUT: pass else: self.PrintError("unknown message")
Example #21
Source File: __init__.py From EventGhost with GNU General Public License v2.0 | 4 votes |
def ReceiveThread(self): from win32event import ( ResetEvent, MsgWaitForMultipleObjects, QS_ALLINPUT, WAIT_OBJECT_0, WAIT_TIMEOUT, ) from win32file import ReadFile, AllocateReadBuffer, GetOverlappedResult from win32api import GetLastError continueLoop = True overlapped = self.serial._overlappedRead hComPort = self.serial.hComPort hEvent = overlapped.hEvent stopEvent = self.stopEvent n = 1 waitingOnRead = False buf = AllocateReadBuffer(n) while continueLoop: if not waitingOnRead: ResetEvent(hEvent) hr, _ = ReadFile(hComPort, buf, overlapped) if hr == 997: waitingOnRead = True elif hr == 0: pass #n = GetOverlappedResult(hComPort, overlapped, 1) #self.HandleChar(str(buf)) else: self.PrintError("error") raise rc = MsgWaitForMultipleObjects( (hEvent, stopEvent), 0, 1000, QS_ALLINPUT ) if rc == WAIT_OBJECT_0: n = GetOverlappedResult(hComPort, overlapped, 1) if n: self.HandleChar(str(buf)) #else: # print "WAIT_OBJECT_0", n, str(buf[:n]) waitingOnRead = False elif rc == WAIT_OBJECT_0+1: continueLoop = False elif rc == WAIT_TIMEOUT: pass else: self.PrintError("unknown message")
Example #22
Source File: packetselector.py From XFLTReaT with MIT License | 4 votes |
def run_windows(self): import win32file import win32event import pywintypes import winerror import win32api # creating events, overlapped structures and a buffer for reading and writing hEvent_read = win32event.CreateEvent(None, 0, 0, None) overlapped_read = pywintypes.OVERLAPPED() overlapped_read.hEvent = hEvent_read overlapped_write = pywintypes.OVERLAPPED() message = win32file.AllocateReadBuffer(4096) while not self._stop: try: # Overlapped/async read, it either blocks or returns pending hr, _ = win32file.ReadFile(self.tunnel, message, overlapped_read) if (hr == winerror.ERROR_IO_PENDING): # when the event gets signalled or timeout happens it will return rc = win32event.WaitForSingleObject(hEvent_read, int(self.timeout*1000)) if rc == winerror.WAIT_TIMEOUT: # timed out, just rerun read continue if rc == win32event.WAIT_OBJECT_0: # read happened, packet is in "message" if (overlapped_read.InternalHigh < 4) or (message[0:1] != "\x45"): #Only care about IPv4 # too small which should not happen or not IPv4, so we just drop it. continue # reading out the packet from the buffer and discarding the rest readytogo = message[0:overlapped_read.InternalHigh] # looking for client for c in self.clients: if c.get_private_ip_addr() == readytogo[16:20]: # client found, writing packet on client's pipe # ignoring outcome, it is async so it will happen when it will happen ;) win32file.WriteFile(c.get_pipe_w(), readytogo, overlapped_write) except win32api.error as e: if e.args[0] == 995: common.internal_print("Interface disappered, exiting PS thread: {0}".format(e), -1) self.stop() continue if e.args[0] == 1453: common.internal_print("OS Internal error: {0}".format(e), -1) self.stop() continue common.internal_print("PS Exception: {0}".format(e), -1) return # stop the so called infinite loop