1
0

cNetwork bindings: Use ApiParamError and CheckParam(Static)Self.

This commit is contained in:
Mattes D 2017-06-26 10:20:53 +02:00
parent e05ca49459
commit 399ea0bf1d

View File

@ -30,7 +30,7 @@ static int tolua_cNetwork_Connect(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserTable(1, "cNetwork") || !S.CheckParamStaticSelf("cNetwork") ||
!S.CheckParamString(2) || !S.CheckParamString(2) ||
!S.CheckParamNumber(3) || !S.CheckParamNumber(3) ||
!S.CheckParamTable(4) || !S.CheckParamTable(4) ||
@ -46,20 +46,13 @@ static int tolua_cNetwork_Connect(lua_State * L)
cLuaState::cTableRefPtr callbacks; cLuaState::cTableRefPtr callbacks;
if (!S.GetStackValues(2, host, port, callbacks)) if (!S.GetStackValues(2, host, port, callbacks))
{ {
LOGWARNING("cNetwork::Connect() cannot read its parameters, failing the request."); return S.ApiParamError("Cannot read parameters.");
S.LogStackTrace();
S.LogStackValues("Values on the stack");
S.Push(false);
return 1;
} }
// Check validity: // Check validity:
if ((port < 0) || (port > 65535)) if ((port < 0) || (port > 65535))
{ {
LOGWARNING("cNetwork:Connect() called with invalid port (%d), failing the request.", port); return S.ApiParamError("Port number out of range (%d, range 0 - 65535)", port);
S.LogStackTrace();
S.Push(false);
return 1;
} }
ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false
@ -85,7 +78,7 @@ static int tolua_cNetwork_CreateUDPEndpoint(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserTable(1, "cNetwork") || !S.CheckParamStaticSelf("cNetwork") ||
!S.CheckParamNumber(2) || !S.CheckParamNumber(2) ||
!S.CheckParamTable(3) || !S.CheckParamTable(3) ||
!S.CheckParamEnd(4) !S.CheckParamEnd(4)
@ -95,30 +88,23 @@ static int tolua_cNetwork_CreateUDPEndpoint(lua_State * L)
} }
// Read the params: // Read the params:
UInt16 port; int port;
cLuaState::cTableRefPtr callbacks; cLuaState::cTableRefPtr callbacks;
if (!S.GetStackValues(2, port, callbacks)) if (!S.GetStackValues(2, port, callbacks))
{ {
LOGWARNING("cNetwork:CreateUDPEndpoint() cannot read its parameters, failing the request."); return S.ApiParamError("Cannot read parameters");
S.LogStackTrace();
S.LogStackValues("Values on the stack");
S.Push(false);
return 1;
} }
// Check validity: // Check validity:
if ((port < 0) || (port > 65535)) if ((port < 0) || (port > 65535))
{ {
LOGWARNING("cNetwork:CreateUDPEndpoint() called with invalid port (%d), failing the request.", port); return S.ApiParamError("Port number out of range (%d, range 0 - 65535)", port);
S.LogStackTrace();
S.Push(false);
return 1;
} }
ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false
// Create the LuaUDPEndpoint glue class: // Create the LuaUDPEndpoint glue class:
auto endpoint = std::make_shared<cLuaUDPEndpoint>(std::move(callbacks)); auto endpoint = std::make_shared<cLuaUDPEndpoint>(std::move(callbacks));
endpoint->Open(port, endpoint); endpoint->Open(static_cast<UInt16>(port), endpoint);
// Register the endpoint to be garbage-collected by Lua: // Register the endpoint to be garbage-collected by Lua:
tolua_pushusertype(L, endpoint.get(), "cUDPEndpoint"); tolua_pushusertype(L, endpoint.get(), "cUDPEndpoint");
@ -141,7 +127,7 @@ static int tolua_cNetwork_EnumLocalIPAddresses(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserTable(1, "cNetwork") || !S.CheckParamStaticSelf("cNetwork") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -165,7 +151,7 @@ static int tolua_cNetwork_HostnameToIP(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserTable(1, "cNetwork") || !S.CheckParamStaticSelf("cNetwork") ||
!S.CheckParamString(2) || !S.CheckParamString(2) ||
!S.CheckParamTable(3) || !S.CheckParamTable(3) ||
!S.CheckParamEnd(4) !S.CheckParamEnd(4)
@ -179,18 +165,13 @@ static int tolua_cNetwork_HostnameToIP(lua_State * L)
cLuaState::cTableRefPtr callbacks; cLuaState::cTableRefPtr callbacks;
if (!S.GetStackValues(2, host, callbacks)) if (!S.GetStackValues(2, host, callbacks))
{ {
LOGWARNING("cNetwork::HostnameToIP() cannot read its parameters, failing the request."); return S.ApiParamError("Cannot read parameters.");
S.LogStackTrace();
S.LogStackValues("Values on the stack");
S.Push(false);
return 1;
} }
ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false
// Try to look up: // Try to look up:
bool res = cNetwork::HostnameToIP(host, std::make_shared<cLuaNameLookup>(host, std::move(callbacks))); bool res = cNetwork::HostnameToIP(host, std::make_shared<cLuaNameLookup>(host, std::move(callbacks)));
S.Push(res); S.Push(res);
return 1; return 1;
} }
@ -206,7 +187,7 @@ static int tolua_cNetwork_IPToHostname(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserTable(1, "cNetwork") || !S.CheckParamStaticSelf("cNetwork") ||
!S.CheckParamString(2) || !S.CheckParamString(2) ||
!S.CheckParamTable(3) || !S.CheckParamTable(3) ||
!S.CheckParamEnd(4) !S.CheckParamEnd(4)
@ -220,18 +201,13 @@ static int tolua_cNetwork_IPToHostname(lua_State * L)
cLuaState::cTableRefPtr callbacks; cLuaState::cTableRefPtr callbacks;
if (!S.GetStackValues(2, ip, callbacks)) if (!S.GetStackValues(2, ip, callbacks))
{ {
LOGWARNING("cNetwork::IPToHostname() cannot read its parameters, failing the request."); return S.ApiParamError("Cannot read parameters.");
S.LogStackTrace();
S.LogStackValues("Values on the stack");
S.Push(false);
return 1;
} }
ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false ASSERT(callbacks != nullptr); // Invalid callbacks would have resulted in GetStackValues() returning false
// Try to look up: // Try to look up:
bool res = cNetwork::IPToHostName(ip, std::make_shared<cLuaNameLookup>(ip, std::move(callbacks))); bool res = cNetwork::IPToHostName(ip, std::make_shared<cLuaNameLookup>(ip, std::move(callbacks)));
S.Push(res); S.Push(res);
return 1; return 1;
} }
@ -247,7 +223,7 @@ static int tolua_cNetwork_Listen(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserTable(1, "cNetwork") || !S.CheckParamStaticSelf("cNetwork") ||
!S.CheckParamNumber(2) || !S.CheckParamNumber(2) ||
!S.CheckParamTable(3) || !S.CheckParamTable(3) ||
!S.CheckParamEnd(4) !S.CheckParamEnd(4)
@ -261,20 +237,13 @@ static int tolua_cNetwork_Listen(lua_State * L)
cLuaState::cTableRefPtr callbacks; cLuaState::cTableRefPtr callbacks;
if (!S.GetStackValues(2, port, callbacks)) if (!S.GetStackValues(2, port, callbacks))
{ {
LOGWARNING("cNetwork::Listen() cannot read its parameters, failing the request."); return S.ApiParamError("Cannot read parameters");
S.LogStackTrace();
S.LogStackValues("Values on the stack");
S.Push(false);
return 1;
} }
// Check the validity: // Check the validity:
if ((port < 0) || (port > 65535)) if ((port < 0) || (port > 65535))
{ {
LOGWARNING("cNetwork:Listen() called with invalid port (%d), failing the request.", port); return S.ApiParamError("Port number out of range (%d, range 0 - 65535)", port);
S.LogStackTrace();
S.Push(false);
return 1;
} }
auto port16 = static_cast<UInt16>(port); auto port16 = static_cast<UInt16>(port);
@ -304,7 +273,8 @@ static int tolua_cNetwork_Listen(lua_State * L)
Close the server and let it deallocate on its own (it's in a SharedPtr). */ Close the server and let it deallocate on its own (it's in a SharedPtr). */
static int tolua_collect_cServerHandle(lua_State * L) static int tolua_collect_cServerHandle(lua_State * L)
{ {
cLuaServerHandle * Srv = static_cast<cLuaServerHandle *>(tolua_tousertype(L, 1, nullptr)); auto Srv = static_cast<cLuaServerHandle *>(tolua_tousertype(L, 1, nullptr));
ASSERT(Srv != nullptr);
Srv->Release(); Srv->Release();
return 0; return 0;
} }
@ -321,7 +291,7 @@ static int tolua_cServerHandle_Close(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cServerHandle") || !S.CheckParamSelf("cServerHandle") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -329,14 +299,8 @@ static int tolua_cServerHandle_Close(lua_State * L)
} }
// Get the server handle: // Get the server handle:
cLuaServerHandle * Srv; auto Srv = *static_cast<cLuaServerHandle **>(lua_touserdata(L, 1));
if (lua_isnil(L, 1)) ASSERT(Srv != nullptr); // Checked by CheckParamSelf()
{
LOGWARNING("cServerHandle:Close(): invalid server handle object. Stack trace:");
S.LogStackTrace();
return 0;
}
Srv = *static_cast<cLuaServerHandle **>(lua_touserdata(L, 1));
// Close it: // Close it:
Srv->Close(); Srv->Close();
@ -355,7 +319,7 @@ static int tolua_cServerHandle_IsListening(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cServerHandle") || !S.CheckParamSelf("cServerHandle") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -363,16 +327,10 @@ static int tolua_cServerHandle_IsListening(lua_State * L)
} }
// Get the server handle: // Get the server handle:
cLuaServerHandle * Srv; auto Srv = *static_cast<cLuaServerHandle **>(lua_touserdata(L, 1));
if (lua_isnil(L, 1)) ASSERT(Srv != nullptr); // Checked by CheckParamSelf()
{
LOGWARNING("cServerHandle:IsListening(): invalid server handle object. Stack trace:");
S.LogStackTrace();
return 0;
}
Srv = *static_cast<cLuaServerHandle **>(lua_touserdata(L, 1));
// Close it: // Query it:
S.Push(Srv->IsListening()); S.Push(Srv->IsListening());
return 1; return 1;
} }
@ -392,7 +350,7 @@ static int tolua_cTCPLink_Close(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cTCPLink") || !S.CheckParamSelf("cTCPLink") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -400,16 +358,10 @@ static int tolua_cTCPLink_Close(lua_State * L)
} }
// Get the link: // Get the link:
cLuaTCPLink * Link; auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
if (lua_isnil(L, 1)) ASSERT(Link != nullptr); // Checked by CheckParamSelf()
{
LOGWARNING("cTCPLink:Close(): invalid link object. Stack trace:");
S.LogStackTrace();
return 0;
}
Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
// CLose the link: // Close the link:
Link->Close(); Link->Close();
return 0; return 0;
} }
@ -426,7 +378,7 @@ static int tolua_cTCPLink_GetLocalIP(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cTCPLink") || !S.CheckParamSelf("cTCPLink") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -434,14 +386,8 @@ static int tolua_cTCPLink_GetLocalIP(lua_State * L)
} }
// Get the link: // Get the link:
cLuaTCPLink * Link; auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
if (lua_isnil(L, 1)) ASSERT(Link != nullptr); // Checked by CheckParamSelf()
{
LOGWARNING("cTCPLink:GetLocalIP(): invalid link object. Stack trace:");
S.LogStackTrace();
return 0;
}
Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
// Get the IP: // Get the IP:
S.Push(Link->GetLocalIP()); S.Push(Link->GetLocalIP());
@ -460,7 +406,7 @@ static int tolua_cTCPLink_GetLocalPort(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cTCPLink") || !S.CheckParamSelf("cTCPLink") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -468,14 +414,8 @@ static int tolua_cTCPLink_GetLocalPort(lua_State * L)
} }
// Get the link: // Get the link:
cLuaTCPLink * Link; auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
if (lua_isnil(L, 1)) ASSERT(Link != nullptr); // Checked by CheckParamSelf()
{
LOGWARNING("cTCPLink:GetLocalPort(): invalid link object. Stack trace:");
S.LogStackTrace();
return 0;
}
Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
// Get the Port: // Get the Port:
S.Push(Link->GetLocalPort()); S.Push(Link->GetLocalPort());
@ -494,7 +434,7 @@ static int tolua_cTCPLink_GetRemoteIP(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cTCPLink") || !S.CheckParamSelf("cTCPLink") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -502,14 +442,8 @@ static int tolua_cTCPLink_GetRemoteIP(lua_State * L)
} }
// Get the link: // Get the link:
cLuaTCPLink * Link; auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
if (lua_isnil(L, 1)) ASSERT(Link != nullptr); // Checked by CheckParamSelf()
{
LOGWARNING("cTCPLink:GetRemoteIP(): invalid link object. Stack trace:");
S.LogStackTrace();
return 0;
}
Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
// Get the IP: // Get the IP:
S.Push(Link->GetRemoteIP()); S.Push(Link->GetRemoteIP());
@ -528,7 +462,7 @@ static int tolua_cTCPLink_GetRemotePort(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cTCPLink") || !S.CheckParamSelf("cTCPLink") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -536,14 +470,8 @@ static int tolua_cTCPLink_GetRemotePort(lua_State * L)
} }
// Get the link: // Get the link:
cLuaTCPLink * Link; auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
if (lua_isnil(L, 1)) ASSERT(Link != nullptr); // Checked by CheckParamSelf()
{
LOGWARNING("cTCPLink:GetRemotePort(): invalid link object. Stack trace:");
S.LogStackTrace();
return 0;
}
Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
// Get the Port: // Get the Port:
S.Push(Link->GetRemotePort()); S.Push(Link->GetRemotePort());
@ -562,7 +490,7 @@ static int tolua_cTCPLink_Send(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cTCPLink") || !S.CheckParamSelf("cTCPLink") ||
!S.CheckParamString(2) || !S.CheckParamString(2) ||
!S.CheckParamEnd(3) !S.CheckParamEnd(3)
) )
@ -571,14 +499,8 @@ static int tolua_cTCPLink_Send(lua_State * L)
} }
// Get the link: // Get the link:
cLuaTCPLink * Link; auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
if (lua_isnil(L, 1)) ASSERT(Link != nullptr); // Checked by CheckParamSelf()
{
LOGWARNING("cTCPLink:Send(): invalid link object. Stack trace:");
S.LogStackTrace();
return 0;
}
Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
// Get the data to send: // Get the data to send:
AString Data; AString Data;
@ -601,7 +523,7 @@ static int tolua_cTCPLink_Shutdown(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cTCPLink") || !S.CheckParamSelf("cTCPLink") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -609,14 +531,8 @@ static int tolua_cTCPLink_Shutdown(lua_State * L)
} }
// Get the link: // Get the link:
cLuaTCPLink * Link; auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
if (lua_isnil(L, 1)) ASSERT(Link != nullptr); // Checked by CheckParamSelf()
{
LOGWARNING("cTCPLink:Shutdown(): invalid link object. Stack trace:");
S.LogStackTrace();
return 0;
}
Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
// Shutdown the link: // Shutdown the link:
Link->Shutdown(); Link->Shutdown();
@ -635,15 +551,14 @@ static int tolua_cTCPLink_StartTLSClient(lua_State * L)
// Get the link: // Get the link:
cLuaState S(L); cLuaState S(L);
if (lua_isnil(L, 1)) if (!S.CheckParamSelf("cTCPLink"))
{ {
LOGWARNING("cTCPLink:StartTLSClient(): invalid link object. Stack trace:");
S.LogStackTrace();
return 0; return 0;
} }
auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1)); auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
ASSERT(Link != nullptr); // Checked by CheckParamSelf()
// Read the params: // Read the (optional) params:
AString OwnCert, OwnPrivKey, OwnPrivKeyPassword; AString OwnCert, OwnPrivKey, OwnPrivKeyPassword;
S.GetStackValues(2, OwnCert, OwnPrivKey, OwnPrivKeyPassword); S.GetStackValues(2, OwnCert, OwnPrivKey, OwnPrivKeyPassword);
@ -669,7 +584,7 @@ static int tolua_cTCPLink_StartTLSServer(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cTCPLink") || !S.CheckParamSelf("cTCPLink") ||
!S.CheckParamString(2, 4) || !S.CheckParamString(2, 4) ||
// Param 5 is optional, don't check // Param 5 is optional, don't check
!S.CheckParamEnd(6) !S.CheckParamEnd(6)
@ -679,14 +594,8 @@ static int tolua_cTCPLink_StartTLSServer(lua_State * L)
} }
// Get the link: // Get the link:
cLuaTCPLink * Link; auto Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
if (lua_isnil(L, 1)) ASSERT(Link != nullptr); // Checked by CheckParamSelf()
{
LOGWARNING("cTCPLink:StartTLSServer(): invalid link object. Stack trace:");
S.LogStackTrace();
return 0;
}
Link = *static_cast<cLuaTCPLink **>(lua_touserdata(L, 1));
// Read the params: // Read the params:
AString OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData; AString OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData;
@ -714,9 +623,9 @@ static int tolua_cTCPLink_StartTLSServer(lua_State * L)
Close the endpoint and let it deallocate on its own (it's in a SharedPtr). */ Close the endpoint and let it deallocate on its own (it's in a SharedPtr). */
static int tolua_collect_cUDPEndpoint(lua_State * L) static int tolua_collect_cUDPEndpoint(lua_State * L)
{ {
LOGD("Lua: Collecting cUDPEndpoint"); auto endpoint = static_cast<cLuaUDPEndpoint *>(tolua_tousertype(L, 1, nullptr));
cLuaUDPEndpoint * Endpoint = static_cast<cLuaUDPEndpoint *>(tolua_tousertype(L, 1, nullptr)); ASSERT(endpoint != nullptr);
Endpoint->Release(); endpoint->Release();
return 0; return 0;
} }
@ -732,7 +641,7 @@ static int tolua_cUDPEndpoint_Close(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cUDPEndpoint") || !S.CheckParamSelf("cUDPEndpoint") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -740,16 +649,11 @@ static int tolua_cUDPEndpoint_Close(lua_State * L)
} }
// Get the endpoint: // Get the endpoint:
if (lua_isnil(L, 1)) auto endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
{ ASSERT(endpoint != nullptr);
LOGWARNING("cUDPEndpoint:Close(): invalid endpoint object. Stack trace:");
S.LogStackTrace();
return 0;
}
auto Endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
// Close it: // Close it:
Endpoint->Close(); endpoint->Close();
return 0; return 0;
} }
@ -765,7 +669,7 @@ static int tolua_cUDPEndpoint_EnableBroadcasts(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cUDPEndpoint") || !S.CheckParamSelf("cUDPEndpoint") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -773,16 +677,11 @@ static int tolua_cUDPEndpoint_EnableBroadcasts(lua_State * L)
} }
// Get the endpoint: // Get the endpoint:
if (lua_isnil(L, 1)) auto endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
{ ASSERT(endpoint != nullptr);
LOGWARNING("cUDPEndpoint:EnableBroadcasts(): invalid endpoint object. Stack trace:");
S.LogStackTrace();
return 0;
}
auto Endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
// Enable the broadcasts: // Enable the broadcasts:
Endpoint->EnableBroadcasts(); endpoint->EnableBroadcasts();
return 0; return 0;
} }
@ -798,7 +697,7 @@ static int tolua_cUDPEndpoint_GetPort(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cUDPEndpoint") || !S.CheckParamSelf("cUDPEndpoint") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -806,16 +705,11 @@ static int tolua_cUDPEndpoint_GetPort(lua_State * L)
} }
// Get the endpoint: // Get the endpoint:
if (lua_isnil(L, 1)) auto endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
{ ASSERT(endpoint != nullptr);
LOGWARNING("cUDPEndpoint:GetPort(): invalid endpoint object. Stack trace:");
S.LogStackTrace();
return 0;
}
auto Endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
// Get the Port: // Get the Port:
S.Push(Endpoint->GetPort()); S.Push(endpoint->GetPort());
return 1; return 1;
} }
@ -831,7 +725,7 @@ static int tolua_cUDPEndpoint_IsOpen(lua_State * L)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cUDPEndpoint") || !S.CheckParamSelf("cUDPEndpoint") ||
!S.CheckParamEnd(2) !S.CheckParamEnd(2)
) )
{ {
@ -839,16 +733,11 @@ static int tolua_cUDPEndpoint_IsOpen(lua_State * L)
} }
// Get the endpoint: // Get the endpoint:
if (lua_isnil(L, 1)) auto endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
{ ASSERT(endpoint != nullptr);
LOGWARNING("cUDPEndpoint:IsListening(): invalid server handle object. Stack trace:");
S.LogStackTrace();
return 0;
}
auto Endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
// Close it: // Close it:
S.Push(Endpoint->IsOpen()); S.Push(endpoint->IsOpen());
return 1; return 1;
} }
@ -860,11 +749,11 @@ static int tolua_cUDPEndpoint_IsOpen(lua_State * L)
static int tolua_cUDPEndpoint_Send(lua_State * L) static int tolua_cUDPEndpoint_Send(lua_State * L)
{ {
// Function signature: // Function signature:
// LinkInstance:Send(DataString) // Endpoint:Send(DataString)
cLuaState S(L); cLuaState S(L);
if ( if (
!S.CheckParamUserType(1, "cUDPEndpoint") || !S.CheckParamSelf("cUDPEndpoint") ||
!S.CheckParamString(2, 3) || !S.CheckParamString(2, 3) ||
!S.CheckParamNumber(4) || !S.CheckParamNumber(4) ||
!S.CheckParamEnd(5) !S.CheckParamEnd(5)
@ -874,30 +763,22 @@ static int tolua_cUDPEndpoint_Send(lua_State * L)
} }
// Get the link: // Get the link:
if (lua_isnil(L, 1)) auto endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
{ ASSERT(endpoint != nullptr);
LOGWARNING("cUDPEndpoint:Send(): invalid endpoint object. Stack trace:");
S.LogStackTrace();
return 0;
}
auto Endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
// Get the data to send: // Get the data to send:
AString Data, RemotePeer; AString data, remotePeer;
int RemotePort = 0; int remotePort = 0;
S.GetStackValues(2, Data, RemotePeer, RemotePort); S.GetStackValues(2, data, remotePeer, remotePort);
// Check the port: // Check the port:
if ((RemotePort < 0) || (RemotePort > USHRT_MAX)) if ((remotePort < 0) || (remotePort > USHRT_MAX))
{ {
LOGWARNING("cUDPEndpoint:Send() called with invalid port (%d), failing.", RemotePort); return S.ApiParamError("Port number out of range (%d, range 0 - 65535)", remotePort);
S.LogStackTrace();
S.Push(false);
return 1;
} }
// Send the data: // Send the data:
S.Push(Endpoint->Send(Data, RemotePeer, static_cast<UInt16>(RemotePort))); S.Push(endpoint->Send(data, remotePeer, static_cast<UInt16>(remotePort)));
return 1; return 1;
} }
@ -1078,20 +959,14 @@ static int tolua_cUrlClient_Request_Common(lua_State * a_LuaState, const AString
cLuaState::cCallbackPtr onCompleteBodyCallback; cLuaState::cCallbackPtr onCompleteBodyCallback;
if (!L.GetStackValues(a_UrlStackIdx, url)) if (!L.GetStackValues(a_UrlStackIdx, url))
{ {
L.LogApiCallParamFailure("cUrlClient:Request()", Printf("URL (%d)", a_UrlStackIdx).c_str()); return L.ApiParamError("Cannot read URL parameter at idx %d", a_UrlStackIdx);
L.Push(false);
L.Push("Invalid params");
return 2;
} }
cUrlClient::cCallbacksPtr urlClientCallbacks; cUrlClient::cCallbacksPtr urlClientCallbacks;
if (lua_istable(L, a_UrlStackIdx + 1)) if (lua_istable(L, a_UrlStackIdx + 1))
{ {
if (!L.GetStackValue(a_UrlStackIdx + 1, callbacks)) if (!L.GetStackValue(a_UrlStackIdx + 1, callbacks))
{ {
L.LogApiCallParamFailure("cUrlClient:Request()", Printf("CallbacksTable (%d)", a_UrlStackIdx + 1).c_str()); return L.ApiParamError("Cannot read the CallbacksTable parameter at idx %d", a_UrlStackIdx + 1);
L.Push(false);
L.Push("Invalid Callbacks param");
return 2;
} }
urlClientCallbacks = cpp14::make_unique<cFullUrlClientCallbacks>(std::move(callbacks)); urlClientCallbacks = cpp14::make_unique<cFullUrlClientCallbacks>(std::move(callbacks));
} }
@ -1099,26 +974,17 @@ static int tolua_cUrlClient_Request_Common(lua_State * a_LuaState, const AString
{ {
if (!L.GetStackValue(a_UrlStackIdx + 1, onCompleteBodyCallback)) if (!L.GetStackValue(a_UrlStackIdx + 1, onCompleteBodyCallback))
{ {
L.LogApiCallParamFailure("cUrlClient:Request()", Printf("CallbacksFn (%d)", a_UrlStackIdx + 1).c_str()); return L.ApiParamError("Cannot read the CallbackFn parameter at idx %d", a_UrlStackIdx + 1);
L.Push(false);
L.Push("Invalid OnCompleteBodyCallback param");
return 2;
} }
urlClientCallbacks = cpp14::make_unique<cSimpleUrlClientCallbacks>(std::move(onCompleteBodyCallback)); urlClientCallbacks = cpp14::make_unique<cSimpleUrlClientCallbacks>(std::move(onCompleteBodyCallback));
} }
else else
{ {
L.LogApiCallParamFailure("cUrlClient:Request()", Printf("Callbacks (%d)", a_UrlStackIdx + 1).c_str()); L.ApiParamError("Invalid Callbacks parameter at %d, expected a table or function, got %s", a_UrlStackIdx + 1, L.GetTypeText(a_UrlStackIdx + 1).c_str());
L.Push(false);
L.Push("Invalid OnCompleteBodyCallback param");
return 2;
} }
if (!L.GetStackValues(a_UrlStackIdx + 2, cLuaState::cOptionalParam<AStringMap>(headers), cLuaState::cOptionalParam<AString>(requestBody), cLuaState::cOptionalParam<AStringMap>(options))) if (!L.GetStackValues(a_UrlStackIdx + 2, cLuaState::cOptionalParam<AStringMap>(headers), cLuaState::cOptionalParam<AString>(requestBody), cLuaState::cOptionalParam<AStringMap>(options)))
{ {
L.LogApiCallParamFailure("cUrlClient:Request()", Printf("Header, Body or Options (%d, %d, %d)", a_UrlStackIdx + 2, a_UrlStackIdx + 3, a_UrlStackIdx + 4).c_str()); L.ApiParamError("Cannot read the Header, Body or Options parameter at idx %d, %d, %d.", a_UrlStackIdx + 2, a_UrlStackIdx + 3, a_UrlStackIdx + 4);
L.Push(false);
L.Push("Invalid params");
return 2;
} }
// Make the request: // Make the request:
@ -1207,7 +1073,9 @@ static int tolua_cUrlClient_Request(lua_State * a_LuaState)
// Check that the Method param is a string: // Check that the Method param is a string:
cLuaState L(a_LuaState); cLuaState L(a_LuaState);
if (!L.CheckParamString(2)) if (
!L.CheckParamStaticSelf("cUrlClient") ||
!L.CheckParamString(2))
{ {
return 0; return 0;
} }
@ -1216,10 +1084,7 @@ static int tolua_cUrlClient_Request(lua_State * a_LuaState)
AString method; AString method;
if (!L.GetStackValue(2, method)) if (!L.GetStackValue(2, method))
{ {
L.LogApiCallParamFailure("cUrlClient:Request", "Method (2)"); L.ApiParamError("Cannot read the Method parameter");
L.Push(cLuaState::Nil);
L.Push("Invalid params");
return 2;
} }
return tolua_cUrlClient_Request_Common(a_LuaState, method, 3); return tolua_cUrlClient_Request_Common(a_LuaState, method, 3);
} }