-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathmain.c
150 lines (139 loc) · 4.08 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
// Copyright (c) 2018-2020 The EFramework Project
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <stdio.h>
#include <signal.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include "framework.h"
ef_runtime_t efr = {0};
#define BUFFER_SIZE 8192
// for performance test
// forward port 8080 <=> 80, HTTP GET only
// let a http server run at localhost:80
// use HTTP/1.0 or set http header 'Connection: Close'
long forward_proc(int fd, ef_routine_t *er)
{
char buffer[BUFFER_SIZE];
ssize_t r = ef_routine_read(er, fd, buffer, BUFFER_SIZE);
if(r <= 0)
{
return r;
}
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr_in = {0};
addr_in.sin_family = AF_INET;
addr_in.sin_port = htons(80);
int ret = ef_routine_connect(er, sockfd, (const struct sockaddr *)&addr_in, sizeof(addr_in));
if(ret < 0)
{
return ret;
}
ssize_t w = ef_routine_write(er, sockfd, buffer, r);
if(w < 0)
{
goto exit_proc;
}
while(1)
{
r = ef_routine_read(er, sockfd, buffer, BUFFER_SIZE);
if(r <= 0)
{
break;
}
ssize_t wrt = 0;
while(wrt < r)
{
w = ef_routine_write(er, fd, &buffer[wrt], r - wrt);
if(w < 0)
{
goto exit_proc;
}
wrt += w;
}
}
exit_proc:
ef_routine_close(er, sockfd);
return ret;
}
long greeting_proc(int fd, ef_routine_t *er)
{
char resp_ok[] = "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 26\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nWelcome to the EFramework!";
char buffer[BUFFER_SIZE];
ssize_t r = ef_routine_read(er, fd, buffer, BUFFER_SIZE);
if(r <= 0)
{
return r;
}
r = sizeof(resp_ok) - 1;
ssize_t wrt = 0;
while(wrt < r)
{
ssize_t w = ef_routine_write(er, fd, &resp_ok[wrt], r - wrt);
if(w < 0)
{
return w;
}
wrt += w;
}
return 0;
}
void signal_handler(int num)
{
efr.stopping = 1;
}
int main(int argc, char *argv[])
{
if (ef_init(&efr, 64 * 1024, 256, 512, 1000 * 60, 16) < 0) {
return -1;
}
struct sigaction sa = {0};
sa.sa_handler = signal_handler;
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
return -1;
}
struct sockaddr_in addr_in = {0};
addr_in.sin_family = AF_INET;
addr_in.sin_port = htons(8080);
int retval = bind(sockfd, (const struct sockaddr *)&addr_in, sizeof(addr_in));
if(retval < 0)
{
return -1;
}
listen(sockfd, 512);
ef_add_listen(&efr, sockfd, forward_proc);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
return -1;
}
addr_in.sin_port = htons(80);
retval = bind(sockfd, (const struct sockaddr *)&addr_in, sizeof(addr_in));
if(retval < 0)
{
return -1;
}
listen(sockfd, 512);
ef_add_listen(&efr, sockfd, greeting_proc);
return ef_run_loop(&efr);
}