-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
127 lines (108 loc) · 2.98 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Server.cpp
*
* Author: 314970054 Ariel Barmats &
* 314985474 Amiram Yasif
*/
#include "Server.h"
string socketIO::read(){
char c=0;
size_t i=0;
string s="";
while(c!='\n'){
recv(clientID,&c,sizeof(char),0);
s+=c;
}
return s;
}
void socketIO::write(string text){
const char* txt=text.c_str();
send(clientID,txt,strlen(txt),0);
}
void socketIO::write(float f){
ostringstream ss;
ss <<f;
string s(ss.str());
write(s);
}
void socketIO::read(float* f){
//recv(clientID,f,sizeof(float),0);
// it will be already in the string line
}
Server::Server(int port) throw (const char*) {
isRunning = true;
fd = socket(AF_INET,SOCK_STREAM,0);
if(fd < 0)
throw "Socket failed";
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
//Global namespace bind method.
if(::bind(fd,(struct sockaddr*)&server, sizeof(server)) < 0)
throw "Failed to bind.";
if(listen(fd, 3)<0)
throw "Failed while listening.";
}
void timeoutHandler(int sigNum){
exit(0);
}
pid_t Server::start(ClientHandler& ch) throw (const char*){
/*serverListenerThread=new thread([&ch,this](){
signal(SIGALRM,timeoutHandler);//
while(isRunning){
socklen_t clientSize=sizeof(client);
alarm(2);//
struct timeval timeout;
timeout.tv_sec = 2;
timeout.tv_usec = 0;
if (setsockopt (this->fd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
sizeof timeout) < 0) {
cout << "Failed to set timeout." << endl;
}
int aClient = accept(fd,(struct sockaddr*)&client,&clientSize);
if(aClient>0){
new thread([&aClient,this,&ch](){
ch.handle(aClient);
close(aClient);
});
}
alarm(0); //
}
close(fd);
});*/
server_pid = fork();
//Child process
if (!server_pid) {
signal(SIGALRM, timeoutHandler);//
while (isRunning) {
socklen_t clientSize = sizeof(client);
alarm(5);//
struct timeval timeout;
/*timeout.tv_sec = 2;
timeout.tv_usec = 0;
if (setsockopt(this->fd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
sizeof timeout) < 0) {
cout << "Failed to set timeout." << endl;
}*/
int aClient = accept(fd, (struct sockaddr *) &client, &clientSize);
alarm(0); //
if (aClient > 0) {
new thread([&aClient, this, &ch]() {
ch.handle(aClient);
close(aClient);
});
}
}
}
return server_pid;
}
void Server::stop(){
isRunning = false;
kill(server_pid, SIGALRM);
//serverListenerThread->join(); // do not delete this!
//serverListenerThread->detach();
//close(fd);
}
Server::~Server() {
// TODO Auto-generated destructor stub
}