Ejemplo n.º 1
0
 public void EnqueueSend(TcpPackage package)
 {
     if (_connection == null)
     {
         throw new InvalidOperationException("Failed connection.");
     }
     _connection.EnqueueSend(_framer.FrameData(package.AsArraySegment()));
 }
Ejemplo n.º 2
0
        public void Send(byte[] bytes, bool checkQueueSize = true)
        {
            if (IsClosed)
            {
                return;
            }

            int queueSize;

            if (checkQueueSize && (queueSize = _tcpConnection.SendQueueSize) > ConnectionQueueSizeThreshold)
            {
                CloseConnectionOnError(string.Format("Connection queue size is too large: {0}.", queueSize));
                return;
            }

            var data   = new ArraySegment <byte>(bytes);
            var framed = _framer.FrameData(data);

            _tcpConnection.EnqueueSend(framed);
        }
Ejemplo n.º 3
0
        public void Handle(Message message)
        {
            Type type = MessageHierarchy.GetMsgType(message.MsgTypeId);

            Log.Trace("Message " + message.MsgId + " (Type " + type.Name + ") to be sent over TCP.");

            if (TcpConnection == null)
            {
                Log.Debug("TCP connection not yet established - Message " + message.MsgId + " (Type " + type.Name + ") will be discarded.");
                return;
            }

            try
            {
                var framed = Framer.FrameData((new TcpMessage(message).AsArraySegment()));
                TcpConnection.EnqueueSend(framed);
            }
            catch (Exception ex)
            {
                Log.ErrorException(ex, "Exception caught while handling Message " + message.MsgId + " (Type " + type.Name + ")");
            }
        }
        public void EnqueueSend(T message)
        {
            var data = _formatter.ToArraySegment(message);

            _connection.EnqueueSend(_framer.FrameData(data));
        }
        private void SendRandomData(ITcpConnection tcpConnection, bool small)
        {
            var rnd = new Random ();
            WaitCallback callBack = state =>
            {

                var outData = new List<ArraySegment<byte>> ();
                int index = 0;

                int size = 1 + ((!small && rnd.Next (2) == 0) ? _data.Length/2 + rnd.Next (_data.Length/2) : rnd.Next (32));
                int totalSize = 0;
                outData.Add (new ArraySegment<byte> (_data, index, size));
                totalSize += size;
                Interlocked.Add (ref _sent, size);
                _totalSent [tcpConnection] += totalSize;
                tcpConnection.EnqueueSend (outData);
            };

            if (rnd.Next (5) != 0) {
                small = true;
                var loopCount = rnd.Next(10000);
                var nextRnd = 	 rnd.Next (4);
                for (var k = 0; k < 10; k++)
                    ThreadPool.QueueUserWorkItem (v =>
                    {
                        long h = 1;
                        // random cpu busy delay
                        for (long i = 0; i < loopCount; i++)
                            h = h + i;
                    // randon cpu free delay
                        callBack (null);
                        if (!small && nextRnd == 0) {
                            //Thread.Sleep (rnd.Next (5));
                            ThreadPool.QueueUserWorkItem (callBack);
                        }
                    }
                    );
            }
            else
            {
                callBack (null);
            //				callBack (null);
            //				callBack (null);
            }
        }
Ejemplo n.º 6
0
 private void OnDataReceived(ITcpConnection conn, IEnumerable <ArraySegment <byte> > data)
 {
     conn.EnqueueSend(data);
     conn.ReceiveAsync(OnDataReceived);
 }
        private void SendRandomData(ITcpConnection tcpConnection, bool small)
        {
            var rnd = new Random ();
            WaitCallback callBack = state =>
            {
                lock (tcpConnection) // to make sure ordered data generation -- even if we internally lock in enqueue
                    // the better test would be to generate consistent packages and verify them without this lock, but
                    // due to internal lock this lock just reduces probability of our problems not eliminating them
                {
                    var outData = new List<ArraySegment<byte>>();
                    var sent = _totalSent[tcpConnection];
                    var index = (int) (sent%_data.Length);

                    int size = (4 + ((!small && rnd.Next(2) == 0) ? _data.Length/2 + rnd.Next(_data.Length/2) : rnd.Next(32)))/4
                               *4;
                    if (index + size <= _data.Length)
                    {
                        outData.Add(new ArraySegment<byte>(_data, index, size));
                    }
                    else
                    {
                        outData.Add(new ArraySegment<byte>(_data, index, _data.Length - index));
                        outData.Add(new ArraySegment<byte>(_data, 0, size - _data.Length + index));
                    }
                    _totalSent[tcpConnection] += size;
                    Interlocked.Add(ref _sent, size);
                    tcpConnection.EnqueueSend(outData);
                }
            };

            if (rnd.Next (3) != 0) {
                small = true;
                var loopCount = rnd.Next(100);
                var nextRnd = 	 rnd.Next (4);
                for (var k = 0; k < 10; k++)
                    ThreadPool.QueueUserWorkItem (v =>
                    {
                        long h = 1;
                        // random cpu busy delay
                        for (long i = 0; i < loopCount; i++)
                            h = h + i;
                    // randon cpu free delay
                        callBack (null);
                        if (!small && nextRnd == 0) {
                            //Thread.Sleep (rnd.Next (5));
                            ThreadPool.QueueUserWorkItem (callBack);
                        }
                    }
                    );
            }
            else
            {
                callBack (null);
            //				callBack (null);
            //				callBack (null);
            }
        }
Ejemplo n.º 8
0
 private void OnMessageArrived(ArraySegment <byte> message)
 {
     byte[] data = new byte[message.Count];
     Array.Copy(message.Array, message.Offset, data, 0, message.Count);
     _messageHandler(_connection, data, reply => _connection.EnqueueSend(_messageFramer.FrameData(new ArraySegment <byte>(reply, 0, reply.Length))));
 }
Ejemplo n.º 9
0
 public void SendMessage(byte[] message)
 {
     _connection.EnqueueSend(_framer.FrameData(new ArraySegment <byte>(message, 0, message.Length)));
 }
Ejemplo n.º 10
0
 private void OnDataReceived(ITcpConnection conn, IEnumerable<ArraySegment<byte>> data)
 {
     conn.EnqueueSend(data);
     conn.ReceiveAsync(OnDataReceived);
 }