summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/UDPEndpointImpl.h
blob: a9c49b5062442afae02a5aa69bc47f1b2292b1ed (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

// UDPEndpointImpl.h

// Declares the cUDPEndpointImpl class representing an implementation of an endpoint in UDP communication





#pragma once

#include "Network.h"
#include <event2/event.h>





// fwd:
class cUDPEndpointImpl;
typedef std::shared_ptr<cUDPEndpointImpl> cUDPEndpointImplPtr;





class cUDPEndpointImpl : public cUDPEndpoint
{
	using Super = cUDPEndpoint;

  public:
	/** Creates a new instance of the endpoint, with the specified callbacks.
	Tries to open on the specified port; if it fails, the endpoint is left in the "closed" state.
	If a_Port is 0, the OS is free to assign any port number it likes to the endpoint. */
	cUDPEndpointImpl(UInt16 a_Port, cUDPEndpoint::cCallbacks & a_Callbacks);

	virtual ~cUDPEndpointImpl() override;

	// cUDPEndpoint overrides:
	virtual void Close(void) override;
	virtual bool IsOpen(void) const override;
	virtual UInt16 GetPort(void) const override;
	virtual bool Send(const AString & a_Payload, const AString & a_Host, UInt16 a_Port) override;
	virtual void EnableBroadcasts(void) override;

  protected:
	/** The local port on which the endpoint is open.
	If this is zero, it means the endpoint is closed - either opening has failed, or it has been closed explicitly. */
	UInt16 m_Port;

	/** The primary underlying OS socket. */
	evutil_socket_t m_MainSock;

	/** True if m_MainSock is in the IPv6 namespace (needs IPv6 addresses for sending). */
	bool m_IsMainSockIPv6;

	/** The secondary OS socket (if primary doesn't support dualstack). */
	evutil_socket_t m_SecondarySock;

	/** The LibEvent handle for the primary socket. */
	event * m_MainEvent;

	/** The LibEvent handle for the secondary socket. */
	event * m_SecondaryEvent;


	/** Creates and opens the socket on the specified port.
	If a_Port is 0, the OS is free to assign any port number it likes to the endpoint.
	If the opening fails, the OnError() callback is called and the endpoint is left "closed" (IsOpen() returns false).
  */
	void Open(UInt16 a_Port);

	/** The callback that LibEvent calls when an event occurs on one of the sockets.
	Calls Callback() on a_Self. */
	static void RawCallback(evutil_socket_t a_Socket, short a_What, void * a_Self);

	/** The callback that is called when an event occurs on one of the sockets. */
	void Callback(evutil_socket_t a_Socket, short a_What);
};