summaryrefslogtreecommitdiff
path: root/ecp/src/rbuf.c
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2022-01-19 02:49:47 +0100
committerUros Majstorovic <majstor@majstor.org>2022-01-19 02:49:47 +0100
commita4f22127be441c4c158c10fe65916872d99253d2 (patch)
tree0d476ce70c63b93ee2c4a2b3fafd0fa3df66b998 /ecp/src/rbuf.c
parente356da0b15f8fef6559c7761a1b04b8f6e2dc649 (diff)
code cleanup
Diffstat (limited to 'ecp/src/rbuf.c')
-rw-r--r--ecp/src/rbuf.c164
1 files changed, 134 insertions, 30 deletions
diff --git a/ecp/src/rbuf.c b/ecp/src/rbuf.c
index 9a5bd8c..9680d14 100644
--- a/ecp/src/rbuf.c
+++ b/ecp/src/rbuf.c
@@ -1,6 +1,6 @@
#include "core.h"
-int ecp_rbuf_init(ECPRBuffer *rbuf, ECPRBMessage *msg, unsigned int msg_size) {
+int _ecp_rbuf_init(ECPRBuffer *rbuf, ECPRBMessage *msg, unsigned int msg_size) {
rbuf->msg = msg;
if (msg_size) {
if (msg == NULL) return ECP_ERR;
@@ -13,6 +13,35 @@ int ecp_rbuf_init(ECPRBuffer *rbuf, ECPRBMessage *msg, unsigned int msg_size) {
return ECP_OK;
}
+int _ecp_rbuf_start(ECPRBuffer *rbuf, ecp_seq_t seq) {
+ rbuf->seq_max = seq;
+ rbuf->seq_start = seq + 1;
+
+ return ECP_OK;
+}
+
+int _ecp_rbuf_msg_idx(ECPRBuffer *rbuf, ecp_seq_t seq) {
+ ecp_seq_t seq_offset = seq - rbuf->seq_start;
+
+ // This also checks for seq_start <= seq if seq type range >> rbuf->msg_size
+ if (seq_offset < rbuf->msg_size) return ECP_RBUF_IDX_MASK(rbuf->msg_start + seq_offset, rbuf->msg_size);
+ return ECP_ERR_RBUF_FULL;
+}
+
+ssize_t _ecp_rbuf_msg_store(ECPRBuffer *rbuf, ecp_seq_t seq, int idx, unsigned char *msg, size_t msg_size, unsigned char test_flags, unsigned char set_flags) {
+ idx = idx < 0 ? _ecp_rbuf_msg_idx(rbuf, seq) : idx;
+ if (idx < 0) return idx;
+
+ if (rbuf->msg == NULL) return 0;
+ if (test_flags && (test_flags & rbuf->msg[idx].flags)) return ECP_ERR_RBUF_DUP;
+
+ if (msg_size) memcpy(rbuf->msg[idx].msg, msg, msg_size);
+ rbuf->msg[idx].size = msg_size;
+ rbuf->msg[idx].flags = set_flags;
+
+ return msg_size;
+}
+
int ecp_rbuf_create(ECPConnection *conn, ECPRBSend *buf_s, ECPRBMessage *msg_s, unsigned int msg_s_size, ECPRBRecv *buf_r, ECPRBMessage *msg_r, unsigned int msg_r_size) {
int rv;
@@ -43,35 +72,6 @@ void ecp_rbuf_destroy(ECPConnection *conn) {
ecp_rbuf_recv_destroy(conn);
}
-int ecp_rbuf_start(ECPRBuffer *rbuf, ecp_seq_t seq) {
- rbuf->seq_max = seq;
- rbuf->seq_start = seq + 1;
-
- return ECP_OK;
-}
-
-int ecp_rbuf_msg_idx(ECPRBuffer *rbuf, ecp_seq_t seq) {
- ecp_seq_t seq_offset = seq - rbuf->seq_start;
-
- // This also checks for seq_start <= seq if seq type range >> rbuf->msg_size
- if (seq_offset < rbuf->msg_size) return ECP_RBUF_IDX_MASK(rbuf->msg_start + seq_offset, rbuf->msg_size);
- return ECP_ERR_RBUF_FULL;
-}
-
-ssize_t ecp_rbuf_msg_store(ECPRBuffer *rbuf, ecp_seq_t seq, int idx, unsigned char *msg, size_t msg_size, unsigned char test_flags, unsigned char set_flags) {
- idx = idx < 0 ? ecp_rbuf_msg_idx(rbuf, seq) : idx;
- if (idx < 0) return idx;
-
- if (rbuf->msg == NULL) return 0;
- if (test_flags && (test_flags & rbuf->msg[idx].flags)) return ECP_ERR_RBUF_DUP;
-
- if (msg_size) memcpy(rbuf->msg[idx].msg, msg, msg_size);
- rbuf->msg[idx].size = msg_size;
- rbuf->msg[idx].flags = set_flags;
-
- return msg_size;
-}
-
ssize_t ecp_rbuf_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags, ecp_seq_t seq) {
ECPSocket *sock = conn->sock;
ECPContext *ctx = sock->ctx;
@@ -96,3 +96,107 @@ ssize_t ecp_rbuf_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *pay
return rv;
}
+int ecp_rbuf_handle_seq(ECPConnection *conn, unsigned char mtype) {
+ if (conn->rbuf.recv || (mtype == ECP_MTYPE_RBACK) || (mtype == ECP_MTYPE_RBFLUSH)) return 1;
+ return 0;
+}
+
+int ecp_rbuf_set_seq(ECPConnection *conn, ECPSeqItem *si, unsigned char *payload, size_t pld_size) {
+ ECPRBSend *buf;
+ unsigned char mtype;
+ int idx;
+ int rv;
+
+ if (si->rb_pass) return ECP_OK;
+
+ buf = conn->rbuf.send;
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&buf->mutex);
+#endif
+ idx = _ecp_rbuf_msg_idx(&buf->rbuf, si->seq);
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_unlock(&buf->mutex);
+#endif
+
+ if (idx < 0) return idx;
+
+ rv = ecp_pld_get_type(payload, pld_size, &mtype);
+ if (rv) return rv;
+
+ si->rb_mtype = mtype;
+ si->rb_idx = idx;
+ buf->rbuf.msg[idx].size = 0;
+ buf->rbuf.msg[idx].flags = 0;
+
+ return ECP_OK;
+}
+
+ssize_t ecp_rbuf_pkt_send(ECPConnection *conn, ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t pkt_size, unsigned char flags, ECPTimerItem *ti, ECPSeqItem *si) {
+ ECPRBSend *buf;
+ int do_send = 1;
+ ssize_t rv = 0;
+
+ buf = conn->rbuf.send;
+ if (!si->rb_pass) {
+ unsigned char flags = 0;
+ ecp_seq_t seq = si->seq;
+ unsigned int idx = si->rb_idx;
+ unsigned char mtype = si->rb_mtype & ECP_MTYPE_MASK;
+
+ if (mtype < ECP_MAX_MTYPE_SYS) flags |= ECP_RBUF_FLAG_SYS;
+
+ rv = _ecp_rbuf_msg_store(&buf->rbuf, seq, idx, packet->buffer, pkt_size, 0, flags);
+ if (rv < 0) return rv;
+
+ if (buf->flags & ECP_RBUF_FLAG_CCONTROL) {
+ int _rv = ECP_OK;
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&buf->mutex);
+#endif
+
+ if (ECP_SEQ_LT(buf->rbuf.seq_max, seq)) buf->rbuf.seq_max = seq;
+
+ if (buf->cnt_cc || (buf->in_transit >= buf->win_size)) {
+ if (!buf->cnt_cc) buf->seq_cc = seq;
+ buf->cnt_cc++;
+ buf->rbuf.msg[idx].flags |= ECP_RBUF_FLAG_IN_CCONTROL;
+ do_send = 0;
+ if (ti) {
+ ECPRBTimer *timer = &buf->timer;
+ ECPRBTimerItem *item = &timer->item[timer->idx_w];
+
+ if (!item->occupied) {
+ item->occupied = 1;
+ item->item = *ti;
+ buf->rbuf.msg[idx].idx_t = timer->idx_w;
+ timer->idx_w = (timer->idx_w) % ECP_MAX_TIMER;
+ } else {
+ _rv = ECP_ERR_MAX_TIMER;
+ }
+ } else {
+ buf->rbuf.msg[idx].idx_t = -1;
+ }
+ } else {
+ buf->in_transit++;
+ }
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_unlock(&buf->mutex);
+#endif
+
+ if (_rv) return _rv;
+ }
+ }
+
+ if (do_send) {
+ if (ti) {
+ int _rv;
+
+ _rv = ecp_timer_push(ti);
+ if (_rv) return _rv;
+ }
+ rv = ecp_pkt_send(sock, addr, packet, pkt_size, flags);
+ }
+ return rv;
+}