aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymaekers Luca <raymaekers.luca@gmail.com>2024-10-21 13:29:33 +0200
committerRaymaekers Luca <raymaekers.luca@gmail.com>2024-10-21 13:31:38 +0200
commitc7687f88bc5cb4d25304b0ee19789c37aebd7b6d (patch)
tree4013a0fe1dedcfb01be2e914e829cd2a27cd08d4
parent94c9a5f9ee31badae78229edfbf1c663acf54c49 (diff)
removed malloc & realloc
-rw-r--r--README.md48
-rw-r--r--client.c50
2 files changed, 57 insertions, 41 deletions
diff --git a/README.md b/README.md
index d20f670..28d96a2 100644
--- a/README.md
+++ b/README.md
@@ -1,30 +1,31 @@
# Chatty
-- use memory arena's to manage memory
-- spec for the stream protocol
-
-# Server
The idea is the following:
- tcp server that you can send messages to
-- encrypted communication
- history upon connecting
- date of messages sent
-- author
-- fingeprint as ID for authorship
+- authentication
+- encrypted communication (tls?)
- client for reading the messages and sending them at the same time
+# Common
+- use memory arena's to manage memory
+- manage memory for what if it will not fit
+ - for just do nothing when the limit is reached
+
+# Server
- min height & width
- wrapping input
-- max y for new messages and make them scroll
-- check resize event
-- asynchronously receive/send a message
-- fix receiving messages with arbitrary text length
-
-## TODO: send message to all other clients
-- implement different rooms
-- implement history
- - [ ] fix server copying the bytes correctly
-- implement tls
+- [ ] history
+- [x] max y for new messages and make them scroll
+- [x] check resize event
+- [x] asynchronously receive/send a message
+- [x] send message to all other clients
+- [x] fix receiving messages with arbitrary text length
+- [x] bug: server copying the bytes correctly
+- rooms
+- encryption
+- authentication
# Client
- bug: when having multiple messages and resizing a lot, the output will be in shambles
@@ -37,3 +38,16 @@ The idea is the following:
# Questions
- will two consecutive sends be read in one recv
- can you recv a message in two messages
+
+# Message protocol
+Version 1
+1 version byte
+4 length bytes
+12 message_author bytes
+- 11 chars + \0
+9 timestamp bytes
+- 8chars + \0
+x text bytes
+- x bytes + \0
+
+The variable text bytes can be calculated by substracting the author and timestamp from the length
diff --git a/client.c b/client.c
index a1800c1..7a727af 100644
--- a/client.c
+++ b/client.c
@@ -1,5 +1,8 @@
// Client for chatty
+// initial size for the messages array
+#define MESSAGES_SIZE 5
+
// clang-format off
#define TB_IMPL
#include "termbox2.h"
@@ -31,21 +34,27 @@ struct message input = {
.timestamp = {0},
.len = 0,
};
-// All messages sent and received in order
-struct message *messages;
// current amount of messages
int nmessages = 0;
// length of messages array
-int messages_len = 2;
+int messages_size = MESSAGES_SIZE;
+// All messages sent and received in order
+struct message messages[MESSAGES_SIZE] = {0};
// incremented each time a new message is printed
int msg_y = 0;
-void cleanup()
+// Cleans up resources, should called before exiting.
+void cleanup(void);
+// Displays an error message msg, followed by the errno variable and exits exeuction.
+void err_exit(const char *msg);
+// Display the welcome ui screen containing the prompt and messages array.
+void scren_welcome(void);
+// Append msg to the messages array. Returns -1 if there was no space in the messages array
+// otherwise returns 0 on success.
+u8 message_add(struct message msg);
+
+void cleanup(void)
{
- free(messages);
- if (messages == NULL) {
- writef("something wrong happened.");
- }
tb_shutdown();
if (serverfd)
if (close(serverfd))
@@ -60,7 +69,7 @@ void err_exit(const char *msg)
_exit(1);
}
-void screen_welcome()
+void screen_welcome(void)
{
tb_set_cursor(curs_offs_x, global.height - prompt_offs_y);
tb_print(0, global.height - prompt_offs_y, 0, 0, ">");
@@ -75,9 +84,12 @@ void screen_welcome()
}
}
-// Append the message to the messages array
-void add_message(struct message msg)
+u8 message_add(struct message msg)
{
+ if (nmessages == messages_size) {
+ return -1;
+ }
+
int i;
messages[nmessages].text = input.text;
;
@@ -92,13 +104,7 @@ void add_message(struct message msg)
nmessages++;
msg_y++;
- if (nmessages == messages_len) {
- int new_size = 5;
- messages = realloc(messages, new_size * sizeof(struct message));
- if (messages == NULL)
- err_exit("Could not reallocate memory for messages.");
- messages_len += new_size;
- }
+ return 0;
}
int main(void)
@@ -120,10 +126,6 @@ int main(void)
{0},
};
- messages = malloc(messages_len * sizeof(struct message));
- if (messages == NULL)
- err_exit("Could not allocate memory for messages.");
-
tb_init();
bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]);
@@ -191,7 +193,7 @@ int main(void)
ltime = localtime(&now);
strftime(input.timestamp, sizeof(input.timestamp), "%H:%M:%S", ltime);
- add_message(input);
+ message_add(input);
if (send(serverfd, &input, sizeof(input), 0) == -1)
err_exit("Error while sending message.");
@@ -242,7 +244,7 @@ int main(void)
} else if (nrecv == -1) {
err_exit("Error while receiveiving from server.");
}
- add_message(msg_recv);
+ message_add(msg_recv);
tb_clear();
screen_welcome();