コード例 #1
0
        /// <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();
        }
コード例 #2
0
        /// <inheritdoc />
        public void Connect()
        {
            if (IsRunning)
            {
                throw new InvalidOperationException("Client is already running.");
            }

            if (IsConnected)
            {
                throw new InvalidOperationException("Client is already connected to remote host.");
            }

            if (ClientConfiguration == null)
            {
                throw new ArgumentNullException(nameof(ClientConfiguration), "Undefined Client configuration.");
            }

            if (ClientConfiguration.Port <= 0)
            {
                throw new ArgumentException($"Invalid port number '{ClientConfiguration.Port}' in configuration.", nameof(ClientConfiguration.Port));
            }

            if (NetworkHelper.BuildIPAddress(ClientConfiguration.Host) == null)
            {
                throw new ArgumentException($"Invalid host address '{ClientConfiguration.Host}' in configuration", nameof(ClientConfiguration.Host));
            }

            SocketError error = connector.Connect(connectSocketArgs);

            if (!IsConnected && error != SocketError.Success)
            {
                OnError(new InvalidOperationException("Could not connect to login server."));
                return;
            }

            receiver = new ClientReceiver(this);
            if (!Socket.ReceiveAsync(receiveSocketArgs))
            {
                receiver.Receive(receiveSocketArgs);
            }

            IsRunning = true;
        }