示例#1
0
文件: Utils.cs 项目: 10sa/IBLVM
        public static void SendPacket(NetworkStream stream, IPacket packet)
        {
            byte[] buffer        = new byte[256];
            Stream payloadStream = packet.GetPayloadStream();
            int    payloadSize   = packet.GetPayloadSize();

            if (payloadSize == -1)
            {
                payloadSize = (int)payloadStream.Length;
                packet.OverridePayloadSize(payloadSize);
            }

            byte[] packetData = packet.GetPacketBytes();
            stream.Write(packetData, 0, packetData.Length);
            using (payloadStream)
            {
                payloadStream.Position = 0;
                int sendBytes  = 0;
                int readedSize = 0;

                while (sendBytes < payloadSize)
                {
                    readedSize = payloadStream.Read(buffer, 0, Math.Min(payloadSize - sendBytes, buffer.Length));
                    stream.Write(buffer, 0, readedSize);

                    sendBytes += readedSize;
                }
            }
        }