-
Notifications
You must be signed in to change notification settings - Fork 0
/
supertcpmanager.h
109 lines (87 loc) · 3.13 KB
/
supertcpmanager.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef SUPERTCPMANAGER_H
#define SUPERTCPMANAGER_H
#include <sys/socket.h>
#include <sys/epoll.h>
#include <vector>
#include <thread>
#include <atomic>
#include <iostream>
#include "epollengineer.h"
#include <memory>
#include <string>
#include <functional>
#include "clientsocket.h"
#include "serversocket.h"
#include <list>
#include <dirent.h>
class Epoll;
class AbstractSocket;
class ServerSocket;
class ClientSocket;
class SuperTcpManager
{
// possible exceptions
public:
struct SuperTcpManagerException : public std::exception {};
struct SuperBindingException : public SuperTcpManagerException {};
struct SuperSocketCreateException : public SuperTcpManagerException {};
struct SuperSocketCloseException : public SuperTcpManagerException {};
struct SuperSocketOptionsException : public SuperTcpManagerException {};
struct SuperListeningException : public SuperTcpManagerException {};
struct SuperEpollException : public SuperTcpManagerException {};
struct SuperConnectException : public SuperTcpManagerException {};
struct SuperInvalidArgumentException : public SuperTcpManagerException {};
struct SuperSendException : public SuperTcpManagerException {};
SuperTcpManager(std::shared_ptr<Epoll> epollEngineer);
SuperTcpManager();
// return id of new connection
ClientSocket *connect(const char *hostname, const char *port, std::function<void(int)> clientFunc, char *clientBuffer, size_t clientBufferSize);
// listen on default port
ServerSocket *listen(std::function<void(ClientSocket *)> newConnection);
ServerSocket *listen(const std::string &port, std::function<void(ClientSocket *)> newConnection);
void close(int fd);
std::shared_ptr<Epoll> getEpollEngineer();
~SuperTcpManager();
const std::string& getServerPort() const;
void setServerPort(const std::string& value);
int getMaxPendingConnections() const;
void setMaxPendingConnections(int value);
const std::string& getServerHostname() const;
void setServerHostname(const std::string& value);
private:
SuperTcpManager(const SuperTcpManager& sp) = delete;
SuperTcpManager(SuperTcpManager&& sp) = delete;
SuperTcpManager& operator=(SuperTcpManager& ep) = delete;
void addSocket(int fd, std::function<void(epoll_event)> callback);
void removeSocket(AbstractSocket* asocket);
void setFdOptions(int fd, unsigned int opt);
static int createAndBind(const char* hostname, const char *port);
static void makeSocketNonBlocking(int sfd);
private:
std::list<std::unique_ptr<AbstractSocket> > sockets;
bool serverRunning;
std::string serverHostname;
std::string serverPort;
int maxPendingConnections;
std::shared_ptr<Epoll> epollEngineer;
friend class Epoll;
friend class ServerSocket;
friend class ClientSocket;
public:
template<typename T, typename ... Args>
static void printMyDebug(T a, Args... args)
{
#ifndef QT_NO_DEBUG
std::cerr << a << " ";
printMyDebug(args ...);
#endif
}
template<typename T>
static void printMyDebug(T a)
{
#ifndef QT_NO_DEBUG
std::cerr << a << "\n";
#endif
}
};
#endif // SUPERTCPMANAGER_H