summaryrefslogtreecommitdiff
path: root/ecp/src/rbuf_send.c
blob: 9f29010d2fa296f65c44050662e76db2c303a381 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "core.h"
#include "tr.h"

#define NACK_RATE_UNIT      10000

#define MIN(X, Y)           (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y)           (((X) > (Y)) ? (X) : (Y))

static ssize_t flush_send(ECPConnection *conn, ECPTimerItem *ti) {
    ECPBuffer packet;
    ECPBuffer payload;
    unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_RBFLUSH, conn)];
    unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_RBFLUSH, conn)];

    packet.buffer = pkt_buf;
    packet.size = ECP_SIZE_PKT_BUF(0, ECP_MTYPE_RBFLUSH, conn);
    payload.buffer = pld_buf;
    payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_RBFLUSH, conn);

    ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_RBFLUSH);
    if (ti == NULL) {
        ECPTimerItem _ti;
        int rv;

        rv = ecp_timer_item_init(&_ti, conn, ECP_MTYPE_RBACK, 3, 500);
        if (rv) return rv;

        _ti.retry = flush_send;
        rv = ecp_timer_push(&_ti);
        if (rv) return rv;
    }
    return ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_RBFLUSH), 0);
}

static void cc_flush(ECPConnection *conn, unsigned char flags) {
    ECPRBSend *buf = conn->rbuf.send;
    ECPRBuffer *rbuf = &buf->rbuf;
    unsigned short idx;
    int rv;

    rv = _ecp_rbuf_msg_idx(rbuf, buf->seq_cc, &idx);
    if (rv) return;

    while (ECP_SEQ_LTE(buf->seq_cc, rbuf->seq_max)) {
        ECPRBTimerItem *ti;
        ECPBuffer packet;

        if ((buf->cnt_cc == 0) || (buf->in_transit >= buf->win_size)) break;

        if ((rbuf->arr.pkt[idx].flags & ECP_RBUF_FLAG_IN_RBUF) && !(rbuf->arr.pkt[idx].flags & ECP_RBUF_FLAG_SKIP)) {
#ifdef ECP_WITH_PTHREAD
            pthread_mutex_unlock(&buf->mutex);
#endif

            ti = rbuf->arr.pkt[idx].timer;
            if (ti) ecp_timer_push(&ti->item);
            packet.buffer = rbuf->arr.pkt[idx].buf;
            packet.size = ECP_MAX_PKT;
            ecp_pkt_send(conn->sock, &conn->node.addr, &packet, rbuf->arr.pkt[idx].size, flags);

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

            if (ti) {
#ifdef ECP_WITH_PTHREAD
                pthread_mutex_lock(&buf->timer->mutex);
#endif
                ti->empty = 1;
#ifdef ECP_WITH_PTHREAD
                pthread_mutex_lock(&buf->timer->mutex);
#endif
            }

            buf->cnt_cc--;
            buf->in_transit++;
        }
        if (!(buf->flags & ECP_RBUF_FLAG_RELIABLE)) {
            rbuf->arr.pkt[idx].flags = 0;
            // if (rbuf->arr.pkt[idx].flags == 0);
        }
        buf->seq_cc++;
        idx = ECP_RBUF_IDX_MASK(idx + 1, rbuf->arr_size);
    }
    if (!(buf->flags & ECP_RBUF_FLAG_RELIABLE)) {
        rbuf->seq_start = buf->seq_cc;
        rbuf->idx_start = idx;
    }
}

ssize_t ecp_rbuf_handle_ack(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) {
    ECPRBSend *buf;
    ECPRBuffer *rbuf;
    ssize_t rsize = sizeof(ecp_seq_t)+sizeof(ecp_ack_t);
    ecp_seq_t seq_ack = 0;
    ecp_ack_t ack_map = 0;
    ecp_seq_t seq_start;
    ecp_seq_t seq_max;
    unsigned short idx;
    unsigned short msg_cnt;
    int do_flush = 0;
    int i;
    int rv;

    buf = conn->rbuf.send;
    if (buf == NULL) return size;

    rbuf = &buf->rbuf;
    if (size < 0) return size;
    if (size < rsize) return ECP_ERR;

    seq_ack = \
        (msg[0] << 24) | \
        (msg[1] << 16) | \
        (msg[2] << 8)  | \
        (msg[3]);
    ack_map = \
        (msg[4] << 24) | \
        (msg[5] << 16) | \
        (msg[6] << 8)  | \
        (msg[7]);

    ecp_tr_release(b->packet, 1);

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

    seq_max = (buf->cnt_cc ? buf->seq_cc - 1 : rbuf->seq_max);
    if (ECP_SEQ_LT(seq_max, seq_ack)) return ECP_ERR;

    if (buf->flags & ECP_RBUF_FLAG_RELIABLE) {
        rv = _ecp_rbuf_msg_idx(rbuf, seq_ack, &idx);
        if (rv) goto handle_ack_fin;
    }

    if (buf->flags & ECP_RBUF_FLAG_CCONTROL) {
        buf->in_transit = seq_max - seq_ack;
    }

    if (ack_map != ECP_ACK_FULL) {
        ecp_ack_t ack_mask = (ecp_ack_t)1 << (ECP_SIZE_ACKB - 1);
        unsigned short nack_cnt = 0;
        int nack_first = 0;

        seq_ack -= (ECP_SIZE_ACKB - 1);
        if (buf->flags & ECP_RBUF_FLAG_RELIABLE) {
            rv = _ecp_rbuf_msg_idx(rbuf, seq_ack, &idx);
            if (rv) goto handle_ack_fin;
        }

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

        for (i=0; i<ECP_SIZE_ACKB; i++) {
            if ((ack_map & ack_mask) == 0) {
                if (ECP_SEQ_LT(buf->seq_nack, seq_ack)) {
                    nack_cnt++;
                    buf->seq_nack = seq_ack;
                }

                if (buf->flags & ECP_RBUF_FLAG_RELIABLE) {
                    if (!(rbuf->arr.pkt[idx].flags & ECP_RBUF_FLAG_IN_RBUF) || (rbuf->arr.pkt[idx].flags & ECP_RBUF_FLAG_SKIP)) {
                        ECPBuffer packet;
                        ECPBuffer payload;
                        unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_NOP, conn)];
                        unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_NOP, conn)];

                        packet.buffer = pkt_buf;
                        packet.size = ECP_SIZE_PKT_BUF(0, ECP_MTYPE_NOP, conn);
                        payload.buffer = pld_buf;
                        payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_NOP, conn);

                        ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_NOP);
                        ecp_rbuf_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_NOP), ECP_SEND_FLAG_MORE, seq_ack);
                    } else {
                        ECPBuffer packet;

                        packet.buffer = rbuf->arr.pkt[idx].buf;
                        packet.size = ECP_MAX_PKT;
                        ecp_pkt_send(conn->sock, &conn->node.addr, &packet, rbuf->arr.pkt[idx].size, ECP_SEND_FLAG_MORE);
                        if (buf->flags & ECP_RBUF_FLAG_CCONTROL) {
                            buf->in_transit++;
                        }
                    }
                    if (!nack_first) {
                        nack_first = 1;
                        seq_start = seq_ack;
                    }
                }
            }
            seq_ack++;
            ack_mask = ack_mask >> 1;
            if (buf->flags & ECP_RBUF_FLAG_RELIABLE) idx = ECP_RBUF_IDX_MASK(idx + 1, rbuf->arr_size);
        }

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

        buf->nack_rate = (buf->nack_rate * 7 + ((ECP_SIZE_ACKB - nack_cnt) * NACK_RATE_UNIT) / ECP_SIZE_ACKB) / 8;
    } else {
        buf->nack_rate = (buf->nack_rate * 7) / 8;
        seq_start = seq_ack + 1;
    }

    if (buf->flags & ECP_RBUF_FLAG_RELIABLE) {
        msg_cnt = seq_start - rbuf->seq_start;
        idx = rbuf->idx_start;
        for (i=0; i<msg_cnt; i++) {
            rbuf->arr.pkt[idx].flags = 0;
            // if (rbuf->arr.pkt[idx].flags == 0);
            idx = ECP_RBUF_IDX_MASK(idx + 1, rbuf->arr_size);
        }
        rbuf->seq_start = seq_start;
        rbuf->idx_start = idx;
    }
    if (buf->flush) {
        if (ECP_SEQ_LT(buf->seq_flush, rbuf->seq_start)) buf->flush = 0;
        if (buf->flush) do_flush = 1;
    }
    if (buf->cnt_cc) cc_flush(conn, ECP_SEND_FLAG_MORE);

handle_ack_fin:

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

    if (!rv && do_flush) {
        ssize_t _rv;

        _rv = flush_send(conn, NULL);
        if (_rv < 0) rv = _rv;
    } else {
        // ecp_tr_nomore();
    }

    ecp_tr_release(b->packet, 0);

    if (rv) return rv;
    return rsize;
}

int ecp_rbuf_send_create(ECPConnection *conn, ECPRBSend *buf, ECPRBPacket *pkt, unsigned short pkt_size) {
    ECPRBuffer *rbuf = &buf->rbuf;
    int rv;

    if (pkt == NULL) return ECP_ERR;

    memset(buf, 0, sizeof(ECPRBRecv));
    memset(pkt, 0, sizeof(ECPRBPacket) * pkt_size);

    rbuf->arr.pkt = pkt;
    rbuf->arr_size = pkt_size;

#ifdef ECP_WITH_PTHREAD
    rv = pthread_mutex_init(&buf->mutex, NULL);
    if (rv) return ECP_ERR;
#endif

    conn->rbuf.send = buf;
    return ECP_OK;
}

void ecp_rbuf_send_destroy(ECPConnection *conn) {
    ECPRBSend *buf = conn->rbuf.send;

    if (buf == NULL) return;

#ifdef ECP_WITH_PTHREAD
    pthread_mutex_destroy(&buf->mutex);
#endif

    conn->rbuf.send = NULL;
}

int ecp_rbuf_send_start(ECPConnection *conn) {
    ECPRBSend *buf = conn->rbuf.send;
    ECPRBuffer *rbuf = &buf->rbuf;

    if (buf == NULL) return ECP_ERR;

    buf->seq_nack = conn->seq_out;
    return _ecp_rbuf_start(rbuf, conn->seq_out);
}

int ecp_rbuf_set_wsize(ECPConnection *conn, ecp_win_t size) {
    ECPRBSend *buf = conn->rbuf.send;

    if (buf == NULL) return ECP_ERR;

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

    buf->win_size = size;
    if (buf->cnt_cc) cc_flush(conn, 0);

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

    return ECP_OK;
}

int ecp_rbuf_flush(ECPConnection *conn) {
    ECPRBSend *buf = conn->rbuf.send;
    ecp_seq_t seq;
    ssize_t rv;

    if (buf == NULL) return ECP_ERR;

#ifdef ECP_WITH_PTHREAD
    pthread_mutex_lock(&conn->mutex);
#endif
    seq = conn->seq_out;
#ifdef ECP_WITH_PTHREAD
    pthread_mutex_unlock(&conn->mutex);
#endif

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

    if (buf->flush) {
        if (ECP_SEQ_LT(buf->seq_flush, seq)) buf->seq_flush = seq;
    } else {
        buf->flush = 1;
        buf->seq_flush = seq;
    }

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

    rv = flush_send(conn, NULL);
    if (rv < 0) return rv;

    return ECP_OK;
}