1
0
cuberite-2a/src/Bindings/LuaUDPEndpoint.h

87 lines
2.4 KiB
C
Raw Normal View History

2015-02-20 08:28:05 -05:00
// LuaUDPEndpoint.h
// Declares the cLuaUDPEndpoint class representing a Lua wrapper for the cUDPEndpoint class and the callbacks it needs
#pragma once
#include "../OSSupport/Network.h"
#include "PluginLua.h"
// fwd:
class cLuaUDPEndpoint;
typedef SharedPtr<cLuaUDPEndpoint> cLuaUDPEndpointPtr;
class cLuaUDPEndpoint:
public cUDPEndpoint::cCallbacks
{
public:
/** Creates a new instance of the endpoint, attached to the specified plugin and wrapping the callbacks that are in a table at the specified stack pos. */
cLuaUDPEndpoint(cPluginLua & a_Plugin, int a_CallbacksTableStackPos);
~cLuaUDPEndpoint();
/** Opens the endpoint so that it starts listening for incoming data on the specified port.
a_Self is the shared pointer to self that the object keeps to keep itself alive for as long as it needs (for Lua). */
bool Open(UInt16 a_Port, cLuaUDPEndpointPtr a_Self);
/** Sends the data contained in the string to the specified remote peer.
Returns true if successful, false on immediate failure (queueing the data failed etc.) */
bool Send(const AString & a_Data, const AString & a_RemotePeer, UInt16 a_RemotePort);
/** Returns the port on which endpoint is listening for incoming data. */
UInt16 GetPort(void) const;
/** Returns true if the endpoint is open for incoming data. */
bool IsOpen(void) const;
/** Closes the UDP listener. */
void Close(void);
/** Enables outgoing broadcasts to be made using this endpoint. */
void EnableBroadcasts(void);
/** Called when Lua garbage-collects the object.
Releases the internal SharedPtr to self, so that the instance may be deallocated. */
void Release(void);
protected:
/** The plugin for which the link is created. */
cPluginLua & m_Plugin;
/** The Lua table that holds the callbacks to be invoked. */
cLuaState::cRef m_Callbacks;
/** SharedPtr to self, so that the object can keep itself alive for as long as it needs (for Lua). */
cLuaUDPEndpointPtr m_Self;
/** The underlying network endpoint.
May be nullptr. */
cUDPEndpointPtr m_Endpoint;
/** Common code called when the endpoint is considered as terminated.
Releases m_Endpoint and m_Callbacks, each when applicable. */
void Terminated(void);
// cUDPEndpoint::cCallbacks overrides:
virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override;
virtual void OnReceivedData(const char * a_Data, size_t a_Size, const AString & a_RemotePeer, UInt16 a_RemotePort) override;
};