public override int Read(byte[] buffer, int offset, int count) { int num = this.readTimeout; WaitHandle[] array = new WaitHandle[] { this.serverWrote, this.connectionClosed }; LowResolutionStopwatch lowResolutionStopwatch = new LowResolutionStopwatch(); while (this.bytesLeft == 0) { lowResolutionStopwatch.Start(); int num2 = WaitHandle.WaitAny(array, num); lowResolutionStopwatch.Stop(); if (num2 == 258) { throw new TimeoutException("Timeout when reading from shared memory"); } if (array[num2] == this.connectionClosed) { throw new MySqlException("Connection to server lost", true, null); } if (this.readTimeout != -1) { num = this.readTimeout - (int)lowResolutionStopwatch.ElapsedMilliseconds; if (num < 0) { throw new TimeoutException("Timeout when reading from shared memory"); } } this.bytesLeft = Marshal.ReadInt32(this.data.View); this.position = 4; } int num3 = Math.Min(count, this.bytesLeft); long num4 = this.data.View.ToInt64() + (long)this.position; int i = 0; while (i < num3) { buffer[offset + i] = Marshal.ReadByte((IntPtr)(num4 + (long)i)); i++; this.position++; } this.bytesLeft -= num3; if (this.bytesLeft == 0) { this.clientRead.Set(); } return(num3); }
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 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 override void Write(byte[] buffer, int offset, int count) { int i = count; int num = offset; WaitHandle[] array = new WaitHandle[] { this.serverRead, this.connectionClosed }; LowResolutionStopwatch lowResolutionStopwatch = new LowResolutionStopwatch(); int num2 = this.writeTimeout; while (i > 0) { lowResolutionStopwatch.Start(); int num3 = WaitHandle.WaitAny(array, num2); lowResolutionStopwatch.Stop(); if (array[num3] == this.connectionClosed) { throw new MySqlException("Connection to server lost", true, null); } if (num3 == 258) { throw new TimeoutException("Timeout when reading from shared memory"); } if (this.writeTimeout != -1) { num2 = this.writeTimeout - (int)lowResolutionStopwatch.ElapsedMilliseconds; if (num2 < 0) { throw new TimeoutException("Timeout when writing to shared memory"); } } int num4 = Math.Min(i, 16004); long value = this.data.View.ToInt64() + 4L; Marshal.WriteInt32(this.data.View, num4); Marshal.Copy(buffer, num, (IntPtr)value, num4); num += num4; i -= num4; if (!this.clientWrote.Set()) { throw new MySqlException("Writing to shared memory failed"); } } }
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"); } } }
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; }
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(); } }