diff options
author | Mattes D <github@xoft.cz> | 2015-02-20 14:28:05 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2015-02-20 14:28:05 +0100 |
commit | 9c5162041e6e0699283862b87e2e424bf8e3b8b8 (patch) | |
tree | 3beed26a11a3fb35b79b444f2380f6b93dc2f3a7 /src/OSSupport/UDPEndpointImpl.h | |
parent | NetworkSingleton: LibEvent thread is joined properly on server exit. (diff) | |
download | cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar.gz cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar.bz2 cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar.lz cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar.xz cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar.zst cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.zip |
Diffstat (limited to 'src/OSSupport/UDPEndpointImpl.h')
-rw-r--r-- | src/OSSupport/UDPEndpointImpl.h | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/OSSupport/UDPEndpointImpl.h b/src/OSSupport/UDPEndpointImpl.h new file mode 100644 index 000000000..75942b0cf --- /dev/null +++ b/src/OSSupport/UDPEndpointImpl.h @@ -0,0 +1,81 @@ + +// 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 SharedPtr<cUDPEndpointImpl> cUDPEndpointImplPtr; + + + + + +class cUDPEndpointImpl: + public cUDPEndpoint +{ + typedef cUDPEndpoint super; + +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); + + // 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); +}; + + + + |