]> git.lizzy.rs Git - dragonnet.git/blob - send.c
Refactor type hooks
[dragonnet.git] / send.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include <dragonnet/send.h>
7
8 void dragonnet_send_raw(DragonnetPeer *p, bool submit, const void *buf, size_t n)
9 {
10         pthread_rwlock_rdlock(&p->mu);
11         int sock = p->sock;
12         pthread_rwlock_unlock(&p->mu);
13
14         ssize_t len = send(sock, buf, n, MSG_NOSIGNAL | (submit ? 0 : MSG_MORE));
15         if (len < 0) {
16                 if (errno == EPIPE) {
17                         dragonnet_peer_close(p);
18                         return;
19                 }
20
21                 perror("send");
22                 dragonnet_peer_delete(p);
23         }
24 }
25
26 void dragonnet_write_raw(u8 **buf, size_t *n, const void *data, size_t len)
27 {
28         *buf = realloc(*buf, len + *n);
29         memcpy(&((*buf)[*n]), data, len);
30         *n += len;
31 }