Пример #1
0
        protected void ProcessReceivedBuffer(Client Client, uint Size)
        {
            Statistics.AddBandwidthIn(Size);
            Client.Statistics.AddBandwidthIn(Size);

            uint totalSize = ReceiveBufferIndex + Size;

            uint index = 0;

            while (index < totalSize)
            {
                uint packetSize = BitConverter.ToUInt32(ReceiveBuffer, (int)index);

                if (totalSize < packetSize)
                {
                    ReceiveBufferIndex = totalSize;
                    return;
                }

                index += Packet.PACKET_SIZE_SIZE;

                HandleIncomingBuffer(Client, new BufferStream(ReceiveBuffer, index, packetSize));

                index += packetSize;
            }

            ReceiveBufferIndex = 0;
        }
Пример #2
0
        protected override void Receive()
        {
            try
            {
                int receiveSize   = 0;
                int availableSize = Socket.Available;

                lock (Socket)
                {
                    if (Socket.Available == 0)
                    {
                        if (!IsReady)
                        {
                            HandleDisconnection(Socket);
                        }

                        return;
                    }

                    receiveSize = Socket.Receive(ReceiveBuffer, (int)ReceiveBufferIndex, availableSize, SocketFlags.None);
                }

                Statistics.AddBandwidthIn((uint)receiveSize);

                uint totalSize = ReceiveBufferIndex + (uint)receiveSize;

                uint index = 0;
                while (index < totalSize)
                {
                    uint packetSize = BitConverter.ToUInt32(ReceiveBuffer, (int)index);

                    if (totalSize < packetSize)
                    {
                        ReceiveBufferIndex = totalSize;
                        return;
                    }

                    index += Packet.PACKET_SIZE_SIZE;

                    HandleIncomingBuffer(new BufferStream(ReceiveBuffer, index, packetSize));

                    index += packetSize;
                }

                ReceiveBufferIndex = 0;
            }
            catch (SocketException e)
            {
                if (e.SocketErrorCode == SocketError.ConnectionReset)
                {
                    HandleDisconnection(Socket);

                    return;
                }

                throw e;
            }
            catch (Exception e)
            {
                HandleDisconnection(Socket);
            }
        }