-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccepter.cpp
87 lines (68 loc) · 2.13 KB
/
Accepter.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
#include "Accepter.h"
#include "Nimble.h"
//#define USEBUFFER
Accepter::Accepter(Nimble& nimble, int port) : _nimble(nimble) {
_port = port;
_listenFd = -1;
setup();
#ifdef USEBUFFER
ThreadManager::Worker(new HandleBufferCommand(this), "HandleBufferCommand");
#endif
}
Accepter::~Accepter() {
if(_listenFd != -1) {
::close(_listenFd);
}
}
void Accepter::handleFdBuffer(void) {
while(1) {
int fd = _fdBuffer.pop();
_nimble.newConnection(fd);
/*
if(fd != 0) {
_nimble.newConnection(fd);
} else {
usleep(1000);
}*/
}
}
void Accepter::run(void) {
if(_listenFd != -1) {
cout << "Ready to accept new clients on port: #" << _port << endl;
sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
while(1) {
int newsockfd = ::accept(_listenFd, (sockaddr *) &cli_addr, &clilen);
#ifdef USEBUFFER
_fdBuffer.push(newsockfd);
#else
_nimble.newConnection(newsockfd);
#endif
}
} else {
throw string("Cannot run accepter thread as it's not setup yet. Call setup() first.");
}
}
// meh
void Accepter::setup() {
sockaddr_in serv_addr;
_listenFd = ::socket(AF_INET, SOCK_STREAM, 0);
if (_listenFd < 0) {
cout << "unable to open socket." << endl;
}
int yes = 1;
int no = 0;
if((::setsockopt(_listenFd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) || // prevents the "port in use" err.
(::setsockopt(_listenFd, SOL_SOCKET, SO_KEEPALIVE, &no, sizeof(int)) == -1 )){
cout << "Error setting option :(\n";
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY; // TODO: variable.
serv_addr.sin_port = htons(_port);
if (::bind(_listenFd, (sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
cout << "ERROR on binding. Perhaps port is taken? Port tried:" << _port << endl;
}
if (::listen(_listenFd, 10) < 0) {
cout << "ERROR unable to listen on port" << _port << endl;
}
}