87 lines
2.3 KiB
C
87 lines
2.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <sys/poll.h>
|
|
#include <netdb.h>
|
|
|
|
/** DATA BUFFER **/
|
|
/*******************/
|
|
|
|
struct buffer_t {
|
|
char* data;
|
|
int length;
|
|
struct buffer_t* next;
|
|
};
|
|
typedef struct buffer_t buffer_t;
|
|
|
|
buffer_t* buffer_create();
|
|
|
|
int buffer_is_empty(buffer_t*);
|
|
int buffer_length(buffer_t*);
|
|
void buffer_append(buffer_t*, const char*, int);
|
|
void buffer_append_str(buffer_t*, const char*);
|
|
|
|
char* buffer_read(buffer_t*, int*);
|
|
char* buffer_read_str(buffer_t*, int*);
|
|
char* buffer_flush(buffer_t*, int*);
|
|
char* buffer_flush_str(buffer_t*, int*);
|
|
|
|
void buffer_clear(buffer_t*);
|
|
void buffer_free(buffer_t*);
|
|
|
|
|
|
/** WEB SOCKET **/
|
|
/******************/
|
|
|
|
typedef enum {
|
|
// block on system calls
|
|
WS_BLOCK = 0,
|
|
// do not block on system calls
|
|
WS_NOBLOCK = 1,
|
|
// block until a full packet is received
|
|
WS_FULL_RECV = 2,
|
|
// block until a full packet is sent
|
|
WS_FULL_SEND = 4
|
|
} ws_mode_t;
|
|
// mode options that are not mutually exclusive can be
|
|
// concatenated using the OR (|) operator. note that
|
|
// WS_BLOCK and WS_NOBLOCK control blocking only on the
|
|
// system socket functions, whereas WS_FULL_RECV and
|
|
// WS_FULL_SEND will block on the protocol level, until
|
|
// a packet is fully sent or received
|
|
|
|
// for example, WS_BLOCK alone will block until data is
|
|
// available, but will immediately return nothing if the
|
|
// data received is not a full packet. however, using
|
|
// WS_BLOCK | WS_FULL_RECV will block until data is
|
|
// available, and then until a full websocket packet is
|
|
// fully received, after which the data is then returned.
|
|
// WS_NOBLOCK behaves in the same way except the system
|
|
// calls will not block, but with WS_FULL_RECV enabled
|
|
// it will then block until a full packet is received
|
|
|
|
// currently WS_FULL_SEND is set to be always enabled
|
|
// when the mode is changed because i do not currently
|
|
// see a need for partial sends in this client, thus
|
|
// all sends will block until the full packet is sent
|
|
// over the wire. if anyone else modifying this client
|
|
// has an EXTREME THIRST for partial sends, they can
|
|
// implement this behavior themselves
|
|
|
|
typedef struct {
|
|
int sock, mode;
|
|
buffer_t buffer;
|
|
} wsock_t;
|
|
|
|
wsock_t* wsock_open(const char*, const char*, uint16_t);
|
|
int wsock_mode_set(wsock_t*, int);
|
|
int wsock_mode_get(wsock_t*);
|
|
|
|
|
|
|
|
int wsock_is_open(wsock_t*);
|
|
void wsock_close(wsock_t*);
|