2022-09-21 05:16:07 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
#include "JSServerWorker.h"
|
2022-09-21 05:16:07 +00:00
|
|
|
#include "ScriptAPI.h"
|
2021-09-03 05:11:04 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
static CJSServerWorker * GetThisServer(duk_context * ctx);
|
2021-09-03 05:11:04 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void ScriptAPI::Define_Server(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
const DukPropListEntry prototype[] = {
|
2022-09-21 05:16:07 +00:00
|
|
|
{"listen", DukCFunction(js_Server_listen)},
|
|
|
|
{"close", DukCFunction(js_Server_close)},
|
|
|
|
{"on", DukCFunction(js__Emitter_on)},
|
|
|
|
{"off", DukCFunction(js__Emitter_off)},
|
|
|
|
{"port", DukGetter(js_Server__get_port)},
|
|
|
|
{"address", DukGetter(js_Server__get_address)},
|
|
|
|
{"addressFamily", DukGetter(js_Server__get_addressFamily)},
|
2021-09-03 05:11:04 +00:00
|
|
|
{ nullptr }
|
|
|
|
};
|
|
|
|
|
|
|
|
DefineGlobalClass(ctx, "Server", js_Server__constructor, prototype);
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_Server__constructor(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
CheckArgs(ctx, {});
|
|
|
|
|
|
|
|
if (!duk_is_constructor_call(ctx))
|
|
|
|
{
|
|
|
|
return DUK_RET_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptInstance * inst = GetInstance(ctx);
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
duk_push_this(ctx);
|
2022-09-21 05:16:07 +00:00
|
|
|
void * objectHeapPtr = duk_get_heapptr(ctx, -1);
|
|
|
|
|
|
|
|
InitEmitter(ctx, -1, {"close", "connection", "error", "listening"});
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
duk_push_c_function(ctx, js_Server__finalizer, 1);
|
|
|
|
duk_set_finalizer(ctx, -2);
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CJSServerWorker * serverWorker = new CJSServerWorker(inst, objectHeapPtr);
|
2021-09-03 05:11:04 +00:00
|
|
|
duk_push_pointer(ctx, serverWorker);
|
|
|
|
duk_put_prop_string(ctx, -2, HS_serverWorkerPtr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_Server__finalizer(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
UnrefObject(ctx, 0);
|
|
|
|
duk_get_prop_string(ctx, 0, HS_serverWorkerPtr);
|
2022-09-21 05:16:07 +00:00
|
|
|
CJSServerWorker * serverWorker = (CJSServerWorker *)duk_get_pointer(ctx, -1);
|
2021-09-03 05:11:04 +00:00
|
|
|
delete serverWorker;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_Server_listen(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
CheckArgs(ctx, {Arg_Number, Arg_OptString, Arg_OptFunction});
|
|
|
|
CJSServerWorker * serverWorker = GetThisServer(ctx);
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
unsigned short port = (unsigned short)duk_get_int(ctx, 0);
|
2022-09-21 05:16:07 +00:00
|
|
|
const char * address = duk_get_string_default(ctx, 1, "0.0.0.0");
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
// todo callback
|
|
|
|
|
|
|
|
duk_push_this(ctx);
|
|
|
|
RefObject(ctx, -1);
|
|
|
|
|
|
|
|
serverWorker->Init(address, port);
|
|
|
|
serverWorker->StartWorkerProc();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_Server_close(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
CheckArgs(ctx, {});
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CJSServerWorker * serverWorker = GetThisServer(ctx);
|
2021-09-03 05:11:04 +00:00
|
|
|
serverWorker->StopWorkerProc();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_Server__get_port(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
CJSServerWorker * serverWorker = GetThisServer(ctx);
|
2021-09-03 05:11:04 +00:00
|
|
|
duk_push_uint(ctx, serverWorker->GetPort());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_Server__get_address(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
CJSServerWorker * serverWorker = GetThisServer(ctx);
|
2021-09-03 05:11:04 +00:00
|
|
|
duk_push_string(ctx, serverWorker->GetAddress().c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_Server__get_addressFamily(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
CJSServerWorker * serverWorker = GetThisServer(ctx);
|
2021-09-03 05:11:04 +00:00
|
|
|
duk_push_string(ctx, serverWorker->GetFamily());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CJSServerWorker * GetThisServer(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
duk_push_this(ctx);
|
|
|
|
duk_get_prop_string(ctx, -1, HS_serverWorkerPtr);
|
2022-09-21 05:16:07 +00:00
|
|
|
CJSServerWorker * serverWorker = (CJSServerWorker *)duk_get_pointer(ctx, -1);
|
2021-09-03 05:11:04 +00:00
|
|
|
duk_pop_n(ctx, 2);
|
|
|
|
return serverWorker;
|
|
|
|
}
|