Ejemplo n.º 1
0
        /// <summary>
        /// Method called when a <see cref="SocketAsyncEventArgs"/> completes an async operation.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        internal void OnSocketCompleted(object s, SocketAsyncEventArgs e)
        {
            try
            {
                switch (e.LastOperation)
                {
                case SocketAsyncOperation.Connect:
                    sender = new ClientSender(sendSocketArgs);
                    sender.Start();
                    OnConnected();
                    connector.ReleaseConnectorLock();
                    break;

                case SocketAsyncOperation.Receive:
                    receiver.Receive(e);
                    break;

                case SocketAsyncOperation.Send:
                    sender.SendOperationCompleted(e);
                    break;

                default: throw new InvalidOperationException("Unexpected SocketAsyncOperation.");
                }
            }
            catch (Exception exception)
            {
                OnError(exception);
            }
        }
Ejemplo n.º 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;
        }