0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-07-10 12:38:36 +02:00
construct/modules/js/listener.cc

102 lines
1.8 KiB
C++
Raw Normal View History

2018-02-04 03:22:01 +01:00
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
2016-10-28 08:00:18 +02:00
#include <ircd/net/listener.h>
#include <ircd/js/js.h>
2016-10-28 08:00:18 +02:00
namespace ircd {
namespace js {
struct listener
:js::trap
2016-10-28 08:00:18 +02:00
{
struct listen;
2016-10-28 08:00:18 +02:00
static const char *const source;
struct module module;
2016-10-28 08:00:18 +02:00
listener();
}
static listener;
2016-10-28 08:00:18 +02:00
struct listener::listen
:trap::function
2016-10-28 08:00:18 +02:00
{
net::listener *l;
value on_call(object::handle obj, value::handle _this, const args &args) override
{
const std::string a(args[0]);
const ircd::json::object o(a);
l = new net::listener(o.get("name", "js"), o, []
(const auto &sock)
{
add_client(sock);
});
return {};
}
using trap::function::function;
2016-10-28 08:00:18 +02:00
}
static listener_listen
{
listener, "listen"
};
} // namespace js
} // namespace ircd
///////////////////////////////////////////////////////////////////////////////
//
// Listener source
//
const char *const
ircd::js::listener::source
{R"(
2016-10-28 08:00:18 +02:00
import * as console from "server.console";
export function listen(opts)
2016-10-28 08:00:18 +02:00
{
__listener.listen(JSON.stringify(opts));
2016-10-28 08:00:18 +02:00
}
)"};
///////////////////////////////////////////////////////////////////////////////
ircd::js::listener::listener()
:js::trap
2016-10-28 08:00:18 +02:00
{
"__listener",
2016-10-28 08:00:18 +02:00
}
,module
2016-10-28 08:00:18 +02:00
{
JS::CompileOptions(*cx),
locale::char16::conv(source),
this,
true
2016-10-28 08:00:18 +02:00
}
{
}
ircd::js::module *
IRCD_JS_MODULE
2016-10-28 08:00:18 +02:00
{
&ircd::js::listener.module
};
2016-10-28 08:00:18 +02:00
ircd::mapi::header
IRCD_MODULE
{
"Network listener socket support for servers"
};