optimizing on packet buffering is hard

This commit is contained in:
mallocnull 2017-10-25 22:00:19 +00:00
parent 8152424c7a
commit a949f596b8
8 changed files with 44 additions and 20 deletions

View file

@ -1,22 +1,19 @@
PROTOCOL
this is almost a carbon copy of every other tcp/ip protocol i write
don't care enough to make it fancy
all numbers are packed unless stated otherwise
packed values are msb
strings and blobs are prefixed with a packed ushort length
byte n is b.n
region n is r.n
---
byte the zero: packet id
byte the one: region count
for 0 <= n < b.1
byte the byte after the previous byte byte:
region length < 128: one byte
region length >= 128: two bytes, highest bit on msb always 1
byte the zero: 0xDE
byte the one: 0xAD
byte the two: packet id
byte the 3-6: total body length - uint
byte the bytes after the header octet bytes bytes:
raw body data shoved next to each other with no separation

View file

@ -0,0 +1,2 @@
#include "packet.h"

View file

@ -0,0 +1,23 @@
#ifndef PACKET_H
#define PACKET_H
#include <stdlib.h>
#include <stdint.h>
#include "list.h"
#define PCK_ID_REGISTER 0
#define PCK_ID_LOGIN 1
#define PCK_ID_CHANGE_CTX 2
typedef struct packet_t packet_t;
struct packet_t {
uint8_t id;
uint32_t length;
uint8_t **regions;
const uint8_t *region_lengths;
uint8_t region_count;
};
#endif

View file

@ -70,8 +70,7 @@ void queue_nodes_free(queue_t *queue) {
queue_node_t *ptr = queue->front, *next;
while(ptr != NULL) {
next = ptr->next;
if(ptr->data != NULL)
free(ptr->data);
free(ptr->data);
free(ptr);
ptr = next;

View file

@ -1,6 +1,7 @@
#include "server.h"
void server() {
user_context_init();
server_context_start();
socket_t *sock = sock_server_init("6770");
@ -30,6 +31,8 @@ void server() {
printf("got %s\r\n", in);
server_context_stop();
user_context_free();
sock_stop(sock);
sock_free(sock);
}

View file

@ -5,7 +5,7 @@ struct {
user_t **users;
} ctx;
void user_ctx_init() {
void user_context_init() {
ctx.active_users = list_init();
ctx.users = (user_t**)malloc(MAX_CONNS * sizeof(user_t*));
@ -14,13 +14,12 @@ void user_ctx_init() {
ctx.users[i] = NULL;
}
void user_ctx_free() {
void user_context_free() {
list_free(ctx.active_users);
int i;
for(i = 0; i < MAX_CONNS; ++i)
if(ctx.users[i] != NULL)
free(ctx.users[i]);
free(ctx.users[i]);
free(ctx.users);
}

View file

@ -14,12 +14,14 @@ struct user_t {
pthread_mutex_t mx_out_packets;
socket_t *sock;
uint16_t context_id;
uint16_t user_id, context_id;
uint16_t x, y;
};
void user_ctx_init();
void user_context_init();
void user_ctx_free();
void user_context_free();
#endif

View file

@ -54,8 +54,7 @@ void stack_nodes_free(stack_t *stack) {
stack_t *ptr = stack, *next;
while(ptr != NULL) {
next = ptr->next;
if(ptr->data != NULL)
free(ptr->data);
free(ptr->data);
free(ptr);
ptr = next;