sptk2 logo
SPTK Home Page
SocketPool.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/Exception.h>
30#include <sptk5/net/BaseSocket.h>
31#include <sptk5/threads/Thread.h>
32
33#include <functional>
34#include <map>
35#include <mutex>
36
37#ifdef _WIN32
38
39// Windows
40#include <WS2tcpip.h>
41#include <WinSock2.h>
42#include <Windows.h>
43#include <wepoll.h>
44using SocketEvent = epoll_event;
45
46#else
47
48#if __linux__ == 1
49// Linux
50#include <sys/epoll.h>
51
52using SocketEvent = epoll_event;
53
54#else
55// BSD
56#include <sys/event.h>
57using SocketEvent = kevent;
58
59#endif
60#endif
61
62namespace sptk {
63
67enum class SocketEventType : uint8_t
68{
69 UNKNOWN,
70 HAS_DATA,
71 CONNECTION_CLOSED
72};
73
77using SocketEventCallback = std::function<void(uint8_t* userData, SocketEventType eventType)>;
78
79#ifdef _WIN32
80#define INVALID_EPOLL nullptr
81#else
82#define INVALID_EPOLL INVALID_SOCKET
83#endif // _WIN32
84
92class SP_EXPORT SocketPool
93 : public std::mutex
94{
95public:
100 explicit SocketPool(const SocketEventCallback& eventsCallback);
101
105 SocketPool(const SocketPool&) noexcept = delete;
106
110 SocketPool& operator=(const SocketPool&) = delete;
111
115 void open();
116
121
127 void waitForEvents(std::chrono::milliseconds timeout) const;
128
132 void close();
133
139 void watchSocket(BaseSocket& socket, uint8_t* userData);
140
146
150 [[nodiscard]] bool active() const;
151
152private:
156#ifdef _WIN32
157 HANDLE m_pool {INVALID_EPOLL};
158#else
159 SOCKET m_pool {INVALID_EPOLL};
160#endif // _WIN32
161
165 SocketEventCallback m_eventsCallback;
166
170 std::map<BaseSocket*, std::shared_ptr<SocketEvent>> m_socketData;
171};
172
173} // namespace sptk
Definition: BaseSocket.h:87
Definition: SocketPool.h:94
void forgetSocket(BaseSocket &socket)
SocketPool & operator=(const SocketPool &)=delete
void watchSocket(BaseSocket &socket, uint8_t *userData)
SocketPool(const SocketPool &) noexcept=delete
void waitForEvents(std::chrono::milliseconds timeout) const
bool active() const
SocketPool(const SocketEventCallback &eventsCallback)

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