-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
110 lines (83 loc) · 2.49 KB
/
Server.cpp
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
110
#include "Server.h"
Server::Server(int port) noexcept(false): port(port) {
// creating the socket
sockID = socket(AF_INET, SOCK_STREAM, 0);
if (sockID < 0) {
throw ("cannot open socket");
}
// creating the struct to hold info
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(port);
// binding the socket
int isBindError = bind(sockID, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
if (isBindError == -1) {
throw ("cannot bind socket");
}
// start listening
int isListenError = listen(sockID, 5);
if (isListenError == -1) {
throw ("cannot listen");
}
isStopped = false;
}
void sigHandler(int sigNum) {
cout << "sidH" << endl;
}
void Server::start(ClientHandler &ch) noexcept(false) {
// Open thread for start.
this->t = new thread([&ch, this]() {
while (!isStopped) {
// trying to accept a connection
socklen_t socketIDLen = sizeof(sockID);
// building the select
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(sockID, &rfds);
timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
int recVal = select(sockID + 1, &rfds, NULL, NULL, &tv);
if (recVal > 0) {
int clientID = accept(sockID, (struct sockaddr *) &clientAddr, &socketIDLen);
ch.handle(clientID);
close(clientID);
}
}
});
}
void Server::stop() {
isStopped = true;
close(sockID);
t->join(); // do not delete this!
}
Server::~Server() {
delete (t);
}
string SocketIO::read() {
// reading a char every time
char buffer = 0;
string result;
// reading first
recv(clientID, &buffer, sizeof(char), 0);
while (buffer != '\n') {
//recv(clientID, &buffer, sizeof(char), 0);
result += buffer;
recv(clientID, &buffer, sizeof(char), 0);
}
return result;
}
void SocketIO::write(string text) {
send(clientID, text.c_str(), text.size(), 0);
}
void SocketIO::write(float f) {
stringstream basicStringstream;
basicStringstream << f;
write(basicStringstream.str());
}
void SocketIO::read(float *f) {
// reading the line
string text = read();
// converting the line to float and inserting into pointer
*f = stof(text);
}