public void VerifyMessageIsCreated_OneMessage()
 {
     var msgData = Log.SerializeLog(CreateLog());
     var data = Message.CreateMessageByte(msgData, MessageTypes.SendLog, Guid.NewGuid());
     var target = new ReceiveStateObject();
     Buffer.BlockCopy(data, 0, target.Buffer, 0, data.Length);
     var ret = target.AppendBuffer(data.Length);
     Assert.That(ret, Is.GreaterThanOrEqualTo(0));
     Assert.That(target.Status, Is.EqualTo(ReceiveStatuses.Finished));
     Assert.That(msgData, Is.EquivalentTo(target.Data));
 }
        public void VerifyMessageIsCleared()
        {
            var msgData = Log.SerializeLog(CreateLog());
            var data = Message.CreateMessageByte(msgData, MessageTypes.SendLog);
            var target = new ReceiveStateObject();
            Buffer.BlockCopy(data, 0, target.Buffer, 0, data.Length);
            var ret = target.AppendBuffer(data.Length);
            Assert.That(ret, Is.GreaterThanOrEqualTo(0));
            Assert.That(target.Status, Is.EqualTo(ReceiveStatuses.Finished));
            Assert.That(data, Is.SubsetOf(target.Buffer));

            target.Clear();
            Assert.That(target.Status, Is.EqualTo(ReceiveStatuses.Receiving));
            Assert.That(target.Data, Is.Null);
        }
        public void VerifyMessageIsCreate_TwoMessages_OneBuffer()
        {
            var msgData1 = Log.SerializeLog(CreateLog());
            var data1 = Message.CreateMessageByte(msgData1, MessageTypes.SendLog);
            var msgData2 = Log.SerializeLog(CreateLog());
            var data2 = Message.CreateMessageByte(msgData2, MessageTypes.SendLog);
            var target = new ReceiveStateObject();
            Buffer.BlockCopy(data1, 0, target.Buffer, 0, data1.Length);
            Buffer.BlockCopy(data2, 0, target.Buffer, data1.Length, data2.Length);
            var ret = target.AppendBuffer(data1.Length + data2.Length);
            Assert.That(ret, Is.GreaterThanOrEqualTo(0));
            Assert.That(target.Status, Is.EqualTo(ReceiveStatuses.Finished));
            Assert.That(target.Data, Is.EquivalentTo(msgData1));

            target.Clear();
            ret = target.AppendBuffer(data1.Length + data2.Length, ret);
            Assert.That(ret, Is.GreaterThanOrEqualTo(0));
            Assert.That(target.Status, Is.EqualTo(ReceiveStatuses.Finished));
            Assert.That(msgData2, Is.EquivalentTo(target.Data));
        }
        public void VerifyMessageThatExceeedsBuffer()
        {
            var msgData = Log.SerializeLog(CreateLog(true));
            var data = Message.CreateMessageByte(msgData, MessageTypes.SendLog);
            var target = new ReceiveStateObject();
            var curIndex = 0;
            var ret = -3;
            while (curIndex < data.Length)
            {
                var len = target.Buffer.Length;
                if (curIndex + target.Buffer.Length > data.Length)
                {
                    len = data.Length - curIndex;
                }
                Buffer.BlockCopy(data, curIndex, target.Buffer, 0, len);
                ret = target.AppendBuffer(len);
                curIndex += target.Buffer.Length;
            }

            Assert.That(ret, Is.GreaterThanOrEqualTo(0));
            Assert.That(target.Status, Is.EqualTo(ReceiveStatuses.Finished));
            Assert.That(msgData, Is.EquivalentTo(target.Data));
        }
Exemplo n.º 5
0
 private void EndAccept_Callback(IAsyncResult ar)
 {
     Socket local = null;
     try
     {
         local = (Socket)ar.AsyncState;
         if (Interlocked.CompareExchange(ref connectionState, 1, 1) == 1)
         {
             var client = local.EndAccept(ar);
             var state = new ReceiveStateObject()
             {
                 ClientSocket = client,
             };
             client.BeginReceive(state.Buffer, 0, ReceiveStateObject.MaxBufferSize, SocketFlags.None, new AsyncCallback(EndReceive_Callback), state);
         }
         else
         {
             local = null;
         }
     }
     finally
     {
         if (local != null)
         {
             local.BeginAccept(new AsyncCallback(EndAccept_Callback), local);
         }
     }
 }
Exemplo n.º 6
0
 private void ProcessItem(ReceiveStateObject state)
 {
     switch (state.Type)
     {
         case MessageTypes.SendLog:
             ProcessSendLog(state.Data);
             break;
         case MessageTypes.Empty:
             ProcessEmptyItem();
             break;
         case MessageTypes.Query:
             ProcessQueryRequest(state.Data, state.ClientSocket, state.MessageGuid);
             break;
         case MessageTypes.GetLogs:
             ProcessLogRequest(state.Data, state.ClientSocket);
             break;
         default:
             if (nLogger.IsWarnEnabled)
             {
                 nLogger.Warn("Invalid Message Type Received");
             }
             break;
     }
     if (nLogger.IsTraceEnabled)
     {
         Interlocked.Increment(ref receiveCount);
         nLogger.Trace("Message Received - Total Count: {0:#,##0}", receiveCount);
     }
 }