private static void HostConnect_StateChanged(object sender, StateChangedEventArgs e)
 {
     Logger.Info("HostConnect_StateChanged: " + e.state);
     if (e.state == HostConnectClientState.Disconnected && !isStopped)
     {
         int timeout = reconnectTimer.GetNextTimeout();
         Logger.Info("HostConnect disconnected. Automatic reconnection in " + (timeout / 1000) + " seconds.");
         SetTimeout.OnBackground(hostConnect.Connect, timeout);
     }
     else if (e.state == HostConnectClientState.Connected)
     {
         reconnectTimer.Reset();
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Writes the specified amount of data to the outbound stream, not returning until all data has been placed in the underlying memory-mapped file. This method typically returns before the remote reader has finished reading the written data.
        /// </summary>
        /// <param name="buffer">The buffer containing data to write.</param>
        /// <param name="offset">The offset within the buffer at which to begin copying data.</param>
        /// <param name="count">The number of bytes to write.  Must be less than or equal to the length of the buffer minus the offset.</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (count == 0)
            {
                return;
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (buffer.Length == 0)
            {
                throw new ArgumentException("buffer must have size larger than 0", "buffer");
            }
            if (count < 0 || count > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException("count", "Value " + count + " is out of range");
            }
            if (offset < 0 || offset >= buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset", "Value " + offset + " is out of range");
            }
            if (count > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException("count", count, "value exceeds the size of the buffer minus the offset");
            }

            int totalAmountWritten         = 0;
            WaitProgressivelyLonger waiter = new WaitProgressivelyLonger();

            do
            {
                ulong bytesReadByRemote              = GetBytesReadByRemote();
                int   bufferSpaceAlreadyConsumed     = (int)(myBytesWritten - bytesReadByRemote);
                int   bufferSpaceAvailable           = bufferSize - bufferSpaceAlreadyConsumed;
                int   currentPositionInMyWriteBuffer = (int)(myBytesWritten % bufferSize);
                int   availableBeforeEndOfBuffer     = bufferSize - currentPositionInMyWriteBuffer;
                int   amountToWrite = Math.Min(bufferSpaceAvailable, Math.Min(count - totalAmountWritten, availableBeforeEndOfBuffer));
                if (amountToWrite > 0)
                {
                    waiter.Reset();
                    long actualWritePosition = myOutgoingStreamStartsAt + currentPositionInMyWriteBuffer;
                    try
                    {
                        viewAccessor.WriteArray(actualWritePosition, buffer, offset + totalAmountWritten, amountToWrite);
                        myBytesWritten     += (ulong)amountToWrite;
                        totalAmountWritten += amountToWrite;
                    }
                    finally
                    {
                        SetBytesWrittenByMe();
                    }
                }
                else
                {
                    waiter.Wait();
                }
            }while (totalAmountWritten < count);
            if (totalAmountWritten != count)
            {
                throw new Exception("Total amount written to viewAccessor (" + totalAmountWritten + ") did not match count (" + count + "). Possible logic error.");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads the specified amount of data from the incoming stream, not returning until all the requested data is read.
        /// </summary>
        /// <param name="buffer">The buffer to store data in as it is read.</param>
        /// <param name="offset">The offset in the buffer to begin storing data.</param>
        /// <param name="count">The number of bytes to read.  Must be less than or equal to the length of the buffer minus the offset.</param>
        /// <returns>The number of bytes read. This will always be equal to count.</returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (count == 0)
            {
                return(0);
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (buffer.Length == 0)
            {
                throw new ArgumentException("buffer must have size larger than 0", "buffer");
            }
            if (count < 0 || count > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException("count", "Value " + count + " is out of range");
            }
            if (offset < 0 || offset >= buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset", "Value " + offset + " is out of range");
            }

            int totalAmountRead            = 0;
            WaitProgressivelyLonger waiter = new WaitProgressivelyLonger();

            do
            {
                ulong bytesWrittenByRemote          = GetBytesWrittenByRemote();
                int   bytesAvailableToRead          = (int)(bytesWrittenByRemote - myBytesRead);
                int   currentPositionInMyReadBuffer = (int)(myBytesRead % bufferSize);
                int   availableBeforeEndOfBuffer    = bufferSize - currentPositionInMyReadBuffer;
                int   amountToRead = Math.Min(bytesAvailableToRead, Math.Min(count - totalAmountRead, availableBeforeEndOfBuffer));
                if (amountToRead > 0)
                {
                    waiter.Reset();
                    long actualReadPosition = myIncomingStreamStartsAt + currentPositionInMyReadBuffer;
                    try
                    {
                        int amountRead = viewAccessor.ReadArray(actualReadPosition, buffer, offset + totalAmountRead, amountToRead);
                        myBytesRead     += (ulong)amountRead;
                        totalAmountRead += amountRead;
                        if (amountRead != amountToRead)
                        {
                            throw new Exception("Read " + amountRead + " from viewAccessor, but meant to read " + amountToRead + ". Possible logic error.");
                        }
                    }
                    finally
                    {
                        SetBytesReadByMe();
                    }
                }
                else
                {
                    waiter.Wait();
                }
            }while (totalAmountRead < count);
            if (totalAmountRead != count)
            {
                throw new Exception("Total amount read from viewAccessor (" + totalAmountRead + ") did not match count (" + count + "). Possible logic error.");
            }
            return(count);
        }