Esempio n. 1
0
        private void OnConstructing(TcpClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (options.RemoteEndPoint == null)
            {
                throw new ArgumentNullException(nameof(options.RemoteEndPoint));
            }

            connection = new TcpConnection(options.RemoteEndPoint, options.LocalEndPoint)
            {
                OnConnected = OnConnectedHandler, OnReceived = OnReceivedHandler, OnDisconnected = OnDisconnectedHandler, OnStarted = OnStartedHandler, OnStopped = OnStoppedHandler, OnException = OnExceptionHandler, ConnectTimeout = options.ConnectTimeout, OnTryConnecting = OnTryConnectingHandler, ReconnectEnable = options.ReconnectEnable, TryConnectionStrategy = options.TryConnectionStrategy
            };

            connection.KeepAlive(KeepAliveOption.KeepAlive, KeepAliveOption.Interval, KeepAliveOption.Span);

            if (options.Id > 0)
            {
                connection.Id = options.Id;
            }
            if (!options.Name.IsNullOrWhiteSpace())
            {
                connection.Name = options.Name;
            }

            if (options.OnConnected != null)
            {
                this.OnConnected = options.OnConnected;
            }
            if (options.OnReceived != null)
            {
                this.OnReceived = options.OnReceived;
            }
            if (options.OnDisconnected != null)
            {
                this.OnDisconnected = options.OnDisconnected;
            }
            if (options.OnStarted != null)
            {
                this.OnStarted = options.OnStarted;
            }
            if (options.OnStopped != null)
            {
                this.OnStopped = options.OnStopped;
            }
            if (options.OnException != null)
            {
                this.OnException = options.OnException;
            }
            if (options.OnTryConnecting != null)
            {
                this.OnTryConnecting = options.OnTryConnecting;
            }
        }
Esempio n. 2
0
        private void OnConstructing(TcpClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (options.RemoteEndPoint == null)
            {
                throw new ArgumentNullException(nameof(options.RemoteEndPoint));
            }

            connection = new TcpConnection(options.RemoteEndPoint, options.LocalEndPoint)
            {
                OnConnected = OnConnectedHandler, OnReceived = OnReceivedHandler, OnDisconnected = OnDisconnectedHandler, OnStarted = OnStartedHandler, OnStopped = OnStoppedHandler, OnException = OnExceptionHandler, ConnectTimeout = options.ConnectTimeout
            };

            if (options.Id > 0)
            {
                connection.Id = options.Id;
            }
            if (!options.Name.IsNullOrWhiteSpace())
            {
                connection.Name = options.Name;
            }

            if (options.OnConnected != null)
            {
                this.OnConnected = options.OnConnected;
            }
            if (options.OnReceived != null)
            {
                this.OnReceived = options.OnReceived;
            }
            if (options.OnDisconnected != null)
            {
                this.OnDisconnected = options.OnDisconnected;
            }
            if (options.OnStarted != null)
            {
                this.OnStarted = options.OnStarted;
            }
            if (options.OnStopped != null)
            {
                this.OnStopped = options.OnStopped;
            }
            if (options.OnException != null)
            {
                this.OnException = options.OnException;
            }
        }
Esempio n. 3
0
        public TcpClient(string remoteHost, int remotePort, int localPort = 0, string localHost = null, int connectTimeout = -1, string name = null, int id = 0)
        {
            try
            {
                TcpClientOptions options = new TcpClientOptions()
                {
                    Id = id, Name = name
                };
                if (connectTimeout > 0)
                {
                    options.ConnectTimeout = connectTimeout;
                }
                options.TryConnectionStrategy = new DefaultTryConnectionStrategy();
                options.RemoteEndPoint        = new IPEndPoint(IPAddress.Parse(remoteHost), remotePort);
                options.LocalEndPoint         = localHost.IsNullOrWhiteSpace() && localPort < 1 ? null : new IPEndPoint(options.RemoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, localPort);

                OnConstructing(options);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 4
0
 public TcpClient(TcpClientOptions options) => OnConstructing(options);
Esempio n. 5
0
 /// <summary>
 /// Creates the TCP client.
 /// </summary>
 /// <param name="options">The options.</param>
 /// <returns></returns>
 public ITcpClient CreateTcpClient(TcpClientOptions options) => new TcpClient(options);