コード例 #1
0
ファイル: Receive.cs プロジェクト: jeason0813/Asmodat
        private bool Receive()
        {
            List <byte> result_buffer = new List <byte>();

            result_buffer.AddToEnd(residue);

            int received     = 0;
            int received_now = 0;

            while (SLClient.IsAvailableToRead())
            {
                try
                {
                    received_now = 0;
                    received_now = SLClient.Receive(buffer_input, 0, buffer_input.Length, SocketFlags.None);
                    //Thread.Sleep(1);
                }
                catch (Exception ex)
                {
                    Exceptions.Write(ex);
                    this.StopClient();
                    break;
                }

                if (received_now > 0)
                {
                    result_buffer.AddRange(buffer_input.Take(received_now));
                    received += received_now;
                }
                else
                {
                    break;
                }
            }

            var packet = result_buffer.GetArray();

            if (packet.IsNullOrEmpty())
            {
                residue = null;
                return(false);
            }

            int  offset;
            int  length;
            byte compression;

            if (!TcpAsyncCommon.RecreatePacket(ref packet, out offset, out length, out compression, PacketMode) || (offset + length) > packet.Length)
            {
                residue = packet;
                return(false);
            }

            byte[] result = packet.SubArray(offset, length);

            //+ 1 represents EndByte
            residue = packet.SubArray((offset + length + 1), packet.Length - (offset + length + 1));

            if (result.IsNullOrEmpty())
            {
                return(false);
            }

            if (compression == 1)
            {
                result = result.UnGZip();
            }

            DBReceive.Write(result);
            return(true);
        }
コード例 #2
0
ファイル: Receive.cs プロジェクト: jeason0813/Asmodat
        private void ReceiveThread2(string key)
        {
            Socket handler = this.GetHandler(key);

            List <byte> result_buffer = new List <byte>();

            result_buffer.AddToEnd(GetResidue(key));

            byte[] buffer         = new byte[PacketSizeTCP];
            int    received       = 0;
            int    bytes_received = 0;

            while (handler.IsAvailableToRead())
            {
                try
                {
                    //while (Speed > MaxSpeed) Thread.Sleep(1);

                    bytes_received = 0;
                    bytes_received = handler.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                    if (bytes_received > 0)
                    {
                        BandwidthBuffer.Write((int)((double)40 * Math.Ceiling((double)bytes_received / 1500)) + bytes_received);
                    }
                }
                catch (Exception ex)
                {
                    Exceptions.Write(ex);
                    break;
                }

                if (bytes_received > 0)
                {
                    result_buffer.AddRange(buffer.Take(bytes_received));
                    received += bytes_received;
                }
                else
                {
                    break;
                }
            }


            var packet = result_buffer.GetArray();

            if (packet.IsNullOrEmpty())
            {
                this.SetResidue(key, null);
                return;
            }

            int  offset;
            int  length;
            byte compression;

            if (!TcpAsyncCommon.RecreatePacket(ref packet, out offset, out length, out compression, PacketMode) || (offset + length) > packet.Length)
            {
                this.SetResidue(key, packet.Copy());
                return;
            }

            byte[] result = packet.SubArray(offset, length);

            //+ 1 represents EndByte
            this.SetResidue(key, packet.SubArray((offset + length + 1), packet.Length - (offset + length + 1)));

            if (result.IsNullOrEmpty())
            {
                return;
            }

            if (compression == 1)
            {
                result = result.UnGZip();
            }

            SetReceiver(key, result);
        }