public static LowResolutionStopwatch StartNew()
        {
            LowResolutionStopwatch sw = new LowResolutionStopwatch();

            sw.Start();
            return(sw);
        }
Esempio n. 2
0
 /// <summary>
 /// Construct a TimedStream
 /// </summary>
 /// <param name="baseStream"> Undelying stream</param>
 public TimedStream(Stream baseStream)
 {
   this.baseStream = baseStream;
   timeout = baseStream.ReadTimeout;
   isClosed = false;
   stopwatch = new LowResolutionStopwatch();
 }
        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");
                }
            }
        }
        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);

                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 static LowResolutionStopwatch StartNew()
 {
   LowResolutionStopwatch sw = new LowResolutionStopwatch();
   sw.Start();
   return sw;
 }
    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;
    }