diff options
author | Mattes D <github@xoft.cz> | 2015-02-21 12:19:03 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2015-02-21 12:19:03 +0100 |
commit | 9971670b4062acbae2f7fba57996f0c1e67c7601 (patch) | |
tree | 4d3de89abd8ece59ebc30b4fec48089070a76eb3 /src/Bindings/LuaUDPEndpoint.h | |
parent | Fixed monster spawn randomness. (diff) | |
parent | APIDump: Added the UDP zero port policy. (diff) | |
download | cuberite-9971670b4062acbae2f7fba57996f0c1e67c7601.tar cuberite-9971670b4062acbae2f7fba57996f0c1e67c7601.tar.gz cuberite-9971670b4062acbae2f7fba57996f0c1e67c7601.tar.bz2 cuberite-9971670b4062acbae2f7fba57996f0c1e67c7601.tar.lz cuberite-9971670b4062acbae2f7fba57996f0c1e67c7601.tar.xz cuberite-9971670b4062acbae2f7fba57996f0c1e67c7601.tar.zst cuberite-9971670b4062acbae2f7fba57996f0c1e67c7601.zip |
Diffstat (limited to 'src/Bindings/LuaUDPEndpoint.h')
-rw-r--r-- | src/Bindings/LuaUDPEndpoint.h | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/Bindings/LuaUDPEndpoint.h b/src/Bindings/LuaUDPEndpoint.h new file mode 100644 index 000000000..0587491ab --- /dev/null +++ b/src/Bindings/LuaUDPEndpoint.h @@ -0,0 +1,86 @@ + +// 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; +}; + + + + |