File size: 5,994 Bytes
83607bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc28fd3
 
 
 
83607bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc28fd3
 
 
 
 
 
 
 
 
 
 
 
 
 
83607bc
 
 
fc28fd3
 
83607bc
 
 
 
 
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
// #include "hv/HttpServer.h"
// using namespace hv;

// int main() {
//     HttpService router;
//     router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
//         print("/ping");
//         return resp->String("pong");
//     });

//     router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
//         print("/data");
//         static char data[] = "0123456789";
//         return resp->Data(data, 10);
//     });

//     router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
//         print("/paths");
//         return resp->Json(router.Paths());
//     });

//     router.GET("/get", [](HttpRequest* req, HttpResponse* resp) {
//         print("/get");
//         resp->json["origin"] = req->client_addr.ip;
//         resp->json["url"] = req->url;
//         resp->json["args"] = req->query_params;
//         resp->json["headers"] = req->headers;
//         hv::Json myArray = hv::Json::array();
//         myArray.push_back("apple");
//         myArray.push_back("banana");
//         myArray.push_back("orange");
//         resp->json["fruits"] = myArray;
//         resp->json["test"]["a"] = "json_serializer";
//         return 200;
//     });

//     router.POST("/echo", [](const HttpContextPtr& ctx) {
//         print(ctx->body());
//         return ctx->send(ctx->body(), ctx->type());
//     });

//     HttpServer server(&router);
//     server.setPort(8080);
//     server.setThreadNum(4);
//     server.run();
//     return 0;
// }

// #include "hv/TcpServer.h"
// using namespace hv;

// int main() {
//     int port = 1234;
//     TcpServer srv;
//     int listenfd = srv.createsocket(port);
//     if (listenfd < 0) {
//         return -1;
//     }
//     printf("server listen on port %d, listenfd=%d ...\n", port, listenfd);
//     srv.onConnection = [](const SocketChannelPtr& channel) {
//         std::string peeraddr = channel->peeraddr();
//         if (channel->isConnected()) {
//             printf("%s connected! connfd=%d\n", peeraddr.c_str(), channel->fd());
//         } else {
//             printf("%s disconnected! connfd=%d\n", peeraddr.c_str(), channel->fd());
//         }
//     };
//     srv.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
//         // echo
//         channel->write(buf);
//     };
//     srv.setThreadNum(4);
//     srv.start();

//     // press Enter to stop
//     while (getchar() != '\n');
//     return 0;
// }

// #include <iostream>
// #include "hv/TcpClient.h"
// using namespace hv;

// int main() {
//     int port = 1234;
//     TcpClient cli;
//     int connfd = cli.createsocket(port);
//     if (connfd < 0) {
//         return -1;
//     }
//     cli.onConnection = [](const SocketChannelPtr& channel) {
//         std::string peeraddr = channel->peeraddr();
//         if (channel->isConnected()) {
//             printf("connected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
//         } else {
//             printf("disconnected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
//         }
//     };
//     cli.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
//         printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
//     };
//     cli.start();

//     std::string str;
//     while (std::getline(std::cin, str)) {
//         if (str == "close") {
//             cli.closesocket();
//         } else if (str == "start") {
//             cli.start();
//         } else if (str == "stop") {
//             cli.stop();
//             break;
//         } else {
//             if (!cli.isConnected()) break;
//             cli.send(str);
//         }
//     }
//     return 0;
// }

/*
 *
 * @build   make examples
 * @server  bin/one-acceptor-multi-workers 1234
 * @client  bin/nc 127.0.0.1 1234
 *          nc     127.0.0.1 1234
 *          telnet 127.0.0.1 1234
 */

#include "hv/hloop.h"
#include "hv/hthread.h"
#include "include/hv_utils.h"
#include "include/tcp_inbound.h"

#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/sinks/stdout_color_sinks.h"

static const char* host = "0.0.0.0";
static int port = 8080;
static int thread_num = 4;
static hloop_t*  accept_loop = NULL;

static void new_conn_event(hevent_t* ev) {
    hloop_t* loop = ev->loop;
    hio_t* io = (hio_t*)hevent_userdata(ev);
    hio_attach(loop, io);

    tcp_on_accept(io, ev);
}

static void on_accept(hio_t* io) {
    hio_detach(io);

    hloop_t* worker_loop = get_next_loop();
    hevent_t ev;
    memset(&ev, 0, sizeof(ev));
    ev.loop = worker_loop;
    ev.cb = new_conn_event;
    ev.userdata = io;
    hloop_post_event(worker_loop, &ev);
}

static HTHREAD_RETTYPE worker_thread(void* userdata) {
    hloop_t* loop = (hloop_t*)userdata;
    hloop_run(loop);
    return 0;
}

static HTHREAD_RETTYPE accept_thread(void* userdata) {
    hloop_t* loop = (hloop_t*)userdata;
    hio_t* listenio = hloop_create_tcp_server(loop, host, port, on_accept);
    if (listenio == NULL) {
        exit(1);
    }
    hloop_run(loop);
    return 0;
}

int main(int argc, char** argv) {
    // if (argc < 2) {
    //     printf("Usage: cmd port\n");
    //     return -10;
    // }
    // port = atoi(argv[1]);
    int cores = std::thread::hardware_concurrency();
    if (cores > 0) {
        thread_num = cores;
    }
    
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    auto logger = std::make_shared<spdlog::logger>("my_logger", console_sink);

    // 设置异步模式,设置线程数(0 表示使用 CPU 核心数)
    spdlog::init_thread_pool(8192, thread_num);

    // 设置异步日志器
    spdlog::set_default_logger(std::make_shared<spdlog::async_logger>(
        "ProxyServer", console_sink, spdlog::thread_pool(), spdlog::async_overflow_policy::block));

    init_loop(thread_num, worker_thread);

    spdlog::info("ProxyServer start: threadNum:%d", thread_num);

    accept_loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
    accept_thread(accept_loop);

    return 0;
}