Cuberite
A lightweight, fast and extensible game server for Minecraft
LuaUDPEndpoint.cpp
Go to the documentation of this file.
1 
2 // LuaUDPEndpoint.cpp
3 
4 // Implements the cLuaUDPEndpoint class representing a Lua wrapper for the cUDPEndpoint class and the callbacks it needs
5 
6 #include "Globals.h"
7 #include "LuaUDPEndpoint.h"
8 
9 
10 
11 
12 
14  m_Callbacks(std::move(a_Callbacks))
15 {
16 }
17 
18 
19 
20 
21 
23 {
24  // If the endpoint is still open, close it:
25  auto endpoint = m_Endpoint;
26  if (endpoint != nullptr)
27  {
28  endpoint->Close();
29  }
30 
31  Terminated();
32 }
33 
34 
35 
36 
37 
39 {
40  ASSERT(m_Self == nullptr); // Must not be opened yet
41  ASSERT(m_Endpoint == nullptr);
42 
43  m_Self = std::move(a_Self);
44  m_Endpoint = cNetwork::CreateUDPEndpoint(a_Port, *this);
45  return m_Endpoint->IsOpen();
46 }
47 
48 
49 
50 
51 
52 bool cLuaUDPEndpoint::Send(const AString & a_Data, const AString & a_RemotePeer, UInt16 a_RemotePort)
53 {
54  // Safely grab a copy of the endpoint:
55  auto endpoint = m_Endpoint;
56  if (endpoint == nullptr)
57  {
58  return false;
59  }
60 
61  // Send the data:
62  return endpoint->Send(a_Data, a_RemotePeer, a_RemotePort);
63 }
64 
65 
66 
67 
68 
70 {
71  // Safely grab a copy of the endpoint:
72  auto endpoint = m_Endpoint;
73  if (endpoint == nullptr)
74  {
75  return 0;
76  }
77 
78  // Get the port:
79  return endpoint->GetPort();
80 }
81 
82 
83 
84 
85 
86 bool cLuaUDPEndpoint::IsOpen(void) const
87 {
88  // Safely grab a copy of the endpoint:
89  auto endpoint = m_Endpoint;
90  if (endpoint == nullptr)
91  {
92  // No endpoint means that we're not open
93  return false;
94  }
95 
96  // Get the state:
97  return endpoint->IsOpen();
98 }
99 
100 
101 
102 
103 
105 {
106  // If the endpoint is still open, close it:
107  auto endpoint = m_Endpoint;
108  if (endpoint != nullptr)
109  {
110  endpoint->Close();
111  m_Endpoint.reset();
112  }
113 
114  Terminated();
115 }
116 
117 
118 
119 
120 
122 {
123  // Safely grab a copy of the endpoint:
124  auto endpoint = m_Endpoint;
125  if (endpoint != nullptr)
126  {
127  endpoint->EnableBroadcasts();
128  }
129 }
130 
131 
132 
133 
134 
136 {
137  // Close the endpoint, if not already closed:
138  Close();
139 
140  // Allow self to deallocate:
141  m_Self.reset();
142 }
143 
144 
145 
146 
147 
149 {
150  // Disable the callbacks:
151  m_Callbacks.reset();
152 
153  // If the endpoint is still open, close it:
154  {
155  auto endpoint = m_Endpoint;
156  if (endpoint != nullptr)
157  {
158  endpoint->Close();
159  m_Endpoint.reset();
160  }
161  }
162 }
163 
164 
165 
166 
167 
168 void cLuaUDPEndpoint::OnReceivedData(const char * a_Data, size_t a_NumBytes, const AString & a_RemotePeer, UInt16 a_RemotePort)
169 {
170  m_Callbacks->CallTableFn("OnReceivedData", this, AString(a_Data, a_NumBytes), a_RemotePeer, a_RemotePort);
171 }
172 
173 
174 
175 
176 
177 void cLuaUDPEndpoint::OnError(int a_ErrorCode, const AString & a_ErrorMsg)
178 {
179  // Notify the plugin:
180  m_Callbacks->CallTableFn("OnError", a_ErrorCode, a_ErrorMsg);
181 
182  // Terminate all processing on the endpoint:
183  Terminated();
184 }
185 
186 
187 
188 
std::shared_ptr< cLuaUDPEndpoint > cLuaUDPEndpointPtr
#define ASSERT(x)
Definition: Globals.h:276
unsigned short UInt16
Definition: Globals.h:158
std::string AString
Definition: StringUtils.h:11
Definition: FastNBT.h:132
std::unique_ptr< cTableRef > cTableRefPtr
Definition: LuaState.h:419
cLuaState::cTableRefPtr m_Callbacks
The Lua table that holds the callbacks to be invoked.
virtual void OnReceivedData(const char *a_Data, size_t a_Size, const AString &a_RemotePeer, UInt16 a_RemotePort) override
Called when there is an incoming datagram from a remote host.
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg) override
Called when an error occurs on the endpoint.
cLuaUDPEndpointPtr m_Self
SharedPtr to self, so that the object can keep itself alive for as long as it needs (for Lua).
virtual ~cLuaUDPEndpoint() override
void Terminated(void)
Common code called when the endpoint is considered as terminated.
void Close(void)
Closes the UDP listener.
cUDPEndpointPtr m_Endpoint
The underlying network endpoint.
void EnableBroadcasts(void)
Enables outgoing broadcasts to be made using this endpoint.
bool IsOpen(void) const
Returns true if the endpoint is open for incoming data.
UInt16 GetPort(void) const
Returns the port on which endpoint is listening for incoming data.
void Release(void)
Called when Lua garbage-collects the object.
cLuaUDPEndpoint(cLuaState::cTableRefPtr &&a_Callbacks)
Creates a new instance of the endpoint, wrapping the callbacks that are in the specified table.
bool Open(UInt16 a_Port, cLuaUDPEndpointPtr a_Self)
Opens the endpoint so that it starts listening for incoming data on the specified port.
bool Send(const AString &a_Data, const AString &a_RemotePeer, UInt16 a_RemotePort)
Sends the data contained in the string to the specified remote peer.
static cUDPEndpointPtr CreateUDPEndpoint(UInt16 a_Port, cUDPEndpoint::cCallbacks &a_Callbacks)
Opens up an UDP endpoint for sending and receiving UDP datagrams on the specified port.