-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketmultiplex.h
73 lines (65 loc) · 2.37 KB
/
socketmultiplex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* dlnatunnel
* Copyright (C) 2023 Stefan Wildemann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LIBSOCKETMULTIPLEX_H
#define __LIBSOCKETMULTIPLEX_H
#include <stdint.h>
#include <vector>
#include <functional>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <vector>
struct listener_helper {
std::function<void(int socket)> f{};
int socket{0};
uint16_t listen_port;
};
struct socket_helper {
std::function<bool(int socket)> f{};
std::function<void(int socket, bool enabled)> onChoke{};
int socket{0};
std::vector<uint8_t> writebuffer{};
bool choked{false};
bool choke_requested{false};
};
class socketmultiplex {
public:
socketmultiplex();
~socketmultiplex();
int add_udp_mcast_listener(const char * url, uint16_t port, std::function<bool(int socket)> f);
int connect_port(const char * url, uint16_t port, std::function<bool(int socket)> f);
int add_port_listener(uint16_t listen_port, std::function<void(int socket)> f);
void remove_port_listener(uint16_t listen_port);
int register_socket_callback(int socket, std::function<bool(int socket)> f);
void remove_socket_callback(int socket);
void add_socket_choke(uint16_t socket, std::function<void(int socket, bool enabled)> onChoke);
ssize_t awrite(int socket, const void *buf, size_t count, bool block=false);
void choke(int socket, bool enable);
void handle_sockets(struct timeval tv);
private:
void remove_attempt(int socket);
std::vector<listener_helper> listener;
std::vector<socket_helper> connections;
std::vector<socket_helper> attempts;
bool m_processing_listener{false};
bool m_processing_connections{false};
bool m_processing_attempts{false};
};
#endif