Skip to content

Commit

Permalink
protocol: improve io error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jul 27, 2020
1 parent 9edb245 commit f9d23de
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ static void read_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
if (nread <= 0) {
if (nread == UV_ENOBUFS || nread == 0) return;
proc->pty_buffer = NULL;
if (nread == UV_EOF)
proc->pty_len = 0;
else
lwsl_err("read_cb: %s\n", uv_err_name(nread));
lwsl_err("read_cb: %s (%s)\n", uv_err_name(nread), uv_strerror(nread));
} else {
proc->pty_buffer = xmalloc(LWS_PRE + 1 + (size_t)nread);
memcpy(proc->pty_buffer + LWS_PRE + 1, buf->base, (size_t)nread);
Expand Down Expand Up @@ -310,15 +307,15 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
}

// read error or client exited, close connection
if (proc->status == 0 || proc->pty_len == 0) {
if (proc->status == 0 || proc->pty_len == UV_EOF) {
lws_close_reason(wsi, LWS_CLOSE_STATUS_NORMAL, NULL, 0);
return 1;
} else if (proc->status > 0 || proc->pty_len < 0) {
lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
return -1;
}

if (proc->pty_buffer == NULL) break;
if (proc->pty_buffer == NULL || proc->pty_len == 0) break;

proc->pty_buffer[LWS_PRE] = OUTPUT;
n = (size_t)(proc->pty_len + 1);
Expand Down Expand Up @@ -369,7 +366,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,

int err = uv_write(req, (uv_stream_t *)&proc->pipe, &b, 1, write_cb);
if (err) {
lwsl_err("uv_write: %s\n", uv_err_name(err));
lwsl_err("uv_write: %s (%s)\n", uv_err_name(err), uv_strerror(err));
return -1;
}
break;
Expand Down

0 comments on commit f9d23de

Please sign in to comment.