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 51 52 53 54 55 56 57 58 59
|
import win32file, win32pipe, pywintypes
PIPE = r"\\.\pipe\echo"
BUFSIZE = 512
class Iocp:
def __init__(self, object):
self.port = win32file.CreateIoCompletionPort(-1, 0, 0, 0)
win32file.CreateIoCompletionPort(object.handle, self.port, 1, 0)
def wait_buggy(self):
win32file.GetQueuedCompletionStatus(self.port, -1)
def wait_good(self):
# keep a reference to the overlapped object
self.result = win32file.GetQueuedCompletionStatus(self.port, -1)[3]
class PipeService:
def __init__(self):
self.handle = win32pipe.CreateNamedPipe(PIPE,
win32pipe.PIPE_ACCESS_DUPLEX|
win32file.FILE_FLAG_OVERLAPPED,
win32pipe.PIPE_TYPE_MESSAGE|
win32pipe.PIPE_READMODE_MESSAGE|
win32pipe.PIPE_WAIT,
1, BUFSIZE, BUFSIZE,
win32pipe.NMPWAIT_WAIT_FOREVER,
None)
win32pipe.ConnectNamedPipe(self.handle, None)
def serve(self):
print "Got connection"
win32file.WriteFile(self.handle, 'Hello!\n')
while 1:
data = win32file.ReadFile(self.handle, BUFSIZE)[1]
print "Got data: %r" % data
if not data[:4] == 'tran':
win32file.WriteFile(self.handle, data)
print "Sent data"
if data[:4] == 'quit':
break
def __del__(self):
win32pipe.DisconnectNamedPipe(self.handle)
if __name__ == '__main__':
import sys
if 's' in sys.argv:
svc = PipeService()
iocp = Iocp(svc)
if 'bug' in sys.argv:
iocp.wait_buggy()
else:
iocp.wait_good()
svc.serve()
elif 'c' in sys.argv:
print win32pipe.CallNamedPipe(PIPE, "Hello there", BUFSIZE, 0)
|