Beispiel #1
0
        public static LowResolutionStopwatch StartNew()
        {
            LowResolutionStopwatch sw = new LowResolutionStopwatch();

            sw.Start();
            return(sw);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        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");
                }
            }
        }
Beispiel #4
0
        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();
            }
        }
        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 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;
        }
        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();
            }
        }