// File: theuni[P]libbtcnet/src/connectionbase.cpp
// Function: ConnectionBase::InitConnection()
void ConnectionBase::InitConnection()
{
    assert(m_bev);
    const CConnection& conn = m_connection;
    const CConnectionOptions& opts = conn.GetOptions();
    const CNetworkConfig& netconfig = m_connection.GetNetConfig();

    evutil_socket_t sock = bufferevent_getfd(m_bev);
    SetSocketOpts(sock);

    bufferevent_disable(m_bev, EV_READ | EV_WRITE);

    // Set the connection's initial timeout. If not connected before this
    // expires, the bufferevent's event callback will fire and it will be
    // disconnected.
    timeval initialTimeout;
    initialTimeout.tv_sec = opts.nInitialTimeout;
    initialTimeout.tv_usec = 0;
    bufferevent_set_timeouts(m_bev, &initialTimeout, &initialTimeout);

    // Set an event for resetting the timeout to the normal send/receive values.
    // This will fire once any data is read from/written to the connection.
    m_first_data_func.reset(m_event_base, sock, EV_READ | EV_WRITE, std::bind(&ConnectionBase::FirstDataInt, this));

    size_t min_read;
    size_t max_read;
    // Setup the read callback for chunks if a chunksize is provided,
    // messages if a headersize is provided, or nothing otherwise.
    if (netconfig.chunk_size > 0) {
        read_cb_ptr = &read_cb_chunk;
        min_read = netconfig.chunk_size;
        max_read = 0;
    } else if (netconfig.header_size > 0) {
        read_cb_ptr = &read_cb_message;
        min_read = netconfig.header_msg_size_offset + netconfig.header_msg_size_size;
        max_read = netconfig.message_max_size;
    } else {
        read_cb_ptr = nullptr;
        min_read = 0;
        max_read = 0;
    }
    bufferevent_setwatermark(m_bev, EV_READ, min_read, max_read);
    bufferevent_setwatermark(m_bev, EV_WRITE, opts.nMaxSendBuffer, 0);

    bufferevent_setcb(m_bev, read_cb_ptr, nullptr, event_cb, this);

    // Add an additional set of callbacks responsible for reporting _all_
    // socket reads/writes, as opposed to the bufferevent read callback, which
    // has a watermark set.
    evbuffer* input = bufferevent_get_input(m_bev);
    evbuffer* output = bufferevent_get_output(m_bev);
    evbuffer_add_cb(input, read_data, this);
    evbuffer_add_cb(output, wrote_data, this);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
addq $0x1a8, %rdi            # imm = 0x1A8
callq 0x19d80
testb $0x1, %al
jne 0x1b4fc
jmp 0x1b4fe
jmp 0x1b51d
leaq 0xb31e(%rip), %rdi      # 0x26823
leaq 0xb46c(%rip), %rsi      # 0x26978
movl $0xb7, %edx
leaq 0xb5cb(%rip), %rcx      # 0x26ae3
callq 0x52e0
movq -0xc0(%rbp), %rax
addq $0x18, %rax
movq %rax, -0xf8(%rbp)
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0xa9c0
movq -0xf8(%rbp), %rdi
movq %rax, -0x18(%rbp)
callq 0xa9e0
movq -0xc0(%rbp), %rdi
movq %rax, -0x20(%rbp)
addq $0x1a8, %rdi            # imm = 0x1A8
movq %rdi, -0xf0(%rbp)
callq 0x1a1a0
movq %rax, %rdi
callq 0x51d0
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %edi
callq 0x1b450
movq -0xf0(%rbp), %rdi
callq 0x1a1a0
movq %rax, %rdi
movl $0x6, %esi
movl %esi, -0xcc(%rbp)
callq 0x55e0
movq -0xf0(%rbp), %rdi
movq -0x18(%rbp), %rax
movslq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
callq 0x1a1a0
movq %rax, %rdi
leaq -0x38(%rbp), %rdx
movq %rdx, %rsi
callq 0x55d0
movq -0xc0(%rbp), %rax
movq %rax, %rcx
addq $0x280, %rcx            # imm = 0x280
movq %rcx, -0xe0(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0xd8(%rbp)
movl -0x24(%rbp), %ecx
movl %ecx, -0xd0(%rbp)
leaq 0x262(%rip), %rcx       # 0x1b860
movq %rcx, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movq %rax, -0x88(%rbp)
leaq -0x70(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
leaq -0x80(%rbp), %rsi
leaq -0x88(%rbp), %rdx
callq 0x1c570
movq -0xe8(%rbp), %rsi
leaq -0x58(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x1c5b0
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movl -0xd0(%rbp), %edx
movl -0xcc(%rbp), %ecx
movq -0xc8(%rbp), %r8
callq 0x238a0
jmp 0x1b66b
leaq -0x58(%rbp), %rdi
callq 0x11120
movq -0x20(%rbp), %rax
cmpl $0x0, 0x28(%rax)
jle 0x1b6d2
movq -0xc0(%rbp), %rax
leaq 0x244(%rip), %rcx       # 0x1b8d0
movq %rcx, 0x2a8(%rax)
movq -0x20(%rbp), %rax
movslq 0x28(%rax), %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
jmp 0x1b741
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x11120
jmp 0x1b850
movq -0x20(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jle 0x1b717
movq -0xc0(%rbp), %rax
leaq 0x406(%rip), %rcx       # 0x1baf0
movq %rcx, 0x2a8(%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movq -0x20(%rbp), %rcx
addl 0x4(%rcx), %eax
cltq
movq %rax, -0xa0(%rbp)
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
movq %rax, -0xa8(%rbp)
jmp 0x1b73f
movq -0xc0(%rbp), %rax
movq $0x0, 0x2a8(%rax)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
jmp 0x1b741
movq -0xc0(%rbp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
callq 0x1a1a0
movq %rax, %rdi
movq -0xa0(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movl $0x2, %esi
callq 0x50f0
movq -0xc0(%rbp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
callq 0x1a1a0
movq %rax, %rdi
movq -0x18(%rbp), %rax
movslq 0x1c(%rax), %rdx
movl $0x4, %esi
xorl %eax, %eax
movl %eax, %ecx
callq 0x50f0
movq -0xc0(%rbp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
callq 0x1a1a0
movq -0xc0(%rbp), %r8
movq %rax, %rdi
movq 0x2a8(%r8), %rsi
xorl %eax, %eax
movl %eax, %edx
leaq -0x43a(%rip), %rcx      # 0x1b390
callq 0x5410
movq -0xc0(%rbp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
callq 0x1a1a0
movq %rax, %rdi
callq 0x5300
movq -0xc0(%rbp), %rdi
movq %rax, -0xb0(%rbp)
addq $0x1a8, %rdi            # imm = 0x1A8
callq 0x1a1a0
movq %rax, %rdi
callq 0x5140
movq -0xc0(%rbp), %rdx
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
leaq 0x708(%rip), %rsi       # 0x1bf30
callq 0x5270
movq -0xc0(%rbp), %rdx
movq -0xb8(%rbp), %rdi
leaq 0x7de(%rip), %rsi       # 0x1c020
callq 0x5270
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x90(%rbp), %rdi
callq 0x5710
nopl (%rax)