]> git.lizzy.rs Git - dragonnet.git/commitdiff
Add number (de)serialization
authorHimbeerserverDE <himbeerserverde@gmail.com>
Tue, 5 Oct 2021 19:40:24 +0000 (21:40 +0200)
committerHimbeerserverDE <himbeerserverde@gmail.com>
Tue, 5 Oct 2021 19:40:24 +0000 (21:40 +0200)
recv.c [new file with mode: 0644]
recv.h [new file with mode: 0644]
recv_thread.c
send.c [new file with mode: 0644]
send.h [new file with mode: 0644]
typegen/main.c

diff --git a/recv.c b/recv.c
new file mode 100644 (file)
index 0000000..9276020
--- /dev/null
+++ b/recv.c
@@ -0,0 +1,84 @@
+#include <dragonnet/recv.h>
+
+static void rcv(DragonnetPeer *p, const void *buf, size_t n)
+{
+       pthread_rwlock_rdlock(&p->mu);
+       int sock = p->sock;
+       pthread_rwlock_unlock(&p->mu);
+
+       ssize_t len = recv(sock, buf, n, MSG_WAITALL);
+       if (len < 0) {
+               perror("recv");
+               dragonnet_peer_delete(p);
+               return;
+       }
+
+       // Connection closed
+       if (len == 0) {
+               pthread_rwlock_wrlock(&p->mu);
+
+               close(p->sock);
+               p->sock = -1;
+               p->state++;
+
+               pthread_rwlock_unlock(&p->mu);
+       }
+}
+
+u8 recv_u8(DragonnetPeer *p)
+{
+       u8 v;
+       rcv(p, &v, sizeof v);
+       return v;
+}
+
+s8 recv_s8(DragonnetPeer *p)
+{
+       return (s8) recv_u8(p);
+}
+
+u16 recv_u16(DragonnetPeer *p)
+{
+       u16 be;
+       rcv(p, &be, sizeof be);
+       return be16toh(be);
+}
+
+s16 recv_s16(DragonnetPeer *p)
+{
+       return (s16) recv_u16(p);
+}
+
+u32 recv_u32(DragonnetPeer *p)
+{
+       u32 be;
+       rcv(p, &be, sizeof be);
+       return be32toh(be);
+}
+
+s32 recv_s32(DragonnetPeer *p)
+{
+       return (s32) recv_u32(p);
+}
+
+u64 recv_u64(DragonnetPeer *p)
+{
+       u64 be;
+       rcv(p, &be, sizeof be);
+       return be64toh(be);
+}
+
+s64 recv_s64(DragonnetPeer *p)
+{
+       return (s64) recv_u64(p);
+}
+
+f32 recv_f32(DragonnetPeer *p)
+{
+       return (f32) recv_u32(p);
+}
+
+f64 recv_f64(DragonnetPeer *p)
+{
+       return (f64) recv_u64(p);
+}
diff --git a/recv.h b/recv.h
new file mode 100644 (file)
index 0000000..748ec5f
--- /dev/null
+++ b/recv.h
@@ -0,0 +1,18 @@
+#ifndef _DRAGONNET_RECV_H_
+#define _DRAGONNET_RECV_H_
+
+#include <dragontype/number.h>
+#include <dragonnet/peer.h>
+
+u8 recv_u8(DragonnetPeer *p);
+s8 recv_s8(DragonnetPeer *p);
+u16 recv_u16(DragonnetPeer *p);
+s16 recv_s16(DragonnetPeer *p);
+u32 recv_u32(DragonnetPeer *p);
+s32 recv_s32(DragonnetPeer *p);
+u64 recv_u64(DragonnetPeer *p);
+s64 recv_s64(DragonnetPeer *p);
+f32 recv_f32(DragonnetPeer *p);
+f64 recv_f64(DragonnetPeer *p);
+
+#endif
index bff48ebb8b6443cd7867db987173137c1f6f8486..b50c5a393e73c6b89fb1af4dd26a7aa8f0fecc91 100644 (file)
@@ -26,7 +26,6 @@ void *dragonnet_peer_recv_thread(void *g_peer)
                pthread_rwlock_unlock(&p->mu);
 
                ssize_t len = recv(sock, &msg, sizeof msg, MSG_WAITALL);
-
                if (len < 0) {
                        perror("recv");
                        dragonnet_peer_delete(p);
diff --git a/send.c b/send.c
new file mode 100644 (file)
index 0000000..b28625b
--- /dev/null
+++ b/send.c
@@ -0,0 +1,75 @@
+#include <errno.h>
+#include <stdio.h>
+
+#include <dragonnet/send.h>
+
+static void snd(DragonnetPeer *p, bool confirm, const void *buf, size_t n)
+{
+       pthread_rwlock_rdlock(&p->mu);
+       int sock = p->sock;
+       pthread_rwlock_unlock(&p->mu);
+
+       ssize_t len = send(sock, buf, n, MSG_NOSIGNAL | (confirm ? 0 : MSG_MORE));
+       if (len < 0) {
+               if (errno == EPIPE) {
+                       dragonnet_peer_close(p);
+                       return;
+               }
+
+               perror("send");
+               dragonnet_peer_delete(p);
+       }
+}
+
+void send_u8(DragonnetPeer *p, bool confirm, u8 v)
+{
+       snd(p, confirm, &v, sizeof v);
+}
+
+void send_s8(DragonnetPeer *p, bool confirm, s8 v)
+{
+       send_u8(p, confirm, (u8) v);
+}
+
+void send_u16(DragonnetPeer *p, bool confirm, u16 v)
+{
+       u16 be = htobe16(v);
+       snd(p, confirm, &be, sizeof be);
+}
+
+void send_s16(DragonnetPeer *p, bool confirm, s16 v)
+{
+       send_u16(p, confirm, (u16) v);
+}
+
+void send_u32(DragonnetPeer *p, bool confirm, u32 v)
+{
+       u32 be = htobe32(v);
+       snd(p, confirm, &be, sizeof be);
+}
+
+void send_s32(DragonnetPeer *p, bool confirm, s32 v)
+{
+       send_u32(p, confirm, (u32) v);
+}
+
+void send_u64(DragonnetPeer *p, bool confirm, u64 v)
+{
+       u64 be = htobe64(v);
+       snd(p, confirm, &be, sizeof be);
+}
+
+void send_s64(DragonnetPeer *p, bool confirm, s64 v)
+{
+       send_u64(p, confirm, (u64) v);
+}
+
+void send_f32(DragonnetPeer *p, bool confirm, f32 v)
+{
+       send_u32(p, confirm, (u32) v);
+}
+
+void send_f64(DragonnetPeer *p, bool confirm, f64 v)
+{
+       send_u64(p, confirm, (u64) v);
+}
diff --git a/send.h b/send.h
new file mode 100644 (file)
index 0000000..c37c420
--- /dev/null
+++ b/send.h
@@ -0,0 +1,20 @@
+#ifndef _DRAGONNET_SEND_H_
+#define _DRAGONNET_SEND_H_
+
+#include <stdbool.h>
+
+#include <dragontype/number.h>
+#include <dragonnet/peer.h>
+
+void send_u8(DragonnetPeer *p, bool confirm, u8 v);
+void send_s8(DragonnetPeer *p, bool confirm, s8 v);
+void send_u16(DragonnetPeer *p, bool confirm, u16 v);
+void send_s16(DragonnetPeer *p, bool confirm, s16 v);
+void send_u32(DragonnetPeer *p, bool confirm, u32 v);
+void send_s32(DragonnetPeer *p, bool confirm, s32 v);
+void send_u64(DragonnetPeer *p, bool confirm, u64 v);
+void send_s64(DragonnetPeer *p, bool confirm, s64 v);
+void send_f32(DragonnetPeer *p, bool confirm, f32 v);
+void send_f64(DragonnetPeer *p, bool confirm, f64 v);
+
+#endif
index c778026e7f2fc34d42241eeca7d7ec67fc37e07b..86cc706dd1cae72ffedf7d626cc61babb29bd15d 100644 (file)
@@ -44,8 +44,9 @@ int main(__attribute((unused)) int argc, __attribute((unused)) char **argv)
        fp = NULL;
 
        FILE *c_fp = fopen("dnet-types.c", "w");
-       fprintf(c_fp, "#include \"dnet-types.h\"\n");
+       fprintf(c_fp, "#include <dragonnet/recv.h>\n");
        fprintf(c_fp, "#include <dragonnet/send.h>\n\n");
+       fprintf(c_fp, "#include \"dnet-types.h\"\n\n");
 
        FILE *h_fp = fopen("dnet-types.h", "w");
        fprintf(h_fp, "#include <dragontype/number.h>\n\n");
@@ -87,7 +88,11 @@ int main(__attribute((unused)) int argc, __attribute((unused)) char **argv)
                        char **tokens;
                        size_t tokens_len = split(&tokens, msgs[i], " ");
 
-                       fprintf(c_fp, "\tsend_%s(p, type.%s);\n", &tokens[0][1], tokens[1]);
+                       if (i >= msgs_len-1 || msgs[1+i][0] != '\t')
+                               fprintf(c_fp, "\tsend_%s(p, true, type.%s);\n", &tokens[0][1], tokens[1]);
+                       else
+                               fprintf(c_fp, "\tsend_%s(p, false, type.%s);\n", &tokens[0][1], tokens[1]);
+
                        free_split(tokens, tokens_len);
                        tokens = NULL;
                }