aboutsummaryrefslogtreecommitdiff
path: root/common.h
blob: 4786c140d370e2633f9703e5b721baf7d3be506a (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
#include <stdint.h>
#include <stdio.h>

#define PORT 9983
// max buffer size sent over network
// TODO: choose a better size
#define BUF_MAX 256
// max size for a message sent
#define MESSAGE_MAX 256
// max length of author field
#define MESSAGE_AUTHOR_LEN 12
// max length of timestamp field
#define MESSAGE_TIMESTAMP_LEN 9
// current user's name
#define USERNAME "Jef Koek"

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;

// To serialize the text that could be arbitrary length the lenght is encoded after the author
// string and before the text.
struct message {
    char author[MESSAGE_AUTHOR_LEN];
    char timestamp[MESSAGE_TIMESTAMP_LEN]; // HH:MM:SS
    u16 len;
    char *text;
};

// printf without buffering using write syscall, works when using sockets
void writef(char *format, ...);

u16 str_len(char *str);
void str_cpy(char *to, char *from);

// save the message msg to file in binary format, returns zero on success, returns 1 if the msg.text
// was empty which should not be allowed.
u8 save_message(struct message *msg, FILE *f);
// load the message msg from file f, returns zero on success, returns 1 if the msg.text
// was empty which should not be allowed.
u8 load_message(struct message *msg, FILE *f);

// Send a stream of bytes containing msg
// return -1 if send() returns -1. Otherwise returns number of bytes sent.
u32 send_message(struct message msg, u32 serverfd);
// Receives a stream of bytes and populates msg with the data received
// if recv() returns 0 or -1 it will return early and return 0 or -1 accordingly.
// Otherwise returns the number of bytes received
u32 receive_message(struct message *msg, u32 clientfd);