2
3
4
5
9#include "Core/Container/LinkedUnorderedMap.h"
16
17
18
19 template<
typename... Args>
25
26
27 using Agent = std::function<
void(Args...)>;
32
33
40
41
45
46
47
48
49 bool Bind(std::function<
void(Args...)> func);
52
53
54
55
56 bool UnBind(std::function<
void(Args...)> func);
59
60
61
65
66
67
71
72
78
79
83 template<
typename ...Args>
88 uint64_t* addr =
reinterpret_cast<uint64_t*>(&func);
91 SPICES_CORE_WARN(
"Agent Function binding repeatly.");
96
97
103 template<
typename ...Args>
108 uint64_t* addr =
reinterpret_cast<uint64_t*>(&func);
111 SPICES_CORE_WARN(
"Agent Function not binded yet.");
116
117
123 template<
typename ...Args>
128 m_Agents->for_each([&](
const auto& k,
const auto& v) {
129 v(std::forward<Args>(args)...);
135
136
137
138#define DELEGATE_NONE_PARAM(name)
139 class Delegate##name : public Spices::Delegate_Basic<>
142 Delegate##name() : Spices::Delegate_Basic<>() {}
143 virtual ~Delegate##name() = default;
147
148
149
150#define DELEGATE_ONE_PARAM(name, p0)
151 class Delegate##name : public Spices::Delegate_Basic<##p0>
154 Delegate##name() : Spices::Delegate_Basic<##p0>() {}
155 virtual ~Delegate##name() = default;
159
160
161
162#define DELEGATE_TWO_PARAM(name, p0, p1)
163 class Delegate##name : public Spices::Delegate_Basic<##p0, ##p1>
166 Delegate##name() : Spices::Delegate_Basic<##p0, ##p1>() {}
167 virtual ~Delegate##name() = default;
171
172
173
174#define DELEGATE_THREE_PARAM(name, p0, p1, p2)
175 class Delegate##name : public Spices::Delegate_Basic<##p0, ##p1, ##p2>
178 Delegate##name() : Spices::Delegate_Basic<##p0, ##p1, ##p2>() {}
179 virtual ~Delegate##name() = default;
183
184
185
186#define DELEGATE_FOUR_PARAM(name, p0, p1, p2, p3)
187 class Delegate##name : public Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3>
190 Delegate##name() : Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3>() {}
191 virtual ~Delegate##name() = default;
195
196
197
198#define DELEGATE_FIVE_PARAM(name, p0, p1, p2, p3, p4)
199 class Delegate##name : public Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3, ##p4>
202 Delegate##name() : Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3, ##p4>() {}
203 virtual ~Delegate##name() = default;
207
208
209
210#define DELEGATE_SIX_PARAM(name, p0, p1, p2, p3, p4, p5)
211 class Delegate##name : public Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3, ##p4, ##p5>
214 Delegate##name() : Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3, ##p4, ##p5>() {}
215 virtual ~Delegate##name() = default;
219
220
221
222#define DELEGATE_SEVEN_PARAM(name, p0, p1, p2, p3, p4, p5, p6)
223 class Delegate##name : public Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3, ##p4, ##p5, ##p6>
226 Delegate##name() : Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3, ##p4, ##p5, ##p6>() {}
227 virtual ~Delegate##name() = default;
231
232
233
234#define DELEGATE_EIGHT_PARAM(name, p0, p1, p2, p3, p4, p5, p6, p7)
235 class Delegate##name : public Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3, ##p4, ##p5, ##p6, ##p7>
238 Delegate##name() : Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3, ##p4, ##p5, ##p6, ##p7>() {}
239 virtual ~Delegate##name() = default;
243
244
245
246#define DELEGATE_NINE_PARAM(name, p0, p1, p2, p3, p4, p5, p6, p7, p8)
247 class Delegate##name : public Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3, ##p4, ##p5, ##p6, ##p7, ##p8>
250 Delegate##name() : Spices::Delegate_Basic<##p0, ##p1, ##p2, ##p3, ##p4, ##p5, ##p6, ##p7, ##p8>() {}
251 virtual ~Delegate##name() = default;
#define DELEGATE_ONE_PARAM(name, p0)
Use this macro to instance a Delegate Class. One Parameter Specific.
#define DELEGATE_TWO_PARAM(name, p0, p1)
Use this macro to instance a Delegate Class. Two Parameter Specific.
#define SPICES_PROFILE_ZONE
size_t size()
Get size of Agents.
bool empty()
Determine if this Delegate is empty;.
std::shared_ptr< scl::linked_unordered_map< uint64_t, Agent > > m_Agents
Map of Agent Function Pointer.
bool Bind(std::function< void(Args...)> func)
Bind Function pointer to delegate.
virtual ~Delegate_Basic()=default
Destructor Function.
Delegate_Basic()
Constructor Function.
void Broadcast(Args &&... args)
Execute all function pointer.
bool UnBind(std::function< void(Args...)> func)
UnBind Function pointer from delegate.
Basic Class of Delegate. Instance inherited from it and use delegate feature.
void Listen()
Listen accept socket.
std::shared_ptr< Channel > m_AcceptChannel
Acceptor Channel.
Socket m_AcceptSocket
Acceptor Socket.
void HandleRead() const
On Read Event Callback.
Acceptor(const Acceptor &)=delete
Copy Constructor Function.
bool IsListening() const
Determine if this is in listening. @reutrn Returns true if is in listening.
void SetConnectionCallback(const ConnectionCallback &cb)
Set ConnectionCallback.
Acceptor(const InetAddress &listenAddress, bool reusePort)
Constructor Function.
ConnectionCallback m_ConnectionCallback
ConnectionCallback.
bool m_IsListening
Boolean of whether is in listening.
virtual ~Acceptor()
Destructor Function.
Acceptor & operator=(const Acceptor &)=delete
Copy Assignment Operation.
Wrapper of Channel and Socket, As the entrance of comm.
Wrapper of readwrite buffer.
~Channel()=default
Destructor Function.
void Update()
Update this Channel state to Poller.
void SetErrorCallback(EventCallback cb)
Set Error Event Callback.
EventCallback m_ReadCallback
Read Event Callback.
bool IsReading() const
Determine whether the event is a Read Event.
EventLoop * m_Loop
This channel interested EventLoop.
void SetWriteCallback(EventCallback cb)
Set Write Event Callback.
Channel(const Channel &)=delete
Copy Constructor Function.
Channel(SOCKET fd, EventLoop *loop)
Constructor Function.
EventCallback m_WriteCallback
Write Event Callback.
void SetCloseCallback(EventCallback cb)
Set Close Event Callback.
void SetRevents(int revt)
Set this REvents type.
void DisableReading()
Disable Read event.
void DisableAll()
Disable All event.
Channel & operator=(const Channel &)=delete
Copy Assignment Operation.
std::optional< std::weak_ptr< void > > m_Tie
int Events() const
Get this Events type.
EventCallback m_ErrorCallback
Error Event Callback.
void HandleEvent() const
Handle happened events on fd.
void HandleEventsWithGuard() const
Internal handle happened events on fd.
void Tie(const std::shared_ptr< void > &obj)
void EnableWriting()
Enable Write event.
SOCKET Fd() const
Get this SOCKET.
void SetPollState(PollState state)
Set Poll State.
bool IsWriting() const
Determine whether the event is a Write Event.
void DisableWriting()
Disable Write event.
int m_Revents
Current happened event type.
void EnableReading()
Enable Read event.
PollState GetPollState() const
Get Poll State.
EventFlags m_Events
This SOCKET interested Events type.
SOCKET m_Fd
This channel interested SOCKET.
EventCallback m_CloseCallback
Close Event Callback.
void Remove()
Remove this Channel state from Poller.
PollState m_PollState
This channel PollState.
bool IsNoneEvent() const
Determine whether the event is a None Event.
void SetReadCallback(EventCallback cb)
Set Read Event Callback.
Wrapper of SOCKET'S Event.
ThreadPool of Multiple threading EventLoop.
EventLoopThreadWrapper()
Constructor Function.
virtual ~EventLoopThreadWrapper()
Destructor Function.
static EventLoop *& GetInst(InetAddress *address=nullptr)
Get EventLoop Instance. @reutrn Returns EventLoop Instance.
EventLoop * instance
This thread EventLoop instance.
Wrapper of Instance/Delete ThreadCache in thread.
EventLoop & operator=(const EventLoop &)=delete
Copy Assignment Operation.
void HandleWakeUp()
Handle WakeUp notify by read one byte data.
std::vector< Functor > m_PendingFunctors
Delay functors.
void DoPendingFunctors()
Execute all pending functors.
std::shared_ptr< Poller > m_Poller
Poller instance.
std::atomic_bool m_IsCallingPendingFunctors
True if is execute pending functors.
std::atomic_bool m_IsLooping
True if is in Looping.
bool HasChannel(Channel *channel) const
Determine if channel is inside poller.
std::mutex m_Mutex
Mutex for PendingFunctors.
EventLoop(const EventLoop &)=delete
Copy Constructor Function.
std::atomic_bool m_IsQuit
True if is quit from Looping.
EventLoop(InetAddress *address=nullptr)
Constructor Function.
void WakeUp()
WakeUp a thread, which wait on recv, by sending one byte data.
std::unique_ptr< Channel > m_WeakupChannel
Wakeup Channel.
void Loop()
Start Event Loop.
bool IsInLoopThread() const
Determine if current thread is in eventloop thread.
void RunInLoop(Functor cb)
Push functor to pending functors.
~EventLoop()
Destructor Function.
Socket m_WakeupFd
Wakeup Socket.
DWORD m_ThreadId
Thread's identify, which is running this EventLoop.
void QueueInLoop(Functor cb)
Execute functor in EventLoop thread.
void UpdateChannel(Channel *channel) const
Update channel state with poller.
void Quit()
Quit from Event Loop.
ChannelList m_ActiveChannels
Channels with events.
void RemoveChannel(Channel *channel) const
Remove channel from poller.
Wrapper of Poller and wakeup socket to acceptor(SubLoop).
const Version GetVersion() const
Get Http Version.
std::string GetHeader(const std::string &name) const
Get a Header from this HttpRequest.
HttpResponse(bool close)
Constructor Function.
void AppendToBuffer(Buffer *output)
Append this response to Buffer.
bool CloseConnection() const
Get CloseConnection.
HttpServer & operator=(const HttpServer &)=delete
Copy Assignment Operation.
HttpServer(const HttpServer &)=delete
Copy Constructor Function.
void OnRequest(const TcpConnectionPtr &connection, const HttpRequest &request)
HttpCallback m_HttpCallback
virtual ~HttpServer()=default
Destructor Function.
void OnConnection(const TcpConnectionPtr &connection)
HttpServer(const InetAddress &listenAddress, int idleSeconds, TcpServer::Option option=TcpServer::Option::NoReusePort)
Constructor Function.
void Start(int threadSize) const
Start ThreadPool and call listen on acceptor.
WeakConnectionList m_ConnectionList
void OnMessage(const TcpConnectionPtr &connection, Buffer *buf)
This class is Wrapper of current socket address.
Inherit from this and Implement Specific Poller.
Socket(const Socket &)=delete
Copy Constructor Function.
void SetReuseAddress(bool on) const
Set socket reuse address option.
void SetTcpNoDelay(bool on) const
Set socket Tcp no delay option.
void Connect(InetAddress *connectAddress) const
Connect to socket.
Socket & operator=(const Socket &)=delete
Copy Assignment Operation.
SOCKET m_SocketFd
This socket fd.
void Send(const std::string &data) const
Send data to server.
SOCKET Accept(InetAddress *peerAddress) const
Accept connection on socket.
Socket()=default
Constructor Function.
void BindAddress(const InetAddress &localAddress) const
Bind address to socket.
void Create()
Create Non Blocking Socket.
virtual ~Socket()
Destructor Function.
std::string Receive() const
Receive data from server.
SOCKET Fd()
Get this socket fd.
Socket(SOCKET socketFd)
Constructor Function.
const SOCKET Fd() const
Get this socket fd.
void SetKeepAlive(bool on) const
Set socket keep alive option.
void SetReusePort(bool on) const
Set socket reuse port option.
void ShutDownWrite() const
Disable writen in socket.
void Listen() const
Listen on socket.
This class is Wrapper of socket.
Combine of Socket Connection data.
void SetThreadInitCallback(const ThreadInitCallback &cb)
Set Thread initialize Call back.
void AddMessageCallback(const DelegateMessageCallback::Agent &cb)
Add Message Call back.
void Start(int threadSize) const
Start ThreadPool and call listen on acceptor.
std::shared_ptr< EventLoopThreadPool > m_ThreadPool
ThreadPool.
void NewConnection(SOCKET socketFd, const InetAddress &peerAddress)
Handle New Connection to Acceptor.
void RemoveConnectionInLoop(const TcpConnectionPtr &connection)
Remove a TcpConnection InLoop.
TcpServer(const TcpServer &)=delete
Copy Constructor Function.
ConnectionMap m_Connections
All TcpConnections.
DelegateWriteCompleteCallback m_WriteCompleteCallback
WriteCompleteCallback.
void RemoveConnection(const TcpConnectionPtr &connection)
Remove a TcpConnection.
TcpServer(const InetAddress &listenAddress, Option option=Option::NoReusePort)
Constructor Function.
TcpServer & operator=(const TcpServer &)=delete
Copy Assignment Operation.
ThreadInitCallback m_ThreadInitCallback
ThreadInitCallback.
std::unique_ptr< Acceptor > m_Acceptor
Acceptor.
std::string m_IpPort
TcpServer Ip and Port.
void AddConnectionCallback(const DelegateConnectionCallback::Agent &cb)
Add Connection Call back.
virtual ~TcpServer()
Destructor Function.
DelegateConnectionCallback m_ConnectionCallback
ConnectionCallback.
int m_NextConnectedId
Next Connection id.
DelegateMessageCallback m_MessageCallback
MessageCallback.
void AddWriteCompleteCallback(const DelegateWriteCompleteCallback::Agent &cb)
Add WriteComplete Call back.
The container combines hashmap and list together. Used in the case that we want iter a hashmap in ord...