Impl basic ws binary proto with data and execution state manipulation

This commit is contained in:
2025-10-10 03:19:45 +03:00
parent 2ff6b1a454
commit 165a96aa3c
44 changed files with 2065 additions and 2269 deletions

View File

@@ -1,21 +0,0 @@
#ifndef __BASE_DEVICE_H__
#define __BASE_DEVICE_H__
#include "hmmmm.h"
typedef struct {
void* specs;
device_lib_t* lib;
device_public_context_t* ctx;
uint64_t clockCycleLimit;
uint64_t clockCycleCounter;
} device_handle_t;
device_handle_t* openBaseDeviceFromConfig(const char* configPath, char* errbuf);
device_handle_t* openBaseDevice(conf_dev_t* devConf, char* errbuf);
conf_dev_t* openBaseDeviceConfig(const char* configPath, char* errbuf);
void closeBaseDevice(device_handle_t* devHandle);
#endif // ifndef __BASE_DEVICE_H__

26
inc/client.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef __CLIENT_H__
#define __CLIENT_H__
#include <stdint.h>
#include <stdlib.h>
#include "sized_ptr.h"
#include "wsServer/include/ws.h"
#include "ptQueue/inc/ptQueue.h"
typedef struct {
ws_cli_conn_t clientId;
uint64_t seatId;
uint8_t isAuthed;
ptQueue* incomeQ;
// ptQueue* outcomeQ;
uint64_t connectedAt;
uint32_t streamRegIterator;
uint64_t orphanedAt;
uint64_t orphanedDeadTimeout;
SizedPtr* fallbackOutcomeQ;
size_t fallbackOutcomeQPadding;
} ClientContext;
#endif //ifndef __CLIENT_H__

View File

@@ -1,41 +0,0 @@
#ifndef __COMPOSE_DEVICE_H__
#define __COMPOSE_DEVICE_H__
#include "base_device.h"
typedef struct {
uint64_t limiter;
} clock_conf_t;
typedef struct {
char** baseAt;
char* baseSeg;
char** target;
uint64_t projectionShift;
} projection_conf_t;
typedef struct {
char** baseAt;
char** pointAt;
uint64_t addr;
char* seg;
} intercept_conf_t;
typedef struct {
clock_conf_t clockConf;
conf_dev_t** baseConfigs;
projection_conf_t** projections;
intercept_conf_t** intercepts;
} compose_dev_conf_t;
typedef struct {
device_handle_t** devHandlers;
} compose_dev_handle_t;
compose_dev_conf_t* openComposeDeviceConfig(const char* configPath, char* errbuf);
device_handle_t** openComposeDevice(compose_dev_conf_t* conf, char* errbuf);
#endif // ifndef __COMPOSE_DEVICE_H__

View File

@@ -1,36 +0,0 @@
#ifndef __HMMMM_CONFIG_H__
#define __HMMMM_CONFIG_H__
#include <unistd.h>
#include <stdint.h>
typedef struct {
char* name;
size_t start;
size_t len;
uint8_t wordLen;
uint8_t isExecutable;
} conf_mem_seg_t;
typedef struct {
conf_mem_seg_t** memSegConfs;
} conf_mem_t;
typedef struct {
char** id;
char** clockId;
uint64_t clockDivider;
uint64_t clockMultipler;
conf_mem_t* memConf;
char* libPath;
} conf_dev_t;
void freeMemSegConf(conf_mem_seg_t* memSegConf);
void freeMemConf(conf_mem_t* memConf);
void freeConf(conf_dev_t* conf);
void freeComposeId(char** id);
uint8_t compareComposeId(char** idA, char** idB);
#endif // ifndef __HMMMM_CONFIG_H__

46
inc/context.h Normal file
View File

@@ -0,0 +1,46 @@
#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;
_Atomic (uint64_t) seatCounter;
} ServerContext;
#endif //ifndef __CONTEXT_H__

13
inc/events.h Normal file
View 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__

View File

@@ -1,33 +0,0 @@
#ifndef __HMMMMM__
#define __HMMMMM__
#include "libhmmmm.h"
#include "config.h"
typedef struct {
void* (*extractPcounterPtr)(device_public_context_t* devContext);
size_t (*extractPcounter)(device_public_context_t* devContext);
size_t (*extractOpcode)(device_mem_t* devMem, size_t _programCounter);
uint8_t (*extractPcounterSizeWords)();
} instruction_simul_handlers_t;
typedef struct {
uint8_t devType;
device_public_context_t* (*init)(void*, char* errbuf);
uint8_t (*makeDeviceTick)(device_public_context_t* devInfo);
void* extendedHandlers;
device_public_context_t* devContext;
void* _dlhandl;
void* (*parseSpecsFromConfig)(const conf_dev_t* devConf, char* errbuf);
void (*freeSpecs)(void* specs);
void (*freeDevMem)(device_mem_t* mem);
void (*fillSmartReadSpecs)(void* specs, smart_read_spec_t* smartReadSpecs, uint64_t smartReadSpecsCount);
void (*fillSmartWriteSpecs)(void* specs, smart_write_spec_t* smartWriteSpecs, uint64_t smartWriteSpecsCount);
} device_lib_t;
device_lib_t* loadDeviceLib(const char *libpath, char* errbuf);
#endif // ifndef __HMMMMM__

View File

@@ -1,37 +0,0 @@
#ifndef __LIBDEVICE_H__
#define __LIBDEVICE_H__
#include <stdint.h>
#include "libmem.h"
#define SMART_ADDR_TYPE_GLOBAL 1
#define SMART_ADDR_TYPE_SEGMENTED 2
typedef struct
{
uint64_t addr;
uint8_t segno;
uint64_t localAddr;
uint8_t addrType;
mem_h_read_func* handler;
uint64_t ident;
} smart_read_spec_t;
typedef struct
{
uint64_t addr;
uint8_t segno;
uint64_t localAddr;
uint8_t addrType;
mem_h_write_func handler;
uint64_t ident;
} smart_write_spec_t;
typedef struct {
device_mem_t* deviceMem;
void* deviceInfo;
} device_public_context_t;
#endif // ifndef __LIBDEVICE_H__

View File

@@ -1,14 +0,0 @@
#ifndef __LIB_HMMMMM__
#define __LIB_HMMMMM__
#include "libmem.h"
#include "libdevice.h"
#define EXTENDED_DEVICE_TYPE_DUMMY 0
#define EXTENDED_DEVICE_TYPE_INSTR_SIMUL 1
#endif // ifndef __LIB_HMMMMM__

View File

@@ -1,52 +0,0 @@
#ifndef __LIBMEM_H__
#define __LIBMEM_H__
#include <stdint.h>
#include <stdlib.h>
#define GET_BIT(n, b) ((n >> b) & 1)
// Internal mem handlers
typedef void* (*mem_h_read_func)(uint64_t ident, uint64_t addr, void* rawCells);
typedef void (*mem_h_write_func)(uint64_t ident, uint64_t addr, void* rawCells, void* data);
typedef struct {
mem_h_read_func func;
uint64_t ident;
} mem_h_read_handler;
typedef struct {
mem_h_write_func func;
uint64_t ident;
} mem_h_write_handler;
typedef struct
{
void* rawCells;
void** cells;
uint64_t smartAddrReadMask;
uint64_t smartAddrWriteMask;
mem_h_read_handler* smartAddrReadHandlers;
mem_h_write_handler* smartAddrWriteHandlers;
char** memsegNames;
uint64_t* memsegShifts;
uint64_t* memreadCellAddrs;
uint64_t* memwriteCellAddrs;
uint8_t memreadLen;
uint8_t memwriteLen;
} device_mem_t;
typedef struct
{
uint64_t start;
uint64_t len;
uint8_t wordLen;
char* name;
} memseg_spec_t;
// External handlers
typedef void* (*ext_h_read_func)(uint64_t addr, void* rawCells, void* devContext);
typedef void (*ext_h_write_func)(uint64_t addr, void* rawCells, void* data, void* devContext);
#endif // ifndef __LIBMEM_H__

13
inc/linkedlist.h Normal file
View 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
View 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
View 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, ClientContext* client, uint8_t* msg, size_t msgLen);
#endif

37
inc/proto/enums.h Normal file
View File

@@ -0,0 +1,37 @@
#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 CTRL_TYPE_LIST_ORPHANED 0b1001
#define CTRL_TYPE_LOAD_FAILED 0b1010
#define CTRL_TYPE_SETUP_CONNECTION 0b1011
#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
View 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
View 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__

View 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, ClientContext* ctx, EmulContext* emulContext);
#endif //ifndef __PROTO_HANDLERS_CONTROL_H__

10
inc/proto/handlers/mem.h Normal file
View 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__

View 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
View 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__

29
inc/proto/msg.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef __PROTO_MSG_H__
#define __PROTO_MSG_H__
#include "wsServer/include/ws.h"
#include <stdlib.h>
#include "client.h"
typedef struct {
ClientContext* client;
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);
void fillHead(uint64_t nonce, uint8_t packetType, uint8_t payloadHeader, uint8_t* outmsg);
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);
uint8_t* createClientSetup(uint64_t nonce, ClientContext* ctx, size_t* lenOut);
#endif //ifndef __PROTO_MSG_H__

30
inc/proto/pack.h Normal file
View 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
View 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
View 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
View 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__