GithubHelp home page GithubHelp logo

State for callbacks about quicly HOT 6 CLOSED

h2o avatar h2o commented on June 28, 2024
State for callbacks

from quicly.

Comments (6)

stablebits avatar stablebits commented on June 28, 2024 1

I guess you can use something like this:

diff --git a/examples/echo.c b/examples/echo.c
index ed1d532..6b52647 100644
--- a/examples/echo.c
+++ b/examples/echo.c
@@ -284,15 +284,37 @@ static int run_loop(int fd, quicly_conn_t *client)
     return 0;
 }
 
+typedef struct {
+    quicly_stream_open_t stream_open;
+    void *user_ctx;
+} custom_quicly_stream_open_t;
+
+typedef struct {
+    quicly_streambuf_t sb;
+    void *user_ctx;
+} custom_quicly_streambuf_t;
+
 static int on_stream_open(quicly_stream_open_t *self, quicly_stream_t *stream)
 {
+    custom_quicly_stream_open_t *real_self = (custom_quicly_stream_open_t *)self;
+
     static const quicly_stream_callbacks_t stream_callbacks = {
         quicly_streambuf_destroy, quicly_streambuf_egress_shift, quicly_streambuf_egress_emit, on_stop_sending, on_receive,
         on_receive_reset};
     int ret;
 
-    if ((ret = quicly_streambuf_create(stream, sizeof(quicly_streambuf_t))) != 0)
+    // real_self->user_ctx is your custom context.
+
+    if ((ret = quicly_streambuf_create(stream, sizeof(custom_quicly_streambuf_t))) != 0)
         return ret;
+
+    custom_quicly_streambuf_t *custom_sbuf = stream->data;
+    custom_sbuf->user_ctx = real_self->user_ctx;
+
+    // In callbacks that get "stream" as an argument:
+    // custom_quicly_streambuf_t *custom_sbuf = stream->data;
+    // and use custom_sbuf->user_ctx
+
     stream->callbacks = &stream_callbacks;
     return 0;
 }
@@ -306,7 +328,7 @@ int main(int argc, char **argv)
         .key_exchanges = ptls_openssl_key_exchanges,
         .cipher_suites = ptls_openssl_cipher_suites,
     };
-    quicly_stream_open_t stream_open = {on_stream_open};
+    custom_quicly_stream_open_t stream_open = {{on_stream_open}, NULL /* your context here */};
     char *host = "127.0.0.1", *port = "4433";
     struct sockaddr_storage sa;
     socklen_t salen;
@@ -316,7 +338,7 @@ int main(int argc, char **argv)
     ctx = quicly_default_context;
     ctx.tls = &tlsctx;
     quicly_amend_ptls_context(ctx.tls);
-    ctx.stream_open = &stream_open;
+    ctx.stream_open = (quicly_stream_open_t *) &stream_open;
 
     /* resolve command line options and arguments */
     while ((ch = getopt(argc, argv, "c:k:p:Eh")) != -1) {

from quicly.

stablebits avatar stablebits commented on June 28, 2024 1

Like this:

--- a/examples/echo.c
+++ b/examples/echo.c
@@ -132,6 +132,9 @@ static int on_receive_reset(quicly_stream_t *stream, int err)
 
 static int on_receive(quicly_stream_t *stream, size_t off, const void *src, size_t len)
 {
+    custom_quicly_streambuf_t *custom_sbuf = stream->data;
+   // User ctx is in custom_sbuf->user_ctx
+
     int ret;
 
     /* read input to receive buffer */
@@ -284,15 +287,37 @@ static int run_loop(int fd, quicly_conn_t *client)
     return 0;
 }

from quicly.

jakobod avatar jakobod commented on June 28, 2024

Hi,
thanks for your quick reply! This was the kind of quick-fix I already cam up with. The problem is that the self-ptr you are using to get the state into the callback does not exist in the on_receive function.. What do you suggest I do in that case?

from quicly.

stablebits avatar stablebits commented on June 28, 2024

But you have "stream" in on receive, don't you? Then you can use the trick with custom_quicly_streambuf_t that I showed...

from quicly.

kazuho avatar kazuho commented on June 28, 2024

As @stablebits says, the expected way of associating user data to a stream is by using quicly_stream_t::data.

In on_stream_open, you can use to quicly_stream_t::conn to obtain a pointer to quicly_conn_t *, then use quicly_get_data(quicly_conn_t *conn) to access user data associated to the connection.

from quicly.

jakobod avatar jakobod commented on June 28, 2024

Thank you! I must have missed that, while going through the examples.

from quicly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.