-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.h
58 lines (47 loc) · 1.03 KB
/
server.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
#ifndef __SERVER_H_
#define __SERVER_H_
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
#include <thread>
#include <set>
#include <map>
#include <functional>
#define DEFAULT_PORT "27015"
struct Client {
int id;
SOCKET sock;
bool operator < (const Client &other) const {
return id < other.id;
}
};
class Server {
private:
WSADATA wsaData;
SOCKET ListenSocket;
std::set< Client > clients;
bool done = false;
std::thread clientAccepter;
std::map< int, std::thread > listeners;
int client_id = 0;
std::function<int(std::string, Client)> callback;
void listener(Client c);
void acceptClients();
bool open(SOCKET s);
public:
Server();
Server(std::function< int (std::string, Client)>);
~Server();
int sendto(Client c, std::string s);
void broadcast(std::string s);
void setCallback(std::function< int (std::string, Client)> _callback);
};
#endif