Trying harder still to curb stale connections.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3900 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-06-17 18:51:24 +00:00
parent 4293e75082
commit b5b6ed8ba5
2 changed files with 61 additions and 8 deletions

View File

@ -45,6 +45,7 @@ typedef struct _socket_t {
JSValue _onConnect;
JSValue _onRead;
JSValue _onError;
JSValue _info;
uint64_t created_ms;
} socket_t;
@ -67,6 +68,7 @@ static JSValue _socket_getPeerName(JSContext* context, JSValueConst this_val, in
static JSValue _socket_getPeerCertificate(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _socket_getNoDelay(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _socket_setNoDelay(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _socket_setInfo(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _sockets_get(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static void _socket_onClose(uv_handle_t* handle);
@ -122,6 +124,7 @@ static void _socket_gc_mark(JSRuntime* runtime, JSValueConst value, JS_MarkFunc
JS_MarkValue(runtime, socket->_onConnect, mark_func);
JS_MarkValue(runtime, socket->_onRead, mark_func);
JS_MarkValue(runtime, socket->_onError, mark_func);
JS_MarkValue(runtime, socket->_info, mark_func);
}
}
@ -212,6 +215,11 @@ socket_t* _socket_create_internal(JSContext* context)
JS_DefinePropertyGetSet(context, object, atom, get_no_delay, set_no_delay, 0);
JS_FreeAtom(context, atom);
atom = JS_NewAtom(context, "info");
JSValue set_info = JS_NewCFunction(context, _socket_setInfo, "setInfo", 1);
JS_DefinePropertyGetSet(context, object, atom, JS_UNDEFINED, set_info, 0);
JS_FreeAtom(context, atom);
++_open_count;
uv_tcp_init(tf_task_get_loop(socket->_task), &socket->_socket);
socket->_socket.data = socket;
@ -1065,6 +1073,15 @@ JSValue _socket_setNoDelay(JSContext* context, JSValueConst this_val, int argc,
return JS_UNDEFINED;
}
JSValue _socket_setInfo(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
socket_t* socket = JS_GetOpaque(this_val, _classId);
JSValue old = socket->_info;
socket->_info = JS_DupValue(context, argv[0]);
JS_FreeValue(context, old);
return JS_UNDEFINED;
}
JSValue _sockets_get(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue array = JS_NewArray(context);
@ -1077,6 +1094,7 @@ JSValue _sockets_get(JSContext* context, JSValueConst this_val, int argc, JSValu
JS_SetPropertyStr(context, entry, "connected", JS_NewBool(context, s->_connected));
JS_SetPropertyStr(context, entry, "tls", JS_NewBool(context, s->_tls != NULL));
JS_SetPropertyStr(context, entry, "age_seconds", JS_NewFloat64(context, (uv_now(tf_task_get_loop(s->_task)) - s->created_ms) / 1000.f));
JS_SetPropertyStr(context, entry, "info", JS_DupValue(context, s->_info));
JS_SetPropertyUint32(context, array, i, entry);
}
return array;