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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include <zencore/iobuffer.h>
#include <zencore/iohash.h>
#include <zencore/logging.h>
#include <zencore/memory.h>
#include <zencore/thread.h>
#include <zencore/uid.h>
#include <zencore/zencore.h>
#pragma warning(push)
#pragma warning(disable : 4127)
#include <tsl/robin_map.h>
#pragma warning(pop)
#include <asio.hpp>
#include <chrono>
struct ZenCacheValue;
namespace spdlog {
class logger;
}
namespace zen {
class CbObjectWriter;
class ZenStructuredCacheClient;
/** Zen mesh tracker
*
* Discovers and tracks local peers
*/
class Mesh
{
public:
Mesh(asio::io_context& IoContext);
~Mesh();
void Start(uint16_t Port);
void Stop();
private:
void Run();
void IssueReceive();
void EnqueueTick();
void OnTick();
void BroadcastPacket(CbObjectWriter&);
enum State
{
kInitializing,
kRunning,
kExiting
};
static const int kMaxMessageSize = 2048;
static const int kMaxUpdateSize = 1400; // We'll try not to send messages larger than this
spdlog::logger& Log() { return m_Log; }
spdlog::logger& m_Log;
std::atomic<State> m_State = kInitializing;
asio::io_context& m_IoContext;
std::unique_ptr<asio::ip::udp::socket> m_UdpSocket;
std::unique_ptr<asio::ip::udp::socket> m_BroadcastSocket;
asio::ip::udp::endpoint m_SenderEndpoint;
std::unique_ptr<std::thread> m_Thread;
uint16_t m_Port = 0;
uint8_t m_MessageBuffer[kMaxMessageSize];
asio::high_resolution_timer m_Timer{m_IoContext};
Oid m_SessionId;
struct PeerInfo
{
Oid SessionId;
std::time_t LastSeen;
std::vector<asio::ip::address> SeenOnIP;
};
RwLock m_SessionsLock;
tsl::robin_map<Oid, PeerInfo, Oid::Hasher> m_KnownPeers;
};
namespace detail {
struct ZenCacheSessionState;
}
struct ZenCacheResult
{
IoBuffer Response;
int64_t Bytes = {};
double ElapsedSeconds = {};
int32_t ErrorCode = {};
std::string Reason;
bool Success = false;
};
/** Zen Structured Cache session
*
* This provides a context in which cache queries can be performed
*
* These are currently all synchronous. Will need to be made asynchronous
*/
class ZenStructuredCacheSession
{
public:
ZenStructuredCacheSession(ZenStructuredCacheClient& OuterClient);
~ZenStructuredCacheSession();
ZenCacheResult CheckHealth();
ZenCacheResult GetCacheRecord(std::string_view BucketId, const IoHash& Key, ZenContentType Type);
ZenCacheResult GetCachePayload(std::string_view BucketId, const IoHash& Key, const IoHash& PayloadId);
ZenCacheResult PutCacheRecord(std::string_view BucketId, const IoHash& Key, IoBuffer Value, ZenContentType Type);
ZenCacheResult PutCachePayload(std::string_view BucketId, const IoHash& Key, const IoHash& PayloadId, IoBuffer Payload);
private:
inline spdlog::logger& Log() { return m_Log; }
spdlog::logger& m_Log;
ZenStructuredCacheClient& m_Client;
detail::ZenCacheSessionState* m_SessionState;
};
/** Zen Structured Cache client
*
* This represents an endpoint to query -- actual queries should be done via
* ZenStructuredCacheSession
*/
class ZenStructuredCacheClient : public RefCounted
{
public:
ZenStructuredCacheClient(std::string_view ServiceUrl);
~ZenStructuredCacheClient();
std::string_view ServiceUrl() const { return m_ServiceUrl; }
inline spdlog::logger& Log() { return m_Log; }
private:
spdlog::logger& m_Log;
std::string m_ServiceUrl;
RwLock m_SessionStateLock;
std::list<detail::ZenCacheSessionState*> m_SessionStateCache;
detail::ZenCacheSessionState* AllocSessionState();
void FreeSessionState(detail::ZenCacheSessionState*);
friend class ZenStructuredCacheSession;
};
} // namespace zen
|