Skip to content

Commit

Permalink
Make h3 video streaming work
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Sep 4, 2022
1 parent 7f94456 commit 3b74757
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
30 changes: 24 additions & 6 deletions src/Http3Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,29 @@ namespace uWS {
new (us_quic_stream_ext(s)) Http3ResponseData();
});
us_quic_socket_context_on_close(context, [](us_quic_socket_t *s) {
printf("Disconnected!\n");
printf("Disconnected from on_close in uws!\n");

});
us_quic_socket_context_on_stream_writable(context, [](us_quic_stream_t *s) {
// Http3ResponseData *responseData = us_quic_stream_ext(s);
Http3ResponseData *responseData = (Http3ResponseData *) us_quic_stream_ext(s);
// responseData->onWritable();

int written = us_quic_stream_write(s, responseData->buffer.data() + responseData->bufferOffset, responseData->buffer.length() - responseData->bufferOffset);

//printf("wrote %d bytes in writable callback\n", written);

// this whole thing should use the BackpressureBuffer class
responseData->bufferOffset += written;//responseData->buffer.substr(written);

printf("remaingin bytes: %ld\n", responseData->buffer.length() - responseData->bufferOffset);

if (responseData->buffer.length() - responseData->bufferOffset == 0) {
printf("wrote until end, shutting down now!\n");
us_quic_stream_shutdown(s);
us_quic_stream_close(s);
}

//printf("stream is now writable!\n");

});
us_quic_socket_context_on_stream_headers(context, [](us_quic_stream_t *s) {
Expand All @@ -52,10 +68,10 @@ namespace uWS {
Http3Request *req = nullptr;

std::string_view upperCasedMethod = req->getHeader(":method");
//std::transform(lowerCasedMethod.begin(), lowerCasedMethod.end(), lowerCasedMethod.begin(), ::tolower);

std::string_view path = req->getHeader(":path");
contextData->router.getUserData() = {(Http3Response *) s, (Http3Request *) nullptr};
contextData->router.route(upperCasedMethod, "/");
contextData->router.route(upperCasedMethod, path);

});
us_quic_socket_context_on_open(context, [](us_quic_socket_t *s, int is_client) {
Expand All @@ -79,7 +95,9 @@ namespace uWS {

us_quic_listen_socket_t *listen() {
/* The listening socket is the actual UDP socket used */
us_quic_listen_socket_t *listen_socket = us_quic_socket_context_listen((us_quic_socket_context_t *) this, "::1", 9004, sizeof(Http3ResponseData)); // sizeof(Http3ResponseData)
us_quic_listen_socket_t *listen_socket = us_quic_socket_context_listen((us_quic_socket_context_t *) this, "::", 9004, sizeof(Http3ResponseData));

printf("Listen socket is: %p\n", listen_socket);

return listen_socket;
}
Expand Down
21 changes: 13 additions & 8 deletions src/Http3Response.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ namespace uWS {

Http3ResponseData *responseData = (Http3ResponseData *) us_quic_stream_ext((us_quic_stream_t *) this);

//printf("Wrapper responseData is %p\n", responseData);
//printf("%s\n", responseData);

// if not already written status then write status

/* Write headers */
Expand All @@ -41,13 +38,21 @@ namespace uWS {
us_quic_socket_context_send_headers(nullptr, (us_quic_stream_t *) this, 1, 1);

/* Write body and shutdown (unknown if content-length must be present?) */
us_quic_stream_write((us_quic_stream_t *) this, (char *) data.data(), data.length());

/* Every request has its own stream, so we conceptually serve requests like in HTTP 1.0 */
us_quic_stream_shutdown((us_quic_stream_t *) this);
int written = us_quic_stream_write((us_quic_stream_t *) this, (char *) data.data(), data.length());

printf("Wrote %d bytes out of %ld\n", written, data.length());

/* Buffer up remains */
if (written != data.length()) {
responseData->buffer.clear();
responseData->bufferOffset = written;
responseData->buffer.append(data);
} else {
/* Every request has its own stream, so we conceptually serve requests like in HTTP 1.0 */
us_quic_stream_shutdown((us_quic_stream_t *) this);
}
}


/* Attach handler for aborted HTTP request */
Http3Response *onAborted(MoveOnlyFunction<void()> &&handler) {
Http3ResponseData *responseData = (Http3ResponseData *) us_quic_stream_ext((us_quic_stream_t *) this);
Expand Down
3 changes: 3 additions & 0 deletions src/Http3ResponseData.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace uWS {

// hasWrittenStatus

std::string buffer;
int bufferOffset = 0;

};
}

Expand Down
2 changes: 1 addition & 1 deletion uSockets
Submodule uSockets updated 2 files
+66 −11 src/quic.c
+2 −1 src/quic.h

0 comments on commit 3b74757

Please sign in to comment.