#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 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::getInstance().get(io); if(cli) { cli->close(); TcpConnMap::getInstance().remove(io); } } static void tcp_on_recv(hio_t* io, void* buf, int readbytes) { spdlog::info("tcp_on_recv fd={} buf({})={}", hio_fd(io), readbytes, (const char*)buf); auto cli = TcpConnMap::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(io); TcpConnMap::getInstance().add(io, client); hio_setcb_close(io, tcp_on_close); hio_setcb_read(io, tcp_on_recv); hio_read(io); }