Ejemplo n.º 1
0
        public TcpServer(int port = 7788, string host = null, int backlog = int.MaxValue, AddressFamily family = AddressFamily.InterNetwork, string name = null, int id = 0)
        {
            try
            {
                TcpServerOptions options = new TcpServerOptions(new IPEndPoint(host.IsNullOrWhiteSpace() ? (family == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any) : IPAddress.Parse(host), port), backlog, family)
                {
                    Id = id, Name = name
                };

                OnConstructing(options);
            }
            catch (Exception ex)
            {
                OnServerException?.Invoke(new NetServerEventArgs(this, LocalEndPoint)
                {
                    Exception = ex
                });
                throw ex;
            }
        }
Ejemplo n.º 2
0
        private void OnConstructing(TcpServerOptions options)
        {
            try
            {
                if (options == null)
                {
                    throw new ArgumentNullException(nameof(options));
                }
                if (options.LocalEndPoint == null)
                {
                    throw new ArgumentNullException(nameof(options.LocalEndPoint));
                }
                if (options.LocalEndPoint.Port.NotIn(IPEndPoint.MinPort, IPEndPoint.MaxPort))
                {
                    throw new ArgumentOutOfRangeException(nameof(options.LocalEndPoint.Port), $"The {nameof(options.LocalEndPoint.Port)} must between {IPEndPoint.MinPort} to {IPEndPoint.MaxPort}.");
                }
                if (options.LocalEndPoint.Port < 1)
                {
                    throw new ArgumentOutOfRangeException(nameof(options.LocalEndPoint.Port));
                }
                if (options.Backlog < 1)
                {
                    throw new ArgumentOutOfRangeException(nameof(options.Backlog));
                }
                if (options.LocalEndPoint.AddressFamily != options.Family)
                {
                    throw new ArgumentException($"The {nameof(options.LocalEndPoint.AddressFamily)} and {nameof(options.Family)} not match.");
                }

                switch (options.Family)
                {
                case AddressFamily.InterNetwork:
                case AddressFamily.InterNetworkV6:
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(options.Family), $"The AddressFamily only support AddressFamily.InterNetwork and AddressFamily.InterNetworkV6.");
                }

                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;
                }

                LocalEndPoint = options.LocalEndPoint;

                Instance = new Socket(options.Family, SocketType.Stream, ProtocolType.Tcp);

                Instance.Bind(options.LocalEndPoint);

                Instance.Listen(options.Backlog);

                this.Key  = options.LocalEndPoint.ToString();
                this.Name = options.Name ?? this.Key;
                this.Id   = options.Id;
            }
            catch (Exception ex)
            {
                if (OnServerException != null)
                {
                    Task.Run(() => OnServerException(new NetServerEventArgs(this, LocalEndPoint)
                    {
                        Exception = ex
                    }));
                }

                throw ex;
            }
        }
Ejemplo n.º 3
0
 public TcpServer(TcpServerOptions options) => OnConstructing(options);
Ejemplo n.º 4
0
 /// <summary>
 /// Creates the TCP server.
 /// </summary>
 /// <param name="options">The options.</param>
 /// <returns></returns>
 public ITcpServer CreateTcpServer(TcpServerOptions options) => new TcpServer(options);