114 lines
2.7 KiB
C
114 lines
2.7 KiB
C
|
|
#include <hv.h>
|
|
#include <hloop.h>
|
|
#include <hsocket.h>
|
|
#include <hssl.h>
|
|
|
|
#define TEST_UNPACK 0
|
|
|
|
#if TEST_UNPACK
|
|
static unpack_setting_t unpack_setting;
|
|
#endif
|
|
|
|
// hloop_create_tcp_server -> on_accept -> hio_read -> on_recv -> hio_write
|
|
|
|
static void on_close(hio_t* io)
|
|
{
|
|
hlogi("on_close fd=%d error=%d", hio_fd(io), hio_error(io));
|
|
}
|
|
|
|
//½ÓÊÕµ½Êý¾Ý
|
|
static void on_recv(hio_t* io, void* buf, int readbytes)
|
|
{
|
|
hlogi("on_recv fd=%d readbytes=%d", hio_fd(io), readbytes);
|
|
char localaddrstr[SOCKADDR_STRLEN] = {0};
|
|
char peeraddrstr[SOCKADDR_STRLEN] = {0};
|
|
hlogi("[%s] <=> [%s]",
|
|
SOCKADDR_STR(hio_localaddr(io), localaddrstr),
|
|
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
|
|
hlogi("<=== %.*s", readbytes, (char*)buf);
|
|
|
|
//// echo
|
|
hlogi("===> %.*s", readbytes, (char*)buf);
|
|
hio_write(io, buf, readbytes);
|
|
|
|
}
|
|
|
|
//½ÓÈë»Øµ÷
|
|
static void on_accept(hio_t* io)
|
|
{
|
|
hlogi("on_accept connfd=%d", hio_fd(io));
|
|
|
|
char localaddrstr[SOCKADDR_STRLEN] = {0};
|
|
char peeraddrstr[SOCKADDR_STRLEN] = {0};
|
|
hlogi("accept connfd=%d [%s] <= [%s]", hio_fd(io),
|
|
SOCKADDR_STR(hio_localaddr(io), localaddrstr),
|
|
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
|
|
|
|
hio_setcb_close(io, on_close);
|
|
hio_setcb_read(io, on_recv);
|
|
|
|
#if TEST_UNPACK
|
|
hio_set_unpack(io, &unpack_setting);
|
|
#endif
|
|
|
|
hio_read_start(io);
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
if (argc < 2)
|
|
{
|
|
printf("Usage: %s port|path\n", argv[0]);
|
|
return -10;
|
|
}
|
|
|
|
const char* host = "0.0.0.0";
|
|
int port = atoi(argv[1]);
|
|
|
|
#if ENABLE_UDS
|
|
if (port == 0)
|
|
{
|
|
host = argv[1];
|
|
port = -1;
|
|
}
|
|
#endif
|
|
|
|
hlog_set_file("/var/log/datahubs.log");
|
|
//# loglevel = [VERBOSE,DEBUG,INFO,WARN,ERROR,FATAL,SILENT]
|
|
hlog_set_level_by_str("DEBUG");
|
|
hlog_set_max_filesize_by_str("10M");
|
|
hlog_set_remain_days(7);
|
|
|
|
// first log here
|
|
hlogi("=========--- Welcome to the Earth ---=========");
|
|
hlogi("%s version: %s", argv[0], hv_compile_version());
|
|
|
|
//hlog_fsync();
|
|
|
|
|
|
#if TEST_UNPACK
|
|
memset(&unpack_setting, 0, sizeof(unpack_setting_t));
|
|
unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
|
|
unpack_setting.mode = UNPACK_BY_DELIMITER;
|
|
unpack_setting.delimiter[0] = '\r';
|
|
unpack_setting.delimiter[1] = '\n';
|
|
unpack_setting.delimiter_bytes = 2;
|
|
#endif
|
|
|
|
hloop_t* loop = hloop_new(0);
|
|
|
|
hio_t* listenio = hloop_create_tcp_server(loop, host, port, on_accept);
|
|
|
|
if (listenio == NULL)
|
|
{
|
|
return -20;
|
|
}
|
|
|
|
hlogi("listenfd=%d", hio_fd(listenio));
|
|
hloop_run(loop);
|
|
hloop_free(&loop);
|
|
hlogi("=========--- I'll be back! ---=========");
|
|
return 0;
|
|
}
|