summaryrefslogtreecommitdiffstats
path: root/src/Bindings/LuaTCPLink.cpp
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2015-01-30 21:24:02 +0100
committerMattes D <github@xoft.cz>2015-02-04 08:40:52 +0100
commit014b96adb33fa902072d9f35661bc4f5e7c323e8 (patch)
tree46dcb57a65829d6364cbabec7add4021b9008ee6 /src/Bindings/LuaTCPLink.cpp
parentcNetwork: Exported lookup functions to Lua API. (diff)
downloadcuberite-014b96adb33fa902072d9f35661bc4f5e7c323e8.tar
cuberite-014b96adb33fa902072d9f35661bc4f5e7c323e8.tar.gz
cuberite-014b96adb33fa902072d9f35661bc4f5e7c323e8.tar.bz2
cuberite-014b96adb33fa902072d9f35661bc4f5e7c323e8.tar.lz
cuberite-014b96adb33fa902072d9f35661bc4f5e7c323e8.tar.xz
cuberite-014b96adb33fa902072d9f35661bc4f5e7c323e8.tar.zst
cuberite-014b96adb33fa902072d9f35661bc4f5e7c323e8.zip
Diffstat (limited to 'src/Bindings/LuaTCPLink.cpp')
-rw-r--r--src/Bindings/LuaTCPLink.cpp97
1 files changed, 86 insertions, 11 deletions
diff --git a/src/Bindings/LuaTCPLink.cpp b/src/Bindings/LuaTCPLink.cpp
index f88aeff84..6b8395806 100644
--- a/src/Bindings/LuaTCPLink.cpp
+++ b/src/Bindings/LuaTCPLink.cpp
@@ -5,6 +5,7 @@
#include "Globals.h"
#include "LuaTCPLink.h"
+#include "LuaServerHandle.h"
@@ -14,6 +15,47 @@ cLuaTCPLink::cLuaTCPLink(cPluginLua & a_Plugin, int a_CallbacksTableStackPos):
m_Plugin(a_Plugin),
m_Callbacks(a_Plugin.GetLuaState(), a_CallbacksTableStackPos)
{
+ // Warn if the callbacks aren't valid:
+ if (!m_Callbacks.IsValid())
+ {
+ LOGWARNING("cTCPLink in plugin %s: callbacks could not be retrieved", m_Plugin.GetName().c_str());
+ cPluginLua::cOperation Op(m_Plugin);
+ Op().LogStackTrace();
+ }
+}
+
+
+
+
+
+cLuaTCPLink::cLuaTCPLink(cPluginLua & a_Plugin, cLuaState::cRef && a_CallbacksTableRef, cLuaServerHandleWPtr a_ServerHandle):
+ m_Plugin(a_Plugin),
+ m_Callbacks(std::move(a_CallbacksTableRef)),
+ m_Server(std::move(a_ServerHandle))
+{
+ // Warn if the callbacks aren't valid:
+ if (!m_Callbacks.IsValid())
+ {
+ LOGWARNING("cTCPLink in plugin %s: callbacks could not be retrieved", m_Plugin.GetName().c_str());
+ cPluginLua::cOperation Op(m_Plugin);
+ Op().LogStackTrace();
+ }
+}
+
+
+
+
+
+cLuaTCPLink::~cLuaTCPLink()
+{
+ // If the link is still open, close it:
+ cTCPLinkPtr Link = m_Link;
+ if (Link != nullptr)
+ {
+ Link->Close();
+ }
+
+ Terminated();
}
@@ -107,15 +149,14 @@ UInt16 cLuaTCPLink::GetRemotePort(void) const
void cLuaTCPLink::Shutdown(void)
{
- // Safely grab a copy of the link:
+ // Safely grab a copy of the link and shut it down:
cTCPLinkPtr Link = m_Link;
- if (Link == nullptr)
+ if (Link != nullptr)
{
- return;
+ Link->Shutdown();
}
- // Shutdown:
- Link->Shutdown();
+ Terminated();
}
@@ -124,17 +165,48 @@ void cLuaTCPLink::Shutdown(void)
void cLuaTCPLink::Close(void)
{
- // Safely grab a copy of the link:
+ // If the link is still open, close it:
cTCPLinkPtr Link = m_Link;
- if (Link == nullptr)
+ if (Link != nullptr)
{
- return;
+ Link->Close();
+ }
+
+ Terminated();
+}
+
+
+
+
+
+void cLuaTCPLink::Terminated(void)
+{
+ // Disable the callbacks:
+ if (m_Callbacks.IsValid())
+ {
+ m_Callbacks.UnRef();
+ }
+
+ // If the managing server is still alive, let it know we're terminating:
+ auto Server = m_Server.lock();
+ if (Server != nullptr)
+ {
+ Server->RemoveLink(this);
}
- // Close the link:
- Link->Close();
+ // If the link is still open, close it:
+ cTCPLinkPtr Link = m_Link;
+ if (Link != nullptr)
+ {
+ Link->Close();
+ m_Link.reset();
+ }
}
+
+
+
+
void cLuaTCPLink::OnConnected(cTCPLink & a_Link)
{
// Check if we're still valid:
@@ -171,6 +243,8 @@ void cLuaTCPLink::OnError(int a_ErrorCode, const AString & a_ErrorMsg)
m_Plugin.GetName().c_str(), a_ErrorCode, a_ErrorMsg.c_str()
);
}
+
+ Terminated();
}
@@ -221,7 +295,8 @@ void cLuaTCPLink::OnRemoteClosed(void)
{
LOGINFO("cTCPLink OnRemoteClosed() callback failed in plugin %s.", m_Plugin.GetName().c_str());
}
- m_Link.reset();
+
+ Terminated();
}