libfishsoup integration
This commit is contained in:
parent
3257bb449e
commit
a2f2f18910
6 changed files with 262 additions and 6 deletions
|
|
@ -392,4 +392,95 @@ const char* suic_uuid_generate_v4(void);
|
|||
/* Generate a random UUID v4 from seed */
|
||||
const char* suic_uuid_generate_v4_seeded(uint64_t seed);
|
||||
|
||||
/* ===== FISHSOUP NETWORKING API ===== */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <poll.h>
|
||||
|
||||
#define FISHSOUP_PORT 3219
|
||||
#define FISHSOUP_PING 3
|
||||
|
||||
enum fishsoup_packet_type {
|
||||
FISHSOUP_PACKET_PING = 0,
|
||||
FISHSOUP_PACKET_PONG
|
||||
};
|
||||
|
||||
#define FISHSOUP_PACKET_FLAG_BROADCAST (1 << 0)
|
||||
|
||||
typedef struct fishsoup_packet_header fishsoup_packet_header_t;
|
||||
typedef struct fishsoup_packet fishsoup_packet_t;
|
||||
typedef struct fishsoup_client fishsoup_client_t;
|
||||
typedef struct fishsoup_server fishsoup_server_t;
|
||||
|
||||
#pragma pack(1)
|
||||
struct fishsoup_packet_header {
|
||||
unsigned char type;
|
||||
unsigned char flag;
|
||||
unsigned short length;
|
||||
};
|
||||
#pragma pack()
|
||||
|
||||
struct fishsoup_packet {
|
||||
fishsoup_packet_header_t header;
|
||||
void* data;
|
||||
};
|
||||
|
||||
struct fishsoup_client {
|
||||
int fd;
|
||||
time_t last_ping;
|
||||
void (*on_packet)(fishsoup_client_t* client, fishsoup_packet_t* packet);
|
||||
struct {
|
||||
int sent_ping;
|
||||
int cleanup;
|
||||
} server;
|
||||
};
|
||||
|
||||
struct fishsoup_server {
|
||||
int fd;
|
||||
struct sockaddr_in address;
|
||||
fishsoup_client_t* clients;
|
||||
void (*on_client)(fishsoup_server_t* server, fishsoup_client_t* client);
|
||||
};
|
||||
|
||||
fishsoup_client_t* fishsoup_client(const char* host, int port);
|
||||
void fishsoup_client_on_packet(fishsoup_client_t* client, void (*on_packet)(fishsoup_client_t* client, fishsoup_packet_t* packet));
|
||||
int fishsoup_client_poll(fishsoup_client_t* client);
|
||||
void fishsoup_client_destroy(fishsoup_client_t* client);
|
||||
|
||||
fishsoup_server_t* fishsoup_server(int port);
|
||||
int fishsoup_server_clients(fishsoup_server_t* server);
|
||||
void fishsoup_server_loop(fishsoup_server_t* server);
|
||||
void fishsoup_server_on_client(fishsoup_server_t* server, void (*on_client)(fishsoup_server_t* server, fishsoup_client_t* client));
|
||||
void fishsoup_server_destroy(fishsoup_server_t* server);
|
||||
|
||||
int fishsoup_fd_send(int fd, const void* buf, int length);
|
||||
int fishsoup_fd_recv(int fd, void* buf, int length);
|
||||
int fishsoup_packet_send(int fd, fishsoup_packet_t* packet);
|
||||
int fishsoup_packet_recv(int fd, fishsoup_packet_t* packet);
|
||||
|
||||
typedef void (*suic_fishsoup_client_on_packet_fn)(fishsoup_client_t* client, fishsoup_packet_t* packet);
|
||||
typedef void (*suic_fishsoup_server_on_client_fn)(fishsoup_server_t* server, fishsoup_client_t* client);
|
||||
|
||||
/* Client functions */
|
||||
fishsoup_client_t* suic_fishsoup_client_create(const char* host, int port);
|
||||
void suic_fishsoup_client_on_packet(fishsoup_client_t* client, suic_fishsoup_client_on_packet_fn on_packet);
|
||||
int suic_fishsoup_client_poll(fishsoup_client_t* client);
|
||||
void suic_fishsoup_client_destroy(fishsoup_client_t* client);
|
||||
|
||||
/* Server functions */
|
||||
fishsoup_server_t* suic_fishsoup_server_create(int port);
|
||||
int suic_fishsoup_server_clients(fishsoup_server_t* server);
|
||||
void suic_fishsoup_server_loop(fishsoup_server_t* server);
|
||||
void suic_fishsoup_server_on_client(fishsoup_server_t* server, suic_fishsoup_server_on_client_fn on_client);
|
||||
void suic_fishsoup_server_destroy(fishsoup_server_t* server);
|
||||
|
||||
/* Packet functions */
|
||||
int suic_fishsoup_packet_send(int fd, fishsoup_packet_t* packet);
|
||||
int suic_fishsoup_packet_recv(int fd, fishsoup_packet_t* packet);
|
||||
|
||||
#endif // LIBSUICMEZ_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue