internal void ReturnSocket(SocketHandler socket)
 {
     TimeSpan span = (TimeSpan) (DateTime.UtcNow - socket.CreationTime);
     bool flag = false;
     lock (this)
     {
         if ((this._socketCachePolicy != SocketCachePolicy.AbsoluteTimeout) || (span < this._socketLifetime))
         {
             for (CachedSocket socket2 = this._socketList; socket2 != null; socket2 = socket2.Next)
             {
                 if (socket == socket2.Handler)
                 {
                     return;
                 }
             }
             this._socketList = new CachedSocket(socket, this._socketList);
             this._socketCount++;
         }
         else
         {
             flag = true;
         }
     }
     if (flag)
     {
         socket.Close();
     }
 }
Example #2
0
        // reject all requests
        internal void Drain()
        {
            _draining = true;
            // wait for all work items to finish
            while (_workItemCount > 0)
            {
                Thread.Sleep(100);
            }

            // is queue empty?
            if (_count == 0)
            {
                return;
            }

            for (;;)
            {
                SocketHandler sh = DequeueRequest(false);
                if (sh == null)
                {
                    break;
                }
                sh.RejectRequestNowSinceServerIsBusy();
            }
        }
Example #3
0
        // method called to process the next request
        internal void ProcessNextRequest(SocketHandler sh)
        {
            sh = GetRequestToExecute(sh);

            if (sh != null)
                sh.ProcessRequestNow();
        } // ProcessNextRequest
        } // CachedSocketList

        internal SocketHandler GetSocket()
        {
            if (_socketCount == 0)
            {
                return(null);
            }

            lock (this)
            {
                if (_socketList != null)
                {
                    SocketHandler socket = _socketList.Handler;
                    _socketList = _socketList.Next;

                    bool bRes = socket.RaceForControl();

                    // We should always have control since there shouldn't
                    //   be contention here.
                    InternalRemotingServices.RemotingAssert(bRes, "someone else has the socket?");

                    _socketCount--;
                    return(socket);
                }
            }

            return(null);
        } // GetSocket
        } // GetSocket

        internal void ReturnSocket(SocketHandler socket)
        {
            TimeSpan socketActiveTime = DateTime.UtcNow - socket.CreationTime;
            bool     closeSocket      = false;

            lock (this)
            {
                // Check if socket active time has exceeded the lifetime
                // If it has just close the Socket
                if (_socketCachePolicy != SocketCachePolicy.AbsoluteTimeout ||
                    socketActiveTime < _socketLifetime)
                {
                    // Ignore duplicate entries for the same socket handler
                    for (CachedSocket curr = _socketList; curr != null; curr = curr.Next)
                    {
                        if (socket == curr.Handler)
                        {
                            return;
                        }
                    }

                    _socketList = new CachedSocket(socket, _socketList);
                    _socketCount++;
                }
                else
                {
                    closeSocket = true;
                }
            }
            if (closeSocket)
            {
                socket.Close();
            }
        } // ReturnSocket
Example #6
0
        // method called to pick up more work
        private void WorkItemCallback(Object state)
        {
            Interlocked.Decrement(ref _workItemCount);

            // is queue empty?
            if (_count == 0)
            {
                return;
            }

            int workerThreads, ioThreads;

            ThreadPool.GetAvailableThreads(out workerThreads, out ioThreads);

            bool bHandledRequest = false;

            // service another request if enough worker threads are available
            if (workerThreads >= _minLocalFreeThreads)
            {
                // pick up request from the queue
                SocketHandler sh = DequeueRequest(workerThreads < _minExternFreeThreads);
                if (sh != null)
                {
                    sh.ProcessRequestNow();
                    bHandledRequest = true;
                }
            }

            if (!bHandledRequest)
            {
                Thread.Sleep(250);
                ScheduleMoreWorkIfNeeded();
            }
        }
Example #7
0
 private void WorkItemCallback(object state)
 {
     Interlocked.Decrement(ref this._workItemCount);
     if (this._count != 0)
     {
         int num;
         int num2;
         ThreadPool.GetAvailableThreads(out num, out num2);
         bool flag = false;
         if (num >= this._minLocalFreeThreads)
         {
             SocketHandler handler = this.DequeueRequest(num < this._minExternFreeThreads);
             if (handler != null)
             {
                 handler.ProcessRequestNow();
                 flag = true;
             }
         }
         if (!flag)
         {
             Thread.Sleep(250);
             this.ScheduleMoreWorkIfNeeded();
         }
     }
 }
Example #8
0
        internal CachedSocket(SocketHandler socket, CachedSocket next)
        {
            _socket         = socket;
            _socketLastUsed = DateTime.Now;

            _next = next;
        } // CachedSocket
Example #9
0
 internal void ProcessNextRequest(SocketHandler sh)
 {
     sh = this.GetRequestToExecute(sh);
     if (sh != null)
     {
         sh.ProcessRequestNow();
     }
 }
Example #10
0
        } // GetSocket

        internal void ReturnSocket(SocketHandler socket)
        {
            lock (this)
            {
                _socketList = new CachedSocket(socket, _socketList);
                _socketCount++;
            }
        } // ReturnSocket
Example #11
0
        private void QueueRequest(SocketHandler sh, bool isLocal) {
            lock (this) {
                if (isLocal)
                    _localQueue.Enqueue(sh);
                else 
                    _externQueue.Enqueue(sh);

                _count++;
            }
        }
Example #12
0
        internal SocketHandler GetSocket()
        {
            SocketHandler socket = this._cachedSocketList.GetSocket();

            if (socket != null)
            {
                return(socket);
            }
            return(this.CreateNewSocket());
        }
 public void ReleaseSocket(string machinePortAndSid, SocketHandler socket)
 {
     RemoteConnection connection = (RemoteConnection) _connections[machinePortAndSid];
     if (connection != null)
     {
         connection.ReleaseSocket(socket);
     }
     else
     {
         socket.Close();
     }
 }
Example #14
0
        } // ProcessNextRequest

        // method called when data arrives for incoming requests
        internal SocketHandler GetRequestToExecute(SocketHandler sh)
        {
            int workerThreads, ioThreads;

            ThreadPool.GetAvailableThreads(out workerThreads, out ioThreads);
            int freeThreads = (ioThreads > workerThreads) ? workerThreads : ioThreads;

            // fast path when there are threads available and nothing queued
            if (freeThreads >= _minExternFreeThreads && _count == 0)
            {
                return(sh);
            }

            bool isLocal = IsLocal(sh);

            // fast path when there are threads for local requests available and nothing queued
            if (isLocal && freeThreads >= _minLocalFreeThreads && _count == 0)
            {
                return(sh);
            }

            // reject if queue limit exceeded
            if (_count >= _queueLimit)
            {
                sh.RejectRequestNowSinceServerIsBusy();
                return(null);
            }

            // can't execute the current request on the current thread -- need to queue
            QueueRequest(sh, isLocal);

            // maybe can execute a request previously queued
            if (freeThreads >= _minExternFreeThreads)
            {
                sh = DequeueRequest(false); // enough threads to process even external requests
            }
            else if (freeThreads >= _minLocalFreeThreads)
            {
                sh = DequeueRequest(true);  // enough threads to process only local requests
            }
            else
            {
                sh = null;
            }

            if (sh == null)                 // not enough threads -> do nothing on this thread
            {
                ScheduleMoreWorkIfNeeded(); // try to schedule to worker thread
            }

            return(sh);
        }
Example #15
0
        public void ReleaseSocket(string machinePortAndSid, SocketHandler socket)
        {
            RemoteConnection connection = (RemoteConnection)_connections[machinePortAndSid];

            if (connection != null)
            {
                connection.ReleaseSocket(socket);
            }
            else
            {
                socket.Close();
            }
        }
        } // RemoteConnection

        internal SocketHandler GetSocket()
        {
            // try the cached socket list
            SocketHandler socketHandler = _cachedSocketList.GetSocket();

            if (socketHandler != null)
            {
                return(socketHandler);
            }

            // Otherwise, we'll just create a new one.
            return(CreateNewSocket());
        } // GetSocket
        } // GetSocket

        public void ReleaseSocket(String machinePortAndSid, SocketHandler socket)
        {
            RemoteConnection connection = (RemoteConnection)_connections[machinePortAndSid];

            if (connection != null)
            {
                connection.ReleaseSocket(socket);
            }
            else
            {
                // there should have been a connection, so let's just close
                //   this socket.
                socket.Close();
            }
        } // ReleaseSocket
Example #18
0
        private void QueueRequest(SocketHandler sh, bool isLocal)
        {
            lock (this) {
                if (isLocal)
                {
                    _localQueue.Enqueue(sh);
                }
                else
                {
                    _externQueue.Enqueue(sh);
                }

                _count++;
            }
        }
 internal SocketHandler GetSocket()
 {
     if (this._socketCount != 0)
     {
         lock (this)
         {
             if (this._socketList != null)
             {
                 SocketHandler handler = this._socketList.Handler;
                 this._socketList = this._socketList.Next;
                 handler.RaceForControl();
                 this._socketCount--;
                 return handler;
             }
         }
     }
     return null;
 }
Example #20
0
        internal SocketHandler GetRequestToExecute(SocketHandler sh)
        {
            int num;
            int num2;

            ThreadPool.GetAvailableThreads(out num, out num2);
            int num3 = (num2 > num) ? num : num2;

            if ((num3 < this._minExternFreeThreads) || (this._count != 0))
            {
                bool isLocal = IsLocal(sh);
                if ((isLocal && (num3 >= this._minLocalFreeThreads)) && (this._count == 0))
                {
                    return(sh);
                }
                if (this._count >= this._queueLimit)
                {
                    sh.RejectRequestNowSinceServerIsBusy();
                    return(null);
                }
                this.QueueRequest(sh, isLocal);
                if (num3 >= this._minExternFreeThreads)
                {
                    sh = this.DequeueRequest(false);
                }
                else if (num3 >= this._minLocalFreeThreads)
                {
                    sh = this.DequeueRequest(true);
                }
                else
                {
                    sh = null;
                }
                if (sh == null)
                {
                    this.ScheduleMoreWorkIfNeeded();
                }
            }
            return(sh);
        }
 internal SocketHandler GetRequestToExecute(SocketHandler sh)
 {
     int num;
     int num2;
     ThreadPool.GetAvailableThreads(out num, out num2);
     int num3 = (num2 > num) ? num : num2;
     if ((num3 < this._minExternFreeThreads) || (this._count != 0))
     {
         bool isLocal = IsLocal(sh);
         if ((isLocal && (num3 >= this._minLocalFreeThreads)) && (this._count == 0))
         {
             return sh;
         }
         if (this._count >= this._queueLimit)
         {
             sh.RejectRequestNowSinceServerIsBusy();
             return null;
         }
         this.QueueRequest(sh, isLocal);
         if (num3 >= this._minExternFreeThreads)
         {
             sh = this.DequeueRequest(false);
         }
         else if (num3 >= this._minLocalFreeThreads)
         {
             sh = this.DequeueRequest(true);
         }
         else
         {
             sh = null;
         }
         if (sh == null)
         {
             this.ScheduleMoreWorkIfNeeded();
         }
     }
     return sh;
 }
Example #22
0
        } // GetSocket

        internal void ReleaseSocket(SocketHandler socket)
        {
            socket.ReleaseControl();
            _cachedSocketList.ReturnSocket(socket);
        } // ReleaseSocket
Example #23
0
        } // GetSocket

        public void ReleaseSocket(String machineAndPort, SocketHandler socket)
        {
            RemoteConnection connection = (RemoteConnection)_connections[machineAndPort];
            if (connection != null)
            {
                connection.ReleaseSocket(socket);
            }
            else
            {
                // there should have been a connection, so let's just close
                //   this socket.
                socket.Close();
            }
        } // ReleaseSocket
Example #24
0
        } // GetSocket


        internal void ReturnSocket(SocketHandler socket)
        {                        
            lock (this)
            {
                _socketList = new CachedSocket(socket, _socketList);
                _socketCount++;                      
            }
        } // ReturnSocket
Example #25
0
 internal CachedSocket(SocketHandler socket, CachedSocket next)
 {
     this._socket         = socket;
     this._socketLastUsed = DateTime.UtcNow;
     this._next           = next;
 }
 internal TcpFixedLengthReadingStream(SocketHandler inputStream, int contentLength)
 {
     this._inputStream = inputStream;
     this._bytesLeft = contentLength;
 }
        } // GetSocket

 
        internal void ReturnSocket(SocketHandler socket)
        { 
            TimeSpan socketActiveTime = DateTime.UtcNow - socket.CreationTime; 
            bool closeSocket = false;
            lock (this) 
            {
                // Check if socket active time has exceeded the lifetime
                // If it has just close the Socket
                if (_socketCachePolicy != SocketCachePolicy.AbsoluteTimeout || 
                    socketActiveTime < _socketLifetime)
                { 
                    // Ignore duplicate entries for the same socket handler 
                    for (CachedSocket curr = _socketList; curr != null; curr = curr.Next){
                        if (socket == curr.Handler) 
                            return;
                    }

                    _socketList = new CachedSocket(socket, _socketList); 
                    _socketCount++;
                } 
                else 
                {
                    closeSocket = true; 
                }
            }
            if(closeSocket)
            { 
                socket.Close();
            } 
        } // ReturnSocket 
Example #28
0
        } // ProcessNextRequest
        

        // method called when data arrives for incoming requests
        internal SocketHandler GetRequestToExecute(SocketHandler sh) {
            int workerThreads, ioThreads;
            ThreadPool.GetAvailableThreads(out workerThreads, out ioThreads);
            int freeThreads = (ioThreads > workerThreads) ? workerThreads : ioThreads;

            // fast path when there are threads available and nothing queued
            if (freeThreads >= _minExternFreeThreads && _count == 0)
                return sh;

            bool isLocal = IsLocal(sh);

            // fast path when there are threads for local requests available and nothing queued
            if (isLocal && freeThreads >= _minLocalFreeThreads && _count == 0)
                return sh;

            // reject if queue limit exceeded
            if (_count >= _queueLimit) {
                sh.RejectRequestNowSinceServerIsBusy();
                return null;
            }

            // can't execute the current request on the current thread -- need to queue
            QueueRequest(sh, isLocal);

            // maybe can execute a request previously queued
            if (freeThreads >= _minExternFreeThreads) {
                sh = DequeueRequest(false); // enough threads to process even external requests
            }
            else if (freeThreads >= _minLocalFreeThreads) {
                sh = DequeueRequest(true);  // enough threads to process only local requests
            }
            else{
                sh = null;
            }

            if (sh == null){                  // not enough threads -> do nothing on this thread
                ScheduleMoreWorkIfNeeded(); // try to schedule to worker thread
            }

            return sh;
        }
 internal TcpChunkedReadingStream(SocketHandler inputStream)
 {
     this._inputStream = inputStream;
     this._bytesLeft = 0;
 }
Example #30
0
 // helpers
 private static bool IsLocal(SocketHandler sh)
 {
     return(sh.IsLocal());
 }
Example #31
0
 // helpers
 private static bool IsLocal(SocketHandler sh) {
     return sh.IsLocal();
 }
        } // GetSocket

        internal void ReleaseSocket(SocketHandler socket)
        {
            socket.ReleaseControl();
            _cachedSocketList.ReturnSocket(socket);
        } // ReleaseSocket
Example #33
0
        internal CachedSocket(SocketHandler socket, CachedSocket next)
        {
            _socket = socket;
            _socketLastUsed = DateTime.Now;

            _next = next;
        } // CachedSocket