Beispiel #1
0
        private void ReceiveCallBack(IAsyncResult ar)
        {
            //need to setup buffer of length PacketController
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            ClientConnection client = (ClientConnection)ar.AsyncState;

            try
            {
                int bytesRead = client.socket.EndReceive(ar);
                Console.WriteLine("bytes read" + bytesRead);
                //allows to pause traffic and restart for debugging purposes.
                bytesRead += client.lastPartialSize;
                if (bytesRead > 0)
                {
                    int offset = 0;

                    //build/compile packets until can no longer or data is finished
                    while (true)
                    {
                        BasePacket basePacket = BuildPacket(ref offset, client.buffer, bytesRead);
                        if (basePacket == null)
                        {
                            break;
                        }
                        else
                        {
                            packetProcessor.ProcessPacket(client, basePacket);
                        }
                    }
                    //Not all bytes consumed, transfer leftover to beginning
                    if (offset < bytesRead)
                    {
                        Array.Copy(client.buffer, offset, client.buffer, 0, bytesRead - offset);
                    }

                    Array.Clear(client.buffer, bytesRead - offset, client.buffer.Length - (bytesRead - offset));

                    //allows to pause traffic and restart for debugging purposes.
                    client.lastPartialSize = bytesRead - offset;

                    //Build any queued subpackets into basepackets and send
                    client.FlushQueuedSendPackets();

                    if (offset < bytesRead)
                    //need offset since not all bytes consumed
                    {
                        client.socket.BeginReceive(client.buffer, bytesRead - offset, client.buffer.Length - (bytesRead - offset), SocketFlags.None, new AsyncCallback(ReceiveCallBack), client);
                    }
                    else
                    {
                        client.socket.BeginReceive(client.buffer, 0, client.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), client);
                    }
                }
                else
                {
                    Console.WriteLine("Client at {0} has disconnected", client.GetFullAddress());

                    lock (mConnectionList)
                    {
                        client.Disconnect();
                        mConnectionList.Remove(client);
                    }
                }
            }
            catch (SocketException)
            {
                if (client.socket != null)
                {
                    Console.WriteLine("Client at {0} has disconnected", client.GetFullAddress());

                    lock (mConnectionList)
                    {
                        mConnectionList.Remove(client);
                    }
                }
            }
        }