sptk2 logo
SPTK Home Page
ServerConnection.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 <sptk5/net/TCPSocket.h>
30#include <sptk5/threads/Runable.h>
31#include <sptk5/threads/Thread.h>
32#include <functional>
33
34#ifndef _WIN32
35
36#include <netinet/in.h>
37
38#endif
39
40namespace sptk {
41
42class TCPServer;
43
54class SP_EXPORT ServerConnection
55 : public Runable
56{
57 friend class TCPServer;
58
59public:
60 enum class Type
61 {
62 TCP,
63 SSL
64 };
65
66 using Function = std::function<void(const Runable& task, TCPSocket& socket, const String& address)>;
67
75 ServerConnection(TCPServer& server, SOCKET connectionSocket, Type type, const sockaddr_in* connectionAddress,
76 const String& taskName, const ServerConnection::Function& connectionFunction = {});
77
82 TCPSocket& socket() const;
83
88 TCPServer& server() const;
89
95 {
96 return m_address;
97 }
98
103 size_t serial() const
104 {
105 return m_serial;
106 }
107
108protected:
113 void setSocket(const STCPSocket& socket);
114
115 void parseAddress(const sockaddr_in* connectionAddress);
116
117 void run() override
118 {
119 if (m_connectionFunction)
120 {
121 m_connectionFunction(*this, socket(), address());
122 }
123 }
124
125private:
126 mutable std::mutex m_mutex;
127 TCPServer& m_server;
128 STCPSocket m_socket;
129 String m_address;
130 size_t m_serial {0};
131 Type m_type;
132 ServerConnection::Function m_connectionFunction;
133
138 static size_t nextSerial();
139};
140
141using SServerConnection = std::shared_ptr<ServerConnection>;
142
146} // namespace sptk
Definition: Runable.h:48
Definition: ServerConnection.h:56
void setSocket(const STCPSocket &socket)
TCPServer & server() const
String address() const
Definition: ServerConnection.h:94
ServerConnection(TCPServer &server, SOCKET connectionSocket, Type type, const sockaddr_in *connectionAddress, const String &taskName, const ServerConnection::Function &connectionFunction={})
void run() override
Definition: ServerConnection.h:117
TCPSocket & socket() const
size_t serial() const
Definition: ServerConnection.h:103
Definition: String.h:49
Definition: TCPServer.h:133
Definition: TCPSocket.h:165

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