ProxyServerTcp / main.cpp
xukc
[fix]log fmt err
94b5d39
raw
history blame
6 kB
// #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;
}