uNetworking / uSockets

Miniscule cross-platform eventing, networking & crypto for async applications
Apache License 2.0
1.29k stars 268 forks source link

Question: Dealing with different endpoint conntects and contextual data #72

Closed Merethiel closed 5 years ago

Merethiel commented 5 years ago

First Question: My client code receives different endpoint nodes to connect to and I'm wondering if I need to create a new socket context with us_create_socket_context for each endpoint or can I use one socket context and connect to any number of different endpoints with that? Since each endpoint needs to hold specific data I'm afraid using a single context would overwrite previous data needed for an onconnectopen callback. If I am creating multiple socket contexts is it ok to keep calling us_create_socket_context or should I call us_create_child_socket_context?

Second Question: Is it possible to change on_data callback for each specific us_socket_t? In my initial on_data callback I set the next part of the request to us_socket_context_on_data(SSL, context, onconnectionrequest); but I still want on_data to be hit for every new connection. So not changing the callback for the entire context..just specific user that just connected. If that makes sense.

Third Question: Do I have to call us_socket_context_free on a child socket if the parent is still alive?

Question two resolved. I think I need to call us_create_child_socket_context inside the onopen callback then us_socket_context_on_data passing the created child socket as an argument? Please correct me if I'm wrong.

ghost commented 5 years ago

You can read the "docs" here https://github.com/uNetworking/uSockets/blob/master/src/libusockets.h

Merethiel commented 5 years ago

The "docs' do not mention whether it's okay to free a child socket context. I'll assume it is though. It also doesn't mention if it's okay to create multiple socket contexts ore rely on just one for many different endpoint connects(This is in the case of a client). What I did end up doing was create a new socket context and connect to a new endpoint since each endpoint requires its own set of data. But I'm not sure if that could have been solved with a creating a child context instead.

The callbacks can be solved by creating a child context then adopting and setting to a new callback. Again no where stating whether its okay to free that context upon closing of the socket.

ghost commented 5 years ago

All of this is very obvious if you take a look at the "docs" and look over an example. There are plenty of comments and you should easily be able to figure things out.

Merethiel commented 5 years ago

Yes it is easy to figure out after trial and error. But typically the idea of documentation is it doesn't have to be a guessing game or trial and error.

Thank you.