@@ -478,25 +478,54 @@ inline void Nghttp2Session::SendPendingData() {
478478 // will not be usable.
479479 if (IsDestroying ())
480480 return ;
481- const uint8_t * data;
482- ssize_t len = 0 ;
483- size_t ncopy = 0 ;
484- uv_buf_t buf;
485- AllocateSend (SEND_BUFFER_RECOMMENDED_SIZE, &buf);
486- while (nghttp2_session_want_write (session_)) {
487- len = nghttp2_session_mem_send (session_, &data);
488- CHECK_GE (len, 0 ); // If this is less than zero, we're out of memory
489- // While len is greater than 0, send a chunk
490- while (len > 0 ) {
491- ncopy = len;
492- if (ncopy > buf.len )
493- ncopy = buf.len ;
494- memcpy (buf.base , data, ncopy);
495- Send (&buf, ncopy);
496- len -= ncopy;
497- CHECK_GE (len, 0 ); // This should never be less than zero
481+
482+ uv_buf_t dest;
483+ AllocateSend (SEND_BUFFER_RECOMMENDED_SIZE, &dest);
484+ size_t destLength = 0 ; // amount of data stored in dest
485+ size_t destRemaining = dest.len ; // amount space remaining in dest
486+ size_t destOffset = 0 ; // current write offset of dest
487+
488+ const uint8_t * src; // pointer to the serialized data
489+ ssize_t srcLength = 0 ; // length of serialized data chunk
490+
491+ // While srcLength is greater than zero
492+ while ((srcLength = nghttp2_session_mem_send (session_, &src)) > 0 ) {
493+ DEBUG_HTTP2 (" Nghttp2Session %s: nghttp2 has %d bytes to send\n " ,
494+ TypeName (), srcLength);
495+ size_t srcRemaining = srcLength;
496+ size_t srcOffset = 0 ;
497+
498+ // The amount of data we have to copy is greater than the space
499+ // remaining. Copy what we can into the remaining space, send it,
500+ // the proceed with the rest.
501+ while (srcRemaining > destRemaining) {
502+ DEBUG_HTTP2 (" Nghttp2Session %s: pushing %d bytes to the socket\n " ,
503+ TypeName (), destRemaining);
504+ memcpy (dest.base + destOffset, src + srcOffset, destRemaining);
505+ destLength += destRemaining;
506+ Send (&dest, destLength);
507+ destOffset = 0 ;
508+ destLength = 0 ;
509+ srcRemaining -= destRemaining;
510+ srcOffset += destRemaining;
511+ destRemaining = dest.len ;
512+ }
513+
514+ if (srcRemaining > 0 ) {
515+ memcpy (dest.base + destOffset, src + srcOffset, srcRemaining);
516+ destLength += srcRemaining;
517+ destOffset += srcRemaining;
518+ destRemaining -= srcRemaining;
519+ srcRemaining = 0 ;
520+ srcOffset = 0 ;
498521 }
499522 }
523+
524+ if (destLength > 0 ) {
525+ DEBUG_HTTP2 (" Nghttp2Session %s: pushing %d bytes to the socket\n " ,
526+ TypeName (), destLength);
527+ Send (&dest, destLength);
528+ }
500529}
501530
502531// Initialize the Nghttp2Session handle by creating and
0 commit comments