Question about the us_socket_context_t and HttpContext #1447
Replies: 3 comments 3 replies
-
Yes that's called black magic 😘 |
Beta Was this translation helpful? Give feedback.
-
I think "pointer black magic" is kind of the best description here. You don't need explicit hierarchies if things map properly either way. But this is not relating to uWS |
Beta Was this translation helpful? Give feedback.
-
uWebSockets is built on uSockets. uSockets is an opaque C library. Meaning, its exact type definitions are unknown (opaque is the opposite of transparent) for anything using it. For all uWebSockets knows, anything provided by uSockets is just a simple undefined void pointer. That's why we do these very liberal casts between seemingly unrelated types. It works because no function has any idea about, or assumes, how the actual implementation of uSockets types look like. The hierarchy is like so:
If you look at HttpContext https://github.com/uNetworking/uWebSockets/blob/master/src/HttpContext.h#L37 you can see that no function ever dereferences "this" - it always treats "this" as a void pointer without a size. All it does is pass "this" as if it indeed was us_socket_context_t, which it is. So the cast to HttpContext never really does anything at all. It's just a perspective change. |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm new to C++ without any knowledge of C (comming from Java/Netty). I'm currently investigate to this project to learn some development skill and have a question that need help.
From EchoServer example, I see that we need uWS::App to use this lib. Tracing down to TemplatedApp I see that you use casting "(struct us_socket_context_t *) httpContext" extensively but I don't see any common between "us_socket_context_t" and "HttpContext" so I don't understand how it can work.
Can you hint/help me something so that I can investigate more? Please...
Beta Was this translation helpful? Give feedback.
All reactions