Updated sockets/queue

This commit is contained in:
Hoguchi-live 2023-01-19 12:59:10 +01:00
parent e752c806b4
commit 5ab200bf3d
14 changed files with 152 additions and 245 deletions

5
.gitignore vendored
View File

@ -6,10 +6,11 @@ bin/**
src/old/** src/old/**
# Bear config # Bear config
./compile_commands.json compile_commands.json
# Shortcut launchers # Shortcut launchers
./*.sh run.sh
memchk.sh
# Prerequisites # Prerequisites
*.d *.d

View File

@ -1,26 +0,0 @@
[
{
"arguments": [
"/usr/bin/clang",
"-Wall",
"-pedantic",
"-Wno-gnu-statement-expression",
"-Ilib/cglm/include",
"-Ilib/glad/include",
"-Ilib/glfw/include",
"-Ilib/stb",
"-Iinclude/",
"-I/usr/include/freetype2",
"-I/usr/include/stb",
"-Ilib",
"-I/usr/local/include",
"-c",
"-o",
"build/main.o",
"src/main.c"
],
"directory": "/home/steaky/git/wotos-mpv",
"file": "/home/steaky/git/wotos-mpv/src/main.c",
"output": "/home/steaky/git/wotos-mpv/build/main.o"
}
]

View File

@ -1,7 +1,12 @@
#include "sock.h" #include "sock.h"
#define QUEUE_SIZE 100
inline bool _SetSocketBlockingEnabled(int fd, bool blocking);
struct Socket *socket_create(){ struct Socket *socket_create(){
struct Socket *self = malloc(sizeof(struct Socket)); struct Socket *self = malloc(sizeof(struct Socket));
self->queue = queue_create();
return self; return self;
} }
@ -27,6 +32,25 @@ void socket_init(struct Socket* self){
perror("Bind failed"); perror("Bind failed");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
queue_init(self->queue, QUEUE_SIZE);
}
struct Socket *socket_init_start(){
struct Socket *sock = socket_create();
socket_init(sock);
socket_listen(sock);
socket_accept(sock);
return sock;
}
void socket_destroy(struct Socket* self){
// Close the endpoint and listening socket
close(self->endpoint);
shutdown(self->server_fd, SHUT_RDWR);
free(self->queue);
free(self);
} }
void socket_listen(struct Socket *self){ void socket_listen(struct Socket *self){
@ -36,3 +60,39 @@ void socket_listen(struct Socket *self){
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
void socket_accept(struct Socket *self){
int addrlen = sizeof(self->address);
if ((self->endpoint = accept(self->server_fd, (struct sockaddr *)&(self->address), (socklen_t *)&addrlen)) < 0)
{
perror("Accept failed");
exit(EXIT_FAILURE);
}
if(_SetSocketBlockingEnabled(self->endpoint, 0)){
perror("Setting selfet as blocking failed");
exit(EXIT_FAILURE);
}
}
void update_queue(struct Socket* self){
}
/**
Sets socket as blocking
Returns true on success, or false if there was an error
*/
bool _SetSocketBlockingEnabled(int fd, bool blocking)
{
if (fd < 0) return false;
#ifdef _WIN32
unsigned long mode = blocking ? 0 : 1;
return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? true : false;
#else
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1) return false;
flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
return (fcntl(fd, F_SETFL, flags) == 0) ? true : false;
#endif
}

View File

@ -3,12 +3,17 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> //TODO Unnecessary
#include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include "../util/safe_queue.h"
#define QUEUE_LENGTH 10 #define QUEUE_LENGTH 10
#define RECV_BUFFER_SIZE 2048 #define RECV_BUFFER_SIZE 2048
@ -19,10 +24,18 @@ struct Socket{
struct sockaddr_in address; // Socket address struct sockaddr_in address; // Socket address
int opt; int opt;
int endpoint; int endpoint;
queue_t *queue;
}; };
struct Socket *socket_create(); struct Socket *socket_create();
void socket_init(struct Socket* socket); void socket_init(struct Socket*);
void socket_listen(struct Socket* socket); void socket_destroy(struct Socket*);
struct Socket *socket_init_start();
void socket_listen(struct Socket*);
void socket_accept();
void update_queue(struct Socket*);
#endif #endif

View File

@ -3,10 +3,7 @@
bool SetSocketBlockingEnabled(int fd, bool blocking); bool SetSocketBlockingEnabled(int fd, bool blocking);
void _setup(){ void _setup(){
sock = socket_create(); sock = socket_init_start();
socket_init(sock);
socket_listen(sock);
socket_accept();
//window = window_create(init, destroy, tick, update, render); //window = window_create(init, destroy, tick, update, render);
//player = player_create(); //player = player_create();
//renderer = renderer_create(); //renderer = renderer_create();

View File

@ -2,7 +2,6 @@
#define _MAIN_H_ #define _MAIN_H_
#include "window_setup.h" #include "window_setup.h"
#include "socket_setup.h"
#include "connection/sock.h" #include "connection/sock.h"
#include <fcntl.h> #include <fcntl.h>

View File

@ -1,35 +0,0 @@
#include "socket_setup.h"
inline bool _SetSocketBlockingEnabled(int fd, bool blocking);
void socket_accept(){
int addrlen = sizeof(sock->address);
if ((sock->endpoint = accept(sock->server_fd, (struct sockaddr *)&(sock->address), (socklen_t *)&addrlen)) < 0)
{
perror("Accept failed");
exit(EXIT_FAILURE);
}
if(_SetSocketBlockingEnabled(sock->endpoint, 0)){
perror("Setting socket as blocking failed");
exit(EXIT_FAILURE);
}
}
/**
Sets socket as blocking
Returns true on success, or false if there was an error
*/
bool _SetSocketBlockingEnabled(int fd, bool blocking)
{
if (fd < 0) return false;
#ifdef _WIN32
unsigned long mode = blocking ? 0 : 1;
return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? true : false;
#else
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1) return false;
flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
return (fcntl(fd, F_SETFL, flags) == 0) ? true : false;
#endif
}

View File

@ -1,7 +0,0 @@
#include "connection/sock.h"
#include <fcntl.h>
#include <stdbool.h> //TODO Unnecessary
extern struct Socket *sock;
void socket_accept();

View File

@ -1,47 +0,0 @@
#ifndef AABB_H
#define AABB_H
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-braces"
#include <cglm/cglm.h>
#include <cglm/struct.h>
#pragma GCC diagnostic pop
#include "types.h"
#include "fmath.h"
typedef vec3s AABB[2];
#define AABB_COPY(_aabb) { (_aabb)[0], (_aabb)[1] }
// returns the depth of collision of aabb a into aabb b
static inline vec3s glms_aabb_aabb_depth(vec3s a[2], vec3s b[2]) {
vec3s result, a_c = glms_aabb_center(a), b_c = glms_aabb_center(b);
for (size_t i = 0; i < 3; i++) {
if (a_c.raw[i] < b_c.raw[i]) {
result.raw[i] = a[1].raw[i] - b[0].raw[i];
} else {
result.raw[i] = b[1].raw[i] - a[0].raw[i];
}
}
return result;
}
// scales an AABB the specified amounts along each axis
static inline void glms_aabb_scale(AABB box, vec3s scale, AABB dest) {
vec3s center = glms_aabb_center(box);
vec3s size = glms_vec3_sub(box[1], box[0]);
vec3s new_size = glms_vec3_mul(size, scale);
vec3s half_new_size = glms_vec3_scale(new_size, 0.5f);
// scaled AABB centered around same center
dest[0] = glms_vec3_add(glms_vec3_scale(half_new_size, -1.0f), center);
dest[1] = glms_vec3_add(glms_vec3_sub(new_size, half_new_size), center);
}
#endif

View File

@ -1,86 +0,0 @@
#include "color.h"
// Conversion code adapted from https://gist.github.com/mattatz/44f081cac87e2f7c8980#file-labcolorspace-cginc-L29
vec3s rgb2xyz(vec3s c) {
vec3s tmp;
tmp.x = ( c.x > 0.04045 ) ? pow( ( c.x + 0.055 ) / 1.055, 2.4 ) : c.x / 12.92;
tmp.y = ( c.y > 0.04045 ) ? pow( ( c.y + 0.055 ) / 1.055, 2.4 ) : c.y / 12.92,
tmp.z = ( c.z > 0.04045 ) ? pow( ( c.z + 0.055 ) / 1.055, 2.4 ) : c.z / 12.92;
const mat3s mat = (mat3s) { .col = {
(vec3s) {{ 0.4124, 0.3576, 0.1805 }},
(vec3s) {{ 0.2126, 0.7152, 0.0722 }},
(vec3s) {{ 0.0193, 0.1192, 0.9505 }}
}};
return glms_vec3_scale(glms_mat3_mulv(mat, tmp), 100.0f);
}
vec3s xyz2lab(vec3s c) {
vec3s n = glms_vec3_div(c, (vec3s){{95.047, 100, 108.883}});
vec3s v;
v.x = (n.x > 0.008856) ? pow(n.x, 1.0 / 3.0) : (7.787 * n.x) + (16.0 / 116.0);
v.y = (n.y > 0.008856) ? pow(n.y, 1.0 / 3.0) : (7.787 * n.y) + (16.0 / 116.0);
v.z = (n.z > 0.008856) ? pow(n.z, 1.0 / 3.0) : (7.787 * n.z) + (16.0 / 116.0);
return (vec3s){{(116.0 * v.y) - 16.0, 500.0 * (v.x - v.y), 200.0 * (v.y - v.z)}};
}
vec3s rgb2lab(vec3s c) {
vec3s lab = xyz2lab(rgb2xyz(c));
return (vec3s){{lab.x / 100.0, 0.5 + 0.5 * (lab.y / 127.0), 0.5 + 0.5 * (lab.z / 127.0)}};
}
vec3s lab2xyz(vec3s c) {
f32 fy = (c.x + 16.0) / 116.0;
f32 fx = c.y / 500.0 + fy;
f32 fz = fy - c.z / 200.0;
return (vec3s){{95.047 * ((fx > 0.206897) ? fx * fx * fx : (fx - 16.0 / 116.0) / 7.787),
100.000 * ((fy > 0.206897) ? fy * fy * fy : (fy - 16.0 / 116.0) / 7.787),
108.883 * ((fz > 0.206897) ? fz * fz * fz : (fz - 16.0 / 116.0) / 7.787)}};
}
vec3s xyz2rgb(vec3s c) {
const mat3s mat = (mat3s) { .col = {
(vec3s) {{ 3.2406, -1.5372, -0.4986 }},
(vec3s) {{ -0.9689, 1.8758, 0.0415 }},
(vec3s) {{ 0.0557, -0.2040, 1.0570 }}
}
};
vec3s v = glms_mat3_mulv(mat, glms_vec3_scale(c, 1.0f / 100.0f));
vec3s r;
r.x = (v.x > 0.0031308) ? ((1.055 * pow(v.x, (1.0 / 2.4))) - 0.055) : 12.92 * v.x;
r.y = (v.y > 0.0031308) ? ((1.055 * pow(v.y, (1.0 / 2.4))) - 0.055) : 12.92 * v.y;
r.z = (v.z > 0.0031308) ? ((1.055 * pow(v.z, (1.0 / 2.4))) - 0.055) : 12.92 * v.z;
return r;
}
vec3s lab2rgb(vec3s c) {
return xyz2rgb(lab2xyz((vec3s){{100.0 * c.x, 2.0 * 127.0 * (c.y - 0.5), 2.0 * 127.0 * (c.z - 0.5)}}));
}
vec3s rgb_brighten(vec3s rgb, f32 d) {
vec3s lab = rgb2lab(rgb);
return lab2rgb((vec3s){{lab.x + d, lab.y, lab.z}});
}
vec4s rgba_brighten(vec4s rgba, f32 d) {
return glms_vec4(rgb_brighten((vec3s){{rgba.x, rgba.y, rgba.z}}, d), rgba.w);
}
vec4s rgba_lerp(vec4s rgba_a, vec4s rgba_b, f32 t) {
vec3s lab_a = rgb2lab(glms_vec3(rgba_a));
vec3s lab_b = rgb2lab(glms_vec3(rgba_b));
return glms_vec4(
lab2rgb(((vec3s){{(lerpf(lab_a.x, lab_b.x, t)),
(lerpf(lab_a.y, lab_b.y, t)),
(lerpf(lab_a.z, lab_b.z, t))}})),
lerpf(rgba_a.w, rgba_b.w, t));
}
vec4s rgba_lerp3(vec4s rgba_a, vec4s rgba_b, vec4s rgba_c, f32 t) {
if (t <= 0.5f) {
return rgba_lerp(rgba_a, rgba_b, t * 2.0f);
}
return rgba_lerp(rgba_b, rgba_c, (t - 0.5f) * 2.0f);
}

View File

@ -1,23 +0,0 @@
#ifndef COLOR_H
#define COLOR_H
#include "../util/util.h"
// converts RGBA in hexadecimal u32 in [0, 255] to vec4s in [0, 1]
#define RGBAX2F(c) ((vec4s) {{\
((c & 0xFF000000) >> 24) / 255.0f,\
((c & 0x00FF0000) >> 16) / 255.0f,\
((c & 0x0000FF00) >> 8) / 255.0f,\
((c & 0x000000FF) >> 0) / 255.0f\
}})
vec3s rgb2lab(vec3s rgb);
vec3s hsv2lab(vec3s hsv);
vec3s rgb_brighten(vec3s rgb, f32 d);
vec4s rgba_brighten(vec4s rgba, f32 d);
vec4s rgba_lerp(vec4s rgba_a, vec4s rgba_b, f32 t);
vec4s rgba_lerp3(vec4s rgba_a, vec4s rgba_b, vec4s rgba_c, f32 t);
#endif

48
src/util/safe_queue.c Normal file
View File

@ -0,0 +1,48 @@
#include "safe_queue.h"
queue_t *queue_create(){
queue_t *self = malloc(sizeof(queue_t));
self->mutex = malloc(sizeof(pthread_mutex_t));
return self;
}
void queue_init(queue_t *self, size_t size){
*self = (queue_t){0, 0, size, NULL,};
}
void queue_destroy(queue_t *self){
free(self->mutex);
free(self);
}
queue_t *queue_init_create(size_t size){
queue_t *self = queue_create();
queue_init(self, size);
return self;
}
void* queue_read(queue_t *self) {
if (self->tail == self->head) {
return NULL;
}
pthread_mutex_lock(self->mutex);
void* handle = self->data[self->tail];
self->data[self->tail] = NULL;
self->tail = (self->tail + 1) % self->size;
pthread_mutex_unlock(self->mutex);
return handle;
}
int queue_write(queue_t *self, void* handle) {
if (((self->head + 1) % self->size) == self->tail) {
return -1;
}
pthread_mutex_lock(self->mutex);
self->data[self->head] = handle;
self->head = (self->head + 1) % self->size;
pthread_mutex_unlock(self->mutex);
return 0;
}

25
src/util/safe_queue.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef _SAFE_QUEUE_H_
#define _SAFE_QUEUE_H_
// Safe queue implementation adapted from https://gist.github.com/ryankurte/
#include <stdlib.h>
#include <pthread.h>
typedef struct {
size_t head;
size_t tail;
size_t size;
void** data;
pthread_mutex_t *mutex;
} queue_t;
queue_t *queue_create();
void queue_init(queue_t *, size_t);
void queue_destroy(queue_t *);
queue_t *queue_init_create(size_t);
void *queue_read(queue_t *queue);
int queue_write(queue_t *queue, void* handle);
#endif

View File

@ -9,7 +9,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
//#include <iostream>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <limits.h> #include <limits.h>
@ -30,18 +29,7 @@
#include "fmath.h" #include "fmath.h"
#include "time.h" #include "time.h"
//#include "direction.h"
#include "types.h" #include "types.h"
//#include "bitmap.h" //#include "bitmap.h"
//#include "color.h"
// TODO: move elsewhere
static inline void memsetl(void *dst, u64 v, size_t sz) {
assert(sz % 8 == 0);
size_t n = sz / 8;
for (size_t i = 0; i < n; i++) {
((u64*)(dst))[i] = v;
}
}
#endif #endif