Ejemplo n.º 1
0
 internal void Remove(TcpItem tcpItem)
 {
     lock (_LockObj)
     {
         try
         {
             _TcpItemPool.Remove(tcpItem);
             tcpItem.TcpClient.Close();
         }
         catch
         {
         }
     }
 }
Ejemplo n.º 2
0
        private void InitPool(HubbleAsyncConnection asyncConnection)
        {
            lock (_LockObj)
            {
                for (int i = 0; i < _MinPoolSize; i++)
                {
                    Hubble.Framework.Net.TcpClient tcpClient;

                    asyncConnection.InitTcpClient(out tcpClient);

                    TcpItem item = new TcpItem();
                    item.TcpClient = tcpClient;

                    _TcpItemPool.AddLast(item);
                }
            }
        }
Ejemplo n.º 3
0
        internal TcpItem GetOne()
        {
            lock (_LockObj)
            {
                if (_TcpItemPool.Count <= 0)
                {
                    throw new System.IO.IOException("Connection Pool is empty");
                }

                if (_TcpItemPool.Count == 1)
                {
                    _TcpItemPool.First.Value.ConnectTimes++;
                    return(_TcpItemPool.First.Value);
                }

                LinkedListNode <TcpItem> node = _TcpItemPool.First;

                while (node.Value.ServerBusy)
                {
                    node = node.Next;

                    if (node == null)
                    {
                        node = _TcpItemPool.First;
                        break;
                    }
                }

                TcpItem item = node.Value;

                item.ConnectTimes++;

                _TcpItemPool.Remove(node);

                _TcpItemPool.AddLast(node);

                return(item);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// open connection with timeout
        /// </summary>
        /// <param name="timeout">in milliseconds</param>
        public void Open(int timeout)
        {
            DateTime start = DateTime.Now;
            DateTime end   = start;

            lock (this)
            {
                ConnectionPool pool = AsyncTcpManager.Get(this, base.ConnectionString);

                _TcpItem = pool.GetOne();
            }

            while ((end - start).TotalMilliseconds <= timeout)
            {
                if (end != start)
                {
                    //not first time

                    lock (this)
                    {
                        //Try to find a connected tcp item to use
                        ConnectionPool pool = AsyncTcpManager.Get(this, base.ConnectionString);

                        try
                        {
                            TcpItem tcpItem = pool.TryGetAConnectedItem();
                            if (tcpItem != null)
                            {
                                _TcpItem = tcpItem;
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                    }
                }

                if (_TcpItem.TcpClient.Closed)
                {
                    if (!_TcpItem.TryGetConnecting())
                    {
                        try
                        {
                            _TcpItem.Enter();
                            _TcpItem.ServerBusy = false;
                            Hubble.Framework.Net.TcpClient tcpClient;
                            InitTcpClient(out tcpClient);
                            tcpClient.ReceiveTimeout = 0;
                            TryConnectTimeout        = 1;
                            CommandReportNoCache     = false;

                            base.Open(tcpClient);

                            if (base.ServerBusy)
                            {
                                _TcpItem.ServerBusy = true;
                                System.Threading.Thread.Sleep(20);
                                end = DateTime.Now;
                            }
                            else
                            {
                                tcpClient.SetToAsync();
                                _TcpItem.TcpClient = tcpClient;
                                return;
                            }
                        }
                        finally
                        {
                            _TcpItem.Connecting = false;
                            _TcpItem.Leave();
                        }
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(20);
                        end = DateTime.Now;
                    }
                }
                else
                {
                    return;
                }
            }

            throw new System.IO.IOException("Connection timeout");
        }