sptk2 logo
SPTK Home Page
TCPServer.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <bitset>
30#include <iostream>
31#include <set>
32#include <sptk5/Logger.h>
33#include <sptk5/net/SSLKeys.h>
34#include <sptk5/net/ServerConnection.h>
35#include <sptk5/threads/SynchronizedQueue.h>
36#include <sptk5/threads/ThreadPool.h>
37
38namespace sptk {
39
40class TCPServerListener;
41
52class SP_EXPORT LogDetails
53{
54public:
58 enum class MessageDetail : uint8_t
59 {
60 SERIAL_ID,
61 SOURCE_IP,
62 REQUEST_NAME,
63 REQUEST_DURATION,
64 REQUEST_DATA,
65 RESPONSE_DATA,
66 THREAD_POOLING,
67 MAX_MESSAGE_DETAIL
68 };
69
70 using MessageDetails = std::set<MessageDetail>;
71
75 LogDetails() = default;
76
81 explicit LogDetails(const MessageDetails& details)
82 : m_details(details)
83 {
84 }
85
90 explicit LogDetails(const Strings& details);
91
96 explicit LogDetails(std::initializer_list<MessageDetail> details)
97 {
98 for (auto detail: details)
99 {
100 m_details.insert(detail);
101 }
102 }
103
104 String toString(const String& delimiter = ",") const;
105
111 bool has(MessageDetail detail) const
112 {
113 return m_details.find(detail) != m_details.end();
114 }
115
116 bool empty() const
117 {
118 return m_details.empty();
119 }
120
121private:
122 MessageDetails m_details;
123 static const std::map<String, MessageDetail> detailNames;
124};
125
131class SP_EXPORT TCPServer
132 : public ThreadPool
133{
134 friend class TCPServerListener;
135
136 friend class ServerConnection;
137
138public:
145 TCPServer(const String& listenerName, ServerConnection::Type connectionType, size_t threadLimit = 16, LogEngine* logEngine = nullptr,
146 const LogDetails& logDetails = LogDetails());
147
151 ~TCPServer() override;
152
157 const Host& host() const;
158
163 void listen(uint16_t port);
164
168 void stop() override;
169
173 bool active() const
174 {
175 return m_listenerThread != nullptr;
176 }
177
183 void log(LogPriority priority, const String& message) const
184 {
185 if (m_logger)
186 {
187 m_logger->log(priority, message);
188 }
189 }
190
191 const LogDetails& logDetails() const
192 {
193 return m_logDetails;
194 }
195
200 void setSSLKeys(std::shared_ptr<SSLKeys> sslKeys);
201
206 const SSLKeys& getSSLKeys() const;
207
215 virtual void onConnection(const ServerConnection::Function& function);
216
217protected:
223 void host(const Host& host);
224
232 virtual bool allowConnection(sockaddr_in* connectionRequest);
233
242 virtual SServerConnection createConnection(SOCKET connectionSocket, const sockaddr_in* peer);
243
252 void threadEvent(Thread* thread, ThreadEvent::Type eventType, SRunable runable) override;
253
254private:
255 mutable SharedMutex m_mutex;
256 std::shared_ptr<TCPServerListener> m_listenerThread;
257 std::shared_ptr<Logger> m_logger;
258 std::shared_ptr<SSLKeys> m_sslKeys;
259 Host m_host;
260 LogDetails m_logDetails;
261 ServerConnection::Type m_connectionType;
262 ServerConnection::Function m_connectionFunction;
263};
264
268} // namespace sptk
Definition: Host.h:55
Definition: TCPServer.h:53
LogDetails(std::initializer_list< MessageDetail > details)
Definition: TCPServer.h:96
LogDetails(const Strings &details)
MessageDetail
Definition: TCPServer.h:59
bool has(MessageDetail detail) const
Definition: TCPServer.h:111
LogDetails()=default
LogDetails(const MessageDetails &details)
Definition: TCPServer.h:81
Definition: LogEngine.h:60
Definition: SSLKeys.h:38
Definition: ServerConnection.h:56
Definition: String.h:49
Definition: Strings.h:52
Definition: TCPServerListener.h:50
Definition: TCPServer.h:133
virtual bool allowConnection(sockaddr_in *connectionRequest)
~TCPServer() override
void host(const Host &host)
TCPServer(const String &listenerName, ServerConnection::Type connectionType, size_t threadLimit=16, LogEngine *logEngine=nullptr, const LogDetails &logDetails=LogDetails())
virtual void onConnection(const ServerConnection::Function &function)
const Host & host() const
void threadEvent(Thread *thread, ThreadEvent::Type eventType, SRunable runable) override
void log(LogPriority priority, const String &message) const
Definition: TCPServer.h:183
bool active() const
Definition: TCPServer.h:173
const SSLKeys & getSSLKeys() const
void listen(uint16_t port)
virtual SServerConnection createConnection(SOCKET connectionSocket, const sockaddr_in *peer)
void stop() override
void setSSLKeys(std::shared_ptr< SSLKeys > sslKeys)
Type
Thread event type.
Definition: ThreadEvent.h:49
Definition: ThreadPool.h:55
Definition: Thread.h:51
LogPriority
Log message priority.
Definition: LogPriority.h:68

Fri Oct 14 2022 09:58:32: SPTK 5.4.1