Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Changes #45

Merged
merged 6 commits into from Apr 5, 2022
Merged

API Changes #45

merged 6 commits into from Apr 5, 2022

Conversation

Theldus
Copy link
Owner

@Theldus Theldus commented Apr 5, 2022

Description

A few days ago I was asked why wsServer only sends broadcast messages if it knows a client beforehand (its 'fd'). Clearly this is an issue as the broadcast must work without the prior knowledge of any connected users.

This PR therefore fixes this bug by changing the signature of the ws_sendframe* routines: if a client is present, send only to it, otherwise broadcast.

In addition, the 'fd' parameter present in the API functions was replaced with 'ws_cli_conn_t', which made the code more intuitive and also allowed a great internal cleaning of the source code.

The new function signatures:

int ws_sendframe(ws_cli_conn_t *cli, const char *msg, uint64_t size, int type);
int ws_sendframe_txt(ws_cli_conn_t *cli, const char *msg);
int ws_sendframe_bin(ws_cli_conn_t *cli, const char *msg, uint64_t size);
int ws_get_state(ws_cli_conn_t *cli);
int ws_close_client(ws_cli_conn_t *cli);

and the events:

void onopen(ws_cli_conn_t *client);
void onclose(ws_cli_conn_t *client);
void onmessage(ws_cli_conn_t *client, const unsigned char *msg, uint64_t size, int type);

A huge thanks to @tinkering4fun and @troglobytor for the discussion over the last few days on how I should approach this issue. I'm happy with the result and hope it meets your expectations.

Feel free whenever you want to suggest something... open an issue, discussion, PR... I'm always trying to listen to the community and adapt wsServer to their needs.

Because there's no reason to keep a single file per folder.

Although the history seems to be lost, it is still possible to find
the old commits with 'git log --follow'.
This commit changes the use of the 'fd' parameter of the wsServer's
API and replaces it with a pointer of type 'ws_cli_conn_t'.

This change brings multiple benefits to the source code:
a) The 'fd' could confuse the user and cause them to use it in other
POSIX routines that accept a file descriptor.

b) Sharing the 'fd' through various functions and iterating over the
list of clients to get the structure of it sounds completely pointless.

Therefore, the natural solution is to share the pointer relative to
the client's connection structure, which improves code complexity,
readability, and reduces the amount of LOCs.

--
Furthermore, this commit also changes the way broadcast messages are
sent: previously, to send a broadcast it was necessary to know the
'fd' of a valid connected client, which also doesn't make sense.
Therefore, the signing of the ws_sendframe* functions has been
simplified, and the 'broadcast' parameter is no longer needed. If the
recipient is null, the message is a broadcast.
---

Note: The multi-port listening feature has been removed in order to
make the broadcast mechanism simpler. However, I intend reintroduce
it in the future by removing all public variables and leaving the
event frame bound to a 'struct ws_server'.
In issue #41 there was a problem getting the IP address during the
onclose() event, as the server was trying to get an IP from an
already disconnected client.

This commit fixes this by saving the IP address of the client right
during the initial stages of the connection, which makes it available
at any time, even when the client is disconnected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant