Beispiel #1
0
        public void Initialize()
        {
            // Load server list from remote config
            _mClientConfig.Refresh();
            serversFromRemoteConfig = _mClientConfig.Servers;
            Logger.Info("Max Queue Size: " + _mClientConfig.MaxQueueSize);
            Logger.Info("Max Queue Byte Size: " + _mClientConfig.MaxQueueByteSize);

            // Connect to the server
            _mActiveChannel = GetActiveConnection(_mClientConfig.Servers, serversFromRemoteConfig);

            // Do not occupty threads in the global ThreadPool, in which the number of threads is limited.
            // ThreadPool.QueueUserWorkItem(ChannelManagementTask);
            Thread channelManagementThread = new Thread(ChannelManagementTask);

            channelManagementThread.IsBackground = true;
            channelManagementThread.Start();
            Logger.Info("Thread(TcpMessageSender-ChannelManagementTask) started.");

            Thread asynchronousSendThread = new Thread(AsynchronousSendTask);

            asynchronousSendThread.IsBackground = true;
            asynchronousSendThread.Start();
            Logger.Info("Thread(TcpMessageSender-AsynchronousSendTask) started.");

            Thread mergeAtomicThread = new Thread(MergeAtomicTask);

            mergeAtomicThread.IsBackground = true;
            mergeAtomicThread.Start();
            Logger.Info("Thread(TcpMessageSender-MergeAtomicTask) started.");
        }
Beispiel #2
0
        public void ChannelManagementTask(object o)
        {
            int count = 0;

            while (true)
            {
                // For every 5 minutes
                try
                {
                    _messageIdFactory.SaveMark(true);
                    if (_mActive)
                    {
                        // 1. If TCP connection is down, try to reconnect.
                        if (_mActiveChannel == null || !_mActiveChannel.Connected)
                        {
                            _mClientConfig.Refresh();
                            serversFromRemoteConfig = _mClientConfig.Servers;
                            if (null != _mActiveChannel)
                            {
                                Logger.Warn("The current TCP connection to " + _mActiveChannel + " is no longer connected. Will try to re-connect to server.");
                            }

                            var channel = GetActiveConnection(_mClientConfig.Servers, serversFromRemoteConfig);
                            if (channel != null && channel != _mActiveChannel)
                            {
                                var lastChannel = _mActiveChannel;
                                _mActiveChannel = channel;
                                if (lastChannel != null)
                                {
                                    lastChannel.Close();
                                }
                                NetworkInterfaceManager.Refresh();
                            }
                        }
                        // 2.If TCP connection is up, rebalance by connecting to a better CAT server, if necessary.
                        else
                        {
                            // Rebalance step (1): for every one hour, refresh router config from CAT server.
                            if (count % (CatConstants.REFRESH_ROUTER_CONFIG_INTERVAL / CatConstants.TCP_RECONNECT_INTERVAL) == 0)
                            {
                                Logger.Info("Refreshing router config from CAT server");
                                _mClientConfig.Refresh();
                            }
                            // Rebalance step (2): for every 10 min, forcefully re-establish TCP connection, according to latest router config
                            if (count % (CatConstants.TCP_REBALANCE_INTERVAL / CatConstants.TCP_RECONNECT_INTERVAL) == 0)
                            {
                                serversFromRemoteConfig = _mClientConfig.Servers;
                                var channel = GetActiveConnection(_mClientConfig.Servers, serversFromRemoteConfig);
                                if (channel != null && channel != _mActiveChannel)
                                {
                                    var lastChannel = _mActiveChannel;
                                    _mActiveChannel = channel;
                                    if (lastChannel != null)
                                    {
                                        if (lastChannel.Client != null && channel.Client != null)
                                        {
                                            Logger.Info("Rebalancing by re-establising TCP connection. Old connection to: "
                                                        + lastChannel.Client.RemoteEndPoint + " New connection to: " + channel.Client.RemoteEndPoint);
                                        }
                                        lastChannel.Close();
                                    }
                                    NetworkInterfaceManager.Refresh();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex) { Cat.lastException = ex; }
                finally { count++; }
                Thread.Sleep(CatConstants.TCP_RECONNECT_INTERVAL);
            }
        }