Пример #1
0
        public async Task Connect()
        {
            if (this.EndPoint == null)
            {
                throw new InvalidOperationException("EndPoint must be set.");
            }
            if (this.Status != SocketStatus.Disconnected)
            {
                throw new InvalidOperationException("You can't connect if the socket isn't disconnected");
            }
            Log.Debug("Connect");
            if (this.Client.IsDisposed())
            {
                this.Client = new TcpClient();
            }
            await this.Client.ConnectAsync(this.EndPoint.Address, this.EndPoint.Port);

            this.Status = SocketStatus.Connected;

            this.CallOnConnectionEvent(this.FirstConnection ? SocketConnectionEvent.Connected : SocketConnectionEvent.Reconnected);
            this.FirstConnection = false;
            var bundle = new SocketReceiveBundle(this.Client);

            this.Client.Client.BeginReceive(
                bundle.Buffer,
                0,
                SocketReceiveBundle.BufferSize,
                SocketFlags.None,
                this.EndReceive,
                bundle);
        }
Пример #2
0
        public void Setup(TcpClient client, ISocketMessageProcessor processor)
        {
            lock (this)
            {
                if (client == null)
                {
                    throw new ArgumentNullException("client");
                }
                if (processor == null)
                {
                    throw new ArgumentNullException("processor");
                }
                if (this.Status != SocketStatus.Disconnected)
                {
                    throw new InvalidOperationException("You can't setup a socket if it isn't disconnected.");
                }
                Log.DebugFormat("Setup {0}", client.Client.RemoteEndPoint);
                this.EndPoint         = client.Client.RemoteEndPoint as IPEndPoint;
                this.MessageProcessor = processor;
                this.Client           = client;
                this.Status           = SocketStatus.Connected;
            }
            this.CallOnConnectionEvent(this.FirstConnection ? SocketConnectionEvent.Connected : SocketConnectionEvent.Reconnected);
            this.FirstConnection = false;
            var bundle = new SocketReceiveBundle(this.Client);

            this.Client.Client.BeginReceive(
                bundle.Buffer,
                0,
                SocketReceiveBundle.BufferSize,
                SocketFlags.None,
                this.EndReceive,
                bundle);
        }
Пример #3
0
        public void Connect()
        {
            lock (this)
            {
                if (this.EndPoint == null) throw new InvalidOperationException("EndPoint must be set.");
                if (this.Status != SocketStatus.Disconnected) throw new InvalidOperationException("You can't connect if the socket isn't disconnected");
                Log.Debug("Connect");
                if (this.Client.IsDisposed())
                {
                    this.Client = new TcpClient();
                }
                this.Client.Connect(this.EndPoint);
				this.Status = SocketStatus.Connected;
            }
            this.CallOnConnectionEvent(this.FirstConnection ? SocketConnectionEvent.Connected : SocketConnectionEvent.Reconnected);
            this.FirstConnection = false;
            var bundle = new SocketReceiveBundle(this.Client);
            this.Client.Client.BeginReceive(
                bundle.Buffer,
                0,
                SocketReceiveBundle.BufferSize,
                SocketFlags.None,
                this.EndReceive,
                bundle);
        }
Пример #4
0
        private void Receive()
        {
            if (!IsListening)
            {
                return;
            }

            var bundle = new SocketReceiveBundle(this.Client);

            this.Client.BeginReceive(EndReceive, bundle);
        }
Пример #5
0
        internal void EndReceive(IAsyncResult res)
        {
            try
            {
                var state = res.AsyncState as SocketReceiveBundle;
                var count = state.TcpClient.Client?.EndReceive(res);

                var receivedData = count > 0;

                if (!receivedData)
                {
                    this.Disconnect();
                    return;
                }

                this.MessageProcessor.AddData(state.Buffer.Take(count.Value).ToArray());

                while (true)
                {
                    var buff = this.MessageProcessor.PopMessage();
                    if (buff == null)
                    {
                        break;
                    }
                    this.OnDataReceived(this, buff);
                    this.DataReceived?.Invoke(this, new DataReceivedEventArgs {
                        Data = buff
                    });
                }

                var bundle = new SocketReceiveBundle(this.Client);
                this.Client.Client.BeginReceive(
                    bundle.Buffer,
                    0,
                    SocketReceiveBundle.BufferSize,
                    SocketFlags.None,
                    this.EndReceive,
                    bundle);
            }
            catch (SocketException e)
            {
                Log.Error("EndReceive", e);
                this.Disconnect();
            }
            catch (ObjectDisposedException e)
            {
                Log.Warn("EndReceive", e);
                this.Disconnect();
            }
            catch (Exception e)
            {
                Log.Error("EndReceive", e);
            }
        }
Пример #6
0
        public void Setup(TcpClient client, ISocketMessageProcessor processor)
        {
            lock (this)
            {
                if(client == null)throw new ArgumentNullException("client");
                if (processor == null) throw new ArgumentNullException("processor");
                if (this.Status != SocketStatus.Disconnected) throw new InvalidOperationException("You can't setup a socket if it isn't disconnected.");
				Log.DebugFormat("Setup {0}",client.Client.RemoteEndPoint);
                this.EndPoint = client.Client.RemoteEndPoint as IPEndPoint;
                this.MessageProcessor = processor;
                this.Client = client;
                this.Status = SocketStatus.Connected;
            }
            this.CallOnConnectionEvent(this.FirstConnection ? SocketConnectionEvent.Connected : SocketConnectionEvent.Reconnected);
            this.FirstConnection = false;
            var bundle = new SocketReceiveBundle(this.Client);
            this.Client.Client.BeginReceive(
                bundle.Buffer,
                0,
                SocketReceiveBundle.BufferSize,
                SocketFlags.None,
                this.EndReceive,
                bundle);
        }
Пример #7
0
        private void Receive()
        {
            var bundle = new SocketReceiveBundle(this.Client);

            this.Client.BeginReceive(EndReceive, bundle);
        }
Пример #8
0
        private void Receive()
        {
            var bundle = new SocketReceiveBundle(this.Client);

            this.Client.BeginReceive(EndReceive, bundle);
        }
Пример #9
0
        internal void EndReceive(IAsyncResult res)
        {
            try
            {
                var state = res.AsyncState as SocketReceiveBundle;
                var count = state.TcpClient.Client.EndReceive(res);

                if (count <= 0)
                {
                    this.Disconnect();
                    return;
                }

                this.MessageProcessor.AddData(state.Buffer.Take(count).ToArray());

                while (true)
                {
                    var buff = this.MessageProcessor.PopMessage();
                    if (buff == null) break;
                    this.CallOnDataReceived(buff);
                }

                var bundle = new SocketReceiveBundle(this.Client);
                this.Client.Client.BeginReceive(
                    bundle.Buffer,
                    0,
                    SocketReceiveBundle.BufferSize,
                    SocketFlags.None,
                    this.EndReceive,
                    bundle);
            }
            catch (SocketException e)
            {
                Log.Warn("EndReceive", e);
                this.Disconnect();
            }
            catch (ObjectDisposedException e)
            {
                Log.Warn("EndReceive", e);
                this.Disconnect();
            }
            catch (Exception e)
            {
                Log.Warn("EndReceive", e);
            }
        }