ProxyServerTcp / src /tcp_inbound.cpp
xukc
[feat]support udp
1bff97b
raw
history blame
1.52 kB
#include "tcp_inbound.h"
#include "conn_map.h"
#include "tcp_client.h"
#include "hv/hsocket.h"
#include "hv/hthread.h"
#include "spdlog/spdlog.h"
#include <string>
static void tcp_on_close(hio_t* io) {
spdlog::info("tcp_on_close fd={} error={}\n", hio_fd(io), hio_error(io));
auto cli = TcpConnMap<hio_t*, TcpClientBolt>::getInstance().get(io);
if(cli) {
TcpConnMap<hio_t*, TcpClientBolt>::getInstance().remove(io);
cli->close();
}
}
static void tcp_on_recv(hio_t* io, void* buf, int readbytes) {
spdlog::info("tcp_on_recv fd={} buf({})={}\n", hio_fd(io), readbytes, (const char*)buf);
auto cli = TcpConnMap<hio_t*, TcpClientBolt>::getInstance().get(io);
if(cli) {
if (cli->hasHandshake())
{
cli->send((char*) buf, readbytes);
} else {
cli->handShake(buf, readbytes);
}
}
}
void tcp_on_accept(hio_t* io, hevent_t* ev) {
hloop_t* loop = ev->loop;
char localaddrstr[SOCKADDR_STRLEN] = {0};
char peeraddrstr[SOCKADDR_STRLEN] = {0};
spdlog::info("tcp_on_accept tid={} connfd={} [{}] <= [{}]\n",
(long)hv_gettid(),
(int)hio_fd(io),
SOCKADDR_STR(hio_localaddr(io), localaddrstr),
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
auto client = std::make_unique<TcpClientBolt>(io);
TcpConnMap<hio_t*, TcpClientBolt>::getInstance().add(io, client);
hio_setcb_close(io, tcp_on_close);
hio_setcb_read(io, tcp_on_recv);
hio_read(io);
}