Spaces:
Paused
Paused
File size: 1,523 Bytes
8122175 83607bc fc28fd3 83607bc 506d0f7 a8ba146 1bff97b a8ba146 99dc157 a8ba146 83607bc c8aad12 1bff97b 83607bc a8ba146 c8aad12 83607bc 506d0f7 83607bc 1bff97b a8ba146 83607bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#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) {
cli->close();
TcpConnMap<hio_t*, TcpClientBolt>::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<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);
} |