summaryrefslogtreecommitdiff
path: root/ecp/src/ecp/ext/rbuf.c
blob: e1fab7bcdfe5fe71c3cbc6b3cdc78e78500d9993 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdlib.h>

#include <ecp/core.h>

#include "rbuf.h"

ECPRBConn *ecp_rbuf_get_rbconn(ECPConnection *conn) {
    if (ecp_conn_has_rbuf(conn)) return (ECPRBConn *)conn;
    return NULL;
}

ECPConnection *ecp_rbuf_get_conn(ECPRBConn *conn) {
    return &conn->b;
}

void _ecp_rbuf_start(ECPRBuffer *rbuf, ecp_seq_t seq) {
    rbuf->seq_max = seq;
    rbuf->seq_start = seq + 1;
}

int _ecp_rbuf_msg_idx(ECPRBuffer *rbuf, ecp_seq_t seq, unsigned short *idx) {
    ecp_seq_t seq_offset = seq - rbuf->seq_start;

    /* This also checks for seq_start <= seq if seq type range >> rbuf->arr_size */
    if (seq_offset >= rbuf->arr_size) return ECP_ERR_FULL;

    if (idx) *idx = ECP_RBUF_IDX_MASK(rbuf->idx_start + seq_offset, rbuf->arr_size);
    return ECP_OK;
}

void ecp_rbuf_init(ECPRBConn *conn) {
    ECPConnection *_conn = ecp_rbuf_get_conn(conn);

    ecp_conn_set_flags(_conn, ECP_CONN_FLAG_RBUF);
    conn->send = NULL;
    conn->recv = NULL;
}

int ecp_rbuf_create(ECPRBConn *conn) {
    int rv;

    if (conn->send) {
        rv = ecp_rbsend_create(conn);
        if (rv) return rv;
    }
    if (conn->recv) {
        rv = ecp_rbrecv_create(conn);
        if (rv) {
            if (conn->send) ecp_rbsend_destroy(conn);
            return rv;
        }
    }

    return ECP_OK;
}

void ecp_rbuf_destroy(ECPRBConn *conn) {
    if (conn->send) ecp_rbsend_destroy(conn);
    if (conn->recv) ecp_rbrecv_destroy(conn);
}

void ecp_rbuf_start(ECPRBConn *conn) {
    ECPConnection *_conn = ecp_rbuf_get_conn(conn);

    if (conn->send) {
        ecp_seq_t seq_out;

#ifdef ECP_WITH_PTHREAD
        pthread_mutex_lock(&_conn->mutex);
#endif

        seq_out = (ecp_seq_t)(_conn->nonce_out);

#ifdef ECP_WITH_PTHREAD
        pthread_mutex_unlock(&_conn->mutex);
#endif

        ecp_rbsend_start(conn, seq_out);
    }

    if (conn->recv) {
        ecp_seq_t seq_in;

#ifdef ECP_WITH_PTHREAD
        pthread_mutex_lock(&_conn->mutex);
#endif

        seq_in = (ecp_seq_t)(_conn->nonce_in);

#ifdef ECP_WITH_PTHREAD
        pthread_mutex_unlock(&_conn->mutex);
#endif

        ecp_rbrecv_start(conn, seq_in);
    }
}

ssize_t ecp_rbuf_msg_handle(ECPRBConn *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) {
    switch (mtype) {
        case ECP_MTYPE_RBACK:
            if (conn->send) return ecp_rbuf_handle_ack(conn, msg, msg_size);
            break;

        case ECP_MTYPE_RBNOP:
            if (conn->recv) return ecp_rbuf_handle_nop(conn, msg, msg_size);
            break;

        case ECP_MTYPE_RBFLUSH:
            if (conn->recv) return ecp_rbuf_handle_flush(conn);
            break;

        default:
            break;
    }

    return ECP_ERR_MTYPE;
}

int ecp_rbuf_err_handle(ECPRBConn *conn, unsigned char mtype, int err) {
    if (conn->recv && (mtype == ECP_MTYPE_RBTIMER)) {
        ecp_rbuf_handle_timer(conn);
        return ECP_OK;
    }
    return ECP_PASS;
}