diff --git a/src/socket.js.c b/src/socket.js.c index f37eb348..a520d1a1 100644 --- a/src/socket.js.c +++ b/src/socket.js.c @@ -369,6 +369,11 @@ JSValue _socket_stopTls(JSContext* context, JSValueConst this_val, int argc, JSV bool _socket_processSomeOutgoingTls(socket_t* socket, promiseid_t promise, uv_write_cb callback) { + if (!socket->_socket.data) + { + return false; + } + char buffer[65536]; int result = tf_tls_session_read_encrypted(socket->_tls, buffer, sizeof(buffer)); if (result > 0) @@ -693,7 +698,7 @@ JSValue _socket_read(JSContext* context, JSValueConst this_val, int argc, JSValu promiseid_t promise = -1; JSValue read_result = tf_task_allocate_promise(socket->_task, &promise); - if (!socket->_reading) + if (!socket->_reading && socket->_socket.data) { int result = uv_read_start((uv_stream_t*)&socket->_socket, _socket_allocateBuffer, _socket_onRead); if (result != 0) @@ -892,6 +897,12 @@ int _socket_writeBytes(socket_t* socket, promiseid_t promise, int (*callback)(so int _socket_writeInternal(socket_t* socket, promiseid_t promise, const char* data, size_t length) { + if (!socket->_socket.data) + { + tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "uv_write: %s", "not connected")); + return -1; + } + char* rawBuffer = tf_malloc(sizeof(uv_write_t) + length); uv_write_t* request = (uv_write_t*)rawBuffer; memcpy(rawBuffer + sizeof(uv_write_t), data, length); @@ -906,6 +917,7 @@ int _socket_writeInternal(socket_t* socket, promiseid_t promise, const char* dat int result = uv_write(request, (uv_stream_t*)&socket->_socket, &buffer, 1, _socket_onWrite); if (result != 0) { + tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "uv_write: %s", uv_strerror(result))); tf_free(rawBuffer); } return result;