diff options
author | Mattes D <github@xoft.cz> | 2015-01-18 12:35:02 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2015-01-22 20:13:02 +0100 |
commit | d4682463a1d503c349ac95e275b11d67d402268c (patch) | |
tree | de3167d2b81eb909a419c1355021353e00e71074 /src/OSSupport/NetworkSingleton.cpp | |
parent | cNetwork: Split the main cpp file into several files. (diff) | |
download | cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar.gz cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar.bz2 cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar.lz cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar.xz cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar.zst cuberite-d4682463a1d503c349ac95e275b11d67d402268c.zip |
Diffstat (limited to 'src/OSSupport/NetworkSingleton.cpp')
-rw-r--r-- | src/OSSupport/NetworkSingleton.cpp | 77 |
1 files changed, 39 insertions, 38 deletions
diff --git a/src/OSSupport/NetworkSingleton.cpp b/src/OSSupport/NetworkSingleton.cpp index c9d9b1d81..8c38fb4eb 100644 --- a/src/OSSupport/NetworkSingleton.cpp +++ b/src/OSSupport/NetworkSingleton.cpp @@ -70,60 +70,40 @@ cNetworkSingleton::cNetworkSingleton(void) -cNetworkSingleton & cNetworkSingleton::Get(void) +cNetworkSingleton::~cNetworkSingleton() { - static cNetworkSingleton Instance; - return Instance; -} - + // Wait for the LibEvent event loop to terminate: + event_base_loopbreak(m_EventBase); + m_EventLoopTerminated.Wait(); - - - -bool cNetworkSingleton::HostnameToIP( - const AString & a_Hostname, - cNetwork::cResolveNameCallbacksPtr a_Callbacks -) -{ - try + // Remove all objects: { - // TODO: This has a race condition with possible memory leak: - // If a lookup finishes immediately, the constructor calls the removal before this addition cCSLock Lock(m_CS); - m_HostnameLookups.push_back(std::make_shared<cHostnameLookup>(a_Hostname, a_Callbacks)); + m_Connections.clear(); + m_Servers.clear(); + m_HostnameLookups.clear(); + m_IPLookups.clear(); } - catch (...) - { - return false; - } - return true; + + // Free the underlying LibEvent objects: + evdns_base_free(m_DNSBase, true); + event_base_free(m_EventBase); } -bool cNetworkSingleton::IPToHostName( - const AString & a_IP, - cNetwork::cResolveNameCallbacksPtr a_Callbacks -) + +cNetworkSingleton & cNetworkSingleton::Get(void) { - try - { - // TODO: This has a race condition with possible memory leak: - // If a lookup finishes immediately, the constructor calls the removal before this addition - cCSLock Lock(m_CS); - m_IPLookups.push_back(std::make_shared<cIPLookup>(a_IP, a_Callbacks)); - } - catch (...) - { - return false; - } - return true; + static cNetworkSingleton Instance; + return Instance; } + void cNetworkSingleton::LogCallback(int a_Severity, const char * a_Msg) { switch (a_Severity) @@ -147,6 +127,17 @@ void cNetworkSingleton::LogCallback(int a_Severity, const char * a_Msg) void cNetworkSingleton::RunEventLoop(cNetworkSingleton * a_Self) { event_base_loop(a_Self->m_EventBase, EVLOOP_NO_EXIT_ON_EMPTY); + a_Self->m_EventLoopTerminated.Set(); +} + + + + + +void cNetworkSingleton::AddHostnameLookup(cHostnameLookupPtr a_HostnameLookup) +{ + cCSLock Lock(m_CS); + m_HostnameLookups.push_back(a_HostnameLookup); } @@ -170,6 +161,16 @@ void cNetworkSingleton::RemoveHostnameLookup(const cHostnameLookup * a_HostnameL +void cNetworkSingleton::AddIPLookup(cIPLookupPtr a_IPLookup) +{ + cCSLock Lock(m_CS); + m_IPLookups.push_back(a_IPLookup); +} + + + + + void cNetworkSingleton::RemoveIPLookup(const cIPLookup * a_IPLookup) { cCSLock Lock(m_CS); |