-
Notifications
You must be signed in to change notification settings - Fork 311
Ensure websockets write all data; Always keep_alive every websocket #178
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,10 +77,26 @@ ws_compute_handshake(struct http_client *c, char *out, size_t *out_sz) { | |
| return 0; | ||
| } | ||
|
|
||
|
|
||
| void | ||
| ws_write_all(int socket, void *buffer, size_t length) { | ||
| size_t written = 0; | ||
| int n; | ||
| while (written < length) { | ||
| int n = write(socket, (char*)buffer + written, length - written); | ||
| if (n <= 0) { | ||
| if (errno == EINTR || errno == EAGAIN) | ||
| continue; | ||
| err(EXIT_FAILURE, "Could not write() data"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use the logging interface instead of terminating the service with #include "slog.h"
// ...
int
ws_write_all(struct server *s, int fd, void *buffer, size_t length) {
// ...
const char err_msg[] = "Could not write data to websocket";
slog(s, WEBDIS_ERROR, err_msg, sizeof(err_msg)-1);
return -1;Instead of calling Unfortunately I see that even if this error is propagated from Something like this. Instead of: if(nparsed < ret) {
http_client_add_to_body(c, c->buffer + nparsed + 1, c->sz - nparsed - 1);
ws_handshake_reply(c);
}you'd want to use the return value: if(nparsed < ret) {
http_client_add_to_body(c, c->buffer + nparsed + 1, c->sz - nparsed - 1);
if(ws_handshake_reply(c) < 0)
c->broken = 1;
}I'm also not sure about this repeated write outside of the event loop. Ideally the call to |
||
| } | ||
| written += n; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| int | ||
| ws_handshake_reply(struct http_client *c) { | ||
|
|
||
| int ret; | ||
| char sha1_handshake[40]; | ||
| char *buffer = NULL, *p; | ||
| const char *origin = NULL, *host = NULL; | ||
|
|
@@ -159,8 +175,7 @@ ws_handshake_reply(struct http_client *c) { | |
| p += sizeof(template4)-1; | ||
|
|
||
| /* send data to client */ | ||
| ret = write(c->fd, buffer, sz); | ||
| (void)ret; | ||
| ws_write_all(c->fd, buffer, sz); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return value of this function should be propagated to the caller in Also, to pass a ret = ws_write_all(c->s, c->fd, buffer, sz);(also see comment below) |
||
| free(buffer); | ||
|
|
||
| return 0; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we'd use return ret; |
||
|
|
@@ -365,9 +380,7 @@ ws_reply(struct cmd *cmd, const char *p, size_t sz) { | |
|
|
||
| /* send WS frame */ | ||
| r = http_response_init(cmd->w, 0, NULL); | ||
| if (cmd_is_subscribe(cmd)) { | ||
| r->keep_alive = 1; | ||
| } | ||
| r->keep_alive = 1; | ||
|
|
||
| if (r == NULL) | ||
| return -1; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the caller of this function uses
bufferas achar *, you can use the same type here – or even better –const char *. You can also remove the cast to(char*)in the call towrite(2).(minor) file descriptors are named
fdthroughout the code base, can you please renamesockettofdfor consistency? Not to mention thatsocketis also a function.