8 #include "ManualBindings.h"
9 #include "tolua++/include/tolua++.h"
15 #include "../HTTP/UrlClient.h"
52 if ((port < 0) || (port > 65535))
54 return S.
ApiParamError(fmt::format(FMT_STRING(
"Port number out of range (got {}, range 0 - 65535)"), port));
56 ASSERT(callbacks !=
nullptr);
59 auto link = std::make_shared<cLuaTCPLink>(std::move(callbacks));
98 if ((port < 0) || (port > 65535))
100 return S.
ApiParamError(fmt::format(FMT_STRING(
"Port number out of range (got {}, range 0 - 65535)"), port));
102 ASSERT(callbacks !=
nullptr);
105 auto endpoint = std::make_shared<cLuaUDPEndpoint>(std::move(callbacks));
106 endpoint->Open(
static_cast<UInt16>(port), endpoint);
109 tolua_pushusertype(L, endpoint.get(),
"cUDPEndpoint");
110 tolua_register_gc(L, lua_gettop(L));
113 S.
Push(endpoint.get());
169 ASSERT(callbacks !=
nullptr);
205 ASSERT(callbacks !=
nullptr);
243 if ((port < 0) || (port > 65535))
245 return S.
ApiParamError(fmt::format(FMT_STRING(
"Port number out of range (got {}, range 0 - 65535)"), port));
247 auto port16 =
static_cast<UInt16>(port);
250 auto srv = std::make_shared<cLuaServerHandle>(port16, std::move(callbacks));
256 tolua_pushusertype(L, srv.get(),
"cServerHandle");
257 tolua_register_gc(L, lua_gettop(L));
275 auto Srv =
static_cast<cLuaServerHandle *
>(tolua_tousertype(L, 1,
nullptr));
333 S.
Push(Srv->IsListening());
360 auto Link = *
static_cast<cLuaTCPLink **
>(lua_touserdata(L, 1));
388 auto Link = *
static_cast<cLuaTCPLink **
>(lua_touserdata(L, 1));
392 S.
Push(Link->GetLocalIP());
416 auto Link = *
static_cast<cLuaTCPLink **
>(lua_touserdata(L, 1));
420 S.
Push(Link->GetLocalPort());
444 auto Link = *
static_cast<cLuaTCPLink **
>(lua_touserdata(L, 1));
448 S.
Push(Link->GetRemoteIP());
472 auto Link = *
static_cast<cLuaTCPLink **
>(lua_touserdata(L, 1));
476 S.
Push(Link->GetRemotePort());
501 auto Link = *
static_cast<cLuaTCPLink **
>(lua_touserdata(L, 1));
533 auto Link = *
static_cast<cLuaTCPLink **
>(lua_touserdata(L, 1));
557 auto Link = *
static_cast<cLuaTCPLink **
>(lua_touserdata(L, 1));
561 AString OwnCert, OwnPrivKey, OwnPrivKeyPassword, TrustedRootCAs;
565 AString res = Link->StartTLSClient(OwnCert, OwnPrivKey, OwnPrivKeyPassword, TrustedRootCAs);
569 FMT_STRING(
"Cannot start TLS on link to {}:{}: {}"),
570 Link->GetRemoteIP(), Link->GetRemotePort(), res
599 auto Link = *
static_cast<cLuaTCPLink **
>(lua_touserdata(L, 1));
603 AString OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData;
604 S.
GetStackValues(2, OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData);
607 AString res = Link->StartTLSServer(OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData);
611 FMT_STRING(
"Cannot start TLS on link to {}:{}: {}"),
612 Link->GetRemoteIP(), Link->GetRemotePort(), res
631 auto endpoint =
static_cast<cLuaUDPEndpoint *
>(tolua_tousertype(L, 1,
nullptr));
632 ASSERT(endpoint !=
nullptr);
657 auto endpoint = *
static_cast<cLuaUDPEndpoint **
>(lua_touserdata(L, 1));
658 ASSERT(endpoint !=
nullptr);
685 auto endpoint = *
static_cast<cLuaUDPEndpoint **
>(lua_touserdata(L, 1));
686 ASSERT(endpoint !=
nullptr);
689 endpoint->EnableBroadcasts();
713 auto endpoint = *
static_cast<cLuaUDPEndpoint **
>(lua_touserdata(L, 1));
714 ASSERT(endpoint !=
nullptr);
717 S.
Push(endpoint->GetPort());
741 auto endpoint = *
static_cast<cLuaUDPEndpoint **
>(lua_touserdata(L, 1));
742 ASSERT(endpoint !=
nullptr);
745 S.
Push(endpoint->IsOpen());
771 auto endpoint = *
static_cast<cLuaUDPEndpoint **
>(lua_touserdata(L, 1));
772 ASSERT(endpoint !=
nullptr);
780 if ((remotePort < 0) || (remotePort > USHRT_MAX))
782 return S.
ApiParamError(fmt::format(FMT_STRING(
"Port number out of range (got {}, range 0 - 65535)"), remotePort));
786 S.
Push(endpoint->Send(data, remotePeer,
static_cast<UInt16>(remotePort)));
826 m_Callbacks->CallTableFnWithSelf(
"OnTlsHandshakeCompleted");
838 m_Callbacks->CallTableFnWithSelf(
"OnStatusLine", a_HttpVersion, a_StatusCode, a_Rest);
844 m_Callbacks->CallTableFnWithSelf(
"OnHeader", a_Key, a_Value);
855 virtual void OnBodyData(
const void * a_Data,
size_t a_Size)
override
857 m_Callbacks->CallTableFnWithSelf(
"OnBodyData",
AString(
static_cast<const char *
>(a_Data), a_Size));
863 m_Callbacks->CallTableFnWithSelf(
"OnBodyFinished");
869 m_Callbacks->CallTableFnWithSelf(
"OnError", a_ErrorMsg);
875 m_Callbacks->CallTableFnWithSelf(
"OnRedirecting", a_NewLocation);
914 virtual void OnBodyData(
const void * a_Data,
size_t a_Size)
override
967 return L.
ApiParamError(fmt::format(FMT_STRING(
"Cannot read URL parameter at idx {}"), a_UrlStackIdx));
970 if (lua_istable(L, a_UrlStackIdx + 1))
974 return L.
ApiParamError(fmt::format(FMT_STRING(
"Cannot read the CallbacksTable parameter at idx {}"), a_UrlStackIdx + 1));
976 urlClientCallbacks = std::make_unique<cFullUrlClientCallbacks>(std::move(callbacks));
978 else if (lua_isfunction(L, a_UrlStackIdx + 1))
980 if (!L.
GetStackValue(a_UrlStackIdx + 1, onCompleteBodyCallback))
982 return L.
ApiParamError(fmt::format(FMT_STRING(
"Cannot read the CallbackFn parameter at idx {}"), a_UrlStackIdx + 1));
984 urlClientCallbacks = std::make_unique<cSimpleUrlClientCallbacks>(std::move(onCompleteBodyCallback));
989 FMT_STRING(
"Invalid Callbacks parameter at {}, expected a table or function, got {}"),
990 a_UrlStackIdx + 1, L.
GetTypeText(a_UrlStackIdx + 1)
996 FMT_STRING(
"Cannot read the Header, Body or Options parameter at idx {}, {}, {}."),
997 a_UrlStackIdx + 2, a_UrlStackIdx + 3, a_UrlStackIdx + 4
1002 auto res =
cUrlClient::Request(a_Method, url, std::move(urlClientCallbacks), std::move(headers), std::move(requestBody), std::move(options));
1110 void cManualBindings::BindNetwork(lua_State * tolua_S)
1113 tolua_usertype(tolua_S,
"cNetwork");
1114 tolua_usertype(tolua_S,
"cServerHandle");
1115 tolua_usertype(tolua_S,
"cTCPLink");
1116 tolua_usertype(tolua_S,
"cUDPEndpoint");
1117 tolua_usertype(tolua_S,
"cUrlClient");
1118 tolua_cclass(tolua_S,
"cNetwork",
"cNetwork",
"",
nullptr);
1120 tolua_cclass(tolua_S,
"cTCPLink",
"cTCPLink",
"",
nullptr);
1122 tolua_cclass(tolua_S,
"cUrlClient",
"cUrlClient",
"",
nullptr);
1125 tolua_beginmodule(tolua_S,
"cNetwork");
1132 tolua_endmodule(tolua_S);
1134 tolua_beginmodule(tolua_S,
"cServerHandle");
1137 tolua_endmodule(tolua_S);
1139 tolua_beginmodule(tolua_S,
"cTCPLink");
1149 tolua_endmodule(tolua_S);
1151 tolua_beginmodule(tolua_S,
"cUDPEndpoint");
1157 tolua_endmodule(tolua_S);
1159 tolua_beginmodule(tolua_S,
"cUrlClient");
1165 tolua_endmodule(tolua_S);
static int tolua_cTCPLink_Send(lua_State *L)
Binds cLuaTCPLink::Send.
static int tolua_cUDPEndpoint_Send(lua_State *L)
Binds cLuaUDPEndpoint::Send.
static int tolua_cUrlClient_Put(lua_State *a_LuaState)
Implements cUrlClient:Put() using cUrlClient::Request().
static int tolua_cTCPLink_GetRemotePort(lua_State *L)
Binds cLuaTCPLink::GetRemotePort.
static int tolua_cTCPLink_GetRemoteIP(lua_State *L)
Binds cLuaTCPLink::GetRemoteIP.
static int tolua_cTCPLink_Shutdown(lua_State *L)
Binds cLuaTCPLink::Shutdown.
static int tolua_cTCPLink_StartTLSServer(lua_State *L)
Binds cLuaTCPLink::StartTLSServer.
static int tolua_cUDPEndpoint_EnableBroadcasts(lua_State *L)
Binds cLuaUDPEndpoint::EnableBroadcasts.
static int tolua_cUrlClient_Request(lua_State *a_LuaState)
Binds cUrlClient::Request().
static int tolua_cNetwork_HostnameToIP(lua_State *L)
Binds cNetwork::HostnameToIP.
static int tolua_collect_cServerHandle(lua_State *L)
Called when Lua destroys the object instance.
static int tolua_cServerHandle_Close(lua_State *L)
Binds cLuaServerHandle::Close.
static int tolua_cTCPLink_GetLocalIP(lua_State *L)
Binds cLuaTCPLink::GetLocalIP.
static int tolua_cUDPEndpoint_Close(lua_State *L)
Binds cLuaUDPEndpoint::Close.
static int tolua_cTCPLink_Close(lua_State *L)
Binds cLuaTCPLink::Close.
static int tolua_cNetwork_Listen(lua_State *L)
Binds cNetwork::Listen.
static int tolua_cUrlClient_Get(lua_State *a_LuaState)
Implements cUrlClient:Get() using cUrlClient::Request().
static int tolua_cTCPLink_StartTLSClient(lua_State *L)
Binds cLuaTCPLink::StartTLSClient.
static int tolua_cNetwork_Connect(lua_State *L)
Binds cNetwork::Connect.
static int tolua_cUrlClient_Request_Common(lua_State *a_LuaState, const AString &a_Method, int a_UrlStackIdx)
Common code shared among the cUrlClient request methods.
static int tolua_collect_cUDPEndpoint(lua_State *L)
Called when Lua destroys the object instance.
static int tolua_cUDPEndpoint_GetPort(lua_State *L)
Binds cLuaUDPEndpoint::GetPort.
static int tolua_cTCPLink_GetLocalPort(lua_State *L)
Binds cLuaTCPLink::GetLocalPort.
static int tolua_cServerHandle_IsListening(lua_State *L)
Binds cLuaServerHandle::IsListening.
static int tolua_cUDPEndpoint_IsOpen(lua_State *L)
Binds cLuaUDPEndpoint::IsOpen.
static int tolua_cNetwork_EnumLocalIPAddresses(lua_State *L)
Binds cNetwork::EnumLocalIPAddresses.
static int tolua_cUrlClient_Post(lua_State *a_LuaState)
Implements cUrlClient:Post() using cUrlClient::Request().
static int tolua_cUrlClient_Delete(lua_State *a_LuaState)
Implements cUrlClient:Get() using cUrlClient::Request().
static int tolua_cNetwork_CreateUDPEndpoint(lua_State *L)
Binds cNetwork::CreateUDPEndpoint.
static int tolua_cNetwork_IPToHostname(lua_State *L)
Binds cNetwork::IPToHostname.
std::map< AString, AString > AStringMap
A string dictionary, used for key-value pairs.
Encapsulates a Lua state and provides some syntactic sugar for common operations.
void Push(Arg1 &&a_Arg1, Arg2 &&a_Arg2, Args &&... a_Args)
Pushes multiple arguments onto the Lua stack.
std::unique_ptr< cCallback > cCallbackPtr
bool CheckParamEnd(int a_Param)
Returns true if the specified parameter on the stack is nil (indicating an end-of-parameters)
bool CheckParamSelf(const char *a_SelfClassName)
Returns true if the first parameter is an instance of the expected class name.
std::unique_ptr< cTableRef > cTableRefPtr
bool CheckParamString(int a_StartParam, int a_EndParam=-1)
Returns true if the specified parameters on the stack are strings; also logs warning if not.
bool CheckParamNumber(int a_StartParam, int a_EndParam=-1)
Returns true if the specified parameters on the stack are numbers; also logs warning if not.
AString GetTypeText(int a_StackPos)
Returns the type of the item on the specified position in the stack.
bool CheckParamTable(int a_StartParam, int a_EndParam=-1)
Returns true if the specified parameters on the stack are tables; also logs warning if not.
bool CheckParamStaticSelf(const char *a_SelfClassName)
Returns true if the first parameter is the expected class (static).
bool GetStackValue(int a_StackPos, AString &a_Value)
int ApiParamError(std::string_view a_Msg)
Prints the message, prefixed with the current function name, then logs the stack contents and raises ...
bool GetStackValues(int a_StartStackPos, Arg1 &&a_Arg1, Args &&... args)
Retrieves a list of values from the Lua stack, starting at the specified index.
Represents a parameter that is optional - calling a GetStackValue() with this object will not fail if...
Used when the cUrlClient Lua request wants all the callbacks.
virtual void OnHeadersFinished() override
Called when the HTTP headers have been fully parsed, unless the response is an allowed redirect.
virtual void OnBodyFinished() override
Called after the response body has been fully reported by OnBody() calls, unless the response is an a...
cFullUrlClientCallbacks(cLuaState::cTableRefPtr &&a_Callbacks)
Creates a new instance bound to the specified table of callbacks.
AStringMap m_Headers
Accumulator for all the headers to be reported in the OnHeadersFinished() callback.
virtual void OnRedirecting(const AString &a_NewLocation) override
Called when a redirect is to be followed.
virtual bool OnCertificateReceived() override
Called for TLS connections, when the server certificate is received.
virtual void OnConnected(cTCPLink &a_Link) override
Called when the TCP connection is established.
virtual void OnHeader(const AString &a_Key, const AString &a_Value) override
Called when a single HTTP header is received and parsed, unless the response is an allowed redirect C...
virtual void OnRequestSent() override
Called after the entire request has been sent to the remote peer.
virtual void OnStatusLine(const AString &a_HttpVersion, int a_StatusCode, const AString &a_Rest) override
Called after the first line of the response is parsed, unless the response is an allowed redirect.
cLuaState::cTableRefPtr m_Callbacks
The Lua table containing the callbacks.
virtual void OnBodyData(const void *a_Data, size_t a_Size) override
Called when the next fragment of the response body is received, unless the response is an allowed red...
virtual void OnError(const AString &a_ErrorMsg) override
Called when an asynchronous error is encountered.
virtual void OnTlsHandshakeCompleted() override
Called for TLS connections, when the TLS handshake has been completed.
Used when the cUrlClient Lua request has just a single callback.
virtual void OnBodyFinished() override
Called after the response body has been fully reported by OnBody() calls, unless the response is an a...
virtual void OnHeader(const AString &a_Key, const AString &a_Value) override
Called when a single HTTP header is received and parsed, unless the response is an allowed redirect C...
cSimpleUrlClientCallbacks(cLuaState::cCallbackPtr &&a_Callback)
Creates a new instance that uses the specified callback to report when request finishes.
AString m_ResponseBody
The accumulator for the partial body data, so that OnBodyFinished() can send the entire thing at once...
AStringMap m_Headers
Accumulator for all the headers to be reported in the combined callback.
virtual void OnError(const AString &a_ErrorMsg) override
Called when an asynchronous error is encountered.
cLuaState::cCallbackPtr m_Callback
The callback to call when the request finishes.
virtual void OnBodyData(const void *a_Data, size_t a_Size) override
Called when the next fragment of the response body is received, unless the response is an allowed red...
std::shared_ptr< cCallbacks > cCallbacksPtr
static std::pair< bool, AString > Request(const AString &a_Method, const AString &a_URL, cCallbacksPtr &&a_Callbacks, AStringMap &&a_Headers, const AString &a_Body, const AStringMap &a_Options)
Makes a network request to the specified URL, using the specified method (if applicable).
Callbacks that are used for progress and result reporting.
Interface that provides the methods available on a single TCP connection.
virtual UInt16 GetRemotePort(void) const =0
Returns the port used by the remote endpoint of the connection.
virtual AString GetRemoteIP(void) const =0
Returns the IP address of the remote endpoint of the connection.
static bool Connect(const AString &a_Host, UInt16 a_Port, cConnectCallbacksPtr a_ConnectCallbacks, cTCPLink::cCallbacksPtr a_LinkCallbacks)
Queues a TCP connection to be made to the specified host.
static AStringVector EnumLocalIPAddresses(void)
Returns all local IP addresses for network interfaces currently available.
static cServerHandlePtr Listen(UInt16 a_Port, cListenCallbacksPtr a_ListenCallbacks)
Opens up the specified port for incoming connections.
static bool HostnameToIP(const AString &a_Hostname, cResolveNameCallbacksPtr a_Callbacks)
Queues a DNS query to resolve the specified hostname to IP address.
static bool IPToHostName(const AString &a_IP, cResolveNameCallbacksPtr a_Callbacks)
Queues a DNS query to resolve the specified IP address to a hostname.