Продолжаем

This commit is contained in:
Shr3dd3r 2023-10-28 04:54:14 +03:00
parent 09aff0e709
commit e8d9782ebc
7 changed files with 73 additions and 44 deletions

3
.gitignore vendored
View File

@ -3,4 +3,5 @@ src/test.cpp
libstadium.so
libstadiumtest_release
libstadiumtest_sanitizer
libstadiumtest_fortified
libstadiumtest_fortified
pseudo_code.md

View File

@ -1,7 +1,7 @@
# libstadium makefile
CC = g++
CFLAGS_DEFAULT = -march=native -std=c++23
CFLAGS_DEFAULT = -march=native -std=c++23 -fno-rtti
CFLAGS_DEBUG = -ggdb -Wall -Werror -Wno-reorder -Wno-uninitialized -fanalyzer -Wno-analyzer-use-of-uninitialized-value
#CFLAGS_DEBUG_S = -O0 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow
CFLAGS_DEBUG_F = -O1 -D_FORTIFY_SOURCE=3

View File

@ -17,11 +17,6 @@ Event::Event () {
this->Type.Category = this->Type.Subcategory = this->AsyncID = 0;
}
// Ctor with parsing of raw char array
Event::Event (const char* data, size_t data_sz) {
// TODO
}
// Ctor with positional arguments
Event::Event (EventType type, KLDRArray<> data, uint16_t id = 0) : Type(std::move(type)), Payload(std::move(data)), AsyncID(id) {
// TODO: calculate hash
@ -34,9 +29,22 @@ Event::Event (uint8_t cat, uint8_t subcat, KLDRArray<> data, uint16_t id = 0) :
// TODO: calculate hash
}
// Parse raw data and fill fields from it
void
Event::MakeFrom (const char *data, size_t data_sz) { // TODO: supply hash type/size (???)
if (data_sz < MIN_RAW_EVENT_SIZE)
throw std::invalid_argument("raw event size cant be less than 21 bytes");
this->Type.Category = data[0];
this->Type.Subcategory = data[1];
this->AsyncID = *reinterpret_cast<const uint16_t*>(&data[2]);
// TODO: parse hash
// TODO: parse payload to KLDRArray
}
// Write to stdout info about event
void
Event::Print () {
Event::Print () const {
printf(
"[Event] Object at: %lu; Category: 0x%.2hhx; Subcategory: 0x%.2hhx; AsyncID: %hu; Payload size: cells: %lu, bytes:%lu\n", // TODO: add hash
reinterpret_cast<size_t>(this),

View File

@ -11,10 +11,16 @@
#include <cstdio>
#include <vector>
#include <string>
#include <stdexcept>
#include "KLDR.hpp"
#define MIN_RAW_EVENT_SIZE 21
namespace Stadium::v1 {
@ -28,31 +34,33 @@ struct EventType {
uint8_t Subcategory;
};
// Get stringized name of event
std::string ResolveEventType (EventType);
// Event class
class Event {
public:
uint32_t Lifetime = 0; // Lifetime of event in seconds
private:
// These are the "physical" members that will be packed to binary
std::vector<char> Hash;
// CryptoAlgo HashAlgo; // TODO
KLDRArray<> Payload;
class Event final {
uint32_t Lifetime = 0; // Lifetime of event in seconds
// CryptoAlgo HashAlgo; // TODO
// std::vector<char> Hash;
KLDRArray<> Payload;
EventType Type;
EventAsyncID AsyncID;
public:
EventType Type;
EventAsyncID AsyncID;
Event ();
Event (const char*, size_t);
Event ();//CryptoAlgo);
Event (EventType, KLDRArray<>, uint16_t);
Event (uint8_t, uint8_t, KLDRArray<>, uint16_t);
void Print ();
void MakeFrom (const char *, size_t);
void Print () const;
};
}
} // namespace Stadium::v1

View File

@ -12,6 +12,7 @@
#include <cstdio>
#include <iomanip>
#include <vector>
#include <string>
#include <random>
#include <exception>
@ -21,7 +22,7 @@ namespace Stadium::v1 {
enum KLDRDefaultKeys : uint8_t {
enum KLDRDefaultKeys : uint8_t { // TODO: think about better way (maybe std::map<string,uint8_t> or smt like that)
Data = 0x01, // Main data of event
ObjectID = 0x02, // ID of object in local context
SrcEventAuthor = 0x03, // Source author of event
@ -30,7 +31,7 @@ enum KLDRDefaultKeys : uint8_t {
BatchNumber = 0x06, // Number of event in chain of events
Path = 0x07, // Path to requested/uploaded resource
Power = 0x08, // Access right to object
ServerSession = 0x09, // ID of server-side session
//ServerSession = 0x09, // ID of server-side session
// Crypto-related
CryptoAlgos = 0x11, // Array of used cryptography algos
CryptoKeyID = 0x12, // ID of crypto key used for data encryption in this event
@ -39,6 +40,11 @@ enum KLDRDefaultKeys : uint8_t {
// Get stringized name of KLDR key
std::string ResolveKLDRKey (KLDRDefaultKeys); // TODO: this is can be better
/*
* KLDR stands for "Key-Length-Data-Repeat".
* Schematically, binary array with KLDR-formatted data looks like this.
@ -52,7 +58,7 @@ enum KLDRDefaultKeys : uint8_t {
* This can be more optimized for performance with cost of more memory usage.
*/
template <typename KeyT = uint8_t, typename LengthT = uint16_t>
class KLDRArray {
class KLDRArray final {
private:
std::vector<KeyT> Keys;
std::vector<LengthT> Lengths;
@ -178,9 +184,9 @@ class KLDRArray {
case v1::KLDRDefaultKeys::Power:
printf("Power");
break;
case v1::KLDRDefaultKeys::ServerSession:
printf("ServerSession");
break;
// case v1::KLDRDefaultKeys::ServerSession:
// printf("ServerSession");
// break;
case v1::KLDRDefaultKeys::CryptoAlgos:
printf("CryptoAlgos");
break;

View File

@ -12,20 +12,13 @@ namespace Stadium::v1 {
// Create event from raw bytes (ATTENTION: maybe move this method to Event class)
Event
Worker::ParseRawData (const char* data) {
// TODO
return Event{};
}
// Parse ingoing packet data to event, check its properties and put it to queue if all okay
void
Worker::PutIngoing (const char* data) {
// TODO
}
// Send outgoing event (NOTICE: maybe rework)
// Send outgoing event
void
Worker::PutOutgoing (EventType type, KLDRArray<> data) {
// TODO
@ -33,7 +26,13 @@ Worker::PutOutgoing (EventType type, KLDRArray<> data) {
// Bind handler for ingoing events
void
Worker::BindHandler (EventType, void()) {
Worker::BindHandler (EventType event_type, EventHandler handle) {
this->Handlers[event_type] = handle;
}
// Expect ingoing event by its ID
Event
Worker::ExpectEvent (EventAsyncID async_id) {
// TODO
}
@ -46,7 +45,7 @@ Worker::IngoingLoop () {
// Get single outgoing event from queue and form it to binary
std::vector<char>
Worker::GetOutgoingDataVec () {
return std::vector<char>{}; // TODO: remove
// TODO
}

View File

@ -12,6 +12,7 @@
#include <atomic>
#include <queue>
#include <unordered_map>
#include <functional>
#include "KLDR.hpp"
#include "Event.hpp"
@ -22,12 +23,17 @@ namespace Stadium::v1 {
// Event handler type
typedef std::function<void()> EventHandler;
// Working class
class Worker {
class Worker final {
private:
static bool Running; // If workers are running
static std::unordered_map<EventType, void()> Handlers; // Map of handlers associated with packet types TODO: may be it can be more optimized with reloaded operators
static std::unordered_map<EventType, EventHandler> Handlers; // Map of handlers associated with packet types
static std::unordered_map<EventAsyncID, Event> ExpectedEvents; // Map of expected events and their async IDs
static std::atomic<EventAsyncID> LastAsyncID; // Increments every use of Expect()
@ -35,11 +41,12 @@ class Worker {
static std::queue<Event> IngoingEvents; // FIFO of ingoing events
public:
Event ParseRawData (const char*);
void PutIngoing (const char*);
void PutOutgoing (EventType, KLDRArray<>);
void BindHandler (EventType, void());
void BindHandler (EventType, EventHandler);
Event ExpectEvent (EventAsyncID);
void IngoingLoop ();
void OutgoingLoop ();
std::vector<char> GetOutgoingDataVec ();
};