/
main.c
54 lines (48 loc) · 1.1 KB
/
main.c
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
#include <sys/socket.h>
#include <stdio.h>
#include "config.h"
#include "setup_tcp.h"
#include "thread_handle.h"
#include "accept_list.h"
#include "fd_pair.h"
#include <arpa/inet.h>
#include <fcntl.h>
int main(void)
{
extern pthread_mutex_t accept_mutex;
extern pthread_mutex_t fp_mutex;
extern struct accept_list *AL;
if (read_config() < 0)
{
return 1;
}
pthread_mutex_init(&accept_mutex, NULL);
pthread_mutex_init(&fp_mutex, NULL);
int sockfd;
sockfd = setup_tcp();
if (sockfd < 0)
{
return 1;
}
int i;
for(i = 0; i < thread_count; i++)
{
pthread_t pthread;
pthread_create(&pthread, NULL, thread_handle, NULL);
}
int fd;
struct sockaddr_in sock_addr;
socklen_t len = sizeof(sock_addr);
while(1)
{
len = sizeof(sock_addr);
fd = accept(sockfd, (struct sockaddr*)&sock_addr, &len);
if (fd > 0)
{
pthread_mutex_lock(&accept_mutex);
AL = accept_list_add(AL, fd);
pthread_mutex_unlock(&accept_mutex);
}
}
return 0;
}