ws server +proto
This commit is contained in:
18
inc/client.h
Normal file
18
inc/client.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef __CLIENT_H__
|
||||
#define __CLIENT_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "wsServer/include/ws.h"
|
||||
#include "ptQueue/inc/ptQueue.h"
|
||||
|
||||
typedef struct {
|
||||
ws_cli_conn_t clientId;
|
||||
uint8_t isAuthed;
|
||||
ptQueue* incomeQ;
|
||||
ptQueue* outcomeQ;
|
||||
uint64_t connectedAt;
|
||||
uint32_t streamRegIterator;
|
||||
} ClientContext;
|
||||
|
||||
#endif //ifndef __CLIENT_H__
|
||||
45
inc/context.h
Normal file
45
inc/context.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef __CONTEXT_H__
|
||||
#define __CONTEXT_H__
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include "ptQueue/inc/ptQueue.h"
|
||||
#include "wsServer/include/ws.h"
|
||||
|
||||
#include "linkedlist.h"
|
||||
#include "sized_ptr.h"
|
||||
|
||||
#include "streamed.h"
|
||||
|
||||
typedef struct {
|
||||
SizedPtr* bufs;
|
||||
uint8_t buffersCount;
|
||||
_Atomic (uint8_t) readRequestIdx;
|
||||
_Atomic (uint8_t) currWritingIdx;
|
||||
} OutgoingBuffers;
|
||||
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint8_t* emulState;
|
||||
uint64_t* clockCounter;
|
||||
LinkedListEntry** clientsHead;
|
||||
uint8_t* utilizedFlag;
|
||||
OutgoingBuffers* outBufs;
|
||||
DeviceSegStreamReg** deviceStreamRegs;
|
||||
uint8_t** devicesMem;
|
||||
size_t devicesCount;
|
||||
} EmulContext;
|
||||
|
||||
|
||||
typedef struct {
|
||||
pthread_mutex_t registerMutex;
|
||||
ptQueue* regQueue;
|
||||
uint8_t* accessToken;
|
||||
EmulContext* emulContext;
|
||||
} ServerContext;
|
||||
|
||||
|
||||
|
||||
#endif //ifndef __CONTEXT_H__
|
||||
13
inc/events.h
Normal file
13
inc/events.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef __EVENTS_H__
|
||||
#define __EVENTS_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include "context.h"
|
||||
|
||||
typedef struct {
|
||||
uint8_t regType;
|
||||
ClientContext* ctx;
|
||||
} ClientRegistrationEvent;
|
||||
|
||||
|
||||
#endif //ifndef __EVENTS_H__
|
||||
0
inc/executor/executor.h
Normal file
0
inc/executor/executor.h
Normal file
13
inc/linkedlist.h
Normal file
13
inc/linkedlist.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef __LINKEDLIST_H__
|
||||
#define __LINKEDLIST_H__
|
||||
|
||||
|
||||
typedef struct LinkedListEntry {
|
||||
struct LinkedListEntry* prevEntry;
|
||||
struct LinkedListEntry* nextEntry;
|
||||
void* payload;
|
||||
} LinkedListEntry;
|
||||
|
||||
void removeLinkedListEntry(LinkedListEntry** head, LinkedListEntry* entry);
|
||||
|
||||
#endif //ifndef __LINKEDLIST_H__
|
||||
42
inc/panic.h
Normal file
42
inc/panic.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef __PANIC_H__
|
||||
#define __PANIC_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <execinfo.h>
|
||||
|
||||
#ifndef PANIC_FD
|
||||
#define PANIC_FD stderr
|
||||
#endif //ifndef PANIC_FD
|
||||
|
||||
#ifndef PANIC_STRACE_LEN
|
||||
#define PANIC_STRACE_LEN 32
|
||||
#endif //ifndef PANIC_STRACE_LEN
|
||||
|
||||
void print_stacktrace(FILE* fd);
|
||||
|
||||
#define panic(...) \
|
||||
do { \
|
||||
fprintf(PANIC_FD, "Panic at %s:%d\n", __FILE__, __LINE__); \
|
||||
fprintf(PANIC_FD, "Panic message:\n\t" __VA_ARGS__); \
|
||||
print_stacktrace(PANIC_FD); \
|
||||
abort(); \
|
||||
} while(0)
|
||||
|
||||
#define NULL_GUARD(ptr, ...) \
|
||||
do { \
|
||||
if((ptr) == NULL) \
|
||||
{ \
|
||||
if(sizeof(#__VA_ARGS__) > 1) \
|
||||
{ \
|
||||
panic(__VA_ARGS__); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
panic("NULL detected in %s", #ptr); \
|
||||
} \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
|
||||
#endif //ifndef __PANIC_H__
|
||||
11
inc/proto/dial.h
Normal file
11
inc/proto/dial.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef __PROTO_DIAL_H__
|
||||
#define __PROTO_DIAL_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "context.h"
|
||||
|
||||
void broadcastClients(EmulContext* emulContext, uint8_t* msg, size_t msgLen);
|
||||
void dispatchOutgoingMessage(OutgoingBuffers* outBufs, ws_cli_conn_t clientIdx, uint8_t* msg, size_t msgLen);
|
||||
|
||||
#endif
|
||||
34
inc/proto/enums.h
Normal file
34
inc/proto/enums.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef __PROTO_ENUMS_H__
|
||||
#define __PROTO_ENUMS_H__
|
||||
|
||||
|
||||
#define REG_EVTYPE_CONNECT 1
|
||||
#define REG_EVTYPE_AUTH 2
|
||||
#define REG_EVTYPE_CLOSE 3
|
||||
|
||||
|
||||
|
||||
|
||||
#define PACKET_TYPE_CTRL 0b0001
|
||||
#define PACKET_TYPE_STREAM 0b0010
|
||||
#define PACKET_TYPE_MEM 0b0011
|
||||
|
||||
|
||||
#define CTRL_TYPE_EXEC 0b0001
|
||||
#define CTRL_TYPE_NOTIF_STATE 0b0010
|
||||
|
||||
#define NOTIF_TYPE_EXEC 0b0000
|
||||
|
||||
#define STREAM_TYPE_REG_REQUEST 0b001
|
||||
#define STREAM_TYPE_REG_DISCARD 0b011
|
||||
#define STREAM_TYPE_REG_CONFIRM 0b101
|
||||
#define STREAM_TYPE_SEND 0b000
|
||||
|
||||
#define MEM_TYPE_READ_REQ 0b0000
|
||||
#define MEM_TYPE_READ_RESP 0b0001
|
||||
#define MEM_TYPE_WRITE_PUSH 0b0010
|
||||
|
||||
|
||||
|
||||
|
||||
#endif //ifndef __PROTO_ENUMS_H__
|
||||
11
inc/proto/handlers.h
Normal file
11
inc/proto/handlers.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef __PROTO_HANDLERS_H__
|
||||
#define __PROTO_HANDLERS_H__
|
||||
|
||||
#include "events.h"
|
||||
|
||||
|
||||
void handleRegEvent(EmulContext* emulContext, ClientRegistrationEvent* ev);
|
||||
void handleAllClients(EmulContext* emulContext);
|
||||
|
||||
|
||||
#endif //ifndef __PROTO_HANDLERS_H__
|
||||
11
inc/proto/handlers/auth.h
Normal file
11
inc/proto/handlers/auth.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef __PROTO_HANDLERS_AUTH_H__
|
||||
#define __PROTO_HANDLERS_AUTH_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include "context.h"
|
||||
|
||||
uint8_t handle_auth(ClientContext* cctx, ws_cli_conn_t client, const uint8_t* msg, uint64_t msgSize, int msgType);
|
||||
LinkedListEntry* disconnectDueTimeout(EmulContext* emulContext, LinkedListEntry* clientEntry);
|
||||
void handleOnClientAuthDone(ClientContext* ctx, EmulContext* emulContext);
|
||||
|
||||
#endif //ifndef __PROTO_HANDLERS_AUTH_H__
|
||||
10
inc/proto/handlers/control.h
Normal file
10
inc/proto/handlers/control.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef __PROTO_HANDLERS_CONTROL_H__
|
||||
#define __PROTO_HANDLERS_CONTROL_H__
|
||||
|
||||
#include "proto/msg.h"
|
||||
#include "proto/dial.h"
|
||||
|
||||
void handleIncomingControlMessage(BaseMessage* msg, EmulContext* emulContext);
|
||||
|
||||
|
||||
#endif //ifndef __PROTO_HANDLERS_CONTROL_H__
|
||||
10
inc/proto/handlers/mem.h
Normal file
10
inc/proto/handlers/mem.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef __PROTO_HANDLERS_MEM_H__
|
||||
#define __PROTO_HANDLERS_MEM_H__
|
||||
|
||||
#include "proto/msg.h"
|
||||
#include "proto/dial.h"
|
||||
|
||||
void handleIncomingMemMessage(BaseMessage* msg, ClientContext* ctx, EmulContext* emulContext);
|
||||
|
||||
#endif //ifndef __PROTO_HANDLERS_MEM_H__
|
||||
|
||||
12
inc/proto/handlers/stream.h
Normal file
12
inc/proto/handlers/stream.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef __PROTO_HANDLERS_STREAM_H__
|
||||
#define __PROTO_HANDLERS_STREAM_H__
|
||||
|
||||
#include "proto/msg.h"
|
||||
#include "proto/dial.h"
|
||||
|
||||
|
||||
void unregisterClientStream(EmulContext* emulContext, ClientContext* ctx, uint32_t regId);
|
||||
void unregisterClientStreams(EmulContext* emulContext, ClientContext* ctx);
|
||||
void handleIncomingStreamMessage(BaseMessage* msg, ClientContext* ctx, EmulContext* emulContext);
|
||||
|
||||
#endif //ifndef __PROTO_HANDLERS_STREAM_H__
|
||||
10
inc/proto/handlers/ws.h
Normal file
10
inc/proto/handlers/ws.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef __PROTO_HANDLERS_WS_H__
|
||||
#define __PROTO_HANDLERS_WS_H__
|
||||
|
||||
#include "wsServer/include/ws.h"
|
||||
|
||||
void onWsMessage(ws_cli_conn_t client, const unsigned char *msg, uint64_t size, int type);
|
||||
void onWsClose(ws_cli_conn_t client);
|
||||
void onWsOpen(ws_cli_conn_t client);
|
||||
|
||||
#endif //ifndef __PROTO_HANDLERS_WS_H__
|
||||
26
inc/proto/msg.h
Normal file
26
inc/proto/msg.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef __PROTO_MSG_H__
|
||||
#define __PROTO_MSG_H__
|
||||
|
||||
#include "wsServer/include/ws.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
ws_cli_conn_t clientIdx;
|
||||
uint8_t* msg;
|
||||
size_t msgLen;
|
||||
} OutgoingMessage;
|
||||
|
||||
typedef struct {
|
||||
uint64_t nonce;
|
||||
uint8_t packetType;
|
||||
uint8_t payloadHeader;
|
||||
const void* payload;
|
||||
size_t payloadLen;
|
||||
} BaseMessage;
|
||||
|
||||
BaseMessage* parseMessage(const uint8_t* bytes, size_t size);
|
||||
uint8_t* createControlNotifyMessage(uint64_t nonce, uint64_t clockCounter, uint8_t newEmulState, size_t* lenOut);
|
||||
uint8_t* createDoneRegMessage(uint64_t nonce, uint8_t X, uint64_t devId, uint64_t segId, uint64_t startAddr, uint64_t segLength, uint32_t regId, size_t* lenOut);
|
||||
uint8_t* createStreamSegmentPush(uint8_t mode, uint32_t regId, uint64_t clockCounter, uint8_t* payload, size_t payloadLen, size_t* lenOut);
|
||||
|
||||
#endif //ifndef __PROTO_MSG_H__
|
||||
30
inc/proto/pack.h
Normal file
30
inc/proto/pack.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef __PROTO_PACK_H__
|
||||
#define __PROTO_PACK_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
uint64_t decodeBytesToU64(const uint8_t* bytes);
|
||||
uint32_t decodeBytesToU32(const uint8_t* bytes);
|
||||
uint16_t decodeBytesToU16(const uint8_t* bytes);
|
||||
|
||||
|
||||
|
||||
void encodeUint8ToBytes(uint8_t num, uint8_t* tgt);
|
||||
void encodeUint16ToBytes(uint16_t num, uint8_t* tgt);
|
||||
void encodeUint32ToBytes(uint32_t num, uint8_t* tgt);
|
||||
void encodeUint64ToBytes(uint64_t num, uint8_t* tgt);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define encodeUintToBytes(num, tgt) _Generic((num), \
|
||||
uint8_t: encodeUint8ToBytes, \
|
||||
uint16_t: encodeUint16ToBytes, \
|
||||
uint32_t: encodeUint32ToBytes, \
|
||||
uint64_t: encodeUint64ToBytes \
|
||||
)(num, tgt)
|
||||
|
||||
#endif //ifndef __PROTO_PACK_H__
|
||||
13
inc/sized_ptr.h
Normal file
13
inc/sized_ptr.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef __SIZED_PTR_H__
|
||||
#define __SIZED_PTR_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
void* ptr;
|
||||
size_t size;
|
||||
size_t allocatedSize;
|
||||
} SizedPtr;
|
||||
|
||||
|
||||
#endif //ifndef __SIZED_PTR_H__
|
||||
20
inc/state.h
Normal file
20
inc/state.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef __EMUL_STATE_H__
|
||||
#define __EMUL_STATE_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define EMUL_STATE_STILL 0
|
||||
#define EMUL_STATE_EXEC 1
|
||||
#define EMUL_STATE_PAUSE 2
|
||||
#define EMUL_STATE_STOP 3
|
||||
|
||||
#define EMUL_STATE_OP_START 1
|
||||
#define EMUL_STATE_OP_PAUSE 2
|
||||
#define EMUL_STATE_OP_RESUME 3
|
||||
#define EMUL_STATE_OP_RESET 4
|
||||
#define EMUL_STATE_OP_STOP 5
|
||||
|
||||
|
||||
uint8_t switchNewEmulState(const uint8_t currentState, const uint8_t controlOp);
|
||||
|
||||
#endif //ifndef __EMUL_STATE_H__
|
||||
27
inc/streamed.h
Normal file
27
inc/streamed.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef __STREAMED_H__
|
||||
#define __STREAMED_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include "client.h"
|
||||
|
||||
#define STREAM_MODE_READ 0
|
||||
#define STREAM_MODE_WRITE 1
|
||||
|
||||
|
||||
typedef struct {
|
||||
ClientContext* clientContext;
|
||||
uint32_t regId;
|
||||
uint64_t startAddr;
|
||||
uint64_t segLen;
|
||||
uint8_t mode;
|
||||
} StreamReg;
|
||||
|
||||
typedef struct {
|
||||
StreamReg* regs;
|
||||
size_t regCount;
|
||||
size_t allocatedSize;
|
||||
} DeviceSegStreamReg;
|
||||
|
||||
|
||||
#endif //ifndef __STREAMED_H__
|
||||
Reference in New Issue
Block a user