diff options
| author | zousar <[email protected]> | 2022-01-27 01:01:05 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-01-27 09:01:05 +0100 |
| commit | cf38d96543b2086a863ef4823ee769ec45cc45a4 (patch) | |
| tree | 1c5c56053f43271398d9f9a469383f29df76c570 /zenutil | |
| parent | Implement SkipData,QueryLocal,StoreLocal for HandleRpcGetCacheRecords (#41) (diff) | |
| download | zen-cf38d96543b2086a863ef4823ee769ec45cc45a4.tar.xz zen-cf38d96543b2086a863ef4823ee769ec45cc45a4.zip | |
Handle HTTP port collisions when initializing server (#40)
Diffstat (limited to 'zenutil')
| -rw-r--r-- | zenutil/include/zenutil/zenserverprocess.h | 9 | ||||
| -rw-r--r-- | zenutil/zenserverprocess.cpp | 28 |
2 files changed, 20 insertions, 17 deletions
diff --git a/zenutil/include/zenutil/zenserverprocess.h b/zenutil/include/zenutil/zenserverprocess.h index 55b9a50cd..2a3146e2d 100644 --- a/zenutil/include/zenutil/zenserverprocess.h +++ b/zenutil/include/zenutil/zenserverprocess.h @@ -100,11 +100,12 @@ public: // additional state. For example, you can use the session ID // to introduce additional named objects std::atomic<uint32_t> Pid; - std::atomic<uint16_t> ListenPort; + std::atomic<uint16_t> DesiredListenPort; std::atomic<uint16_t> Flags; uint8_t SessionId[12]; std::atomic<uint32_t> SponsorPids[8]; - uint8_t Padding[12]; + std::atomic<uint16_t> EffectiveListenPort; + uint8_t Padding[10]; enum class FlagsEnum : uint16_t { @@ -125,8 +126,8 @@ public: void Initialize(); [[nodiscard]] bool InitializeReadOnly(); - [[nodiscard]] ZenServerEntry* Lookup(int ListenPort); - ZenServerEntry* Register(int ListenPort); + [[nodiscard]] ZenServerEntry* Lookup(int DesiredListenPort); + ZenServerEntry* Register(int DesiredListenPort); void Sweep(); void Snapshot(std::function<void(const ZenServerEntry&)>&& Callback); inline bool IsReadOnly() const { return m_IsReadOnly; } diff --git a/zenutil/zenserverprocess.cpp b/zenutil/zenserverprocess.cpp index fe6236d18..5bddc72bc 100644 --- a/zenutil/zenserverprocess.cpp +++ b/zenutil/zenserverprocess.cpp @@ -230,11 +230,11 @@ ZenServerState::InitializeReadOnly() } ZenServerState::ZenServerEntry* -ZenServerState::Lookup(int ListenPort) +ZenServerState::Lookup(int DesiredListenPort) { for (int i = 0; i < m_MaxEntryCount; ++i) { - if (m_Data[i].ListenPort == ListenPort) + if (m_Data[i].DesiredListenPort == DesiredListenPort) { return &m_Data[i]; } @@ -244,7 +244,7 @@ ZenServerState::Lookup(int ListenPort) } ZenServerState::ZenServerEntry* -ZenServerState::Register(int ListenPort) +ZenServerState::Register(int DesiredListenPort) { if (m_Data == nullptr) { @@ -259,17 +259,18 @@ ZenServerState::Register(int ListenPort) { ZenServerEntry& Entry = m_Data[i]; - if (Entry.ListenPort.load(std::memory_order_relaxed) == 0) + if (Entry.DesiredListenPort.load(std::memory_order_relaxed) == 0) { uint16_t Expected = 0; - if (Entry.ListenPort.compare_exchange_strong(Expected, uint16_t(ListenPort))) + if (Entry.DesiredListenPort.compare_exchange_strong(Expected, uint16_t(DesiredListenPort))) { // Successfully allocated entry m_OurEntry = &Entry; - Entry.Pid = Pid; - Entry.Flags = 0; + Entry.Pid = Pid; + Entry.EffectiveListenPort = 0; + Entry.Flags = 0; const Oid SesId = GetSessionId(); memcpy(Entry.SessionId, &SesId, sizeof SesId); @@ -296,11 +297,11 @@ ZenServerState::Sweep() { ZenServerEntry& Entry = m_Data[i]; - if (Entry.ListenPort) + if (Entry.DesiredListenPort) { if (IsProcessRunning(Entry.Pid) == false) { - ZEN_DEBUG("Sweep - pid {} not running, reclaiming entry (port {})", Entry.Pid, Entry.ListenPort); + ZEN_DEBUG("Sweep - pid {} not running, reclaiming entry (port {})", Entry.Pid, Entry.DesiredListenPort); Entry.Reset(); } @@ -320,7 +321,7 @@ ZenServerState::Snapshot(std::function<void(const ZenServerEntry&)>&& Callback) { ZenServerEntry& Entry = m_Data[i]; - if (Entry.ListenPort) + if (Entry.DesiredListenPort) { Callback(Entry); } @@ -330,9 +331,10 @@ ZenServerState::Snapshot(std::function<void(const ZenServerEntry&)>&& Callback) void ZenServerState::ZenServerEntry::Reset() { - Pid = 0; - ListenPort = 0; - Flags = 0; + Pid = 0; + DesiredListenPort = 0; + Flags = 0; + EffectiveListenPort = 0; } void |