Ejemplo n.º 1
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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
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);
 }
Ejemplo n.º 5
0
        internal void EndReceive(IAsyncResult res)
        {
            try
            {
                var state = res.AsyncState as SocketReceiveBundle;
                var count = state.Client.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.Error("EndReceive", e);
                this.Disconnect();
            }
            catch (ObjectDisposedException e)
            {
                Log.Error("EndReceive", e);
                this.Disconnect();
            }
            catch (Exception e)
            {
                Log.Error("EndReceive", e);
            }
        }
Ejemplo n.º 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);
 }