summaryrefslogtreecommitdiffstats
path: root/src/Bindings/LuaUDPEndpoint.h
blob: 37dd92178cf56f4f219bbd1a657902808e7a8994 (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

// 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 "LuaState.h"





// fwd:
class cLuaUDPEndpoint;
typedef std::shared_ptr<cLuaUDPEndpoint> cLuaUDPEndpointPtr;





class cLuaUDPEndpoint : public cUDPEndpoint::cCallbacks
{
  public:
	/** Creates a new instance of the endpoint, wrapping the callbacks that are in the specified table. */
	cLuaUDPEndpoint(cLuaState::cTableRefPtr && a_Callbacks);

	virtual ~cLuaUDPEndpoint() override;

	/** 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 Lua table that holds the callbacks to be invoked. */
	cLuaState::cTableRefPtr 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;
};