SpiecsEngine
 
Loading...
Searching...
No Matches
EventLoop.h
Go to the documentation of this file.
1/**
2* @file EventLoop.h.
3* @brief The EventLoop Class Definitions.
4* @author Spices & Muduo.
5*/
6
7#pragma once
8
9#include "Core/Core.h"
10#include "Socket.h"
11#include <atomic>
12#include <windows.h>
13
14namespace Spices {
15
16namespace Net {
17
18 /**
19 * @brief Forward Declare.
20 */
21 class Channel;
22 class Poller;
23
24 /**
25 * @brief Wrapper of Poller and wakeup socket to acceptor(SubLoop).
26 */
28 {
29 public:
30
31 using Functor = std::function<void()>;
33
34 public:
35
36 /**
37 * @brief Constructor Function.
38 * @param[in] address ListenAddress.
39 */
40 EventLoop(InetAddress* address = nullptr);
41
42 /**
43 * @brief Destructor Function.
44 */
45 ~EventLoop();
46
47 /**
48 * @brief Copy Constructor Function.
49 * @note This Class not allowed copy behaves.
50 */
51 EventLoop(const EventLoop&) = delete;
52
53 /**
54 * @brief Copy Assignment Operation.
55 * @note This Class not allowed copy behaves.
56 */
57 EventLoop& operator=(const EventLoop&) = delete;
58
59 /**
60 * @brief Start Event Loop.
61 */
62 void Loop();
63
64 /**
65 * @brief Quit from Event Loop.
66 */
67 void Quit();
68
69 /**
70 * @brief Push functor to pending functors.
71 * @param[in] cb Functor.
72 */
73 void RunInLoop(Functor cb);
74
75 /**
76 * @brief Execute functor in EventLoop thread.
77 * @param[in] cb Functor.
78 */
79 void QueueInLoop(Functor cb);
80
81 /**
82 * @brief WakeUp a thread, which wait on recv, by sending one byte data.
83 */
84 void WakeUp();
85
86 /**
87 * @brief Update channel state with poller.
88 * @param[in] channel Channel.
89 */
90 void UpdateChannel(Channel* channel) const;
91
92 /**
93 * @brief Remove channel from poller.
94 * @param[in] channel Channel.
95 */
96 void RemoveChannel(Channel* channel) const;
97
98 /**
99 * @brief Determine if channel is inside poller.
100 * @param[in] channel Channel.
101 * @return Returns true if channel is inside poller.
102 */
103 bool HasChannel(Channel* channel) const;
104
105 /**
106 * @brief Determine if current thread is in eventloop thread.
107 * @return Returns true if is in eventloop thread.
108 */
109 bool IsInLoopThread() const { return m_ThreadId == GetCurrentThreadId(); }
110
111 private:
112
113 /**
114 * @brief Handle WakeUp notify by read one byte data.
115 */
116 void HandleWakeUp();
117
118 /**
119 * @brief Execute all pending functors.
120 */
121 void DoPendingFunctors();
122
123 private:
124
125 /**
126 * @brief True if is in Looping.
127 */
128 std::atomic_bool m_IsLooping;
129
130 /**
131 * @brief True if is quit from Looping.
132 */
133 std::atomic_bool m_IsQuit;
134
135 /**
136 * @brief True if is execute pending functors.
137 */
139
140 /**
141 * @brief Thread's identify, which is running this EventLoop.
142 */
144
145 /**
146 * @brief Poller instance.
147 */
148 std::shared_ptr<Poller> m_Poller;
149
150 /**
151 * @brief Wakeup Socket.
152 */
154
155 /**
156 * @brief Wakeup Channel.
157 */
159
160 /**
161 * @brief Channels with events.
162 */
164
165 /**
166 * @brief Delay functors.
167 */
169
170 /**
171 * @brief Mutex for PendingFunctors.
172 */
174 };
175
176 /**
177 * @brief Wrapper of Instance/Delete ThreadCache in thread.
178 */
180 {
181 public:
182
183 /**
184 * @brief Constructor Function.
185 */
187
188 /**
189 * @brief Destructor Function.
190 */
191 virtual ~EventLoopThreadWrapper();
192
193 /**
194 * @brief Get EventLoop Instance.
195 * @reutrn Returns EventLoop Instance.
196 */
197 static EventLoop*& GetInst(InetAddress* address = nullptr);
198
199 private:
200
201 /**
202 * @brief This thread EventLoop instance.
203 */
205 };
206
207}
208
209}
#define SPICES_PROFILE_ZONE
Wrapper of readwrite buffer.
Definition Buffer.h:19
Wrapper of SOCKET'S Event.
Definition Channel.h:27
ThreadPool of Multiple threading EventLoop.
EventLoopThreadWrapper()
Constructor Function.
Definition EventLoop.h:186
virtual ~EventLoopThreadWrapper()
Destructor Function.
static EventLoop *& GetInst(InetAddress *address=nullptr)
Get EventLoop Instance. @reutrn Returns EventLoop Instance.
EventLoop * instance
This thread EventLoop instance.
Definition EventLoop.h:204
Wrapper of Instance/Delete ThreadCache in thread.
Definition EventLoop.h:180
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.
Definition EventLoop.h:168
void DoPendingFunctors()
Execute all pending functors.
std::shared_ptr< Poller > m_Poller
Poller instance.
Definition EventLoop.h:148
std::atomic_bool m_IsCallingPendingFunctors
True if is execute pending functors.
Definition EventLoop.h:138
std::atomic_bool m_IsLooping
True if is in Looping.
Definition EventLoop.h:128
bool HasChannel(Channel *channel) const
Determine if channel is inside poller.
std::mutex m_Mutex
Mutex for PendingFunctors.
Definition EventLoop.h:173
EventLoop(const EventLoop &)=delete
Copy Constructor Function.
std::atomic_bool m_IsQuit
True if is quit from Looping.
Definition EventLoop.h:133
EventLoop(InetAddress *address=nullptr)
Constructor Function.
Definition EventLoop.cpp:22
void WakeUp()
WakeUp a thread, which wait on recv, by sending one byte data.
std::unique_ptr< Channel > m_WeakupChannel
Wakeup Channel.
Definition EventLoop.h:158
void Loop()
Start Event Loop.
Definition EventLoop.cpp:48
bool IsInLoopThread() const
Determine if current thread is in eventloop thread.
Definition EventLoop.h:109
void RunInLoop(Functor cb)
Push functor to pending functors.
Definition EventLoop.cpp:79
~EventLoop()
Destructor Function.
Definition EventLoop.cpp:42
Socket m_WakeupFd
Wakeup Socket.
Definition EventLoop.h:153
DWORD m_ThreadId
Thread's identify, which is running this EventLoop.
Definition EventLoop.h:143
void QueueInLoop(Functor cb)
Execute functor in EventLoop thread.
Definition EventLoop.cpp:91
void UpdateChannel(Channel *channel) const
Update channel state with poller.
void Quit()
Quit from Event Loop.
Definition EventLoop.cpp:69
ChannelList m_ActiveChannels
Channels with events.
Definition EventLoop.h:163
void RemoveChannel(Channel *channel) const
Remove channel from poller.
Wrapper of Poller and wakeup socket to acceptor(SubLoop).
Definition EventLoop.h:28
const Version GetVersion() const
Get Http Version.
Definition HttpRequest.h:89
std::string GetHeader(const std::string &name) const
Get a Header from this HttpRequest.
Http Request body.
Definition HttpRequest.h:18
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
Definition HttpServer.h:75
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
Definition HttpServer.h:78
void OnMessage(const TcpConnectionPtr &connection, Buffer *buf)
This class is Wrapper of current socket address.
Definition InetAddress.h:22
Inherit from this and Implement Specific Poller.
Definition Poller.h:25
This class is Wrapper of socket.
Definition Socket.h:18
Combine of Socket Connection data.
void SetThreadInitCallback(const ThreadInitCallback &cb)
Set Thread initialize Call back.
Definition TcpServer.h:71
void AddMessageCallback(const DelegateMessageCallback::Agent &cb)
Add Message Call back.
Definition TcpServer.h:89
void Start(int threadSize) const
Start ThreadPool and call listen on acceptor.
Definition TcpServer.cpp:46
std::shared_ptr< EventLoopThreadPool > m_ThreadPool
ThreadPool.
Definition TcpServer.h:145
void NewConnection(SOCKET socketFd, const InetAddress &peerAddress)
Handle New Connection to Acceptor.
Definition TcpServer.cpp:57
void RemoveConnectionInLoop(const TcpConnectionPtr &connection)
Remove a TcpConnection InLoop.
Definition TcpServer.cpp:97
TcpServer(const TcpServer &)=delete
Copy Constructor Function.
ConnectionMap m_Connections
All TcpConnections.
Definition TcpServer.h:175
DelegateWriteCompleteCallback m_WriteCompleteCallback
WriteCompleteCallback.
Definition TcpServer.h:160
void RemoveConnection(const TcpConnectionPtr &connection)
Remove a TcpConnection.
Definition TcpServer.cpp:92
TcpServer(const InetAddress &listenAddress, Option option=Option::NoReusePort)
Constructor Function.
Definition TcpServer.cpp:16
TcpServer & operator=(const TcpServer &)=delete
Copy Assignment Operation.
ThreadInitCallback m_ThreadInitCallback
ThreadInitCallback.
Definition TcpServer.h:165
std::unique_ptr< Acceptor > m_Acceptor
Acceptor.
Definition TcpServer.h:140
std::string m_IpPort
TcpServer Ip and Port.
Definition TcpServer.h:135
void AddConnectionCallback(const DelegateConnectionCallback::Agent &cb)
Add Connection Call back.
Definition TcpServer.h:80
virtual ~TcpServer()
Destructor Function.
Definition TcpServer.cpp:33
DelegateConnectionCallback m_ConnectionCallback
ConnectionCallback.
Definition TcpServer.h:150
int m_NextConnectedId
Next Connection id.
Definition TcpServer.h:170
DelegateMessageCallback m_MessageCallback
MessageCallback.
Definition TcpServer.h:155
void AddWriteCompleteCallback(const DelegateWriteCompleteCallback::Agent &cb)
Add WriteComplete Call back.
Definition TcpServer.h:98
Sample of a TcpServer.
Definition TcpServer.h:29