summaryrefslogtreecommitdiffstats
path: root/src/Bindings/LuaServerHandle.cpp
blob: 9cc8ad350963f2d93e453ebd36d670460edee5a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

// LuaServerHandle.cpp

// Implements the cLuaServerHandle class wrapping the cServerHandle functionality for Lua API

#include "Globals.h"
#include "LuaServerHandle.h"
#include "LuaTCPLink.h"
#include "tolua++/include/tolua++.h"





cLuaServerHandle::cLuaServerHandle(UInt16 a_Port, cPluginLua & a_Plugin, int a_CallbacksTableStackPos):
	m_Plugin(a_Plugin),
	m_Callbacks(cPluginLua::cOperation(a_Plugin)(), a_CallbacksTableStackPos),
	m_Port(a_Port)
{
}






cLuaServerHandle::~cLuaServerHandle()
{
	// If the server handle is still open, close it explicitly:
	Close();
}





void cLuaServerHandle::SetServerHandle(cServerHandlePtr a_ServerHandle, cLuaServerHandlePtr a_Self)
{
	ASSERT(m_ServerHandle == nullptr);  // The handle can be set only once

	m_ServerHandle = a_ServerHandle;
	m_Self = a_Self;
}





void cLuaServerHandle::Close(void)
{
	// Safely grab a copy of the server handle:
	cServerHandlePtr ServerHandle = m_ServerHandle;
	if (ServerHandle == nullptr)
	{
		return;
	}

	// Close the underlying server handle:
	ServerHandle->Close();

	// Close all connections at this server:
	cLuaTCPLinkPtrs Connections;
	{
		cCSLock Lock(m_CSConnections);
		std::swap(Connections, m_Connections);
	}
	for (auto & conn: Connections)
	{
		conn->Close();
	}
	Connections.clear();

	// Allow the internal server handle to be deallocated:
	m_ServerHandle.reset();
}





bool cLuaServerHandle::IsListening(void)
{
	// Safely grab a copy of the server handle:
	cServerHandlePtr ServerHandle = m_ServerHandle;
	if (ServerHandle == nullptr)
	{
		return false;
	}

	// Query the underlying server handle:
	return ServerHandle->IsListening();
}





void cLuaServerHandle::RemoveLink(cLuaTCPLink * a_Link)
{
	cCSLock Lock(m_CSConnections);
	for (auto itr = m_Connections.begin(), end = m_Connections.end(); itr != end; ++itr)
	{
		if (itr->get() == a_Link)
		{
			m_Connections.erase(itr);
			break;
		}
	}  // for itr - m_Connections[]
}





void cLuaServerHandle::Release(void)
{
	// Close the server, if it isn't closed yet:
	Close();

	// Allow self to deallocate:
	m_Self.reset();
}





cTCPLink::cCallbacksPtr cLuaServerHandle::OnIncomingConnection(const AString & a_RemoteIPAddress, UInt16 a_RemotePort)
{
	// If not valid anymore, drop the connection:
	if (!m_Callbacks.IsValid())
	{
		return nullptr;
	}

	// Ask the plugin for link callbacks:
	cPluginLua::cOperation Op(m_Plugin);
	cLuaState::cRef LinkCallbacks;
	if (
		!Op().Call(cLuaState::cTableRef(m_Callbacks, "OnIncomingConnection"), a_RemoteIPAddress, a_RemotePort, m_Port, cLuaState::Return, LinkCallbacks) ||
		!LinkCallbacks.IsValid()
	)
	{
		LOGINFO("cNetwork server (port %d) OnIncomingConnection callback failed in plugin %s. Dropping connection.",
			m_Port, m_Plugin.GetName().c_str()
		);
		return nullptr;
	}

	// Create the link wrapper to use with the callbacks:
	auto res = std::make_shared<cLuaTCPLink>(m_Plugin, std::move(LinkCallbacks), m_Self);

	// Add the link to the list of our connections:
	cCSLock Lock(m_CSConnections);
	m_Connections.push_back(res);

	return res;
}





void cLuaServerHandle::OnAccepted(cTCPLink & a_Link)
{
	// Check if we're still valid:
	if (!m_Callbacks.IsValid())
	{
		return;
	}

	// Notify the plugin:
	cPluginLua::cOperation Op(m_Plugin);
	if (!Op().Call(cLuaState::cTableRef(m_Callbacks, "OnAccepted"), static_cast<cLuaTCPLink *>(a_Link.GetCallbacks().get())))
	{
		LOGINFO("cNetwork server (port %d) OnAccepted callback failed in plugin %s, connection to %s:%d.",
			m_Port, m_Plugin.GetName().c_str(), a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()
		);
		return;
	}
}





void cLuaServerHandle::OnError(int a_ErrorCode, const AString & a_ErrorMsg)
{
	// Check if we're still valid:
	if (!m_Callbacks.IsValid())
	{
		return;
	}

	// Notify the plugin:
	cPluginLua::cOperation Op(m_Plugin);
	if (!Op().Call(cLuaState::cTableRef(m_Callbacks, "OnError"), a_ErrorCode, a_ErrorMsg))
	{
		LOGINFO("cNetwork server (port %d) OnError callback failed in plugin %s. The error is %d (%s).",
			m_Port, m_Plugin.GetName().c_str(), a_ErrorCode, a_ErrorMsg.c_str()
		);
		return;
	}
}