public override int Read(byte[] buffer, int offset, int count) { int timeLeft = readTimeout; WaitHandle[] waitHandles = { serverWrote, connectionClosed }; LowResolutionStopwatch stopwatch = new LowResolutionStopwatch(); while (bytesLeft == 0) { stopwatch.Start(); int index = WaitHandle.WaitAny(waitHandles, timeLeft); stopwatch.Stop(); if (index == WaitHandle.WaitTimeout) { throw new TimeoutException("Timeout when reading from shared memory"); } if (waitHandles[index] == connectionClosed) { throw new MySqlException("Connection to server lost", true, null); } if (readTimeout != System.Threading.Timeout.Infinite) { timeLeft = readTimeout - (int)stopwatch.ElapsedMilliseconds; if (timeLeft < 0) { throw new TimeoutException("Timeout when reading from shared memory"); } } bytesLeft = Marshal.ReadInt32(data.View); position = 4; } int len = Math.Min(count, bytesLeft); long baseMem = data.View.ToInt64() + position; for (int i = 0; i < len; i++, position++) { buffer[offset + i] = Marshal.ReadByte((IntPtr)(baseMem + i)); } bytesLeft -= len; if (bytesLeft == 0) { clientRead.Set(); } return(len); }
public override void Write(byte[] buffer, int offset, int count) { int leftToDo = count; int buffPos = offset; WaitHandle[] waitHandles = { serverRead, connectionClosed }; LowResolutionStopwatch stopwatch = new LowResolutionStopwatch(); int timeLeft = writeTimeout; while (leftToDo > 0) { stopwatch.Start(); int index = WaitHandle.WaitAny(waitHandles, timeLeft); stopwatch.Stop(); if (waitHandles[index] == connectionClosed) { throw new MySqlException("Connection to server lost", true, null); } if (index == WaitHandle.WaitTimeout) { throw new TimeoutException("Timeout when reading from shared memory"); } if (writeTimeout != System.Threading.Timeout.Infinite) { timeLeft = writeTimeout - (int)stopwatch.ElapsedMilliseconds; if (timeLeft < 0) { throw new TimeoutException("Timeout when writing to shared memory"); } } int bytesToDo = Math.Min(leftToDo, BUFFERLENGTH); long baseMem = data.View.ToInt64() + 4; Marshal.WriteInt32(data.View, bytesToDo); Marshal.Copy(buffer, buffPos, (IntPtr)baseMem, bytesToDo); buffPos += bytesToDo; leftToDo -= bytesToDo; if (!clientWrote.Set()) { throw new MySqlException("Writing to shared memory failed"); } } }
private IPHostEntry GetDnsHostEntry(string hostname) { LowResolutionStopwatch stopwatch = new LowResolutionStopwatch(); try { stopwatch.Start(); return(Dns.GetHostEntry(hostname)); } catch (SocketException ex) { string message = String.Format(Resources.GetHostEntryFailed, stopwatch.Elapsed, hostname, ex.SocketErrorCode, ex.ErrorCode, ex.NativeErrorCode); throw new Exception(message, ex); } finally { stopwatch.Stop(); } }
public void Open(string path, FileAccess mode, uint timeout) { IntPtr nativeHandle; for (;;) { nativeHandle = NativeMethods.CreateFile(path, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 0, null, 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); }
private IPHostEntry GetDnsHostEntry(string hostname) { LowResolutionStopwatch stopwatch = new LowResolutionStopwatch(); try { stopwatch.Start(); var taskDns = Dns.GetHostEntryAsync(hostname); taskDns.RunSynchronously(); return(taskDns.Result); } catch (SocketException ex) { string message = String.Format(ResourceStrings.GetHostEntryFailed, stopwatch.Elapsed, hostname, ex.SocketErrorCode, ex.HResult, ex.Message); throw new Exception(message, ex); } finally { stopwatch.Stop(); } }
private IPHostEntry GetDnsHostEntry(string hostname) { LowResolutionStopwatch stopwatch = new LowResolutionStopwatch(); try { stopwatch.Start(); return Dns.GetHostEntry(hostname); } catch (SocketException ex) { string message = String.Format(Resources.GetHostEntryFailed, stopwatch.Elapsed, hostname, ex.SocketErrorCode, ex.ErrorCode, ex.NativeErrorCode); throw new Exception(message, ex); } finally { stopwatch.Stop(); } }
public override void Write(byte[] buffer, int offset, int count) { int leftToDo = count; int buffPos = offset; WaitHandle[] waitHandles = { serverRead, connectionClosed }; LowResolutionStopwatch stopwatch = new LowResolutionStopwatch(); int timeLeft = writeTimeout; while (leftToDo > 0) { stopwatch.Start(); int index = WaitHandle.WaitAny(waitHandles, timeLeft); stopwatch.Stop(); if (waitHandles[index] == connectionClosed) throw new MySqlException("Connection to server lost", true, null); if (index == WaitHandle.WaitTimeout) throw new TimeoutException("Timeout when reading from shared memory"); if (writeTimeout != System.Threading.Timeout.Infinite) { timeLeft = writeTimeout - (int)stopwatch.ElapsedMilliseconds; if (timeLeft < 0) throw new TimeoutException("Timeout when writing to shared memory"); } int bytesToDo = Math.Min(leftToDo, BUFFERLENGTH); long baseMem = data.View.ToInt64() + 4; Marshal.WriteInt32(data.View, bytesToDo); Marshal.Copy(buffer, buffPos, (IntPtr)baseMem, bytesToDo); buffPos += bytesToDo; leftToDo -= bytesToDo; if (!clientWrote.Set()) throw new MySqlException("Writing to shared memory failed"); } }
public override int Read(byte[] buffer, int offset, int count) { int timeLeft = readTimeout; WaitHandle[] waitHandles = { serverWrote, connectionClosed }; LowResolutionStopwatch stopwatch = new LowResolutionStopwatch(); while (bytesLeft == 0) { stopwatch.Start(); int index = WaitHandle.WaitAny(waitHandles, timeLeft); stopwatch.Stop(); if (index == WaitHandle.WaitTimeout) throw new TimeoutException("Timeout when reading from shared memory"); if (waitHandles[index] == connectionClosed) throw new MySqlException("Connection to server lost", true, null); if (readTimeout != System.Threading.Timeout.Infinite) { timeLeft = readTimeout - (int)stopwatch.ElapsedMilliseconds; if (timeLeft < 0) throw new TimeoutException("Timeout when reading from shared memory"); } bytesLeft = Marshal.ReadInt32(data.View); position = 4; } int len = Math.Min(count, bytesLeft); long baseMem = data.View.ToInt64() + position; for (int i = 0; i < len; i++, position++) buffer[offset + i] = Marshal.ReadByte((IntPtr)(baseMem + i)); bytesLeft -= len; if (bytesLeft == 0) clientRead.Set(); return len; }
private IPHostEntry GetDnsHostEntry(string hostname) { LowResolutionStopwatch stopwatch = new LowResolutionStopwatch(); try { stopwatch.Start(); var taskDns = Dns.GetHostEntryAsync(hostname); taskDns.RunSynchronously(); return taskDns.Result; } catch (SocketException ex) { string message = String.Format(ResourceStrings.GetHostEntryFailed, stopwatch.Elapsed, hostname, ex.SocketErrorCode, ex.HResult, ex.Message); throw new Exception(message, ex); } finally { stopwatch.Stop(); } }