Exemple #1
0
 /// <summary>
 /// This function is used to cancel the connection for specified connection string.
 /// </summary>
 /// <param name="connectionString">specified connection string</param>
 public static void Cancel(string connectionString)
 {
     AsyncTcpManager.Remove(connectionString);
 }
Exemple #2
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");
        }