File size: 1,536 Bytes
83607bc
 
a8ba146
83607bc
 
 
fc28fd3
83607bc
 
 
506d0f7
a8ba146
 
 
 
 
 
83607bc
 
 
506d0f7
a8ba146
83607bc
a8ba146
 
 
 
 
 
 
83607bc
 
 
 
 
 
 
 
506d0f7
83607bc
 
 
 
 
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 "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);
}