summaryrefslogtreecommitdiff
path: root/ecp/test/init_vconn.c
blob: ba72c01686917e598050bf1906804db9a365ae70 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

#include <ecp/core.h>
#include <ecp/vconn/vconn.h>

static void handle_err(ECPConnection *conn, unsigned char mtype, int err) {
    printf("ERR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err);
}

static ECPConnection *conn_new(ECPSocket *sock, ECPConnection *parent, unsigned char type) {
    ECPConnection *conn = NULL;

    switch (type) {
#ifdef ECP_WITH_HTABLE
        case ECP_CTYPE_VCONN: {
            ECPVConnInb *_conn;

            _conn = malloc(sizeof(ECPVConnInb));
            if (_conn) {
                ecp_vconn_init_inb(_conn, sock);
                conn = &_conn->b;
            }
            break;
        }
#endif

        case ECP_CTYPE_VLINK: {
            conn = malloc(sizeof(ECPConnection));
            if (conn) ecp_vlink_init(conn, sock);
            break;
        }

        default: {
            conn = malloc(sizeof(ECPConnection));
            if (conn) ecp_conn_init(conn, sock, type);
            break;
        }
    }

    if (conn) ecp_conn_set_flags(conn, ECP_CONN_FLAG_GC);
    return conn;
}

static void conn_free(ECPConnection *conn) {
    /* outbound connections are statically allocated */
    if (ecp_conn_is_inb(conn)) free(conn);
}

int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler) {
    int rv;

    rv = ecp_ctx_init(ctx, NULL, conn_new, conn_free, handle_err);
    if (rv) return rv;

    rv = ecp_vconn_handler_init(ctx, vconn_handler);
    if (rv) return rv;

    rv = ecp_vlink_handler_init(ctx, vlink_handler);
    if (rv) return rv;

    return ECP_OK;
}