Cuberite
A lightweight, fast and extensible game server for Minecraft
LuaServerHandle.cpp
Go to the documentation of this file.
1 
2 // LuaServerHandle.cpp
3 
4 // Implements the cLuaServerHandle class wrapping the cServerHandle functionality for Lua API
5 
6 #include "Globals.h"
7 #include "LuaServerHandle.h"
8 #include "LuaTCPLink.h"
9 #include "tolua++/include/tolua++.h"
10 
11 
12 
13 
14 
16  m_Callbacks(std::move(a_Callbacks)),
17  m_Port(a_Port)
18 {
19 }
20 
21 
22 
23 
24 
26 {
27  // If the server handle is still open, close it explicitly:
28  Close();
29 }
30 
31 
32 
33 
34 
36 {
37  ASSERT(m_ServerHandle == nullptr); // The handle can be set only once
38 
39  m_ServerHandle = std::move(a_ServerHandle);
40  m_Self = std::move(a_Self);
41 }
42 
43 
44 
45 
46 
48 {
49  // Safely grab a copy of the server handle:
50  cServerHandlePtr ServerHandle = m_ServerHandle;
51  if (ServerHandle == nullptr)
52  {
53  return;
54  }
55 
56  // Close the underlying server handle:
57  ServerHandle->Close();
58 
59  // Close all connections at this server:
60  cLuaTCPLinkPtrs Connections;
61  {
63  std::swap(Connections, m_Connections);
64  }
65  for (auto & conn: Connections)
66  {
67  conn->Close();
68  }
69  Connections.clear();
70 
71  // Allow the internal server handle to be deallocated:
72  m_ServerHandle.reset();
73 }
74 
75 
76 
77 
78 
80 {
81  // Safely grab a copy of the server handle:
82  cServerHandlePtr ServerHandle = m_ServerHandle;
83  if (ServerHandle == nullptr)
84  {
85  return false;
86  }
87 
88  // Query the underlying server handle:
89  return ServerHandle->IsListening();
90 }
91 
92 
93 
94 
95 
97 {
99  for (auto itr = m_Connections.begin(), end = m_Connections.end(); itr != end; ++itr)
100  {
101  if (itr->get() == a_Link)
102  {
103  m_Connections.erase(itr);
104  break;
105  }
106  } // for itr - m_Connections[]
107 }
108 
109 
110 
111 
112 
114 {
115  // Close the server, if it isn't closed yet:
116  Close();
117 
118  // Allow self to deallocate:
119  m_Self.reset();
120 }
121 
122 
123 
124 
125 
127 {
128  // Ask the plugin for link callbacks:
129  cLuaState::cTableRefPtr LinkCallbacks;
130  if (
131  !m_Callbacks->CallTableFn("OnIncomingConnection", a_RemoteIPAddress, a_RemotePort, m_Port, cLuaState::Return, LinkCallbacks) ||
132  !LinkCallbacks->IsValid()
133  )
134  {
135  LOGINFO("cNetwork server (port %d) OnIncomingConnection callback failed. Dropping connection.", m_Port);
136  return nullptr;
137  }
138 
139  // Create the link wrapper to use with the callbacks:
140  auto res = std::make_shared<cLuaTCPLink>(std::move(LinkCallbacks), m_Self);
141 
142  // Add the link to the list of our connections:
143  cCSLock Lock(m_CSConnections);
144  m_Connections.push_back(res);
145 
146  return res;
147 }
148 
149 
150 
151 
152 
154 {
155  // Notify the plugin:
156  m_Callbacks->CallTableFn("OnAccepted", static_cast<cLuaTCPLink *>(a_Link.GetCallbacks().get()));
157 }
158 
159 
160 
161 
162 
163 void cLuaServerHandle::OnError(int a_ErrorCode, const AString & a_ErrorMsg)
164 {
165  // Notify the plugin:
166  m_Callbacks->CallTableFn("OnError", a_ErrorCode, a_ErrorMsg);
167 }
168 
169 
170 
171 
172 
std::shared_ptr< cLuaServerHandle > cLuaServerHandlePtr
std::vector< cLuaTCPLinkPtr > cLuaTCPLinkPtrs
#define ASSERT(x)
Definition: Globals.h:276
unsigned short UInt16
Definition: Globals.h:158
void LOGINFO(std::string_view a_Format, const Args &... args)
Definition: LoggerSimple.h:61
std::shared_ptr< cServerHandle > cServerHandlePtr
Definition: Network.h:28
std::string AString
Definition: StringUtils.h:11
Definition: FastNBT.h:132
void SetServerHandle(cServerHandlePtr a_ServerHandle, cLuaServerHandlePtr a_Self)
Called by cNetwork::Listen()'s binding.
cLuaServerHandle(UInt16 a_Port, cLuaState::cTableRefPtr &&a_Callbacks)
Creates a new instance of the server handle, wrapping the (listen-) callbacks that are in the specifi...
bool IsListening(void)
Returns true if the server is currently listening.
void Close(void)
Terminates all connections and closes the listening socket.
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg) override
Called when the socket fails to listen on the specified port.
void Release(void)
Called when Lua garbage-collects the object.
cServerHandlePtr m_ServerHandle
The cServerHandle around which this instance is wrapped.
virtual cTCPLink::cCallbacksPtr OnIncomingConnection(const AString &a_RemoteIPAddress, UInt16 a_RemotePort) override
Called when the TCP server created with Listen() receives a new incoming connection.
void RemoveLink(cLuaTCPLink *a_Link)
Removes the link from the list of links that the server is currently tracking.
cLuaServerHandlePtr m_Self
SharedPtr to self, given out to newly created links.
cLuaTCPLinkPtrs m_Connections
All connections that are currently active in this server.
cCriticalSection m_CSConnections
Protects m_Connections against multithreaded access.
cLuaState::cTableRefPtr m_Callbacks
The Lua table that holds the callbacks to be invoked.
UInt16 m_Port
The port on which the server is listening.
virtual void OnAccepted(cTCPLink &a_Link) override
Called when the TCP server created with Listen() creates a new link for an incoming connection.
virtual ~cLuaServerHandle() override
static const cRet Return
Definition: LuaState.h:445
std::unique_ptr< cTableRef > cTableRefPtr
Definition: LuaState.h:419
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
Interface that provides the methods available on a single TCP connection.
Definition: Network.h:42
cCallbacksPtr GetCallbacks(void) const
Returns the callbacks that are used.
Definition: Network.h:135
std::shared_ptr< cCallbacks > cCallbacksPtr
Definition: Network.h:71