diff options
Diffstat (limited to 'send.c')
-rw-r--r-- | send.c | 46 |
1 files changed, 24 insertions, 22 deletions
@@ -3,10 +3,11 @@ #include <arpa/inet.h> #include <assert.h> #include <stdarg.h> -#include <string.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> +#include "arena.h" #include "common.h" int main(int argc, char **argv) @@ -29,33 +30,34 @@ int main(int argc, char **argv) err = connect(serverfd, (struct sockaddr *)&address, sizeof(address)); assert(err == 0); - { - u32 author_len = strlen(argv[1]) + 1; // add 1 for null terminator - assert(author_len <= AUTHOR_LEN); + u32 author_len = strlen(argv[1]); // add 1 for null terminator + assert(author_len + 1 <= AUTHOR_LEN); - // convert text to wide string - u32 text_len = strlen(argv[2]) + 1; - wchar_t text_wide[text_len]; - u32 size = mbstowcs(text_wide, argv[2], text_len - 1); - assert(size == text_len - 1); - // null terminate - text_wide[text_len - 1] = 0; + // convert text to wide string + u32 text_len = strlen(argv[2]) + 1; + wchar_t text_wide[text_len]; + u32 size = mbstowcs(text_wide, argv[2], text_len - 1); + assert(size == text_len - 1); + // null terminate + text_wide[text_len - 1] = 0; - u8 buf[STREAM_BUF] = {0}; - Message *m = (Message *)buf; + Arena *bufArena = ArenaAlloc(); + u8 *buf = ArenaPush(bufArena, (text_len - 1) * sizeof(*text_wide)); + Message *mbuf = (Message *)buf; - memcpy(m->author, argv[1], author_len - 1); - message_timestamp(m->timestamp); - m->text_len = text_len; - memcpy(&m->text, text_wide, m->text_len * sizeof(wchar_t)); + memcpy(mbuf->author, argv[1], author_len); + message_timestamp(mbuf->timestamp); + mbuf->text_len = text_len; + memcpy(&mbuf->text, text_wide, mbuf->text_len * sizeof(wchar_t)); - nsend = send(serverfd, buf, MESSAGELENP(m), 0); + nsend = send(serverfd, buf, MESSAGELENP(mbuf), 0); - assert(nsend >= 0); + assert(nsend >= 0); - printf("text_len: %d\n", text_len); - fprintf(stdout, "Sent %d bytes.\n", nsend); - } + printf("text_len: %d\n", text_len); + fprintf(stdout, "Sent %d bytes.\n", nsend); + + ArenaRelease(bufArena); return 0; } |