comparison Windows/WindowsNamedPipe.cs @ 110:5e717aac4c1d

Improvements in RemotingManager (close event) and Windows Named Pipe (accept time-out)
author Ivo Smits <Ivo@UCIS.nl>
date Fri, 07 Nov 2014 18:33:34 +0100
parents ec222fb577dd
children
comparison
equal deleted inserted replaced
100:2b5e7bb9b979 110:5e717aac4c1d
26 static extern SafeFileHandle CreateNamedPipe([MarshalAs(UnmanagedType.LPStr)] String lpName, UInt32 dwOpenMode, UInt32 dwPipeMode, UInt32 nMaxInstances, UInt32 nOutBufferSize, UInt32 nInBufferSize, UInt32 nDefaultTimeOut, IntPtr lpSecurityAttributes); 26 static extern SafeFileHandle CreateNamedPipe([MarshalAs(UnmanagedType.LPStr)] String lpName, UInt32 dwOpenMode, UInt32 dwPipeMode, UInt32 nMaxInstances, UInt32 nOutBufferSize, UInt32 nInBufferSize, UInt32 nDefaultTimeOut, IntPtr lpSecurityAttributes);
27 [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] 27 [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
28 static extern unsafe bool ConnectNamedPipe(SafeFileHandle hNamedPipe, NativeOverlapped* lpOverlapped); 28 static extern unsafe bool ConnectNamedPipe(SafeFileHandle hNamedPipe, NativeOverlapped* lpOverlapped);
29 [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] 29 [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
30 static extern unsafe bool GetNamedPipeInfo(SafeFileHandle hNamedPipe, out UInt32 lpFlags, IntPtr lpOutBufferSize, IntPtr lpInBufferSize, IntPtr lpMaxInstances); 30 static extern unsafe bool GetNamedPipeInfo(SafeFileHandle hNamedPipe, out UInt32 lpFlags, IntPtr lpOutBufferSize, IntPtr lpInBufferSize, IntPtr lpMaxInstances);
31 [DllImport("kernel32.dll")]
32 [return: MarshalAs(UnmanagedType.Bool)]
33 static extern bool CancelIo(SafeFileHandle hFile);
31 34
32 const UInt32 PIPE_ACCESS_DUPLEX = 0x00000003; 35 const UInt32 PIPE_ACCESS_DUPLEX = 0x00000003;
33 const UInt32 FILE_FLAG_OVERLAPPED = 0x40000000; 36 const UInt32 FILE_FLAG_OVERLAPPED = 0x40000000;
34 const UInt32 PIPE_TYPE_BYTE = 0x00000000; 37 const UInt32 PIPE_TYPE_BYTE = 0x00000000;
35 const UInt32 PIPE_TYPE_MESSAGE = 0x00000004; 38 const UInt32 PIPE_TYPE_MESSAGE = 0x00000004;
58 if (!SetNamedPipeHandleState(handle, ref lpMode, IntPtr.Zero, IntPtr.Zero)) throw new Win32Exception(Marshal.GetLastWin32Error()); 61 if (!SetNamedPipeHandleState(handle, ref lpMode, IntPtr.Zero, IntPtr.Zero)) throw new Win32Exception(Marshal.GetLastWin32Error());
59 return new WindowsNamedPipe(handle); 62 return new WindowsNamedPipe(handle);
60 } 63 }
61 64
62 public unsafe void WaitForClient() { 65 public unsafe void WaitForClient() {
66 WaitForClient(-1);
67 }
68 public unsafe void WaitForClient(int millisecondsTimeout) {
63 uint nread; 69 uint nread;
64 using (ManualResetEvent evt = new ManualResetEvent(false)) { 70 using (ManualResetEvent evt = new ManualResetEvent(false)) {
65 NativeOverlapped overlapped = new NativeOverlapped(); 71 NativeOverlapped overlapped = new NativeOverlapped();
66 overlapped.EventHandle = evt.SafeWaitHandle.DangerousGetHandle(); 72 overlapped.EventHandle = evt.SafeWaitHandle.DangerousGetHandle();
67 if (!ConnectNamedPipe(PipeHandle, &overlapped)) { 73 if (!ConnectNamedPipe(PipeHandle, &overlapped)) {
68 int err = Marshal.GetLastWin32Error(); 74 int err = Marshal.GetLastWin32Error();
69 if (err != 997) throw new Win32Exception(err); 75 if (err != 997) throw new Win32Exception(err);
70 evt.WaitOne(); 76 if (!evt.WaitOne(millisecondsTimeout)) {
77 CancelIo(PipeHandle);
78 throw new TimeoutException("The operation timed out");
79 }
71 if (!GetOverlappedResult(PipeHandle, &overlapped, out nread, false)) throw new Win32Exception(Marshal.GetLastWin32Error()); 80 if (!GetOverlappedResult(PipeHandle, &overlapped, out nread, false)) throw new Win32Exception(Marshal.GetLastWin32Error());
72 } 81 }
73 } 82 }
74 } 83 }
75 84