SpiecsEngine
 
Loading...
Searching...
No Matches
TcpServer.h
Go to the documentation of this file.
1/**
2* @file TcpServer.h.
3* @brief The TcpServer Class Definitions.
4* @author Spices & Muduo.
5*/
6
7#pragma once
8#include "Core/Core.h"
9#include "EventLoop.h"
10#include "Acceptor.h"
11#include "InetAddress.h"
12#include "Callbacks.h"
13#include "Buffer.h"
14#include "TcpConnection.h"
15
16namespace Spices {
17
18namespace Net {
19
20 /**
21 * @brief Forward Declare.
22 */
24
25 /**
26 * @brief Sample of a TcpServer.
27 */
29 {
30 public:
31
32 using ThreadInitCallback = std::function<void(EventLoop*)>;
34
35 enum class Option
36 {
37 NoReusePort = 0,
38 ReusePort = 1
39 };
40
41 public:
42
43 /**
44 * @brief Constructor Function.
45 * @param[in] listenAddress Server listen address.
46 * @param[in] option Is reuse port.
47 */
48 TcpServer(const InetAddress& listenAddress, Option option = Option::NoReusePort);
49
50 /**
51 * @brief Destructor Function.
52 */
53 virtual ~TcpServer();
54
55 /**
56 * @brief Copy Constructor Function.
57 * @note This Class not allowed copy behaves.
58 */
59 TcpServer(const TcpServer&) = delete;
60
61 /**
62 * @brief Copy Assignment Operation.
63 * @note This Class not allowed copy behaves.
64 */
65 TcpServer& operator=(const TcpServer&) = delete;
66
67 /**
68 * @brief Set Thread initialize Call back.
69 * @param[in] cb ThreadInitCallback.
70 */
71 void SetThreadInitCallback(const ThreadInitCallback& cb)
72 {
73 m_ThreadInitCallback = cb;
74 }
75
76 /**
77 * @brief Add Connection Call back.
78 * @param[in] cb DelegateConnectionCallback::Agent.
79 */
80 void AddConnectionCallback(const DelegateConnectionCallback::Agent& cb)
81 {
82 m_ConnectionCallback.Bind(cb);
83 }
84
85 /**
86 * @brief Add Message Call back.
87 * @param[in] cb DelegateMessageCallback::Agent.
88 */
89 void AddMessageCallback(const DelegateMessageCallback::Agent& cb)
90 {
91 m_MessageCallback.Bind(cb);
92 }
93
94 /**
95 * @brief Add WriteComplete Call back.
96 * @param[in] cb DelegateWriteCompleteCallback::Agent.
97 */
98 void AddWriteCompleteCallback(const DelegateWriteCompleteCallback::Agent& cb)
99 {
100 m_WriteCompleteCallback.Bind(cb);
101 }
102
103 /**
104 * @brief Start ThreadPool and call listen on acceptor.
105 * @param[in] threadSize ThreadPool size.
106 */
107 void Start(int threadSize) const;
108
109 private:
110
111 /**
112 * @brief Handle New Connection to Acceptor.
113 * @param socketFd Accept return SOCKET.
114 * @param peerAddress Accept return Address.
115 */
116 void NewConnection(SOCKET socketFd, const InetAddress& peerAddress);
117
118 /**
119 * @brief Remove a TcpConnection.
120 * @param connection TcpConnectionPtr.
121 */
122 void RemoveConnection(const TcpConnectionPtr& connection);
123
124 /**
125 * @brief Remove a TcpConnection InLoop.
126 * @param connection TcpConnectionPtr.
127 */
128 void RemoveConnectionInLoop(const TcpConnectionPtr& connection);
129
130 private:
131
132 /**
133 * @brief TcpServer Ip and Port.
134 */
135 std::string m_IpPort;
136
137 /**
138 * @brief Acceptor.
139 */
141
142 /**
143 * @brief ThreadPool
144 */
146
147 /**
148 * @brief ConnectionCallback.
149 */
150 DelegateConnectionCallback m_ConnectionCallback;
151
152 /**
153 * @brief MessageCallback.
154 */
155 DelegateMessageCallback m_MessageCallback;
156
157 /**
158 * @brief WriteCompleteCallback.
159 */
160 DelegateWriteCompleteCallback m_WriteCompleteCallback;
161
162 /**
163 * @brief ThreadInitCallback.
164 */
165 ThreadInitCallback m_ThreadInitCallback;
166
167 /**
168 * @brief Next Connection id.
169 */
171
172 /**
173 * @brief All TcpConnections.
174 */
176
177 };
178
179}
180
181}
#define SPICES_PROFILE_ZONE
Wrapper of readwrite buffer.
Definition Buffer.h:19
ThreadPool of Multiple threading EventLoop.
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
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