예제 #1
0
        private void StartConnect()
        {
            zooKeeper.State = ZooKeeper.States.CONNECTING;
            TcpClient  tempClient = null;
            WaitHandle wh         = null;

            do
            {
                if (zkEndpoints.EndPointID != -1)
                {
                    try
                    {
                        Thread.Sleep(new TimeSpan(0, 0, 0, 0, random.Next(0, 50)));
                    }
                    catch (ThreadInterruptedException e1)
                    {
                        LOG.Warn("Unexpected exception", e1);
                    }
                    if (!zkEndpoints.IsNextEndPointAvailable)
                    {
                        try
                        {
                            // Try not to spin too fast!
                            Thread.Sleep(1000);
                        }
                        catch (ThreadInterruptedException e)
                        {
                            LOG.Warn("Unexpected exception", e);
                        }
                    }
                }

                //advance through available connections;
                zkEndpoints.GetNextAvailableEndpoint();

                Cleanup(tempClient);
                LOG.InfoFormat("Opening socket connection to server {0}", zkEndpoints.CurrentEndPoint.ServerAddress);
                tempClient             = new TcpClient();
                tempClient.LingerState = new LingerOption(false, 0);
                tempClient.NoDelay     = true;

                Interlocked.Exchange(ref initialized, 0);
                IsConnectionClosedByServer = false;

                try
                {
                    IAsyncResult ar = tempClient.BeginConnect(zkEndpoints.CurrentEndPoint.ServerAddress.Address,
                                                              zkEndpoints.CurrentEndPoint.ServerAddress.Port,
                                                              null,
                                                              null);

                    wh = ar.AsyncWaitHandle;
                    if (!ar.AsyncWaitHandle.WaitOne(conn.ConnectionTimeout, false))
                    {
                        Cleanup(tempClient);
                        tempClient = null;
                        throw new TimeoutException();
                    }

                    tempClient.EndConnect(ar);

                    break;
                }
                catch (Exception ex)
                {
                    if (ex is SocketException || ex is TimeoutException)
                    {
                        Cleanup(tempClient);
                        tempClient = null;
                        zkEndpoints.CurrentEndPoint.SetAsFailure();

                        LOG.WarnFormat(string.Format("Failed to connect to {0}:{1}.",
                                                     zkEndpoints.CurrentEndPoint.ServerAddress.Address.ToString(),
                                                     zkEndpoints.CurrentEndPoint.ServerAddress.Port.ToString()));
                    }
                    else
                    {
                        throw;
                    }
                }
                finally
                {
                    wh.Close();
                }
            }while (zkEndpoints.IsNextEndPointAvailable);

            if (tempClient == null)
            {
                throw KeeperException.Create(KeeperException.Code.CONNECTIONLOSS);
            }

            Interlocked.Exchange(ref client, tempClient);
            client.GetStream().BeginRead(incomingBuffer, 0, incomingBuffer.Length, ReceiveAsynch, Tuple.Create(client, incomingBuffer));
            PrimeConnection();
        }
        private void StartConnect()
        {
            zooKeeper.State = ZooKeeper.States.CONNECTING;
            TcpClient  tempClient = null;
            WaitHandle wh         = null;

            do
            {
                if (zkEndpoints.EndPointID != -1)
                {
                    try
                    {
                        Thread.Sleep(new TimeSpan(0, 0, 0, 0, random.Next(0, 50)));
                    }
#if !NET_CORE
                    catch (ThreadInterruptedException e)
                    {
                        LOG.Error("Event thread exiting due to interruption", e);
                    }
#endif
#if NET_CORE
                    catch (Exception e)
                    {
                    }
#endif
                    if (!zkEndpoints.IsNextEndPointAvailable)
                    {
                        try
                        {
                            // Try not to spin too fast!
                            Thread.Sleep(1000);
                        }
#if !NET_CORE
                        catch (ThreadInterruptedException e)
                        {
                            LOG.Error("Event thread exiting due to interruption", e);
                        }
#endif
#if NET_CORE
                        catch (Exception e)
                        {
                        }
#endif
                    }
                }

                //advance through available connections;
                zkEndpoints.GetNextAvailableEndpoint();

                Cleanup(tempClient);
                Console.WriteLine("Opening socket connection to server {0}", zkEndpoints.CurrentEndPoint.ServerAddress);
#if !NET_CORE
                LOG.InfoFormat("Opening socket connection to server {0}", zkEndpoints.CurrentEndPoint.ServerAddress);
#endif
                tempClient             = new TcpClient();
                tempClient.LingerState = new LingerOption(false, 0);
                tempClient.NoDelay     = true;

                Interlocked.Exchange(ref initialized, 0);
                IsConnectionClosedByServer = false;

                try
                {
                    IAsyncResult ar = tempClient.BeginConnect(zkEndpoints.CurrentEndPoint.ServerAddress.Address,
                                                              zkEndpoints.CurrentEndPoint.ServerAddress.Port,
                                                              null,
                                                              null);

                    wh = ar.AsyncWaitHandle;
#if !NET_CORE
                    if (!ar.AsyncWaitHandle.WaitOne(conn.ConnectionTimeout, false))
                    {
                        Cleanup(tempClient);
                        tempClient = null;
                        throw new TimeoutException();
                    }
#else
                    if (!ar.AsyncWaitHandle.WaitOne(conn.ConnectionTimeout))
                    {
                        Cleanup(tempClient);
                        tempClient = null;
                        throw new TimeoutException();
                    }
#endif

                    //tempClient.EndConnect(ar);

                    zkEndpoints.CurrentEndPoint.SetAsSuccess();

                    break;
                }
                catch (Exception ex)
                {
                    if (ex is SocketException || ex is TimeoutException)
                    {
                        Cleanup(tempClient);
                        tempClient = null;
                        zkEndpoints.CurrentEndPoint.SetAsFailure();
#if !NET_CORE
                        LOG.WarnFormat(string.Format("Failed to connect to {0}:{1}.",
                                                     zkEndpoints.CurrentEndPoint.ServerAddress.Address.ToString(),
                                                     zkEndpoints.CurrentEndPoint.ServerAddress.Port.ToString()));
#endif
                    }
                    else
                    {
                        throw;
                    }
                }
                finally
                {
#if !NET_CORE
                    wh.Close();
#endif
#if NET_CORE
                    wh.Dispose();
#endif
                }
            }while (zkEndpoints.IsNextEndPointAvailable);

            if (tempClient == null)
            {
                throw KeeperException.Create(KeeperException.Code.CONNECTIONLOSS);
            }

            client = tempClient;
#if !NET_CORE
            client.GetStream().BeginRead(incomingBuffer, 0, incomingBuffer.Length, ReceiveAsynch, incomingBuffer);
#endif

#if NET_CORE
            byte[] byteData    = incomingBuffer;
            var    tokenSource = new CancellationTokenSource();
            var    token       = tokenSource.Token;
            client.GetStream().ReadAsync(incomingBuffer, 0, incomingBuffer.Length, token);
            tokenSource.Cancel();
            MyReceiveAsynch(-1, byteData);
#endif

            PrimeConnection();
        }