Пример #1
0
 public AsyncConnectionPoolRequest(ConnectionPool pool, object owningObject, GeneralAsyncDelegate asyncCallback, int creationTimeout)
 {
     this.Pool            = pool;
     this.OwningObject    = owningObject;
     this.AsyncCallback   = asyncCallback;
     this.CreationTimeout = creationTimeout;
 }
 protected bool Activate(object owningObject, bool async, int timeout, GeneralAsyncDelegate asyncCallback)
 {
     GlobalLog.Assert(owningObject == Owner || Owner == null, "PooledStream::Activate|Owner is not the same as expected.");
     try {
         if (m_Initalizing)
         {
             IPAddress address = null;
             m_AsyncCallback = asyncCallback;
             Socket socket = ServicePoint.GetConnection(this, owningObject, async, out address, ref m_AbortSocket, ref m_AbortSocket6, timeout);
             if (socket != null)
             {
                 m_NetworkStream.InitNetworkStream(socket, FileAccess.ReadWrite);
                 m_ServerAddress = address;
                 m_Initalizing   = false;
                 m_JustConnected = true;
                 m_AbortSocket   = null;
                 m_AbortSocket6  = null;
                 return(true);
             }
             return(false);
         }
         else if (async && asyncCallback != null)
         {
             asyncCallback(owningObject, this);
         }
         return(true);
     } catch {
         m_Initalizing = false;
         throw;
     }
 }
Пример #3
0
        internal PooledStream GetConnection(object owningObject, GeneralAsyncDelegate asyncCallback, int creationTimeout)
        {
            int          num;
            PooledStream pooledStream = null;
            bool         continueLoop = true;
            bool         flag2        = asyncCallback != null;

            if (this.m_State != State.Running)
            {
                throw new InternalException();
            }
            Interlocked.Increment(ref this.m_WaitCount);
            WaitHandle[] waitHandles = this.m_WaitHandles;
            if (!flag2)
            {
                while ((pooledStream == null) && continueLoop)
                {
                    num          = WaitHandle.WaitAny(waitHandles, creationTimeout, false);
                    pooledStream = this.Get(owningObject, num, ref continueLoop, ref waitHandles);
                }
            }
            else
            {
                num = WaitHandle.WaitAny(waitHandles, 0, false);
                if (num != 0x102)
                {
                    pooledStream = this.Get(owningObject, num, ref continueLoop, ref waitHandles);
                }
                if (pooledStream == null)
                {
                    AsyncConnectionPoolRequest asyncRequest = new AsyncConnectionPoolRequest(this, owningObject, asyncCallback, creationTimeout);
                    this.QueueRequest(asyncRequest);
                }
            }
            if (pooledStream != null)
            {
                if (!pooledStream.IsInitalizing)
                {
                    asyncCallback = null;
                }
                try
                {
                    if (!pooledStream.Activate(owningObject, asyncCallback))
                    {
                        pooledStream = null;
                    }
                    return(pooledStream);
                }
                catch
                {
                    this.PutConnection(pooledStream, owningObject, creationTimeout, false);
                    throw;
                }
            }
            if (!flag2)
            {
                throw new InternalException();
            }
            return(pooledStream);
        }
 internal void Deactivate()
 {
     this.m_AsyncCallback = null;
     if (!this.m_ConnectionIsDoomed && this.m_CheckLifetime)
     {
         this.CheckLifetime();
     }
 }
Пример #5
0
        internal void Deactivate()
        {
            // Called when the connection is about to be placed back into the pool; this

            m_AsyncCallback = null;

            if (!m_ConnectionIsDoomed && m_CheckLifetime)
            {
                // check lifetime here - as a side effect it will doom connection if
                // it's lifetime has elapsed
                CheckLifetime();
            }
        }
        protected bool Activate(object owningObject, bool async, int timeout, GeneralAsyncDelegate asyncCallback)
        {
            bool flag;

            try
            {
                if (this.m_Initalizing)
                {
                    IPAddress address = null;
                    this.m_AsyncCallback = asyncCallback;
                    System.Net.Sockets.Socket socket = this.ServicePoint.GetConnection(this, owningObject, async, out address, ref this.m_AbortSocket, ref this.m_AbortSocket6, timeout);
                    if (socket != null)
                    {
                        if (Logging.On)
                        {
                            Logging.PrintInfo(Logging.Web, this, SR.GetString("net_log_socket_connected", new object[] { socket.LocalEndPoint, socket.RemoteEndPoint }));
                        }
                        this.m_NetworkStream.InitNetworkStream(socket, FileAccess.ReadWrite);
                        this.m_ServerAddress = address;
                        this.m_Initalizing   = false;
                        this.m_JustConnected = true;
                        this.m_AbortSocket   = null;
                        this.m_AbortSocket6  = null;
                        return(true);
                    }
                    return(false);
                }
                if (async && (asyncCallback != null))
                {
                    asyncCallback(owningObject, this);
                }
                flag = true;
            }
            catch
            {
                this.m_Initalizing = false;
                throw;
            }
            return(flag);
        }
 protected bool Activate(object owningObject, bool async, int timeout, GeneralAsyncDelegate asyncCallback)
 {
     bool flag;
     try
     {
         if (this.m_Initalizing)
         {
             IPAddress address = null;
             this.m_AsyncCallback = asyncCallback;
             System.Net.Sockets.Socket socket = this.ServicePoint.GetConnection(this, owningObject, async, out address, ref this.m_AbortSocket, ref this.m_AbortSocket6, timeout);
             if (socket != null)
             {
                 if (Logging.On)
                 {
                     Logging.PrintInfo(Logging.Web, this, SR.GetString("net_log_socket_connected", new object[] { socket.LocalEndPoint, socket.RemoteEndPoint }));
                 }
                 this.m_NetworkStream.InitNetworkStream(socket, FileAccess.ReadWrite);
                 this.m_ServerAddress = address;
                 this.m_Initalizing = false;
                 this.m_JustConnected = true;
                 this.m_AbortSocket = null;
                 this.m_AbortSocket6 = null;
                 return true;
             }
             return false;
         }
         if (async && (asyncCallback != null))
         {
             asyncCallback(owningObject, this);
         }
         flag = true;
     }
     catch
     {
         this.m_Initalizing = false;
         throw;
     }
     return flag;
 }
Пример #8
0
 protected bool Activate(object owningObject, bool async, GeneralAsyncDelegate asyncCallback)
 {
     GlobalLog.Assert(owningObject == Owner || Owner == null, "PooledStream::Activate|Owner is not the same as expected.");
     try {
         if (m_Initalizing)
         {
             IPAddress address = null;
             m_AsyncCallback = asyncCallback;
             Socket socket = ServicePoint.GetConnection(this, owningObject, async, out address, ref m_AbortSocket, ref m_AbortSocket6);
             if (socket != null)
             {
                 if (Logging.On)
                 {
                     Logging.PrintInfo(Logging.Web, this,
                                       SR.GetString(SR.net_log_socket_connected, socket.LocalEndPoint,
                                                    socket.RemoteEndPoint));
                 }
                 m_NetworkStream.InitNetworkStream(socket, FileAccess.ReadWrite);
                 m_ServerAddress = address;
                 m_Initalizing   = false;
                 m_JustConnected = true;
                 m_AbortSocket   = null;
                 m_AbortSocket6  = null;
                 return(true);
             }
             return(false);
         }
         else if (async && asyncCallback != null)
         {
             asyncCallback(owningObject, this);
         }
         return(true);
     } catch {
         m_Initalizing = false;
         throw;
     }
 }
        internal void Deactivate() {
            // Called when the connection is about to be placed back into the pool; this

            m_AsyncCallback = null;

            if (!m_ConnectionIsDoomed && m_CheckLifetime) {
                // check lifetime here - as a side effect it will doom connection if
                // it's lifetime has elapsed
                CheckLifetime();
            }
        }
 protected bool Activate(object owningObject, bool async, int timeout, GeneralAsyncDelegate asyncCallback)
 {
     GlobalLog.Assert(owningObject == Owner || Owner == null, "PooledStream::Activate|Owner is not the same as expected.");
     try {
         if (m_Initalizing) {
             IPAddress address = null;
             m_AsyncCallback = asyncCallback;
             Socket socket = ServicePoint.GetConnection(this, owningObject, async, out address, ref m_AbortSocket, ref m_AbortSocket6, timeout);
             if (socket != null) {
                 m_NetworkStream.InitNetworkStream(socket, FileAccess.ReadWrite);
                 m_ServerAddress = address;
                 m_Initalizing = false;
                 m_JustConnected = true;
                 m_AbortSocket = null;
                 m_AbortSocket6 = null;
                 return true;
             }
             return false;
         }
         else if (async && asyncCallback != null)
         {
             asyncCallback(owningObject, this);
         }
         return true;
     } catch {
         m_Initalizing = false;
         throw;
     }
 }
 internal bool Activate(object owningObject, GeneralAsyncDelegate asyncCallback)
 {
     return Activate(owningObject, asyncCallback != null, Timeout.Infinite, asyncCallback);
 }
        /// <devdoc>
        ///    <para>Attempts to create a PooledStream, by trying to get a pooled Connection,
        ///         or by creating its own new one</para>
        /// </devdoc>
        internal PooledStream GetConnection(object owningObject, 
                                            GeneralAsyncDelegate asyncCallback, 
                                            int creationTimeout) {
            int result;
            PooledStream stream = null;
            bool continueLoop = true;
            bool async = (asyncCallback != null) ? true : false;

            GlobalLog.Enter("ConnectionPool#" + ValidationHelper.HashString(this) + "::GetConnection");

            if(m_State != State.Running) {
                throw new InternalException();
            }

            Interlocked.Increment(ref m_WaitCount);
            WaitHandle[] localWaitHandles = m_WaitHandles;

            if (async) {
                result = WaitHandle.WaitAny(localWaitHandles, 0, false);
                if (result != WaitTimeout) {
                    stream = Get(owningObject, result, ref continueLoop, ref localWaitHandles);
                }
                if (stream == null) {
                    GlobalLog.Print("GetConnection:"+ValidationHelper.HashString(this)+" going async");
                    AsyncConnectionPoolRequest asyncState = new AsyncConnectionPoolRequest(this, owningObject, asyncCallback, creationTimeout);
                    QueueRequest(asyncState);
                }
            } else {
                // loop while we don't have an error/timeout and we haven't gotten a stream yet
                while ((stream == null) && continueLoop) {
                    result = WaitHandle.WaitAny(localWaitHandles, creationTimeout, false);
                    stream = Get(owningObject, result, ref continueLoop, ref localWaitHandles);
                }
            }

            if (null != stream) {
                // if there is already a stream, then we're not going async
                if (!stream.IsInitalizing) {
                    asyncCallback = null;
                }

                try{
                    // If activate returns false, it is going to finish asynchronously 
                    // and therefore the stream will be returned in a callback and
                    // we should not return it here (return null)
                    if (stream.Activate(owningObject, asyncCallback) == false)
                        stream = null;
                }
                catch{
                    stream.Close();
                    PutConnection(stream,owningObject,creationTimeout);
                    throw;
                }
            } else if (!async) {
                throw new InternalException();
            }

            GlobalLog.Leave("ConnectionPool#" + ValidationHelper.HashString(this) + "::GetConnection", ValidationHelper.HashString(stream));
            return(stream);
        }
 public AsyncConnectionPoolRequest(ConnectionPool pool, object owningObject, GeneralAsyncDelegate asyncCallback, int creationTimeout) {
     Pool = pool;
     OwningObject = owningObject;
     AsyncCallback = asyncCallback;
     CreationTimeout = creationTimeout;
 }
        /// <devdoc>
        ///    <para>Attempts to create a PooledStream, by trying to get a pooled Connection,
        ///         or by creating its own new one</para>
        /// </devdoc>
        internal PooledStream GetConnection(object owningObject,
                                            GeneralAsyncDelegate asyncCallback,
                                            int creationTimeout)
        {
            int          result;
            PooledStream stream       = null;
            bool         continueLoop = true;
            bool         async        = (asyncCallback != null) ? true : false;

            GlobalLog.Enter("ConnectionPool#" + ValidationHelper.HashString(this) + "::GetConnection");

            if (m_State != State.Running)
            {
                throw new InternalException();
            }

            Interlocked.Increment(ref m_WaitCount);
            WaitHandle[] localWaitHandles = m_WaitHandles;

            if (async)
            {
                result = WaitHandle.WaitAny(localWaitHandles, 0, false);
                if (result != WaitTimeout)
                {
                    stream = Get(owningObject, result, ref continueLoop, ref localWaitHandles);
                }
                if (stream == null)
                {
                    GlobalLog.Print("GetConnection:" + ValidationHelper.HashString(this) + " going async");
                    AsyncConnectionPoolRequest asyncState = new AsyncConnectionPoolRequest(this, owningObject, asyncCallback, creationTimeout);
                    QueueRequest(asyncState);
                }
            }
            else
            {
                // loop while we don't have an error/timeout and we haven't gotten a stream yet
                while ((stream == null) && continueLoop)
                {
                    result = WaitHandle.WaitAny(localWaitHandles, creationTimeout, false);
                    stream = Get(owningObject, result, ref continueLoop, ref localWaitHandles);
                }
            }

            if (null != stream)
            {
                // if there is already a stream, then we're not going async
                if (!stream.IsInitalizing)
                {
                    asyncCallback = null;
                }

                try{
                    // If activate returns false, it is going to finish asynchronously
                    // and therefore the stream will be returned in a callback and
                    // we should not return it here (return null)
                    if (stream.Activate(owningObject, asyncCallback) == false)
                    {
                        stream = null;
                    }
                }
                catch {
                    stream.Close();
                    PutConnection(stream, owningObject, creationTimeout);
                    throw;
                }
            }
            else if (!async)
            {
                throw new InternalException();
            }

            GlobalLog.Leave("ConnectionPool#" + ValidationHelper.HashString(this) + "::GetConnection", ValidationHelper.HashString(stream));
            return(stream);
        }
 internal void Deactivate()
 {
     this.m_AsyncCallback = null;
     if (!this.m_ConnectionIsDoomed && this.m_CheckLifetime)
     {
         this.CheckLifetime();
     }
 }
Пример #16
0
 internal bool Activate(object owningObject, GeneralAsyncDelegate asyncCallback)
 {
     return(Activate(owningObject, asyncCallback != null, asyncCallback));
 }
 internal bool Activate(object owningObject, GeneralAsyncDelegate asyncCallback)
 {
     return this.Activate(owningObject, asyncCallback != null, -1, asyncCallback);
 }
 internal PooledStream GetConnection(object owningObject, GeneralAsyncDelegate asyncCallback, int creationTimeout)
 {
     int num;
     PooledStream pooledStream = null;
     bool continueLoop = true;
     bool flag2 = asyncCallback != null;
     if (this.m_State != State.Running)
     {
         throw new InternalException();
     }
     Interlocked.Increment(ref this.m_WaitCount);
     WaitHandle[] waitHandles = this.m_WaitHandles;
     if (!flag2)
     {
         while ((pooledStream == null) && continueLoop)
         {
             num = WaitHandle.WaitAny(waitHandles, creationTimeout, false);
             pooledStream = this.Get(owningObject, num, ref continueLoop, ref waitHandles);
         }
     }
     else
     {
         num = WaitHandle.WaitAny(waitHandles, 0, false);
         if (num != 0x102)
         {
             pooledStream = this.Get(owningObject, num, ref continueLoop, ref waitHandles);
         }
         if (pooledStream == null)
         {
             AsyncConnectionPoolRequest asyncRequest = new AsyncConnectionPoolRequest(this, owningObject, asyncCallback, creationTimeout);
             this.QueueRequest(asyncRequest);
         }
     }
     if (pooledStream != null)
     {
         if (!pooledStream.IsInitalizing)
         {
             asyncCallback = null;
         }
         try
         {
             if (!pooledStream.Activate(owningObject, asyncCallback))
             {
                 pooledStream = null;
             }
             return pooledStream;
         }
         catch
         {
             this.PutConnection(pooledStream, owningObject, creationTimeout, false);
             throw;
         }
     }
     if (!flag2)
     {
         throw new InternalException();
     }
     return pooledStream;
 }
Пример #19
0
 protected bool Activate(object owningObject, bool async, GeneralAsyncDelegate asyncCallback)
 {
     GlobalLog.Assert(owningObject == Owner || Owner == null, "PooledStream::Activate|Owner is not the same as expected.");
     try {
         if (m_Initalizing) {
             IPAddress address = null;
             m_AsyncCallback = asyncCallback;
             Socket socket = ServicePoint.GetConnection(this, owningObject, async, out address, ref m_AbortSocket, ref m_AbortSocket6);
             if (socket != null) {
                 if (Logging.On) {
                     Logging.PrintInfo(Logging.Web, this, 
                         SR.GetString(SR.net_log_socket_connected, socket.LocalEndPoint, 
                             socket.RemoteEndPoint));
                 }
                 m_NetworkStream.InitNetworkStream(socket, FileAccess.ReadWrite);
                 m_ServerAddress = address;
                 m_Initalizing = false;
                 m_JustConnected = true;
                 m_AbortSocket = null;
                 m_AbortSocket6 = null;
                 return true;
             }
             return false;
         }
         else if (async && asyncCallback != null)
         {
             asyncCallback(owningObject, this);
         }
         return true;
     } catch {
         m_Initalizing = false;
         throw;
     }
 }