]> prime8.dev >> repos - ttyd.git/commitdiff
protocol: replace uv_try_write with uv_write
authorTian Yuanhao <tianyuanhao@aliyun.com>
Tue, 10 Mar 2020 16:13:51 +0000 (00:13 +0800)
committerShuanglei Tao <tsl0922@gmail.com>
Tue, 10 Mar 2020 16:39:16 +0000 (00:39 +0800)
src/protocol.c

index 2200f1b9a724f3289eb4df578cdffbd2c40c5b1d..dccee3b28ce336005e01a1e9f18ab4df8b9bc7de 100644 (file)
@@ -233,6 +233,14 @@ kill_process(struct pty_proc *proc) {
     }
 }
 
+void
+write_cb(uv_write_t* req, int status) {
+    if (status != 0)
+        lwsl_warn("uv_write callback returned status: %d\n", status);
+    free(req->data);
+    free(req);
+}
+
 int
 callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
              void *user, void *in, size_t len) {
@@ -370,10 +378,17 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
                         break;
                     if (server->readonly)
                         return 0;
-                    uv_buf_t b = { pss->buffer + 1, pss->len - 1 };
-                    int err = uv_try_write((uv_stream_t *) &proc->pipe, &b, 1);
-                    if (err < 0) {
-                        lwsl_err("uv_try_write: %s\n", uv_err_name(err));
+
+                    char *data = xmalloc(pss->len - 1);
+                    memcpy(data, pss->buffer + 1, pss->len - 1);
+
+                    uv_buf_t b = { data, pss->len - 1 };
+                    uv_write_t *req = xmalloc(sizeof(uv_write_t));
+                    req->data = data;
+
+                    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));
                         return -1;
                     }
                     break;