Exemple #1
0
        public static void SendData(StreamHandler streamHandler, int streamId, byte[] data,
                                    string contentType = "text/plain", bool cont = false)
        {
            if (data.Length < 1)
            {
                SendNotFound(streamHandler, streamId);
                return;
            }
            if (!cont)
            {
                List <HeaderField> headers = new List <HeaderField>()
                {
                    HEADER_OK,
                    new HeaderField {
                        Name = "content-type", Value = contentType, Sensitive = false
                    },
                };
                SendHeader(streamHandler, streamId, headers, true, false);
            }


            // send data
            try
            {
                int length = data.Length;
                int sent   = 0;

                while (sent < length)
                {
                    int len      = ((length - sent) > Server.MAX_HTTP2_FRAME_SIZE) ? Server.MAX_HTTP2_FRAME_SIZE : (length - sent);
                    int overflow = streamHandler.ReduceWindowSize((uint)streamId, (uint)len);
                    len = (overflow >= 0) ? len : len + overflow;
                    byte[] d = new byte[len];
                    for (long j = 0; j < len; j++)
                    {
                        d[j] = data[sent++];
                    }

                    streamHandler.SendFrame(new HTTP2Frame((int)streamId).AddDataPayload(d, endStream: (length == sent)));
                    if (overflow < 0)
                    {
                        byte[] bufferedData = Bytes.GetPartOfByteArray(sent, length, data);
                        streamHandler.BufferDataForWindowUpdate((uint)streamId, bufferedData, contentType);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("SendData()\n" + ex);
            }
        }
Exemple #2
0
        private static void SendFileAsDataFrames(StreamHandler streamHandler, FileInfo fi, int streamId)
        {
            using (FileStream fs = fi.OpenRead())
                using (BinaryReader reader = new BinaryReader(fs))
                {
                    long   length       = fs.Length;
                    int    maxFrameSize = (int)streamHandler.owner.settings.MaxFrameSize;
                    byte[] d            = new byte[maxFrameSize];
                    int    len          = 0;
                    bool   buffer       = false;
                    int    readBytes    = 0;
                    for (long i = 0; i < length - maxFrameSize; i += maxFrameSize)
                    {
                        len = streamHandler.ReduceWindowSize((uint)streamId, (uint)maxFrameSize);

                        if (len < 0)
                        {
                            maxFrameSize += len;
                            d             = new byte[maxFrameSize];
                            buffer        = true;
                        }
                        readBytes += maxFrameSize;
                        reader.Read(d, 0, maxFrameSize);
                        streamHandler.SendFrame(new HTTP2Frame(streamId).AddDataPayload(d));
                        if (buffer)
                        {
                            break;
                        }
                    }
                    int rest = (int)length - readBytes;
                    if (rest > 0)
                    {
                        d = new byte[rest];
                        reader.Read(d, 0, rest);
                        if (buffer)
                        {
                            streamHandler.BufferDataForWindowUpdate((uint)streamId, d, "");
                        }
                        else
                        {
                            streamHandler.ReduceWindowSize((uint)streamId, (uint)rest);
                            streamHandler.SendFrame(new HTTP2Frame(streamId).AddDataPayload(d, 0, true));
                        }
                    }
                }
            if (fi.Extension.Equals(".gz"))
            {
                fi.Delete();
            }
        }