-
Notifications
You must be signed in to change notification settings - Fork 2
/
Server.h
46 lines (36 loc) · 1.3 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
#pragma once
#include <ws2tcpip.h>
#include "Thread.h"
#pragma comment(lib, "ws2_32.lib")
#define CLIENTS (5) // Maximum number of clients that can be handled at once
using FnPtr = void (*)(Thread &);
using SocketSet = fd_set;
class Server
{
friend void worker(Data *);
friend class Thread;
public:
Server(FnPtr function, int port = 7777) : handler(function), m_port(port) {};
~Server();
bool start();
void run();
private:
WSAData wsaData;
SocketSet sockets;
SOCKET server;
// by default, the server will operate on local host
const std::string m_ipAddress = "127.0.0.1";
const unsigned m_port = 7777;
const FnPtr handler = NULL;
bool exit = false;
Thread pool[CLIENTS]; // Thread pool
HANDLE handles[CLIENTS]; // Thread Handles
void Listen(); // create a listening socket
void processReq(SocketSet &); // handels client request
void createThreads(); // resumes already suspended threads
void threadPool(SOCKET &); // passes the client to thread pool
// extra function;
void updateActivity(SOCKET&); // updates the activity file
std::string getActivity(); // recceive the activity file
void closeSocket(SOCKET&);
};