aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymaekers Luca <raymaekers.luca@gmail.com>2024-10-20 19:40:09 +0200
committerRaymaekers Luca <raymaekers.luca@gmail.com>2024-10-20 19:40:09 +0200
commit4fe82b42b6f6fb1a69da2fb2823831e6b3eaa036 (patch)
treef96543bf338941c20aaa761428a231db6fcd36b8
parentff0aae89238f4d60267def24476e8b9f4cb596cf (diff)
Added mvp implementations of the server and client for testing
-rw-r--r--.gitignore1
-rw-r--r--client.c6
-rw-r--r--common.c9
-rw-r--r--common.h17
-rw-r--r--config.h11
-rw-r--r--minirecv.c67
-rw-r--r--send.c76
-rw-r--r--server.c21
8 files changed, 175 insertions, 33 deletions
diff --git a/.gitignore b/.gitignore
index 691531d..79557cf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
chatty
server
tags
+tmp
diff --git a/client.c b/client.c
index 9a22d90..b9d96d3 100644
--- a/client.c
+++ b/client.c
@@ -10,7 +10,6 @@
#include "termbox2.h"
// clang-format on
#include "common.h"
-#include "config.h"
#include <arpa/inet.h>
#include <errno.h>
@@ -58,6 +57,7 @@ void cleanup()
writef("Error while closing server socket. errno: %d\n", errno);
}
+// panic
void err_exit(const char *msg)
{
cleanup();
@@ -227,8 +227,8 @@ int main(void)
}
// append pressed character to input.text
- // TODO: wrap instead
- if (ev.ch > 0 && input.len < MSG_MAX && input.len < global.width - 3 - 1) {
+ // TODO: wrap instead, allocate more ram for the message instead
+ if (ev.ch > 0 && input.len < MESSAGE_MAX && input.len < global.width - 3 - 1) {
tb_printf(global.cursor_x, global.cursor_y, 0, 0, "%c", ev.ch);
global.cursor_x++;
diff --git a/common.c b/common.c
index d72ead3..232cf64 100644
--- a/common.c
+++ b/common.c
@@ -1,5 +1,4 @@
#include "common.h"
-#include "config.h"
#include <stdarg.h>
#include <stdint.h>
@@ -9,8 +8,8 @@
void writef(char *format, ...)
{
+ char buf[255 + 1];
va_list args;
- char buf[BUF_MAX + 1];
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
@@ -50,8 +49,8 @@ u8 save_message(struct message *msg, FILE *f)
if (len == 0)
err = 1;
- fwrite(&msg->timestamp, sizeof(*msg->timestamp) * MSG_TIMESTAMP_LEN, 1, f);
- fwrite(&msg->author, sizeof(*msg->author) * MSG_AUTHOR_LEN, 1, f);
+ fwrite(&msg->timestamp, sizeof(*msg->timestamp) * MESSAGE_TIMESTAMP_LEN, 1, f);
+ fwrite(&msg->author, sizeof(*msg->author) * MESSAGE_AUTHOR_LEN, 1, f);
fwrite(&len, sizeof(len), 1, f);
fputs(msg->text, f);
@@ -60,7 +59,7 @@ u8 save_message(struct message *msg, FILE *f)
u8 load_message(struct message *msg, FILE *f)
{
- fread(msg, sizeof(*msg->timestamp) * MSG_TIMESTAMP_LEN + sizeof(*msg->author) * MSG_AUTHOR_LEN, 1, f);
+ fread(msg, sizeof(*msg->timestamp) * MESSAGE_TIMESTAMP_LEN + sizeof(*msg->author) * MESSAGE_AUTHOR_LEN, 1, f);
u16 len;
fread(&len, sizeof(len), 1, f);
if (len == 0) {
diff --git a/common.h b/common.h
index f737e2f..fcf6c53 100644
--- a/common.h
+++ b/common.h
@@ -1,6 +1,19 @@
#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;
@@ -8,8 +21,8 @@ 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 timestamp[9]; // HH:MM:SS
- char author[12];
+ char author[MESSAGE_AUTHOR_LEN];
+ char timestamp[MESSAGE_TIMESTAMP_LEN]; // HH:MM:SS
u16 len;
char *text;
};
diff --git a/config.h b/config.h
deleted file mode 100644
index 626ccad..0000000
--- a/config.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#define PORT 9983
-// max size for a message sent
-#define BUF_MAX 255
-// max length of messages
-#define MSG_MAX 256
-// max length of author field
-#define MSG_AUTHOR_LEN 12
-// max length of timestamp field
-#define MSG_TIMESTAMP_LEN 9
-// current user's name
-#define USERNAME "Jef Koek"
diff --git a/minirecv.c b/minirecv.c
new file mode 100644
index 0000000..bba373a
--- /dev/null
+++ b/minirecv.c
@@ -0,0 +1,67 @@
+#include "common.h"
+#include <arpa/inet.h>
+#include <poll.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <errno.h>
+
+int main(void)
+{
+ int serverfd, clientfd;
+ int on = 1;
+
+ const struct sockaddr_in address = {
+ AF_INET,
+ htons(9999),
+ {0},
+ };
+
+ serverfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
+ if (bind(serverfd, (struct sockaddr *)&address, sizeof(address)))
+ return 1;
+
+ listen(serverfd, 256);
+
+ writef("serverfd: %d\n", serverfd);
+ clientfd = accept(serverfd, 0, 0);
+ writef("clientfd: %d\n", clientfd);
+
+ struct pollfd fds[1] = {
+ {clientfd, POLLIN, 0},
+ };
+
+ for (;;) {
+ int ret = poll(fds, 1, 50000);
+ if (ret == -1)
+ return 2;
+
+ if (fds[0].revents & POLLIN) {
+ int nrecv;
+
+ char buf[20];
+
+ nrecv = recv(clientfd, buf, sizeof(buf), 0);
+ printf("received %d bytes\n", nrecv);
+ nrecv = recv(clientfd, buf, sizeof(buf), 0);
+ printf("received %d bytes\n", nrecv);
+ nrecv = recv(clientfd, buf, sizeof(buf), 0);
+ printf("received %d bytes\n", nrecv);
+
+ return 3;
+
+ if (nrecv == -1) {
+ return errno;
+ } else if (nrecv == 0) {
+ writef("Disconnect.\n");
+ fds[0].fd = -1;
+ fds[0].revents = 0;
+ close(clientfd);
+ }
+
+ writef("received: %d bytes\n", nrecv);
+ }
+ }
+
+ return 0;
+}
diff --git a/send.c b/send.c
new file mode 100644
index 0000000..50ab373
--- /dev/null
+++ b/send.c
@@ -0,0 +1,76 @@
+#include "common.h"
+#include <arpa/inet.h>
+#include <errno.h>
+#include <signal.h>
+#include <time.h>
+#include <unistd.h>
+
+// NOTE: Errno could be unset and contain an error for a previous command
+void debug_panic(const char *msg)
+{
+ writef("%s errno: %d\n", msg, errno);
+ raise(SIGINT);
+}
+
+int main(void)
+{
+ // time for a new entered message
+ time_t now;
+ // localtime of new sent message
+ struct tm *ltime;
+
+ int serverfd;
+
+ struct message input = {
+ .author = "Friendship",
+ };
+
+ serverfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (serverfd == -1)
+ debug_panic("Error while getting socket.");
+
+ // Set timestamp for the message
+ time(&now);
+ ltime = localtime(&now);
+ strftime(input.timestamp, sizeof(input.timestamp), "%H:%M:%S", ltime);
+
+ input.text = "HII!!";
+ input.len = str_len(input.text);
+
+ const struct sockaddr_in address = {
+ AF_INET,
+ htons(9999),
+ {0},
+ };
+
+ if (connect(serverfd, (struct sockaddr *)&address, sizeof(address)))
+ debug_panic("Error while connecting.");
+
+ u16 buf_len = MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN + input.len;
+ printf("buf_len: %d\n", buf_len);
+ char buf[buf_len];
+ str_cpy(buf, input.author);
+ str_cpy(buf + MESSAGE_AUTHOR_LEN, input.timestamp);
+ str_cpy(buf + MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN, input.text);
+
+ int n = send(serverfd, &buf, buf_len, 0);
+ if (n == -1)
+ debug_panic("Error while sending message");
+ writef("%d bytes sent.\n", n);
+
+ {
+ input.text = "cleared";
+ u16 buf_len = MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN + input.len;
+ printf("buf_len: %d\n", buf_len);
+ char buf[buf_len];
+ str_cpy(buf, input.author);
+ str_cpy(buf + MESSAGE_AUTHOR_LEN, input.timestamp);
+ str_cpy(buf + MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN, input.text);
+ int n = send(serverfd, &buf, buf_len, 0);
+ if (n == -1)
+ debug_panic("Error while sending message");
+ writef("%d bytes sent.\n", n);
+ }
+
+ return 0;
+}
diff --git a/server.c b/server.c
index 675bfb7..06a26b3 100644
--- a/server.c
+++ b/server.c
@@ -20,10 +20,10 @@
// TODO: send message to all other clients
// - implement different rooms
// - implement history
+// - [ ] fix server copying the bytes correctly
// - implement tls
#include "common.h"
-#include "config.h"
#include <arpa/inet.h>
#include <errno.h>
#include <poll.h>
@@ -62,17 +62,15 @@ int main(void)
const struct sockaddr_in address = {
AF_INET,
- htons(PORT),
+ htons(9999),
{0},
};
- if (bind(serverfd, (struct sockaddr *)&address, sizeof(address))) {
+ if (bind(serverfd, (struct sockaddr *)&address, sizeof(address)))
err_exit("Error while binding.");
- }
- if (listen(serverfd, BUF_MAX)) {
+ if (listen(serverfd, BUF_MAX))
err_exit("Error while listening");
- }
writef("Listening on localhost:%d\n", PORT);
@@ -144,6 +142,7 @@ int main(void)
nclient--;
break;
} else if (nrecv == -1) {
+ // TODO: this can happen when connect is reset by pear
err_exit("Error while receiving from client socket.");
}
@@ -160,12 +159,10 @@ int main(void)
printf("Retransmitted message to client %d.\n", j);
}
- // // TODO: check if bytes are correct
- // FILE *f = fopen("srv_recv.bin", "wb");
- // fwrite(&msg_recv, sizeof(struct message), 1, f);
- // fclose(f);
- //
- // printf("written %lu bytes to srv_recv.bin\n", sizeof(msg_recv));
+ // // TODO: Serialize received message
+ FILE *f = fopen(filename, "wb");
+ save_message(&msg_recv, f);
+ fclose(f);
// return 0;
}
}