2015-01-28 09:14:05 -05:00
|
|
|
|
|
|
|
// ManualBindings_Network.cpp
|
|
|
|
|
|
|
|
// Implements the cNetwork-related API bindings for Lua
|
2016-08-23 07:20:43 -04:00
|
|
|
// Also implements the cUrlClient bindings
|
2015-01-28 09:14:05 -05:00
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "ManualBindings.h"
|
|
|
|
#include "tolua++/include/tolua++.h"
|
|
|
|
#include "LuaState.h"
|
|
|
|
#include "LuaTCPLink.h"
|
2015-01-29 05:09:56 -05:00
|
|
|
#include "LuaNameLookup.h"
|
2015-01-30 15:24:02 -05:00
|
|
|
#include "LuaServerHandle.h"
|
2015-02-20 08:28:05 -05:00
|
|
|
#include "LuaUDPEndpoint.h"
|
2016-08-23 07:20:43 -04:00
|
|
|
#include "../HTTP/UrlClient.h"
|
2015-01-28 09:14:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cNetwork API functions:
|
|
|
|
|
|
|
|
/** Binds cNetwork::Connect */
|
|
|
|
static int tolua_cNetwork_Connect(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// cNetwork:Connect(Host, Port, Callbacks) -> bool
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamStaticSelf("cNetwork") ||
|
2015-01-28 09:14:05 -05:00
|
|
|
!S.CheckParamString(2) ||
|
|
|
|
!S.CheckParamNumber(3) ||
|
|
|
|
!S.CheckParamTable(4) ||
|
|
|
|
!S.CheckParamEnd(5)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-07-06 14:52:04 -04:00
|
|
|
// Read the params:
|
|
|
|
AString host;
|
|
|
|
int port = 0;
|
|
|
|
cLuaState::cTableRefPtr callbacks;
|
|
|
|
if (!S.GetStackValues(2, host, port, callbacks))
|
2015-01-28 09:14:05 -05:00
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return S.ApiParamError("Cannot read parameters.");
|
2015-01-28 09:14:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check validity:
|
2016-07-06 14:52:04 -04:00
|
|
|
if ((port < 0) || (port > 65535))
|
2015-01-28 09:14:05 -05:00
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return S.ApiParamError("Port number out of range (%d, range 0 - 65535)", port);
|
2015-01-28 09:14:05 -05:00
|
|
|
}
|
2016-07-06 14:52:04 -04:00
|
|
|
ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false
|
2015-01-28 09:14:05 -05:00
|
|
|
|
|
|
|
// Create the LuaTCPLink glue class:
|
2016-07-06 14:52:04 -04:00
|
|
|
auto link = std::make_shared<cLuaTCPLink>(std::move(callbacks));
|
2015-01-28 09:14:05 -05:00
|
|
|
|
|
|
|
// Try to connect:
|
2016-07-06 14:52:04 -04:00
|
|
|
bool res = cNetwork::Connect(host, static_cast<UInt16>(port), link, link);
|
2015-01-28 09:14:05 -05:00
|
|
|
S.Push(res);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
/** Binds cNetwork::CreateUDPEndpoint */
|
|
|
|
static int tolua_cNetwork_CreateUDPEndpoint(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// cNetwork:CreateUDPEndpoint(Port, Callbacks) -> cUDPEndpoint
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamStaticSelf("cNetwork") ||
|
2015-02-20 08:28:05 -05:00
|
|
|
!S.CheckParamNumber(2) ||
|
|
|
|
!S.CheckParamTable(3) ||
|
|
|
|
!S.CheckParamEnd(4)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-07-06 14:52:04 -04:00
|
|
|
// Read the params:
|
2017-06-26 04:20:53 -04:00
|
|
|
int port;
|
2016-07-06 14:52:04 -04:00
|
|
|
cLuaState::cTableRefPtr callbacks;
|
|
|
|
if (!S.GetStackValues(2, port, callbacks))
|
2015-02-20 08:28:05 -05:00
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return S.ApiParamError("Cannot read parameters");
|
2015-02-20 08:28:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check validity:
|
2016-07-06 14:52:04 -04:00
|
|
|
if ((port < 0) || (port > 65535))
|
2015-02-20 08:28:05 -05:00
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return S.ApiParamError("Port number out of range (%d, range 0 - 65535)", port);
|
2015-02-20 08:28:05 -05:00
|
|
|
}
|
2016-07-06 14:52:04 -04:00
|
|
|
ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false
|
2015-02-20 08:28:05 -05:00
|
|
|
|
|
|
|
// Create the LuaUDPEndpoint glue class:
|
2016-07-06 14:52:04 -04:00
|
|
|
auto endpoint = std::make_shared<cLuaUDPEndpoint>(std::move(callbacks));
|
2017-06-26 04:20:53 -04:00
|
|
|
endpoint->Open(static_cast<UInt16>(port), endpoint);
|
2015-02-20 08:28:05 -05:00
|
|
|
|
|
|
|
// Register the endpoint to be garbage-collected by Lua:
|
2016-07-06 14:52:04 -04:00
|
|
|
tolua_pushusertype(L, endpoint.get(), "cUDPEndpoint");
|
2015-02-20 08:28:05 -05:00
|
|
|
tolua_register_gc(L, lua_gettop(L));
|
|
|
|
|
|
|
|
// Return the endpoint object:
|
2016-07-06 14:52:04 -04:00
|
|
|
S.Push(endpoint.get());
|
2015-02-20 08:28:05 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-22 04:51:16 -05:00
|
|
|
/** Binds cNetwork::EnumLocalIPAddresses */
|
|
|
|
static int tolua_cNetwork_EnumLocalIPAddresses(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// cNetwork:EnumLocalIPAddresses() -> {string, ...}
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamStaticSelf("cNetwork") ||
|
2015-02-22 04:51:16 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-02-22 04:51:16 -05:00
|
|
|
// Push the enumerated addresses:
|
|
|
|
S.Push(cNetwork::EnumLocalIPAddresses());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-30 15:24:02 -05:00
|
|
|
/** Binds cNetwork::HostnameToIP */
|
2015-01-29 05:09:56 -05:00
|
|
|
static int tolua_cNetwork_HostnameToIP(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// cNetwork:HostnameToIP(Host, Callbacks) -> bool
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamStaticSelf("cNetwork") ||
|
2015-01-29 05:09:56 -05:00
|
|
|
!S.CheckParamString(2) ||
|
|
|
|
!S.CheckParamTable(3) ||
|
|
|
|
!S.CheckParamEnd(4)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-07-06 14:52:04 -04:00
|
|
|
// Read the params:
|
|
|
|
AString host;
|
|
|
|
cLuaState::cTableRefPtr callbacks;
|
|
|
|
if (!S.GetStackValues(2, host, callbacks))
|
2015-01-29 05:09:56 -05:00
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return S.ApiParamError("Cannot read parameters.");
|
2015-01-29 05:09:56 -05:00
|
|
|
}
|
2016-07-06 14:52:04 -04:00
|
|
|
ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false
|
2015-01-29 05:09:56 -05:00
|
|
|
|
|
|
|
// Try to look up:
|
2016-07-06 14:52:04 -04:00
|
|
|
bool res = cNetwork::HostnameToIP(host, std::make_shared<cLuaNameLookup>(host, std::move(callbacks)));
|
2015-01-29 05:09:56 -05:00
|
|
|
S.Push(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-30 15:24:02 -05:00
|
|
|
/** Binds cNetwork::IPToHostname */
|
2015-01-29 05:09:56 -05:00
|
|
|
static int tolua_cNetwork_IPToHostname(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// cNetwork:IPToHostname(IP, Callbacks) -> bool
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamStaticSelf("cNetwork") ||
|
2015-01-29 05:09:56 -05:00
|
|
|
!S.CheckParamString(2) ||
|
|
|
|
!S.CheckParamTable(3) ||
|
|
|
|
!S.CheckParamEnd(4)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-07-06 14:52:04 -04:00
|
|
|
// Read the params:
|
|
|
|
AString ip;
|
|
|
|
cLuaState::cTableRefPtr callbacks;
|
|
|
|
if (!S.GetStackValues(2, ip, callbacks))
|
2015-01-29 05:09:56 -05:00
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return S.ApiParamError("Cannot read parameters.");
|
2015-01-29 05:09:56 -05:00
|
|
|
}
|
2016-07-06 14:52:04 -04:00
|
|
|
ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false
|
2015-01-29 05:09:56 -05:00
|
|
|
|
|
|
|
// Try to look up:
|
2016-07-06 14:52:04 -04:00
|
|
|
bool res = cNetwork::IPToHostName(ip, std::make_shared<cLuaNameLookup>(ip, std::move(callbacks)));
|
2015-01-29 05:09:56 -05:00
|
|
|
S.Push(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-30 15:24:02 -05:00
|
|
|
/** Binds cNetwork::Listen */
|
|
|
|
static int tolua_cNetwork_Listen(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
2015-02-20 08:28:05 -05:00
|
|
|
// cNetwork:Listen(Port, Callbacks) -> cServerHandle
|
2015-01-30 15:24:02 -05:00
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamStaticSelf("cNetwork") ||
|
2015-01-30 15:24:02 -05:00
|
|
|
!S.CheckParamNumber(2) ||
|
|
|
|
!S.CheckParamTable(3) ||
|
|
|
|
!S.CheckParamEnd(4)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-07-06 14:52:04 -04:00
|
|
|
// Read the params:
|
|
|
|
int port = 0;
|
|
|
|
cLuaState::cTableRefPtr callbacks;
|
|
|
|
if (!S.GetStackValues(2, port, callbacks))
|
2015-01-30 15:24:02 -05:00
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return S.ApiParamError("Cannot read parameters");
|
2015-01-30 15:24:02 -05:00
|
|
|
}
|
|
|
|
|
2016-07-06 14:52:04 -04:00
|
|
|
// Check the validity:
|
|
|
|
if ((port < 0) || (port > 65535))
|
2015-01-30 15:24:02 -05:00
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return S.ApiParamError("Port number out of range (%d, range 0 - 65535)", port);
|
2015-01-30 15:24:02 -05:00
|
|
|
}
|
2016-07-06 14:52:04 -04:00
|
|
|
auto port16 = static_cast<UInt16>(port);
|
2015-01-30 15:24:02 -05:00
|
|
|
|
|
|
|
// Create the LuaTCPLink glue class:
|
2016-07-06 14:52:04 -04:00
|
|
|
auto srv = std::make_shared<cLuaServerHandle>(port16, std::move(callbacks));
|
2015-01-30 15:24:02 -05:00
|
|
|
|
|
|
|
// Listen:
|
2016-07-06 14:52:04 -04:00
|
|
|
srv->SetServerHandle(cNetwork::Listen(port16, srv), srv);
|
2015-01-30 15:24:02 -05:00
|
|
|
|
|
|
|
// Register the server to be garbage-collected by Lua:
|
2016-07-06 14:52:04 -04:00
|
|
|
tolua_pushusertype(L, srv.get(), "cServerHandle");
|
2015-01-30 15:24:02 -05:00
|
|
|
tolua_register_gc(L, lua_gettop(L));
|
|
|
|
|
|
|
|
// Return the server handle wrapper:
|
2016-07-06 14:52:04 -04:00
|
|
|
S.Push(srv.get());
|
2015-01-30 15:24:02 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cServerHandle bindings (routed through cLuaServerHandle):
|
|
|
|
|
|
|
|
/** Called when Lua destroys the object instance.
|
|
|
|
Close the server and let it deallocate on its own (it's in a SharedPtr). */
|
|
|
|
static int tolua_collect_cServerHandle(lua_State * L)
|
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
auto Srv = static_cast<cLuaServerHandle *>(tolua_tousertype(L, 1, nullptr));
|
|
|
|
ASSERT(Srv != nullptr);
|
2015-02-20 08:28:05 -05:00
|
|
|
Srv->Release();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Binds cLuaServerHandle::Close */
|
|
|
|
static int tolua_cServerHandle_Close(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// ServerInstance:Close()
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cServerHandle") ||
|
2015-02-20 08:28:05 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
// Get the server handle:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto Srv = *static_cast<cLuaServerHandle **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(Srv != nullptr); // Checked by CheckParamSelf()
|
2015-02-20 08:28:05 -05:00
|
|
|
|
|
|
|
// Close it:
|
|
|
|
Srv->Close();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Binds cLuaServerHandle::IsListening */
|
|
|
|
static int tolua_cServerHandle_IsListening(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// ServerInstance:IsListening() -> bool
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cServerHandle") ||
|
2015-02-20 08:28:05 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
// Get the server handle:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto Srv = *static_cast<cLuaServerHandle **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(Srv != nullptr); // Checked by CheckParamSelf()
|
2015-02-20 08:28:05 -05:00
|
|
|
|
2017-06-26 04:20:53 -04:00
|
|
|
// Query it:
|
2015-02-20 08:28:05 -05:00
|
|
|
S.Push(Srv->IsListening());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-28 09:14:05 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cTCPLink bindings (routed through cLuaTCPLink):
|
|
|
|
|
2015-02-13 12:31:54 -05:00
|
|
|
/** Binds cLuaTCPLink::Close */
|
|
|
|
static int tolua_cTCPLink_Close(lua_State * L)
|
2015-01-28 09:14:05 -05:00
|
|
|
{
|
|
|
|
// Function signature:
|
2015-02-13 12:31:54 -05:00
|
|
|
// LinkInstance:Close()
|
2015-01-28 09:14:05 -05:00
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cTCPLink") ||
|
2015-02-13 12:31:54 -05:00
|
|
|
!S.CheckParamEnd(2)
|
2015-01-28 09:14:05 -05:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-01-28 09:14:05 -05:00
|
|
|
// Get the link:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(Link != nullptr); // Checked by CheckParamSelf()
|
2015-01-28 09:14:05 -05:00
|
|
|
|
2017-06-26 04:20:53 -04:00
|
|
|
// Close the link:
|
2015-02-13 12:31:54 -05:00
|
|
|
Link->Close();
|
2015-01-28 09:14:05 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-30 15:24:02 -05:00
|
|
|
/** Binds cLuaTCPLink::GetLocalIP */
|
2015-01-28 09:14:05 -05:00
|
|
|
static int tolua_cTCPLink_GetLocalIP(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// LinkInstance:GetLocalIP() -> string
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cTCPLink") ||
|
2015-01-28 09:14:05 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-01-28 09:14:05 -05:00
|
|
|
// Get the link:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(Link != nullptr); // Checked by CheckParamSelf()
|
2015-01-28 09:14:05 -05:00
|
|
|
|
|
|
|
// Get the IP:
|
|
|
|
S.Push(Link->GetLocalIP());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-30 15:24:02 -05:00
|
|
|
/** Binds cLuaTCPLink::GetLocalPort */
|
2015-01-28 09:14:05 -05:00
|
|
|
static int tolua_cTCPLink_GetLocalPort(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// LinkInstance:GetLocalPort() -> number
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cTCPLink") ||
|
2015-01-28 09:14:05 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-01-28 09:14:05 -05:00
|
|
|
// Get the link:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(Link != nullptr); // Checked by CheckParamSelf()
|
2015-01-28 09:14:05 -05:00
|
|
|
|
|
|
|
// Get the Port:
|
|
|
|
S.Push(Link->GetLocalPort());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-30 15:24:02 -05:00
|
|
|
/** Binds cLuaTCPLink::GetRemoteIP */
|
2015-01-28 09:14:05 -05:00
|
|
|
static int tolua_cTCPLink_GetRemoteIP(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// LinkInstance:GetRemoteIP() -> string
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cTCPLink") ||
|
2015-01-28 09:14:05 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-01-28 09:14:05 -05:00
|
|
|
// Get the link:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(Link != nullptr); // Checked by CheckParamSelf()
|
2015-01-28 09:14:05 -05:00
|
|
|
|
|
|
|
// Get the IP:
|
|
|
|
S.Push(Link->GetRemoteIP());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-30 15:24:02 -05:00
|
|
|
/** Binds cLuaTCPLink::GetRemotePort */
|
2015-01-28 09:14:05 -05:00
|
|
|
static int tolua_cTCPLink_GetRemotePort(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// LinkInstance:GetRemotePort() -> number
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cTCPLink") ||
|
2015-01-28 09:14:05 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-01-28 09:14:05 -05:00
|
|
|
// Get the link:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(Link != nullptr); // Checked by CheckParamSelf()
|
2015-01-28 09:14:05 -05:00
|
|
|
|
|
|
|
// Get the Port:
|
|
|
|
S.Push(Link->GetRemotePort());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-13 12:31:54 -05:00
|
|
|
/** Binds cLuaTCPLink::Send */
|
|
|
|
static int tolua_cTCPLink_Send(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// LinkInstance:Send(DataString)
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cTCPLink") ||
|
2015-02-13 12:31:54 -05:00
|
|
|
!S.CheckParamString(2) ||
|
|
|
|
!S.CheckParamEnd(3)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-02-13 12:31:54 -05:00
|
|
|
// Get the link:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(Link != nullptr); // Checked by CheckParamSelf()
|
2015-02-13 12:31:54 -05:00
|
|
|
|
|
|
|
// Get the data to send:
|
|
|
|
AString Data;
|
|
|
|
S.GetStackValues(2, Data);
|
|
|
|
|
|
|
|
// Send the data:
|
|
|
|
Link->Send(Data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Binds cLuaTCPLink::Shutdown */
|
|
|
|
static int tolua_cTCPLink_Shutdown(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// LinkInstance:Shutdown()
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cTCPLink") ||
|
2015-02-13 12:31:54 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-02-13 12:31:54 -05:00
|
|
|
// Get the link:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(Link != nullptr); // Checked by CheckParamSelf()
|
2015-02-13 12:31:54 -05:00
|
|
|
|
|
|
|
// Shutdown the link:
|
|
|
|
Link->Shutdown();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-12 14:05:55 -05:00
|
|
|
/** Binds cLuaTCPLink::StartTLSClient */
|
|
|
|
static int tolua_cTCPLink_StartTLSClient(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// LinkInstance:StartTLSClient(OwnCert, OwnPrivKey, OwnPrivKeyPassword) -> [true] or [nil, ErrMsg]
|
|
|
|
|
|
|
|
// Get the link:
|
2015-10-08 10:14:50 -04:00
|
|
|
cLuaState S(L);
|
2017-06-26 04:20:53 -04:00
|
|
|
if (!S.CheckParamSelf("cTCPLink"))
|
2015-02-12 14:05:55 -05:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-08 10:14:50 -04:00
|
|
|
auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
|
2017-06-26 04:20:53 -04:00
|
|
|
ASSERT(Link != nullptr); // Checked by CheckParamSelf()
|
2015-02-12 14:05:55 -05:00
|
|
|
|
2017-06-26 04:20:53 -04:00
|
|
|
// Read the (optional) params:
|
2015-02-12 14:05:55 -05:00
|
|
|
AString OwnCert, OwnPrivKey, OwnPrivKeyPassword;
|
|
|
|
S.GetStackValues(2, OwnCert, OwnPrivKey, OwnPrivKeyPassword);
|
|
|
|
|
|
|
|
// Start the TLS handshake:
|
|
|
|
AString res = Link->StartTLSClient(OwnCert, OwnPrivKey, OwnPrivKeyPassword);
|
|
|
|
if (!res.empty())
|
|
|
|
{
|
2016-08-16 07:02:08 -04:00
|
|
|
S.Push(cLuaState::Nil, Printf("Cannot start TLS on link to %s:%d: %s", Link->GetRemoteIP().c_str(), Link->GetRemotePort(), res.c_str()));
|
2015-02-12 14:05:55 -05:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-13 17:18:22 -05:00
|
|
|
/** Binds cLuaTCPLink::StartTLSServer */
|
|
|
|
static int tolua_cTCPLink_StartTLSServer(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// LinkInstance:StartTLSServer(OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData) -> [true] or [nil, ErrMsg]
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cTCPLink") ||
|
2015-02-13 17:18:22 -05:00
|
|
|
!S.CheckParamString(2, 4) ||
|
|
|
|
// Param 5 is optional, don't check
|
|
|
|
!S.CheckParamEnd(6)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-02-13 17:18:22 -05:00
|
|
|
// Get the link:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(Link != nullptr); // Checked by CheckParamSelf()
|
2015-02-13 17:18:22 -05:00
|
|
|
|
|
|
|
// Read the params:
|
|
|
|
AString OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData;
|
|
|
|
S.GetStackValues(2, OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData);
|
|
|
|
|
|
|
|
// Start the TLS handshake:
|
|
|
|
AString res = Link->StartTLSServer(OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData);
|
|
|
|
if (!res.empty())
|
|
|
|
{
|
2016-08-16 07:02:08 -04:00
|
|
|
S.Push(cLuaState::Nil, Printf("Cannot start TLS on link to %s:%d: %s", Link->GetRemoteIP().c_str(), Link->GetRemotePort(), res.c_str()));
|
2015-02-13 17:18:22 -05:00
|
|
|
return 2;
|
|
|
|
}
|
2016-08-16 07:02:08 -04:00
|
|
|
S.Push(true);
|
2015-02-13 17:18:22 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-30 15:24:02 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2015-02-20 08:28:05 -05:00
|
|
|
// cUDPEndpoint bindings (routed through cLuaUDPEndpoint):
|
2015-01-30 15:24:02 -05:00
|
|
|
|
|
|
|
/** Called when Lua destroys the object instance.
|
2015-02-20 08:28:05 -05:00
|
|
|
Close the endpoint and let it deallocate on its own (it's in a SharedPtr). */
|
|
|
|
static int tolua_collect_cUDPEndpoint(lua_State * L)
|
2015-01-30 15:24:02 -05:00
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
auto endpoint = static_cast<cLuaUDPEndpoint *>(tolua_tousertype(L, 1, nullptr));
|
|
|
|
ASSERT(endpoint != nullptr);
|
|
|
|
endpoint->Release();
|
2015-01-30 15:24:02 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
/** Binds cLuaUDPEndpoint::Close */
|
|
|
|
static int tolua_cUDPEndpoint_Close(lua_State * L)
|
2015-01-30 15:24:02 -05:00
|
|
|
{
|
|
|
|
// Function signature:
|
2015-02-20 08:28:05 -05:00
|
|
|
// EndpointInstance:Close()
|
2015-01-30 15:24:02 -05:00
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cUDPEndpoint") ||
|
2015-01-30 15:24:02 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
// Get the endpoint:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(endpoint != nullptr);
|
2015-01-30 15:24:02 -05:00
|
|
|
|
|
|
|
// Close it:
|
2017-06-26 04:20:53 -04:00
|
|
|
endpoint->Close();
|
2015-01-30 15:24:02 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
/** Binds cLuaUDPEndpoint::EnableBroadcasts */
|
|
|
|
static int tolua_cUDPEndpoint_EnableBroadcasts(lua_State * L)
|
2015-01-30 15:24:02 -05:00
|
|
|
{
|
|
|
|
// Function signature:
|
2015-02-20 08:28:05 -05:00
|
|
|
// EndpointInstance:EnableBroadcasts()
|
2015-01-30 15:24:02 -05:00
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cUDPEndpoint") ||
|
2015-01-30 15:24:02 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
// Get the endpoint:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(endpoint != nullptr);
|
2015-02-20 08:28:05 -05:00
|
|
|
|
|
|
|
// Enable the broadcasts:
|
2017-06-26 04:20:53 -04:00
|
|
|
endpoint->EnableBroadcasts();
|
2015-02-20 08:28:05 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Binds cLuaUDPEndpoint::GetPort */
|
|
|
|
static int tolua_cUDPEndpoint_GetPort(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// Endpoint:GetPort() -> number
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cUDPEndpoint") ||
|
2015-02-20 08:28:05 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
// Get the endpoint:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(endpoint != nullptr);
|
2015-02-20 08:28:05 -05:00
|
|
|
|
|
|
|
// Get the Port:
|
2017-06-26 04:20:53 -04:00
|
|
|
S.Push(endpoint->GetPort());
|
2015-02-20 08:28:05 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Binds cLuaUDPEndpoint::IsOpen */
|
|
|
|
static int tolua_cUDPEndpoint_IsOpen(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
|
|
|
// Endpoint:IsOpen() -> bool
|
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cUDPEndpoint") ||
|
2015-02-20 08:28:05 -05:00
|
|
|
!S.CheckParamEnd(2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
// Get the endpoint:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(endpoint != nullptr);
|
2015-01-30 15:24:02 -05:00
|
|
|
|
|
|
|
// Close it:
|
2017-06-26 04:20:53 -04:00
|
|
|
S.Push(endpoint->IsOpen());
|
2015-02-20 08:28:05 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Binds cLuaUDPEndpoint::Send */
|
|
|
|
static int tolua_cUDPEndpoint_Send(lua_State * L)
|
|
|
|
{
|
|
|
|
// Function signature:
|
2017-06-26 04:20:53 -04:00
|
|
|
// Endpoint:Send(DataString)
|
2015-02-20 08:28:05 -05:00
|
|
|
|
|
|
|
cLuaState S(L);
|
|
|
|
if (
|
2017-06-26 04:20:53 -04:00
|
|
|
!S.CheckParamSelf("cUDPEndpoint") ||
|
2015-02-20 08:28:05 -05:00
|
|
|
!S.CheckParamString(2, 3) ||
|
|
|
|
!S.CheckParamNumber(4) ||
|
|
|
|
!S.CheckParamEnd(5)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
// Get the link:
|
2017-06-26 04:20:53 -04:00
|
|
|
auto endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
|
|
|
|
ASSERT(endpoint != nullptr);
|
2015-02-20 08:28:05 -05:00
|
|
|
|
|
|
|
// Get the data to send:
|
2017-06-26 04:20:53 -04:00
|
|
|
AString data, remotePeer;
|
|
|
|
int remotePort = 0;
|
|
|
|
S.GetStackValues(2, data, remotePeer, remotePort);
|
2015-02-20 08:28:05 -05:00
|
|
|
|
|
|
|
// Check the port:
|
2017-06-26 04:20:53 -04:00
|
|
|
if ((remotePort < 0) || (remotePort > USHRT_MAX))
|
2015-02-20 08:28:05 -05:00
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return S.ApiParamError("Port number out of range (%d, range 0 - 65535)", remotePort);
|
2015-02-20 08:28:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the data:
|
2017-06-26 04:20:53 -04:00
|
|
|
S.Push(endpoint->Send(data, remotePeer, static_cast<UInt16>(remotePort)));
|
2015-01-30 15:24:02 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-23 07:20:43 -04:00
|
|
|
/** Used when the cUrlClient Lua request wants all the callbacks.
|
|
|
|
Maps each callback onto a Lua function callback in the callback table. */
|
|
|
|
class cFullUrlClientCallbacks:
|
|
|
|
public cUrlClient::cCallbacks
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Creates a new instance bound to the specified table of callbacks. */
|
|
|
|
cFullUrlClientCallbacks(cLuaState::cTableRefPtr && a_Callbacks):
|
|
|
|
m_Callbacks(std::move(a_Callbacks))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// cUrlClient::cCallbacks overrides:
|
|
|
|
virtual void OnConnected(cTCPLink & a_Link) override
|
|
|
|
{
|
|
|
|
// TODO: Cannot push a cTCPLink to Lua, need to translate via cLuaTCPLink
|
|
|
|
m_Callbacks->CallTableFnWithSelf("OnConnected", cLuaState::Nil, a_Link.GetRemoteIP(), a_Link.GetRemotePort());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual bool OnCertificateReceived() override
|
|
|
|
{
|
|
|
|
// TODO: The received cert needs proper type specification from the underlying cUrlClient framework and in the Lua engine as well
|
|
|
|
bool res = true;
|
|
|
|
m_Callbacks->CallTableFnWithSelf("OnCertificateReceived", cLuaState::Return, res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnTlsHandshakeCompleted() override
|
|
|
|
{
|
|
|
|
m_Callbacks->CallTableFnWithSelf("OnTlsHandshakeCompleted");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnRequestSent() override
|
|
|
|
{
|
|
|
|
m_Callbacks->CallTableFnWithSelf("OnRequestSent");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnStatusLine(const AString & a_HttpVersion, int a_StatusCode, const AString & a_Rest) override
|
|
|
|
{
|
|
|
|
m_Callbacks->CallTableFnWithSelf("OnStatusLine", a_HttpVersion, a_StatusCode, a_Rest);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnHeader(const AString & a_Key, const AString & a_Value) override
|
|
|
|
{
|
|
|
|
m_Callbacks->CallTableFnWithSelf("OnHeader", a_Key, a_Value);
|
|
|
|
m_Headers[a_Key] = a_Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnHeadersFinished() override
|
|
|
|
{
|
|
|
|
m_Callbacks->CallTableFnWithSelf("OnHeadersFinished", m_Headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnBodyData(const void * a_Data, size_t a_Size) override
|
|
|
|
{
|
2018-05-02 03:50:36 -04:00
|
|
|
m_Callbacks->CallTableFnWithSelf("OnBodyData", AString(static_cast<const char *>(a_Data), a_Size));
|
2016-08-23 07:20:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnBodyFinished() override
|
|
|
|
{
|
|
|
|
m_Callbacks->CallTableFnWithSelf("OnBodyFinished");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnError(const AString & a_ErrorMsg) override
|
|
|
|
{
|
|
|
|
m_Callbacks->CallTableFnWithSelf("OnError", a_ErrorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnRedirecting(const AString & a_NewLocation) override
|
|
|
|
{
|
|
|
|
m_Callbacks->CallTableFnWithSelf("OnRedirecting", a_NewLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/** The Lua table containing the callbacks. */
|
|
|
|
cLuaState::cTableRefPtr m_Callbacks;
|
|
|
|
|
|
|
|
/** Accumulator for all the headers to be reported in the OnHeadersFinished() callback. */
|
|
|
|
AStringMap m_Headers;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Used when the cUrlClient Lua request has just a single callback.
|
|
|
|
The callback is used to report the entire body at once, together with the HTTP headers, or to report an error:
|
|
|
|
callback("BodyContents", {headers})
|
|
|
|
callback(nil, "ErrorMessage")
|
|
|
|
Accumulates the body contents into a single string until the body is finished.
|
|
|
|
Accumulates all HTTP headers into an AStringMap. */
|
|
|
|
class cSimpleUrlClientCallbacks:
|
|
|
|
public cUrlClient::cCallbacks
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Creates a new instance that uses the specified callback to report when request finishes. */
|
|
|
|
cSimpleUrlClientCallbacks(cLuaState::cCallbackPtr && a_Callback):
|
|
|
|
m_Callback(std::move(a_Callback))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnHeader(const AString & a_Key, const AString & a_Value) override
|
|
|
|
{
|
|
|
|
m_Headers[a_Key] = a_Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnBodyData(const void * a_Data, size_t a_Size) override
|
|
|
|
{
|
2018-05-02 03:50:36 -04:00
|
|
|
m_ResponseBody.append(static_cast<const char *>(a_Data), a_Size);
|
2016-08-23 07:20:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnBodyFinished() override
|
|
|
|
{
|
|
|
|
m_Callback->Call(m_ResponseBody, m_Headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnError(const AString & a_ErrorMsg) override
|
|
|
|
{
|
|
|
|
m_Callback->Call(cLuaState::Nil, a_ErrorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/** The callback to call when the request finishes. */
|
|
|
|
cLuaState::cCallbackPtr m_Callback;
|
|
|
|
|
|
|
|
/** The accumulator for the partial body data, so that OnBodyFinished() can send the entire thing at once. */
|
|
|
|
AString m_ResponseBody;
|
|
|
|
|
|
|
|
/** Accumulator for all the headers to be reported in the combined callback. */
|
|
|
|
AStringMap m_Headers;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Common code shared among the cUrlClient request methods.
|
|
|
|
a_Method is the method name to be used in the request.
|
|
|
|
a_UrlStackIdx is the position on the Lua stack of the Url parameter. */
|
|
|
|
static int tolua_cUrlClient_Request_Common(lua_State * a_LuaState, const AString & a_Method, int a_UrlStackIdx)
|
|
|
|
{
|
|
|
|
// Check params:
|
|
|
|
cLuaState L(a_LuaState);
|
|
|
|
if (!L.CheckParamString(a_UrlStackIdx))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read params:
|
|
|
|
AString url, requestBody;
|
|
|
|
AStringMap headers, options;
|
|
|
|
cLuaState::cTableRefPtr callbacks;
|
|
|
|
cLuaState::cCallbackPtr onCompleteBodyCallback;
|
|
|
|
if (!L.GetStackValues(a_UrlStackIdx, url))
|
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return L.ApiParamError("Cannot read URL parameter at idx %d", a_UrlStackIdx);
|
2016-08-23 07:20:43 -04:00
|
|
|
}
|
|
|
|
cUrlClient::cCallbacksPtr urlClientCallbacks;
|
|
|
|
if (lua_istable(L, a_UrlStackIdx + 1))
|
|
|
|
{
|
|
|
|
if (!L.GetStackValue(a_UrlStackIdx + 1, callbacks))
|
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return L.ApiParamError("Cannot read the CallbacksTable parameter at idx %d", a_UrlStackIdx + 1);
|
2016-08-23 07:20:43 -04:00
|
|
|
}
|
|
|
|
urlClientCallbacks = cpp14::make_unique<cFullUrlClientCallbacks>(std::move(callbacks));
|
|
|
|
}
|
|
|
|
else if (lua_isfunction(L, a_UrlStackIdx + 1))
|
|
|
|
{
|
|
|
|
if (!L.GetStackValue(a_UrlStackIdx + 1, onCompleteBodyCallback))
|
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
return L.ApiParamError("Cannot read the CallbackFn parameter at idx %d", a_UrlStackIdx + 1);
|
2016-08-23 07:20:43 -04:00
|
|
|
}
|
|
|
|
urlClientCallbacks = cpp14::make_unique<cSimpleUrlClientCallbacks>(std::move(onCompleteBodyCallback));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
L.ApiParamError("Invalid Callbacks parameter at %d, expected a table or function, got %s", a_UrlStackIdx + 1, L.GetTypeText(a_UrlStackIdx + 1).c_str());
|
2016-08-23 07:20:43 -04:00
|
|
|
}
|
|
|
|
if (!L.GetStackValues(a_UrlStackIdx + 2, cLuaState::cOptionalParam<AStringMap>(headers), cLuaState::cOptionalParam<AString>(requestBody), cLuaState::cOptionalParam<AStringMap>(options)))
|
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
L.ApiParamError("Cannot read the Header, Body or Options parameter at idx %d, %d, %d.", a_UrlStackIdx + 2, a_UrlStackIdx + 3, a_UrlStackIdx + 4);
|
2016-08-23 07:20:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the request:
|
|
|
|
auto res = cUrlClient::Request(a_Method, url, std::move(urlClientCallbacks), std::move(headers), std::move(requestBody), std::move(options));
|
|
|
|
if (!res.first)
|
|
|
|
{
|
|
|
|
L.Push(false);
|
|
|
|
L.Push(res.second);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
L.Push(true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Implements cUrlClient:Get() using cUrlClient::Request(). */
|
|
|
|
static int tolua_cUrlClient_Delete(lua_State * a_LuaState)
|
|
|
|
{
|
|
|
|
/* Function signatures:
|
|
|
|
cUrlClient:Delete(URL, {CallbacksFnTable}, [{HeadersMapTable}], [RequestBody], [{OptionsMapTable}]) -> true / false + string
|
|
|
|
cUrlClient:Delete(URL, OnCompleteBodyCallback, [{HeadersMapTable}], [RequestBody], [{OptionsMapTable}]) -> true / false + string
|
|
|
|
*/
|
|
|
|
|
|
|
|
return tolua_cUrlClient_Request_Common(a_LuaState, "DELETE", 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Implements cUrlClient:Get() using cUrlClient::Request(). */
|
|
|
|
static int tolua_cUrlClient_Get(lua_State * a_LuaState)
|
|
|
|
{
|
|
|
|
/* Function signatures:
|
|
|
|
cUrlClient:Get(URL, {CallbacksFnTable}, [{HeadersMapTable}], [RequestBody], [{OptionsMapTable}]) -> true / false + string
|
|
|
|
cUrlClient:Get(URL, OnCompleteBodyCallback, [{HeadersMapTable}], [RequestBody], [{OptionsMapTable}]) -> true / false + string
|
|
|
|
*/
|
|
|
|
|
|
|
|
return tolua_cUrlClient_Request_Common(a_LuaState, "GET", 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Implements cUrlClient:Post() using cUrlClient::Request(). */
|
|
|
|
static int tolua_cUrlClient_Post(lua_State * a_LuaState)
|
|
|
|
{
|
|
|
|
/* Function signatures:
|
|
|
|
cUrlClient:Post(URL, {CallbacksFnTable}, [{HeadersMapTable}], [RequestBody], [{OptionsMapTable}]) -> true / false + string
|
|
|
|
cUrlClient:Post(URL, OnCompleteBodyCallback, [{HeadersMapTable}], [RequestBody], [{OptionsMapTable}]) -> true / false + string
|
|
|
|
*/
|
|
|
|
|
|
|
|
return tolua_cUrlClient_Request_Common(a_LuaState, "POST", 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Implements cUrlClient:Put() using cUrlClient::Request(). */
|
|
|
|
static int tolua_cUrlClient_Put(lua_State * a_LuaState)
|
|
|
|
{
|
|
|
|
/* Function signatures:
|
|
|
|
cUrlClient:Put(URL, {CallbacksFnTable}, [{HeadersMapTable}], [RequestBody], [{OptionsMapTable}]) -> true / false + string
|
|
|
|
cUrlClient:Put(URL, OnCompleteBodyCallback, [{HeadersMapTable}], [RequestBody], [{OptionsMapTable}]) -> true / false + string
|
|
|
|
*/
|
|
|
|
|
|
|
|
return tolua_cUrlClient_Request_Common(a_LuaState, "PUT", 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Binds cUrlClient::Request(). */
|
|
|
|
static int tolua_cUrlClient_Request(lua_State * a_LuaState)
|
|
|
|
{
|
|
|
|
/* Function signatures:
|
|
|
|
cUrlClient:Request(Method, URL, {CallbacksFnTable}, [{HeadersMapTable}], [RequestBody], [{OptionsMapTable}]) -> true / false + string
|
|
|
|
cUrlClient:Request(Method, URL, OnCompleteBodyCallback, [{HeadersMapTable}], [RequestBody], [{OptionsMapTable}]) -> true / false + string
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Check that the Method param is a string:
|
|
|
|
cLuaState L(a_LuaState);
|
2017-06-26 04:20:53 -04:00
|
|
|
if (
|
|
|
|
!L.CheckParamStaticSelf("cUrlClient") ||
|
|
|
|
!L.CheckParamString(2))
|
2016-08-23 07:20:43 -04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect the rest to the common code:
|
|
|
|
AString method;
|
|
|
|
if (!L.GetStackValue(2, method))
|
|
|
|
{
|
2017-06-26 04:20:53 -04:00
|
|
|
L.ApiParamError("Cannot read the Method parameter");
|
2016-08-23 07:20:43 -04:00
|
|
|
}
|
|
|
|
return tolua_cUrlClient_Request_Common(a_LuaState, method, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-28 09:14:05 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Register the bindings:
|
|
|
|
|
2015-05-13 05:30:57 -04:00
|
|
|
void cManualBindings::BindNetwork(lua_State * tolua_S)
|
2015-01-28 09:14:05 -05:00
|
|
|
{
|
|
|
|
// Create the cNetwork API classes:
|
|
|
|
tolua_usertype(tolua_S, "cNetwork");
|
2015-01-30 15:24:02 -05:00
|
|
|
tolua_usertype(tolua_S, "cServerHandle");
|
2015-05-13 05:30:57 -04:00
|
|
|
tolua_usertype(tolua_S, "cTCPLink");
|
2015-02-20 08:28:05 -05:00
|
|
|
tolua_usertype(tolua_S, "cUDPEndpoint");
|
2016-08-23 07:20:43 -04:00
|
|
|
tolua_usertype(tolua_S, "cUrlClient");
|
2015-05-13 05:30:57 -04:00
|
|
|
tolua_cclass(tolua_S, "cNetwork", "cNetwork", "", nullptr);
|
|
|
|
tolua_cclass(tolua_S, "cServerHandle", "cServerHandle", "", tolua_collect_cServerHandle);
|
|
|
|
tolua_cclass(tolua_S, "cTCPLink", "cTCPLink", "", nullptr);
|
|
|
|
tolua_cclass(tolua_S, "cUDPEndpoint", "cUDPEndpoint", "", tolua_collect_cUDPEndpoint);
|
2016-08-23 07:20:43 -04:00
|
|
|
tolua_cclass(tolua_S, "cUrlClient", "cUrlClient", "", nullptr);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-01-28 09:14:05 -05:00
|
|
|
// Fill in the functions (alpha-sorted):
|
|
|
|
tolua_beginmodule(tolua_S, "cNetwork");
|
2015-02-22 04:51:16 -05:00
|
|
|
tolua_function(tolua_S, "Connect", tolua_cNetwork_Connect);
|
|
|
|
tolua_function(tolua_S, "CreateUDPEndpoint", tolua_cNetwork_CreateUDPEndpoint);
|
|
|
|
tolua_function(tolua_S, "EnumLocalIPAddresses", tolua_cNetwork_EnumLocalIPAddresses);
|
|
|
|
tolua_function(tolua_S, "HostnameToIP", tolua_cNetwork_HostnameToIP);
|
|
|
|
tolua_function(tolua_S, "IPToHostname", tolua_cNetwork_IPToHostname);
|
|
|
|
tolua_function(tolua_S, "Listen", tolua_cNetwork_Listen);
|
2015-02-20 08:28:05 -05:00
|
|
|
tolua_endmodule(tolua_S);
|
|
|
|
|
|
|
|
tolua_beginmodule(tolua_S, "cServerHandle");
|
|
|
|
tolua_function(tolua_S, "Close", tolua_cServerHandle_Close);
|
|
|
|
tolua_function(tolua_S, "IsListening", tolua_cServerHandle_IsListening);
|
2015-01-28 09:14:05 -05:00
|
|
|
tolua_endmodule(tolua_S);
|
|
|
|
|
|
|
|
tolua_beginmodule(tolua_S, "cTCPLink");
|
2015-02-13 12:31:54 -05:00
|
|
|
tolua_function(tolua_S, "Close", tolua_cTCPLink_Close);
|
2015-02-12 14:05:55 -05:00
|
|
|
tolua_function(tolua_S, "GetLocalIP", tolua_cTCPLink_GetLocalIP);
|
|
|
|
tolua_function(tolua_S, "GetLocalPort", tolua_cTCPLink_GetLocalPort);
|
|
|
|
tolua_function(tolua_S, "GetRemoteIP", tolua_cTCPLink_GetRemoteIP);
|
|
|
|
tolua_function(tolua_S, "GetRemotePort", tolua_cTCPLink_GetRemotePort);
|
2015-02-13 12:31:54 -05:00
|
|
|
tolua_function(tolua_S, "Send", tolua_cTCPLink_Send);
|
|
|
|
tolua_function(tolua_S, "Shutdown", tolua_cTCPLink_Shutdown);
|
2015-02-12 14:05:55 -05:00
|
|
|
tolua_function(tolua_S, "StartTLSClient", tolua_cTCPLink_StartTLSClient);
|
2015-02-13 17:18:22 -05:00
|
|
|
tolua_function(tolua_S, "StartTLSServer", tolua_cTCPLink_StartTLSServer);
|
2015-01-28 09:14:05 -05:00
|
|
|
tolua_endmodule(tolua_S);
|
2015-01-30 15:24:02 -05:00
|
|
|
|
2015-02-20 08:28:05 -05:00
|
|
|
tolua_beginmodule(tolua_S, "cUDPEndpoint");
|
|
|
|
tolua_function(tolua_S, "Close", tolua_cUDPEndpoint_Close);
|
|
|
|
tolua_function(tolua_S, "EnableBroadcasts", tolua_cUDPEndpoint_EnableBroadcasts);
|
|
|
|
tolua_function(tolua_S, "GetPort", tolua_cUDPEndpoint_GetPort);
|
|
|
|
tolua_function(tolua_S, "IsOpen", tolua_cUDPEndpoint_IsOpen);
|
|
|
|
tolua_function(tolua_S, "Send", tolua_cUDPEndpoint_Send);
|
2015-01-30 15:24:02 -05:00
|
|
|
tolua_endmodule(tolua_S);
|
2015-02-20 08:28:05 -05:00
|
|
|
|
2016-08-23 07:20:43 -04:00
|
|
|
tolua_beginmodule(tolua_S, "cUrlClient");
|
|
|
|
tolua_function(tolua_S, "Delete", tolua_cUrlClient_Delete);
|
|
|
|
tolua_function(tolua_S, "Get", tolua_cUrlClient_Get);
|
|
|
|
tolua_function(tolua_S, "Post", tolua_cUrlClient_Post);
|
|
|
|
tolua_function(tolua_S, "Put", tolua_cUrlClient_Put);
|
|
|
|
tolua_function(tolua_S, "Request", tolua_cUrlClient_Request);
|
|
|
|
tolua_endmodule(tolua_S);
|
2015-01-28 09:14:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|