-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
336 lines (285 loc) · 7.66 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <sys/time.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
typedef void (*fs_callback)(void*);
typedef void (*callback)();
typedef struct {
double time;
callback c;
} Timer;
typedef struct {
int top;
Timer** array;
} Stack;
typedef struct {
int rear;
int front;
void** array;
} Queue;
typedef struct {
char* path;
void* content;
callback c;
pthread_t thread;
int connection;
int port;
} File;
typedef struct {
int top;
File** array;
} FSStack;
typedef struct {
double time;
Stack* timers;
Stack* microtasks;
FSStack* fs;
Queue* queue;
} Loop;
Loop loop;
struct timeval tv;
void enQueue(void* value) {
if (loop.queue->front == -1) loop.queue->front = 0;
loop.queue->rear++;
loop.queue->array[loop.queue->rear] = value;
}
void deQueue() {
loop.queue->front++;
if (loop.queue->front > loop.queue->rear) {
loop.queue->front = loop.queue->rear = -1;
}
}
void uv_loop_init() {
gettimeofday(&tv, NULL);
loop.time = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
Stack* s = (Stack*)malloc(sizeof(Stack));
s->top = -1;
s->array = malloc(10 * sizeof(Timer));
loop.timers = s;
Stack* m = (Stack*)malloc(sizeof(Stack));
m->top = -1;
m->array = malloc(10 * sizeof(Timer));
loop.microtasks = m;
FSStack* fs = (FSStack*)malloc(sizeof(FSStack));
fs->top = -1;
fs->array = malloc(10 * sizeof(File));
loop.fs = fs;
Queue* queue = (Queue*)malloc(sizeof(Queue));
queue->front = -1;
queue->rear = -1;
queue->array = malloc(10 * sizeof(void*));
loop.queue = queue;
}
void uv_run_timers() {
for (int i = 0; i < loop.timers->top + 1; i++) {
Timer* t = loop.timers->array[i];
if (t->time < loop.time) {
t->c();
if (loop.timers->top > -1) {
for(int j = i; j < loop.timers->top; j++) {
loop.timers->array[j] = loop.timers->array[j + 1];
}
loop.timers->top--;
i--;
}
}
}
}
void uv_run_tick() {
for (int i = 0; i < loop.microtasks->top + 1; i++) {
Timer* t = loop.microtasks->array[i];
t->c();
if (loop.microtasks->top > -1) {
for(int j = i; j < loop.microtasks->top; j++) {
loop.microtasks->array[j] = loop.microtasks->array[j + 1];
}
loop.microtasks->top--;
i--;
}
}
}
void uv_set_timer(callback c, double time) {
Timer* t = malloc(sizeof(Timer));
t->time = loop.time + time;
t->c = c;
loop.timers->array[++loop.timers->top] = t;
}
void uv_next_tick(callback c) {
Timer* t = malloc(sizeof(Timer));
t->time = 0;
t->c = c;
loop.microtasks->array[++loop.microtasks->top] = t;
}
void* fs_read(void* data) {
File* t = data;
FILE *stream;
stream = fopen("test.txt", "r");
if (stream == NULL) {
exit(1);
}
fseek(stream, 0, SEEK_END);
size_t inputSize = ftell(stream);
void* p = malloc(inputSize);
memcpy(&(t->content), &p, sizeof(void*));
fseek(stream, 0, SEEK_SET);
fread(p, sizeof(char), inputSize, stream);
fclose(stream);
enQueue(t);
return NULL;
}
void uv_fs_read(char* path, fs_callback c) {
File* t = malloc(sizeof(File));
t->c = c;
t->path = path;
t->content = malloc(sizeof(void*));
loop.fs->array[++loop.fs->top] = t;
int status = pthread_create(&(t->thread), NULL, fs_read, t);
}
void* response(void* data) {
File* t = data;
char* p = malloc(1024 * 16);
char* s = p;
memcpy(&(t->content), &p, sizeof(void*));
char* res = "HTTP/1.1 200 Ok\r\nContent-Length: 6\r\nContent-Type: text/html; charset=UTF-8\r\n\r\nTest\r\n";
bool reading = true;
while(reading) {
int bytes = recv( t->connection, p, 1024 * 16, 0);
p += bytes;
if (strstr(s, "\r\n\r\n") != NULL) {
reading = false;
}
}
send(t->connection, res, strlen(res), 0);
close(t->connection);
enQueue(t);
return NULL;
}
void* run_server(void* data) {
File* t = data;
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(t->port);
bind(listenfd, (struct sockaddr*)&address, sizeof(address));
listen(listenfd, 10);
while (true) {
int connfd = accept(listenfd, NULL, NULL);
pthread_t thread;
t->connection = connfd;
int status = pthread_create(&thread, NULL, response, t);
}
return NULL;
}
void uv_tcp_listen(int port, fs_callback c) {
File* t = malloc(sizeof(File));
t->c = c;
t->port = port;
t->content = malloc(sizeof(void*));
loop.fs->array[++loop.fs->top] = t;
int status = pthread_create(&(t->thread), NULL, run_server, t);
}
void* request(void* data) {
File* t = data;
char* url = t->path;
struct addrinfo *res;
struct addrinfo hints;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo(url, "80", &hints, &res);
char* p = malloc(1024 * 5);
memcpy(&(t->content), &p, sizeof(void*));
char req[50];
sprintf(req, "GET / HTTP/1.1\r\nHost: %s\r\n\r\n", url);
struct sockaddr_in addr;
int sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr.s_addr = inet_addr(url);
connect(sock, res->ai_addr, res->ai_addrlen);
send(sock, req, 50, 0);
while (recv(sock, p, 1024 * 5, 0) > 0) {};
enQueue(t);
close(sock);
return NULL;
}
void uv_request(char* url, fs_callback c) {
File* t = malloc(sizeof(File));
t->c = c;
t->path = url;
t->content = malloc(sizeof(void*));
loop.fs->array[++loop.fs->top] = t;
int status = pthread_create(&(t->thread), NULL, request, t);
}
void uv_update_time() {
gettimeofday(&tv, NULL);
loop.time = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
}
void uv_poll() {
if(loop.queue->rear == -1) {
return;
}
for (int i = loop.queue->front; i <= loop.queue->rear; i++) {
File* t = loop.queue->array[i];
deQueue();
t->c(t->content);
if (loop.fs->top > -1) {
for(int j = i; j < loop.fs->top; j++) {
loop.fs->array[j] = loop.fs->array[j + 1];
}
loop.fs->top--;
i--;
}
}
}
void uv_run() {
bool alive = true;
while(alive) {
uv_update_time();
uv_run_timers();
uv_run_tick();
uv_poll();
uv_run_tick();
if (loop.fs->top == -1 && loop.timers->top == -1) {
alive = false;
}
}
}
void timer_callback() {
printf("I'm a timer\n");
fflush(stdout);
}
void tick_callback() {
printf("I'm a tick\n");
fflush(stdout);
}
void read_fs_callback(void* data) {
printf("I'm a file: %s\n", (char*)data);
fflush(stdout);
}
void listen_callback(void* data) {
printf("I'm a request: %s\n", (char*)data);
fflush(stdout);
}
void request_callback(void* data) {
printf("I'm a response: %s\n", (char*)data);
fflush(stdout);
}
int main() {
uv_loop_init();
uv_request("example.com", request_callback);
uv_next_tick(tick_callback);
uv_set_timer(timer_callback, 1000);
uv_set_timer(timer_callback, 2000);
uv_set_timer(timer_callback, 3000);
uv_fs_read("test.txt", read_fs_callback);
uv_tcp_listen(9000, listen_callback);
uv_run();
}