opcode logic change

master
wipedlife 2021-06-29 17:33:06 +03:00
parent 168d9c4be4
commit 2a8a8c4f2f
4 changed files with 81 additions and 68 deletions

View File

@ -9,6 +9,7 @@ AM_INIT_AUTOMAKE
AC_PROG_RANLIB([RANLIB])
# Checks for programs.
AC_PROG_CC
AC_PROG_CXX
# Checks for libraries.
# FIXME: Replace `main' with a function in `-lpthread':

View File

@ -1,6 +1,6 @@
AM_CFLAGS =
bin_PROGRAMS = bbootstrap-server
bbootstrap_server_SOURCES = main.c
bbootstrap_server_SOURCES = main.cpp
bbootstrap_server_DEPENDENCIES = $(top_builddir)/libbacteria/libbacteria.a
bbootstrap_server_LDADD = $(top_builddir)/libbacteria/libbacteria.a -lluajit-5.1
bbootstrap_server_CFLAGS= -g

View File

@ -1,67 +0,0 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZEOFOPCODE 5
#define OPCODEDEBUG
#define INITOPCODE(a,b,c,d,rfun)\
{.o[0]=a, .o[1]=b, .o[2]=c, .o[3]=d, .o[4]=0x0, .fun=rfun}
#define OPCODEEQUAL(op,n,d,i) op[n].o[i] == d[i]
#define FOPCODEEQUAL(op,n,d) OPCODEEQUAL(op,i,data,0) \
&& OPCODEEQUAL(op,i,d,1) \
&& OPCODEEQUAL(op,i,d,2) \
&& OPCODEEQUAL(op,i,d,3)
typedef void(*eventfun)(const char params[], ...);
typedef struct{
char o[SIZEOFOPCODE];
eventfun fun;
}opcode;
extern opcode create_opcode(unsigned char a, unsigned char b, unsigned char c, unsigned char d, eventfun fun);
extern void event0(const char params[], ...);
static const opcode opcodes[]={
//keys opcodes and initilization
INITOPCODE(0x01,0x00,0x0,0x00, NULL), // Initilization of connect. RSA+ed25519+x25519 exchange. add to network
INITOPCODE(0x01,0x00,0x0,0x01, NULL), // update server keys (old keys + new verified keys)
// info opcodes
INITOPCODE(0x03,0x00,0x0,0x00, NULL), // Register server ( keys )
INITOPCODE(0x03,0x00,0x0,0x01, NULL), // Update server info (online/offline/users count/cryptocoins/exchanges/etc)
//list opcodes
INITOPCODE(0x04,0x00,0x0,0x00, NULL), // get list of servers
INITOPCODE(0x04,0x00,0x0,0x01, NULL), // get list of users
INITOPCODE(0x04,0x00,0x0,0x02, NULL), // get list of bootstrap servers
{"",NULL}
};
void event0(const char params[], ...){
puts("event0");
}
void run_opcode(const char data[]){
for(unsigned int i = 0;i< sizeof(opcodes);i++){
if(opcodes[i].fun == NULL) break;
if( FOPCODEEQUAL(opcodes,i,data) ){
printf("Found Opcode. start func\n");
opcodes[i].fun("");
return;
}
}
puts("opcode not found");
}
//#define INITDATAOPCODE(n, __VA_ARGS__)
int
main(void)
{
char data[4] = {0x01, 0x00, 0x0, 0x01};
char data1[4] = {0x01, 0x00, 0x0, 0x00};
run_opcode(data);
run_opcode(data1);
}

View File

@ -0,0 +1,79 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<array>
using eventfun = void(*)(const char params[], ...);
constexpr short opcode_size = 4;
using opcode_data = std::array<char, opcode_size>;
typedef struct{
opcode_data data;
eventfun fun;
}opcode;
extern opcode create_opcode(unsigned char a, unsigned char b, unsigned char c, unsigned char d, eventfun fun);
extern void event0(const char params[], ...);
extern void event1(const char params[],...);
static const opcode opcodes[]={
{ {0x01,0x00,0x00,0x00}, event0 },
{ {0x04,0x06,0x00,0x07}, event1 },
{"",NULL}
};
void event1(const char params[],...){
puts("event1");
}
void event0(const char params[], ...){
puts("event0");
}
template <typename T, typename T1, size_t sarr>
bool equal_array(std::array<T,sarr> arr, T1 data[]){
for(unsigned short i = 0; i < sarr; i++){
if(arr[i] != data[i]) return false;
}
return true;
}
template <typename T, typename T1, size_t sarr, size_t sdata>
bool equal_array(std::array<T,sarr> arr, std::array<T,sdata> data){
if(sdata != sarr) return false;
equal_array( arr, data.to_array() );
}
void run_opcode(const char data[opcode_size]){
for(auto op : opcodes){
if(op.fun == NULL) break;
if( equal_array(op.data, data) ){
op.fun("");
return;
}
}
puts("opcode not found");
}
void run_opcode(opcode_data data){
char d[opcode_size];
for(unsigned short el =0; el < data.size(); el++)
d[el] = data[el];
run_opcode( d );
}
//#define INITDATAOPCODE(n, __VA_ARGS__)
int
main(void)
{
char data[4] = {0x01, 0x00, 0x0, 0x01};
opcode_data data1 = {0x01,0x00,0x00,0x00};
run_opcode(data);
run_opcode(data1);
run_opcode(opcode_data{0x04,0x06,0x00,0x07});
}