sptk2 logo
SPTK Home Page
TCPServerListener.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 <iostream>
30#include <set>
31#include <sptk5/Logger.h>
32#include <sptk5/net/ServerConnection.h>
33#include <sptk5/threads/SynchronizedQueue.h>
34
35namespace sptk {
36
37class TCPServer;
38
48 : public Thread
49 , public std::mutex
50{
51public:
57 TCPServerListener(TCPServer* server, uint16_t port);
58
62 void threadFunction() override;
63
67 void terminate() override;
68
72 void listen()
73 {
74 if (!running())
75 {
76 m_listenerSocket.listen();
77 run();
78 }
79 }
80
84 uint16_t port() const
85 {
86 return m_listenerSocket.host().port();
87 }
88
92 String error() const
93 {
94 return m_error;
95 }
96
100 void stop();
101
102private:
103 std::shared_ptr<TCPServer> m_server;
104 TCPSocket m_listenerSocket;
105 String m_error;
106
107 void acceptConnection(std::chrono::milliseconds timeout);
108};
109
113} // namespace sptk
void listen(uint16_t portNumber=0)
void host(const Host &host)
Definition: String.h:49
Definition: TCPServerListener.h:50
TCPServerListener(TCPServer *server, uint16_t port)
String error() const
Definition: TCPServerListener.h:92
uint16_t port() const
Definition: TCPServerListener.h:84
void threadFunction() override
void terminate() override
void listen()
Definition: TCPServerListener.h:72
Definition: TCPServer.h:133
Definition: TCPSocket.h:165
Definition: Thread.h:51
bool running() const
virtual void run()

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