示例#1
0
 public void ReconnectToServer()
 {
     _connection.ConnectionClosed -= OnConnectionClosed;
     _connection = new TcpClientConnector().ConnectTo(Guid.NewGuid(), _serverEndPoint, ConnectionTimeout, OnConnectionEstablished, OnConnectionFailed);
     _connection.ConnectionClosed += OnConnectionClosed;
     _connection.ReceiveAsync(OnRawDataReceived);
 }
示例#2
0
 public void Start(int connectTimeoutMilliseconds = 5000)
 {
     _connection = new TcpClientConnector().ConnectTo(Guid.NewGuid(), _localEndPoint, _serverEndPoint, OnConnectionEstablished, OnConnectionFailed);
     _connection.ConnectionClosed += OnConnectionClosed;
     _connection.ReceiveAsync(OnRawDataReceived);
     _waitHandle.WaitOne(connectTimeoutMilliseconds);
 }
示例#3
0
 public SocketClient()
 {
     var remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), DEFAULT_PORT);
     var connector = new TcpClientConnector();
     _connection = connector.ConnectTo(_connectionId, remoteEndPoint, ConnectionTimeout, OnConnectionEstablished, OnConnectionFailed);
     _connection.ConnectionClosed += OnConnectionClosed;
     _connection.ReceiveAsync(OnRawDataReceived);
 }
示例#4
0
 public void StartReceiving()
 {
     if (_connection == null)
     {
         throw new InvalidOperationException("Failed connection.");
     }
     _connection.ReceiveAsync(OnRawDataReceived);
 }
示例#5
0
 public void Start()
 {
     _connection = new TcpClientConnector().ConnectTo(Guid.NewGuid(), _serverEndPoint, ConnectionTimeout, OnConnectionEstablished, OnConnectionFailed);
     _connection.ConnectionClosed += OnConnectionClosed;
     _connection.ReceiveAsync(OnRawDataReceived);
     _startWaitHandle.WaitOne();
     IsStarted = true;
 }
示例#6
0
        public void ReceiveAsync(Action <TcpTypedConnection <T>, T> callback)
        {
            if (_receiveCallback != null)
            {
                throw new InvalidOperationException("ReceiveAsync should be called just once.");
            }

            _receiveCallback = callback ?? throw new ArgumentNullException(nameof(callback));

            _connection.ReceiveAsync(OnRawDataReceived);
        }
示例#7
0
        private void ConfigureTcpListener(ITcpConnection conn)
        {
            Framer.RegisterMessageArrivedCallback(TcpMessageArrived);
            Action <ITcpConnection, IEnumerable <ArraySegment <byte> > > callback = null;

            callback = (x, data) =>
            {
                try
                {
                    Framer.UnFrameData(data);
                }
                catch (PackageFramingException exc)
                {
                    Log.ErrorException(exc, "LengthPrefixMessageFramer.UnFrameData() threw an exception:");
                    // SendBadRequestAndClose(Guid.Empty, string.Format("Invalid TCP frame received. Error: {0}.", exc.Message));
                    return;
                }
                conn.ReceiveAsync(callback);
            };
            conn.ReceiveAsync(callback);
        }
示例#8
0
        private void OnRawDataReceived(ITcpConnection connection, IEnumerable <ArraySegment <byte> > data)
        {
            try {
                _framer.UnFrameData(data);
            } catch (PackageFramingException exc) {
                Log.InfoException(exc, "Invalid TCP frame received.");
                Close("Invalid TCP frame received.");
                return;
            }

            connection.ReceiveAsync(OnRawDataReceived);
        }
 private void OnRawDataReceived(ITcpConnection connection, IEnumerable<ArraySegment<byte>> data)
 {
     try
     {
         _framer.UnFrameData(data);
     }
     catch (PackageFramingException exc)
     {
         Console.WriteLine(exc.Message);
         return;
     }
     connection.ReceiveAsync(OnRawDataReceived);
 }
示例#10
0
 private void OnRawDataReceived(ITcpConnection connection, IEnumerable <ArraySegment <byte> > data)
 {
     try
     {
         _messageFramer.UnFrameData(data);
     }
     catch (PackageFramingException ex)
     {
         _logger.Error("Unframe data has exception.", ex);
         return;
     }
     connection.ReceiveAsync(OnRawDataReceived);
 }
示例#11
0
        private void OnRawDataReceived(ITcpConnection connection, IEnumerable <ArraySegment <byte> > data)
        {
            try {
                _framer.UnFrameData(data);
            } catch (PackageFramingException exc) {
                _log.Error(exc, "TcpPackageConnection: [{0}, L{1}, {2:B}]. Invalid TCP frame received.", RemoteEndPoint,
                           LocalEndPoint, ConnectionId);
                Close("Invalid TCP frame received.");
                return;
            }

            //NOTE: important to be the last statement in the callback
            connection.ReceiveAsync(OnRawDataReceived);
        }
示例#12
0
        private void OnDataReceived(ITcpConnection conn, IEnumerable<ArraySegment<byte>> data)
        {
            IMessageFramer framer;
            Tuple<ITcpConnection, IMessageFramer> pair;
            if (!_clientFramers.TryGetValue(conn.ConnectionId, out pair))
            {
                framer = new CrappyTemporaryFramer();
                framer.RegisterMessageArrivedCallback(CompleteMessageArrived);
                _clientFramers.TryAdd(conn.ConnectionId, new Tuple<ITcpConnection, IMessageFramer>(conn, framer));

                //Note: we stick the connection ID in the first part of the message just so we
                // can find it later. This isn't especially nice and is fixed in real code
                var connectionId = conn.ConnectionId.ToByteArray();
                framer.UnFrameData(new ArraySegment<byte>(connectionId, 0, connectionId.Length));
            }
            else
            {
                framer = pair.Item2;
            }

            framer.UnFrameData(data);
            conn.ReceiveAsync(OnDataReceived);
        }
示例#13
0
        private void OnDataReceived(ITcpConnection conn, IEnumerable <ArraySegment <byte> > data)
        {
            IMessageFramer framer;
            Tuple <ITcpConnection, IMessageFramer> pair;

            if (!_clientFramers.TryGetValue(conn.ConnectionId, out pair))
            {
                framer = new CrappyTemporaryFramer();
                framer.RegisterMessageArrivedCallback(CompleteMessageArrived);
                _clientFramers.TryAdd(conn.ConnectionId, new Tuple <ITcpConnection, IMessageFramer>(conn, framer));

                //Note: we stick the connection ID in the first part of the message just so we
                // can find it later. This isn't especially nice and is fixed in real code
                var connectionId = conn.ConnectionId.ToByteArray();
                framer.UnFrameData(new ArraySegment <byte>(connectionId, 0, connectionId.Length));
            }
            else
            {
                framer = pair.Item2;
            }

            framer.UnFrameData(data);
            conn.ReceiveAsync(OnDataReceived);
        }
示例#14
0
 private void OnDataReceived(ITcpConnection conn, IEnumerable <ArraySegment <byte> > data)
 {
     conn.EnqueueSend(data);
     conn.ReceiveAsync(OnDataReceived);
 }
示例#15
0
 public void Start(int connectTimeoutMilliseconds = 5000)
 {
     _connection = new TcpClientConnector().ConnectTo(Guid.NewGuid(), _localEndPoint, _serverEndPoint, OnConnectionEstablished, OnConnectionFailed);
     _connection.ConnectionClosed += OnConnectionClosed;
     _connection.ReceiveAsync(OnRawDataReceived);
     _waitHandle.WaitOne(connectTimeoutMilliseconds);
 }
示例#16
0
 public void ReconnectToServer()
 {
     _connection.ConnectionClosed -= OnConnectionClosed;
     _connection = new TcpClientConnector().ConnectTo(Guid.NewGuid(), _localEndPoint, _serverEndPoint, OnConnectionEstablished, OnConnectionFailed);
     _connection.ConnectionClosed += OnConnectionClosed;
     _connection.ReceiveAsync(OnRawDataReceived);
 }
示例#17
0
 private void OnDataReceived(ITcpConnection conn, IEnumerable<ArraySegment<byte>> data)
 {
     conn.EnqueueSend(data);
     conn.ReceiveAsync(OnDataReceived);
 }
示例#18
0
 private void OnRawDataReceived(ITcpConnection connection, IEnumerable<ArraySegment<byte>> data)
 {
     _framer.UnFrameData(data);
     connection.ReceiveAsync(OnRawDataReceived);
 }
示例#19
0
        private void OnRawDataReceived(ITcpConnection connection, IEnumerable<ArraySegment<byte>> data)
        {
            try
            {
                _framer.UnFrameData(data);
            }
            catch (PackageFramingException exc)
            {
                Log.Debug(exc, "Invalid TCP frame received.");
                Close();
                return;
            }

            connection.ReceiveAsync(OnRawDataReceived);
        }
示例#20
0
 public void StartReceiving()
 {
     _tcpConnection.ReceiveAsync(OnRawDataReceived);
 }
示例#21
0
 private void OnRawDataReceived(ITcpConnection connection, IEnumerable <ArraySegment <byte> > data)
 {
     _framer.UnFrameData(data);
     connection.ReceiveAsync(OnRawDataReceived);
 }
        private void OnRawDataReceived(ITcpConnection connection, IEnumerable<ArraySegment<byte>> data)
        {
            try
            {
                _framer.UnFrameData(data);
            }
            catch (PackageFramingException exc)
            {
                _log.Error(exc, "Invalid TCP frame received.");
                Close("Invalid TCP frame received.");
                return;
            }

            //NOTE: important to be the last statement in the callback
            connection.ReceiveAsync(OnRawDataReceived);
        }
 private void ClientReceiveCallback(ITcpConnection tcpConnection, IEnumerable<ArraySegment<byte>> arraySegments)
 {
     tcpConnection.ReceiveAsync(ClientReceiveCallback);
     ReceiveAndSend(tcpConnection, arraySegments);
 }
示例#24
0
 private void OnRawDataReceived(ITcpConnection connection, IEnumerable<ArraySegment<byte>> data)
 {
     try
     {
         _framer.UnFrameData(data);
     }
     catch (PackageFramingException ex)
     {
         _logger.Error("UnFrame data has exception.", ex);
         return;
     }
     connection.ReceiveAsync(OnRawDataReceived);
 }