/// <inheritdoc /> public void Connect() { if (this.IsRunning) { throw new InvalidOperationException("Client is already running."); } if (this.IsConnected) { throw new InvalidOperationException("Client is already connected to remote host."); } if (this.ClientConfiguration == null) { throw new ArgumentNullException(nameof(this.ClientConfiguration), "Undefined Client configuration."); } if (this.ClientConfiguration.Port <= 0) { throw new ArgumentException($"Invalid port number '{this.ClientConfiguration.Port}' in configuration.", nameof(this.ClientConfiguration.Port)); } if (NetworkHelper.BuildIPAddress(this.ClientConfiguration.Host) == null) { throw new ArgumentException($"Invalid host address '{this.ClientConfiguration.Host}' in configuration", nameof(this.ClientConfiguration.Host)); } if (this.ClientConfiguration.BufferSize <= 0) { throw new ArgumentException($"Invalid buffer size '{this.ClientConfiguration.BufferSize}' in configuration.", nameof(this.ClientConfiguration.BufferSize)); } this.sender = new ClientSender(this.CreateSocketEventArgs(null)); this.receiver = new ClientReceiver(this); this.connectSocket = this.CreateSocketEventArgs(null); this.connectSocket.RemoteEndPoint = NetworkHelper.CreateIPEndPoint(this.ClientConfiguration.Host, this.ClientConfiguration.Port); SocketError error = this.connector.Connect(this.connectSocket); if (!this.IsConnected && error != SocketError.Success) { this.OnError(new InvalidOperationException("No se puede conectar con el servidor.")); return; } this.IsRunning = true; this.sender.Start(); }
/// <summary> /// Creates a new <see cref="Client"/> instance. /// </summary> /// <param name="socketConnection"></param> protected Client(ClientConfiguration clientConfiguration) { ClientConfiguration = clientConfiguration; connector = new ClientConnector(this); connectSocketArgs = CreateSocketEventArgs(null); connectSocketArgs.RemoteEndPoint = NetworkHelper.CreateIPEndPoint(ClientConfiguration.Host, ClientConfiguration.Port); sendSocketArgs = CreateSocketEventArgs(null); sendSocketArgs.RemoteEndPoint = NetworkHelper.CreateIPEndPoint(ClientConfiguration.Host, ClientConfiguration.Port); receiveSocketArgs = CreateSocketEventArgs(1024); receiveSocketArgs.RemoteEndPoint = NetworkHelper.CreateIPEndPoint(ClientConfiguration.Host, ClientConfiguration.Port); }
/// <summary> /// Start the server. /// </summary> public void Start() { if (this.IsRunning) { throw new InvalidOperationException("Server is already running."); } this.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); this.Socket.Bind(NetworkHelper.CreateIPEndPoint(this.ServerConfiguration.Host, this.ServerConfiguration.Port)); this.Socket.Listen(this.ServerConfiguration.Backlog); this.FillPool(); this.IsRunning = true; this.OnStart(); this.sender.Start(); this.acceptor.StartAccept(); }