Exemple #1
0
        /// <inheritdoc />
        public void Start()
        {
            if (this.IsRunning)
            {
                throw new InvalidOperationException("Server is already running.");
            }

            this.CheckConfiguration();

            for (var i = 0; i < this.Configuration.MaximumNumberOfConnections; i++)
            {
                this._readPool.Push(NetUtils.CreateSocketAsync(null, this.IO_Completed, this.Configuration.BufferSize));
                this._writePool.Push(NetUtils.CreateSocketAsync(null, this.IO_Completed, this.Configuration.BufferSize));
            }

            this.Initialize();
            this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
            this.Socket.Bind(NetUtils.CreateIpEndPoint(this.Configuration.Host, this.Configuration.Port));
            this.Socket.Listen(this.Configuration.Backlog);
            this.IsRunning = true;
            this.StartAccept(NetUtils.CreateSocketAsync(null, this.IO_Completed));

            if (this.Configuration.Blocking)
            {
                this._manualResetEvent.WaitOne();
            }
        }
Exemple #2
0
 /// <summary>
 /// Creates a new <see cref="NetClient"/> instance.
 /// </summary>
 /// <param name="host">Remote host or ip</param>
 /// <param name="port">Remote port</param>
 /// <param name="bufferSize">Buffer size</param>
 public NetClient(string host, int port, int bufferSize)
 {
     this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
     this._ipEndPoint        = NetUtils.CreateIpEndPoint(host, port);
     this._socketSendArgs    = NetUtils.CreateSocketAsync(this.Socket, -1, this.IO_Completed);
     this._socketReceiveArgs = NetUtils.CreateSocketAsync(this, bufferSize, this.IO_Completed);
     _autoConnectEvent       = new AutoResetEvent(false);
 }
Exemple #3
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.");
            }

            this.CheckConfiguration();

            this.Socket             = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this._socketSendArgs    = NetUtils.CreateSocketAsync(this.Socket, this.IO_Completed);
            this._socketReceiveArgs = NetUtils.CreateSocketAsync(this, this.IO_Completed, this.Configuration.BufferSize);

            SocketAsyncEventArgs connectSocket = NetUtils.CreateSocketAsync(this.Socket, this.IO_Completed);

            connectSocket.RemoteEndPoint = NetUtils.CreateIpEndPoint(this.Configuration.Host, this.Configuration.Port);

            if (this.Socket.ConnectAsync(connectSocket))
            {
                this._autoConnectEvent.WaitOne();
            }

            SocketError errorCode = connectSocket.SocketError;

            if (errorCode != SocketError.Success)
            {
                throw new SocketException((int)errorCode);
            }

            this._sendingQueueWorker.Start();
            this._receivingQueueWorker.Start();
            this.Token.Socket         = this.Socket;
            this.Token.MessageHandler = data => this._receivingQueue.Add(data, this._cancelToken);
            this.IsRunning            = true;

            if (!this.Socket.ReceiveAsync(this._socketReceiveArgs))
            {
                this.ProcessReceive(this._socketReceiveArgs);
            }
        }
Exemple #4
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.");
            }

            this.CheckConfiguration();

            this.Socket             = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this._socketSendArgs    = NetUtils.CreateSocketAsync(this.Socket, this.IO_Completed);
            this._socketReceiveArgs = NetUtils.CreateSocketAsync(this, this.IO_Completed, this.Configuration.BufferSize);

            SocketAsyncEventArgs connectSocket = NetUtils.CreateSocketAsync(this.Socket, this.IO_Completed);

            connectSocket.RemoteEndPoint = NetUtils.CreateIpEndPoint(this.Configuration.Host, this.Configuration.Port);

            SocketError errorCode = ConnectSocketToServer(connectSocket);

            if (!IsConnected)
            {
                if (this.Configuration.RetryMode == NetClientRetryOptions.Limited)
                {
                    int count = 0;

                    while (!IsConnected && count < this.Configuration.MaxRetryAttempts)
                    {
                        errorCode = ConnectSocketToServer(connectSocket);
                        count++;
                    }
                }
                else if (this.Configuration.RetryMode == NetClientRetryOptions.Infinite)
                {
                    while (!IsConnected)
                    {
                        errorCode = ConnectSocketToServer(connectSocket);
                    }
                }

                if (!IsConnected)
                {
                    this.OnSocketError(errorCode);
                    return;
                }
            }

            this._sendingQueueWorker.Start();
            this._receivingQueueWorker.Start();
            this.Token.Socket         = this.Socket;
            this.Token.MessageHandler = data => this._receivingQueue.Add(data, this._cancelToken);
            this.IsRunning            = true;

            if (!this.Socket.ReceiveAsync(this._socketReceiveArgs))
            {
                this.ProcessReceive(this._socketReceiveArgs);
            }
        }