Пример #1
0
        private void InitClientInternal(string host, int port, TcpClientCom client)
        {
            this.connectionType        = ConnectionType.Client;
            client.Logger              = this.logger;
            client.ReceiveBufferSize   = this.ReceiveBufferSize;
            client.SendBufferSize      = this.SendBufferSize;
            client.AdjustSocketHandler = this.AdjustSocketHandler;
            SubscribeCommunicationEvents(client);

            originalTarget = new TcpTarget()
            {
                Host = host, Port = port
            };

            client.Init(host, port);

            this.communication = client;

            if (LogDataStream)
            {
                dataStreamLogger.Init(this, host.Replace('.', '_') + "-" + port, Config != null ? Config.Root : null);
            }

            // async client connect
            Task.Run(StartAutoReconnectAsync);
        }
Пример #2
0
        private void RotateFallbackClientTargets()
        {
            if (clientConnectCount > 0 &&
                this.configObject != null &&
                this.configObject.ClientFallbackTargets != null &&
                this.configObject.ClientFallbackTargets.Count > 0 &&
                communication is TcpClientCom client)
            {
                int targetFallbackIndex = clientConnectCount % (configObject.ClientFallbackTargets.Count + 1);

                TcpTarget nextTcpTarget = null;
                if (targetFallbackIndex == 0)
                {
                    nextTcpTarget = originalTarget;
                }
                else
                {
                    int targetListIndex = targetFallbackIndex - 1;
                    nextTcpTarget = configObject.ClientFallbackTargets[targetListIndex];
                }

                if (nextTcpTarget != null)
                {
                    try
                    {
                        client.SetEndPoint(nextTcpTarget.Host, nextTcpTarget.Port);

                        if (Logger != null)
                        {
                            Logger.Info($"Rotate client fallback endpoint to \"{nextTcpTarget.Host}:{nextTcpTarget.Port}\"");
                        }
                    }
                    catch (Exception setEndpointEx)
                    {
                        if (Logger != null)
                        {
                            Logger.Error($"Could not set fallback endpoint! Host: {nextTcpTarget.Host}; Port: {nextTcpTarget.Port}; Exception: {setEndpointEx}");
                        }
                    }
                }
            }
        }