ProxyServerTcp / tcp_inbound.cpp
xukc
[fix]support tcp
a8ba146
raw
history blame
1.54 kB
#include "include/tcp_inbound.h"
#include "include/conn_map.h"
#include "include/tcp_client.h"
#include "hv/hsocket.h"
#include "hv/hthread.h"
#include "hv/TcpClient.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 = ConnMap<hio_t*, TcpClientBolt>::getInstance().get(io);
if(cli) {
ConnMap<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 = ConnMap<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));
ConnMap<hio_t*, TcpClientBolt>::getInstance().add(io, std::make_unique<TcpClientBolt>(io));
hio_setcb_close(io, tcp_on_close);
hio_setcb_read(io, tcp_on_recv);
hio_read(io);
}