public void Open(string path, FileAccess mode, uint timeout) { IntPtr nativeHandle; for (; ;) { NativeMethods.SecurityAttributes security = new NativeMethods.SecurityAttributes(); security.inheritHandle = true; security.Length = Marshal.SizeOf(security); MySqlSecurityPermission.CreatePermissionSet(false).Assert(); nativeHandle = NativeMethods.CreateFile(path, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 0, security, NativeMethods.OPEN_EXISTING, NativeMethods.FILE_FLAG_OVERLAPPED, 0); if (nativeHandle != IntPtr.Zero) { break; } if (Marshal.GetLastWin32Error() != ERROR_PIPE_BUSY) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Error opening pipe"); } LowResolutionStopwatch sw = LowResolutionStopwatch.StartNew(); bool success = NativeMethods.WaitNamedPipe(path, timeout); sw.Stop(); if (!success) { if (timeout < sw.ElapsedMilliseconds || Marshal.GetLastWin32Error() == ERROR_SEM_TIMEOUT) { throw new TimeoutException("Timeout waiting for named pipe"); } else { throw new Win32Exception(Marshal.GetLastWin32Error(), "Error waiting for pipe"); } } timeout -= (uint)sw.ElapsedMilliseconds; } handle = new SafeFileHandle(nativeHandle, true); fileStream = new FileStream(handle, mode, 4096, true); }
public void Open(string path, FileAccess mode, uint timeout) { IntPtr intPtr; LowResolutionStopwatch lowResolutionStopwatch; while (true) { NativeMethods.SecurityAttributes securityAttributes = new NativeMethods.SecurityAttributes(); securityAttributes.inheritHandle = true; securityAttributes.Length = Marshal.SizeOf(securityAttributes); intPtr = NativeMethods.CreateFile(path, 3221225472u, 0u, securityAttributes, 3u, 1073741824u, 0u); if (intPtr != IntPtr.Zero) { goto IL_AA; } if (Marshal.GetLastWin32Error() != 231) { break; } lowResolutionStopwatch = LowResolutionStopwatch.StartNew(); bool arg_6A_0 = NativeMethods.WaitNamedPipe(path, timeout); lowResolutionStopwatch.Stop(); if (!arg_6A_0) { goto Block_2; } timeout -= (uint)lowResolutionStopwatch.ElapsedMilliseconds; } throw new Win32Exception(Marshal.GetLastWin32Error(), "Error opening pipe"); Block_2: if ((ulong)timeout < (ulong)lowResolutionStopwatch.ElapsedMilliseconds || Marshal.GetLastWin32Error() == 121) { throw new TimeoutException("Timeout waiting for named pipe"); } throw new Win32Exception(Marshal.GetLastWin32Error(), "Error waiting for pipe"); IL_AA: this.handle = new SafeFileHandle(intPtr, true); this.fileStream = new FileStream(this.handle, mode, 4096, true); }