-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
81 lines (67 loc) · 1.87 KB
/
main.cpp
File metadata and controls
81 lines (67 loc) · 1.87 KB
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
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <iostream>
#include <signal.h>
#include "thread_pool.hpp"
#define ERROR -1
#define NUM_THREADS 2
int main(int argc, char** argv){
if (argc < 2 || argc > 3){
std::cerr << "proxy usage:\n./proxy <port to listen connections> <num_threads>\n";
return 0;
}
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGPIPE);
if (pthread_sigmask(SIG_BLOCK, &set, NULL)){
perror("can't modify signal mask");
return ERROR;
}
init_global_storage();
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1){
perror("create socket");
return ERROR;
}
struct sockaddr_in listen_addr;
memset(&listen_addr, 0x0, sizeof(listen_addr));
int port = std::stoi(argv[1]);
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
listen_addr.sin_port = htons(port);
if (bind(sock, (struct sockaddr*) &listen_addr, sizeof(listen_addr))){
perror("bind socket");
close(sock);
return ERROR;
}
if (listen(sock, 5)){
perror("make socket listen");
close(sock);
return -1;
}
size_t num_threads = NUM_THREADS;
if (argc == 3){
num_threads = std::stoul(argv[2]);
if (num_threads == 0){
std::cerr << "expected not 0 number of threads\n";
return ERROR;
}
}
thread_pool_t thread_pool(num_threads);
for(;;){
int sock_fd = accept(sock, NULL, NULL);
if (sock_fd == -1){
perror("accept new connection");
continue;
}
thread_pool.add_new_connection(sock_fd);
std::clog << "new socket " << sock_fd << "\n";
}
return 0;
}