@@ -38,12 +38,11 @@ namespace HttpServer
3838
3939 std::vector<char > buf;
4040 buf.reserve (4096 );
41+ buf.resize (Http2::FRAME_HEADER_SIZE);
4142
4243 HPack::pack (buf, headers, this ->stream ->conn .encoding_dynamic_table );
4344
44- const uint32_t frame_size = buf.size ();
45-
46- buf.insert (buf.begin (), Http2::FRAME_HEADER_SIZE, 0 );
45+ const uint32_t frame_size = buf.size () - Http2::FRAME_HEADER_SIZE;
4746
4847 Http2::FrameFlag flags = Http2::FrameFlag::END_HEADERS;
4948
@@ -54,7 +53,16 @@ namespace HttpServer
5453
5554 this ->stream ->setHttp2FrameHeader (reinterpret_cast <uint8_t *>(buf.data () ), frame_size, Http2::FrameType::HEADERS, flags);
5655
57- return this ->sock .nonblock_send (buf.data (), buf.size (), timeout) > 0 ; // >= 0
56+ this ->stream ->lock ();
57+
58+ auto const is_sended = this ->sock .nonblock_send (buf.data (), buf.size (), timeout) > 0 ; // >= 0;
59+
60+ if (endStream || false == is_sended)
61+ {
62+ this ->stream ->unlock ();
63+ }
64+
65+ return is_sended;
5866 }
5967
6068 long ServerHttp2Protocol::sendData (const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const
@@ -70,8 +78,6 @@ namespace HttpServer
7078
7179 while (size != 0 )
7280 {
73- buf.resize (0 );
74-
7581 // TODO: test with data_size == 1 (padding length == 0)
7682 size_t data_size = setting.max_frame_size < size ? setting.max_frame_size : size;
7783
@@ -88,6 +94,8 @@ namespace HttpServer
8894
8995 const size_t frame_size = data_size + padding_size;
9096
97+ buf.resize (frame_size + Http2::FRAME_HEADER_SIZE);
98+
9199 /* if (this->stream->window_size_out - frame_size <= 0)
92100 {
93101 size_t update_size = (dt->full_size - dt->send_total) - this->stream->window_size_out;
@@ -109,30 +117,33 @@ namespace HttpServer
109117 flags |= Http2::FrameFlag::END_STREAM;
110118 }
111119
120+ size_t cur = Http2::FRAME_HEADER_SIZE;
121+
112122 if (padding_size)
113123 {
114124 flags |= Http2::FrameFlag::PADDED;
115125
116- buf.insert (buf.begin (), sizeof (uint8_t ), padding);
117- }
126+ buf[cur] = padding;
118127
119- buf.insert (buf.begin (), Http2::FRAME_HEADER_SIZE, 0 );
128+ ++cur;
129+ }
120130
121131 const Http2::FrameType frame_type = Http2::FrameType::DATA;
122132
123133 this ->stream ->setHttp2FrameHeader (buf.data (), frame_size, frame_type, flags);
124134
125- std::copy (data, data + data_size, std::back_inserter ( buf) );
135+ std::copy (data, data + data_size, buf. begin () + cur );
126136
127137 if (padding)
128138 {
129- buf. insert (buf.end (), padding, 0 );
139+ std::fill (buf.end () - padding, buf. end () , 0 );
130140 }
131141
132142 long sended = this ->sock .nonblock_send (buf.data (), buf.size (), timeout);
133143
134144 if (sended <= 0 )
135145 {
146+ send_size = sended;
136147 break ;
137148 }
138149
@@ -144,6 +155,11 @@ namespace HttpServer
144155 size -= data_size;
145156 }
146157
158+ if (0 >= send_size || dt->full_size == dt->send_total )
159+ {
160+ this ->stream ->unlock ();
161+ }
162+
147163 return send_size;
148164 }
149165
@@ -163,6 +179,7 @@ namespace HttpServer
163179 Utils::packNumber (buf, this ->stream ->conn .client_settings .max_frame_size );
164180 Utils::packNumber (buf, this ->stream ->conn .client_settings .max_header_list_size );
165181 Utils::packContainer (buf, this ->stream ->conn .encoding_dynamic_table .getList () );
182+ Utils::packPointer (buf, &this ->stream ->conn .sync .mtx );
166183 Utils::packContainer (buf, req.incoming_headers );
167184 Utils::packContainer (buf, req.incoming_data );
168185 Utils::packFilesIncoming (buf, req.incoming_files );
0 commit comments