-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsockets.h
More file actions
98 lines (80 loc) · 2.17 KB
/
sockets.h
File metadata and controls
98 lines (80 loc) · 2.17 KB
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
#ifndef SOCKETS_H
#define SOCKETS_H
#include <string>
#include <vector>
#include <winsock2.h>
#include <windows.h>
using namespace std;
struct Connection{
SOCKET sock;
string ip;
Connection():sock(INVALID_SOCKET){}
};
typedef void(TCPSocketCallback)(Connection c);
string recv(SOCKET s, size_t maxChars=1024);
bool send(SOCKET s, string msg);
void setBlocking(SOCKET sock, bool blocking);
Connection getNewClient(unsigned short port);
class TCPRawServer{
SOCKET _listener;
unsigned short _port;
bool _blocking;
bool _on;
public:
TCPRawServer();
TCPRawServer(unsigned short port);
~TCPRawServer();
bool start(unsigned short port);
void finish();
Connection newClient();
bool newClient(TCPSocketCallback* callback, bool detach = true);
bool isOn()const;
unsigned short getPort()const;
void setBlocking(bool blocking);
bool isBlocking()const;
};
class TCPServer:public TCPRawServer{
struct _client{
SOCKET socket;
string ip;
bool blocking;
vector<string> data;
_client():socket(INVALID_SOCKET),blocking(0){}
};
vector<_client> _clients;
public:
TCPServer();
TCPServer(unsigned short port);
~TCPServer();
bool newClient();
void disconnectClient(size_t clientN);
string recv(size_t clientN, size_t maxChars=1024);
bool send(size_t clientN, string msg);
vector<string>* getData(size_t clientN);
string getIp(size_t clientN)const;
void setBlocking(size_t clientN, bool blocking);
bool isBlocking(size_t clientN)const;
size_t getClientCount()const;
};
class TCPClient{
SOCKET _socket;
string _ip;
unsigned short _port;
bool _connected;
bool _blocking;
public:
TCPClient();
TCPClient(string ip, unsigned short port);
~TCPClient();
bool connect(string ip, unsigned short port);
void connect(SOCKET sock, string ip, unsigned short port);
void disconnect();
string recv(int maxChars=1024);
bool send(const string& msg);
bool isConnected()const;
string getIp()const;
unsigned short getPort()const;
void setBlocking(bool blocking);
bool isBlocking()const;
};
#endif // SOCKETS_H