Server handles join packets nicely.

This commit is contained in:
2026-02-21 11:28:00 -05:00
parent e81b5e5b48
commit 226c4a8b52
7 changed files with 180 additions and 174 deletions

View File

@@ -1,12 +1,13 @@
#ifndef SESSION_H
#define SESSION_H
#ifndef SESSION__H
#define SESSION__H
#include <libwebsockets.h>
#include <stdbool.h>
#include <stdint.h>
#include "data.h"
// Includes terminating null.
#define SESSION_USERNAME_MAX_LEN 32
#define SESSION_CHAT_BUF_SIZE 32768
/**
@@ -14,13 +15,14 @@
* Represents a single WebSocket client session in the chat server.
*/
typedef struct SESSION {
struct lws* wsi; // Libwebsockets connection handle.
uint64_t id; // Unique session ID.
struct SESSION* next; // Next session in the internal list.
char name[SESSION_USERNAME_MAX_LEN]; // Stored username.
bool named; // True once username is set.
unsigned char buf[LWS_PRE + SESSION_CHAT_BUF_SIZE]; // Outgoing buffer per session
size_t buf_len; // Length of data in buf
struct lws* wsi; // Libwebsockets connection handle.
uint64_t id; // Unique session ID.
struct SESSION* next; // Next session in the internal list.
Name name; // Stored name.
bool named; // True once name is set.
unsigned char
buf[LWS_PRE + SESSION_CHAT_BUF_SIZE]; // Outgoing buffer per session
size_t buf_len; // Length of data in buf
} Session;
/**
@@ -58,31 +60,31 @@ Session* session_get_head(void);
void session_broadcast(void (*cb)(Session* sess, void* user), void* user);
/**
* session_set_username
* Store a username in the given session.
* session_set_name
* Store a name with the given session.
*
* @param sess The session to update.
* @param username Null-terminated string to copy into the session.
* @param name Null-terminated string to copy into the session.
*/
void session_set_username(Session* sess, const char* username);
void session_set_name(Session* sess, const char* name);
/**
* session_get_username
* Fetch the username stored in the session.
* session_get_name
* Fetch the name stored in the session.
*
* @param sess The session to query.
* @return Pointer to the stored username (readonly).
* @return Pointer to the stored name (readonly).
*/
const char* session_get_username(const Session* sess);
const char* session_get_name(const Session* sess);
/**
* session_has_username
* Check whether a session has already set a username.
* session_has_name
* Check whether a session has already set a name.
*
* @param sess The session to query.
* @return True if a username is set, false otherwise.
* @return True if a name is set, false otherwise.
*/
bool session_has_username(const Session* sess);
bool session_has_name(const Session* sess);
/**
* session_get_id
@@ -93,4 +95,4 @@ bool session_has_username(const Session* sess);
*/
uint64_t session_get_id(const Session* sess);
#endif // SESSION_H
#endif