-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
163 lines (128 loc) · 4.15 KB
/
main.cpp
File metadata and controls
163 lines (128 loc) · 4.15 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <jorgeLua.h>
#include <jorgeNetwork.h>
#include <pthread.h>
// TODO embed sqlite just for fun?
// TODO implement openssl
// TODO port over to select on thread pools, instead of spawning worker threads
// TODO make it full http compliant
#define PORT "8080" // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue will hold
// A thread running this is spawned for every request
void *req_thread(void *data){
// Copy data from main thread
int conn_fd = *((int *)data);
free(data);
// Get request
char *request; // We need to be able to free the full request data, so we get this
struct jnet_request_data req_data = jnet_read_request(conn_fd, &request);
if(req_data.verb){
printf("%s %s through http/1.%c\n", req_data.verb, req_data.path, req_data.version);
jnet_request_header *walker = req_data.header;
for (int i = 1; walker != NULL; ++i) {
//printf("header %d is : %s : %s\n", i, walker->value, walker->field);
walker = walker->next;
}
//printf("Body is : \n%s\n", req_data.body);
// Defer everything to the Lua subsystem
jlua_interpret(conn_fd, req_data);
}
// Cleanup
jnet_request_header *walker = req_data.header;
for (int i = 1; walker != NULL; ++i) {
jnet_request_header *erasable = walker;
walker = walker->next;
free(erasable);
}
free(request);
close(conn_fd);
fflush(stdout);
return NULL;
}
// Entry point
// Sets up the socket and accepts connections.
// the main loop awaits connections e spawns threads for each request.
int main(void){
// Get server info
struct addrinfo *servinfo;
{
struct addrinfo hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // Works for IPv4 and IPv6
hints.ai_socktype = SOCK_STREAM;// TCP
hints.ai_flags = AI_PASSIVE; // Use system's IP
int status = getaddrinfo(NULL, PORT, &hints, &servinfo);
if (status != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 1;
}
}
// Create default folders and files
jlua_setup_environment();
// Get socket
int sock_fd = -1;
{
struct addrinfo *p;
for(p = servinfo; p != NULL; p = p->ai_next) {
sock_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (sock_fd == -1) {
perror("server: socket");
continue;
}
int reuseAddress=1;
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &reuseAddress,
sizeof(int)) == -1) {
perror("server: sock options");
exit(1);
}
if (bind(sock_fd, p->ai_addr, p->ai_addrlen) == -1) {
close(sock_fd);
perror("server: bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "server: failed socket startup\n");
exit(1);
}
}
freeaddrinfo(servinfo);
//Start socket
if (listen(sock_fd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
// Start main loop
printf("server: waiting for connections...\n");
bool shutdown = false; // connected socket file descriptor
while(!shutdown) { // main accept() loop
char incoming_ip[INET6_ADDRSTRLEN]; // since the biggest ip would be ipv6, we allocate that
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size = sizeof their_addr;
int *conn_fd = (int *) malloc(sizeof(*conn_fd)); //Put this on the heap so we can pass it to the thread
// Blocking accept
// TODO look into select intead of the blocking approach.
// The block here makes sense though
*conn_fd = accept(sock_fd, (struct sockaddr *)&their_addr, &sin_size);
if (*conn_fd == -1) {
perror("accept");
continue;
}
inet_ntop(
their_addr.ss_family,
jnet_get_req_ip((struct sockaddr *)&their_addr),
incoming_ip, sizeof incoming_ip);
// Start manager thread
printf("%s: ", incoming_ip);
pthread_t req_thread_pointer;
pthread_create(&req_thread_pointer, 0, req_thread, conn_fd);
}
return 0;
}