Files
CPP_Core/Core/transmit_protocol/global.h
T
2026-06-16 10:56:40 +08:00

119 lines
3.3 KiB
C++

#pragma once
#include "../Base/global_include.h"
#include "core/main.h"
#include "core/statistics.h"
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
typedef struct KCPCB KCPCB;
IUINT16 xcrc16 (void *buf, IUINT16 len);
IUINT32 xcrc32 (void *buf, IUINT32 len);
IUINT16 rand_range_16(IUINT16 min, IUINT16 max);
int check_cycle_0_9(const char *str, int len);
// 这是一个协议串联结构,把每一个结构可以灵活组合实现各种特性
// 串联的协议都是点对点的协议,从而实现协议的灵活组合
typedef struct Proto_Tree_Node Proto_Tree_Node;
typedef struct Cache_List_Node Cache_List_Node;
typedef struct Cache_List Cache_List;
typedef struct Proto_Tree Proto_Tree;
struct Base_CB {
using Next_handle = std::function<void(Base_CB* bcb, Buf_Type buf, Len_Type len, void* user)>;
Next_handle next_send{};
Next_handle next_recv{};
Cache_List_Node* pn{};
virtual void send(Buf_Type buf, Len_Type len, void* user) = 0;
virtual void recv(Buf_Type buf, Len_Type len, void* user) = 0;
virtual void print_state_info(){};
virtual ~Base_CB() = default;
virtual void final_init(){};
virtual void update() {};
};
void test_proto_chain();
typedef IUINT32 (*Get_Millisecond)();
// tree 型协议
// typedef struct Speed_Statistics Speed_Statistics;
// typedef struct Value_Statistics Value_Statistics;
// typedef struct Probability_Statistics Probability_Statistics;
typedef struct {
Proto_Tree_Node *head, *tail;
} Node_List;
struct Proto_Tree_Node {
using Handle = std::function<void(Proto_Tree_Node* node, Buf_Type buf, Len_Type len)>;
//using Handle = void(*)(Proto_Tree_Node* node, Buf_Type buf, Len_Type len);
Base_CB* cb{};
// 结构数据
Proto_Tree_Node *prev{}, *next{};
Proto_Tree_Node* parent{};
Node_List children{};
Proto_Tree* tr{};
// 该节点 极其子节点组成整体协议的勾子函数
Handle pack_send{};
Handle pack_recv{};
Handle true_recv{};
Handle true_send{};
IUINT8 deep{};
// 可以存放统计信息节点 供测试节点打印
// const char* object_name; // 节点对象名称
explicit Proto_Tree_Node(Base_CB* cb);
~Proto_Tree_Node();
};
struct Cache_List_Node {
Cache_List_Node *next, *prev;
Proto_Tree_Node* tn;
};
struct Cache_List {
Cache_List_Node *head, *tail;
};
struct Proto_Tree {
//using Handle = void(*)(Proto_Tree* tr, Buf_Type buf, Len_Type len);
using Handle = std::function<void(Proto_Tree* tr, Buf_Type buf, Len_Type len, void* user)>;
Cache_List cache_list{};
Node_List children{};
void* user{};
Handle pack_send{};
Handle pack_recv{};
Handle true_recv{};
Handle true_send{};
Proto_Tree();
~Proto_Tree();
void send( Buf_Type buf, Len_Type len);
void recv( Buf_Type buf, Len_Type len);
void print_state_info();
void create();
Proto_Tree_Node* append(Proto_Tree_Node* par, Base_CB* cb);
Proto_Tree_Node* prepend(Proto_Tree_Node* par, Base_CB* cb);
};
// true pack
// send 【真实数据】 顺着链表,不断增加 数据 【包头数据】 finial_send
// recv 【包头数据】 顺着链表,不断减少 数据 【真实数据】 finial_recv
void print_hex(const char* prefix, const uint8_t* data, int len, const char* suffix);