private void ReceiveCallback(IAsyncResult ar)
 {
     try{
         StateObject state     = (StateObject)ar.AsyncState;
         Socket      client    = state.workSocket;
         int         bytesRead = client.EndReceive(ar);
         if (!state.sizeIsKnown)
         {
             if ((state.sizeOfMessageSizeBuffer + bytesRead) > 3)
             {
                 Buffer.BlockCopy(state.buffer, 0, state.messageSizeBuffer, state.sizeOfMessageSizeBuffer, 4 - state.sizeOfMessageSizeBuffer);
                 state.messageSize = BitConverter.ToInt32(state.messageSizeBuffer, 0);
                 var amountOfMainBytes = bytesRead + state.sizeOfMessageSizeBuffer - 4;
                 Buffer.BlockCopy(state.buffer, bytesRead - amountOfMainBytes, state.mainBuffer, state.DataSize, amountOfMainBytes);
                 state.DataSize   += amountOfMainBytes;
                 state.sizeIsKnown = true;
                 if (state.DataSize == state.messageSize)
                 {
                     LockFreeQueue.Push(Message.Parser.ParseFrom(state.mainBuffer, 0, state.DataSize));
                     receiveDone.Set();
                 }
                 else
                 {
                     client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, ReceiveCallback, state);
                 }
             }
             else
             {
                 //(state.sizeOfMessageSizeBuffer + bytesRead) < 3
                 Buffer.BlockCopy(state.buffer, 0, state.messageSizeBuffer, state.sizeOfMessageSizeBuffer, bytesRead);
                 state.sizeOfMessageSizeBuffer += bytesRead;
                 client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, ReceiveCallback, state);
             }
         }
         else if (state.DataSize < state.messageSize)
         {
             Buffer.BlockCopy(state.buffer, 0, state.mainBuffer, state.DataSize, bytesRead);
             state.DataSize += bytesRead;
             if (state.DataSize == state.messageSize)
             {
                 LockFreeQueue.Push(Message.Parser.ParseFrom(state.mainBuffer, 0, state.DataSize));
                 receiveDone.Set();
             }
             else
             {
                 client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, ReceiveCallback, state);
             }
         }
         else
         {
             LockFreeQueue.Push(Message.Parser.ParseFrom(state.mainBuffer, 0, state.DataSize));
             receiveDone.Set();
         }
     }
     catch (Exception e) {
         Console.WriteLine(e.ToString());
     }
 }
Ejemplo n.º 2
0
        private static void ClientHandler(Object list)
        {
            var listOfSockets = (List <Socket>)list;

            foreach (var client in listOfSockets)
            {
                //async listen
            }
            while (!_exit)
            {
                var testmsg = LockFreeQueue.Pop();
                if (testmsg != null)
                {
                    if (!MessageHandler(testmsg))
                    {
                        break;
                    }
                }
            }
        }