Lua serv event
parent
5092405373
commit
102e876b6a
|
@ -1,5 +1,9 @@
|
|||
[Anjuta]
|
||||
Geometry=976x744+10+10
|
||||
Maximized=1
|
||||
Geometry=1280x979+0+0
|
||||
|
||||
[File Loader]
|
||||
Files=../../src/main.c#51%%%../../src/bootstrap/main.cpp#84%%%../../src/bootstrap/serv.cpp#39%%%../../src/bootstrap/serv.h#16%%%../../src/bootstrap/opcodes.h#1%%%../../src/bootstrap/events.h#6%%%../../src/async_serv.c#1%%%../../src/async_serv.h#8
|
||||
|
||||
[Document Manager]
|
||||
bookmarks=<?xml version="1.0" encoding="UTF-8"?>\n<bookmarks/>\n
|
||||
|
@ -9,7 +13,10 @@ Run in terminal=2
|
|||
Working directories=../../.
|
||||
|
||||
[Project Manager]
|
||||
Shortcut=bacteria//libbacteria//libbacteria.a%%%bacteria//src//bootstrap//bbootstrap-server%%%bacteria//src//client//bacteria-client%%%bacteria//src//bacteria
|
||||
Expand=bacteria
|
||||
|
||||
[Debugger]
|
||||
Stop at beginning=2
|
||||
|
||||
[Build]
|
||||
Configuration list=1:Default:%%%1:Debug:Debug%%%1:Profiling:Profiling%%%1:Optimized:Optimized
|
||||
|
@ -18,6 +25,3 @@ BuildArgs/Default=--enable-maintainer-mode
|
|||
BuildArgs/Debug=--enable-maintainer-mode 'CFLAGS=-g -O0' 'CXXFLAGS=-g -O0' 'JFLAGS=-g -O0' 'FFLAGS=-g -O0'
|
||||
BuildArgs/Profiling=--enable-maintainer-mode 'CFLAGS=-g -pg' 'CXXFLAGS=-g -pg' 'JFLAGS=-g -pg' 'FFLAGS=-g -pg'
|
||||
BuildArgs/Optimized=--enable-maintainer-mode 'CFLAGS=-O2' 'CXXFLAGS=-O2' 'JFLAGS=-O2' 'FFLAGS=-O2'
|
||||
|
||||
[Debugger]
|
||||
Stop at beginning=2
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<?xml version="1.0"?>
|
||||
<dock-layout>
|
||||
|
||||
<layout name="__default__"><dock name="__dock_1" floating="no" width="-1" height="-1" floatx="0" floaty="0" skip-taskbar="yes"><paned orientation="vertical" resize="yes" locked="no" iconified="no" closed="no" position="326"><paned orientation="horizontal" resize="yes" locked="no" iconified="no" closed="no" position="249"><notebook orientation="vertical" resize="yes" locked="no" iconified="no" closed="no" page="1"><item name="AnjutaFileManager" orientation="vertical" resize="yes" locked="no" iconified="no" closed="no"/><item name="AnjutaProjectManager" orientation="vertical" resize="yes" locked="no" iconified="no" closed="no"/><item name="AnjutaSymbolBrowser" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaSymbolDB" orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDevhelpIndex" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaGladeTree" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaGladePalette" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerRegisters" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/></notebook><paned orientation="horizontal" resize="yes" locked="no" iconified="no" closed="no" position="500"><paned orientation="vertical" resize="yes" locked="no" iconified="no" closed="no" position="200"><notebook orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes" page="-1"/><notebook orientation="vertical" resize="yes" locked="no" iconified="no" closed="no" page="1"><item name="AnjutaStarter" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDocumentManager" orientation="vertical" resize="yes" locked="no" iconified="no" closed="no"/><item name="AnjutaDevhelpDisplay" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaTodoPlugin" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaClassInheritance" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaSamplePlugin" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaGladeEditor" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerMemory" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaSubversionLogViewer" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/></notebook></paned><notebook orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes" page="-1"/></paned></paned><notebook orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes" page="-1"><item name="AnjutaTerminal" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaMessageView" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerStack" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerLocals" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerWatch" orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerBreakpoints" orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes"/></notebook></paned></dock></layout></dock-layout>
|
||||
<layout name="__default__"><dock name="__dock_1" floating="no" width="-1" height="-1" floatx="0" floaty="0" skip-taskbar="yes"><paned orientation="vertical" resize="yes" locked="no" iconified="no" closed="no" position="326"><paned orientation="horizontal" resize="yes" locked="no" iconified="no" closed="no" position="318"><notebook orientation="vertical" resize="yes" locked="no" iconified="no" closed="no" page="1"><item name="AnjutaFileManager" orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaProjectManager" orientation="vertical" resize="yes" locked="no" iconified="no" closed="no"/><item name="AnjutaSymbolBrowser" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaSymbolDB" orientation="vertical" resize="yes" locked="no" iconified="yes" closed="yes"/><item name="AnjutaDevhelpIndex" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaGladeTree" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaGladePalette" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerRegisters" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/></notebook><paned orientation="horizontal" resize="yes" locked="no" iconified="no" closed="no" position="500"><paned orientation="vertical" resize="yes" locked="no" iconified="no" closed="no" position="200"><notebook orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes" page="-1"/><notebook orientation="vertical" resize="yes" locked="no" iconified="no" closed="no" page="1"><item name="AnjutaStarter" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDocumentManager" orientation="vertical" resize="yes" locked="no" iconified="no" closed="no"/><item name="AnjutaDevhelpDisplay" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaTodoPlugin" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaClassInheritance" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaSamplePlugin" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaGladeEditor" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerMemory" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaSubversionLogViewer" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/></notebook></paned><notebook orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes" page="-1"/></paned></paned><notebook orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes" page="-1"><item name="AnjutaTerminal" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaMessageView" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerStack" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerLocals" orientation="horizontal" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerWatch" orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes"/><item name="AnjutaDebuggerBreakpoints" orientation="vertical" resize="yes" locked="no" iconified="no" closed="yes"/></notebook></paned></dock></layout></dock-layout>
|
||||
|
|
Binary file not shown.
|
@ -6,11 +6,9 @@
|
|||
|
||||
namespace events{
|
||||
//is idea to disable lua for opcodes as wrapper if is need.
|
||||
#ifndef DISABLELUA
|
||||
using fun = void(*)(lua_State * L, ...);
|
||||
#else
|
||||
using fun = void(*)(const char params[], ...);
|
||||
#endif
|
||||
#define EVENTDEF lua_State * L, int sock, const char * uIp, uint16_t uPort, char* buf
|
||||
using fun = void(*)(EVENTDEF, ...);
|
||||
|
||||
class event{
|
||||
protected:
|
||||
fun m_fun;
|
||||
|
@ -18,11 +16,10 @@ namespace events{
|
|||
event(fun f): m_fun(f)
|
||||
{}
|
||||
template<typename ...args>
|
||||
#ifndef DISABLELUA
|
||||
void run(lua_State * L,const char params[], args ... values){ m_fun(L,params, values...); };
|
||||
#else
|
||||
void run(const char params[], args ... values){ m_fun(params, values...); };
|
||||
#endif
|
||||
void run(EVENTDEF, args ... values){
|
||||
m_fun(L, sock, (const char*)uIp, uPort, buf, values...);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
|
|
@ -1 +1,6 @@
|
|||
print("bootstrap server inited")
|
||||
--int sock, const char * uIp, uint16_t uPort, char* buf
|
||||
function event1(sock, ip, port, buf)
|
||||
print("Message: ", buf, " from ", ip, " on port: ", port)
|
||||
return "Hewwo?"
|
||||
end
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include"opcodes.h"
|
||||
|
||||
#include<iostream>
|
||||
#include<thread>
|
||||
|
||||
|
@ -84,18 +84,5 @@ servArgs luasarg = {(char*)luahost.c_str(),luaport,L};
|
|||
serv.join();
|
||||
lua_close(L);
|
||||
#endif
|
||||
opcode::opcode_data data1 = {0x01, opcode::ignorebyte, opcode::ignorebyte,
|
||||
0x01};
|
||||
|
||||
opcode::opcode op{{0x01, opcode::ignorebyte, opcode::ignorebyte, 0x01},
|
||||
opcode::event0};
|
||||
opcode::opcode op1{{0x02, opcode::ignorebyte, opcode::ignorebyte, 0x01},
|
||||
NULL};
|
||||
opcode::opcode op2{{0x01, opcode::ignorebyte, opcode::ignorebyte, 0x01},
|
||||
NULL};
|
||||
std::cout << (op == op2) << std::endl;
|
||||
std::cout << (op == data1) << std::endl;
|
||||
op.getEvent().run(NULL, "", NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,11 +1,47 @@
|
|||
// extern void event1(const char params[],...);
|
||||
#include "lua.h"
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include"opcodes.h"
|
||||
|
||||
extern "C" {
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
#include <lauxlib.h>
|
||||
#include"lua/luaserv.h"
|
||||
};
|
||||
|
||||
namespace opcode {
|
||||
|
||||
void event1(lua_State *L, ...) { puts("event1 -"); }
|
||||
void event1(lua_State * L, int sock, const char * uIp, uint16_t uPort, char* buf, ...) {
|
||||
puts("event1 -"); //
|
||||
lua_getglobal(L, "event1");
|
||||
lua_pushnumber(L, sock);
|
||||
lua_pushstring(L, uIp);
|
||||
lua_pushnumber(L, uPort);
|
||||
lua_pushstring(L, (buf+4));
|
||||
|
||||
void event0(lua_State *L, ...) { puts("event0 - "); }
|
||||
if (lua_pcall(L, /*args*/ 4, /*results*/ 1, 0) != 0)
|
||||
luaL_error(L, "error running function `event1': %s",
|
||||
lua_tostring(L, -1));
|
||||
unsigned char retdata[512];
|
||||
if (!lua_isnumber(L, -1)) {
|
||||
printf("(check retval[lua]) Ret val: %s\n", lua_tostring(L, -1));
|
||||
sprintf((char*)retdata,"%s", lua_tostring(L,-1));
|
||||
} else{
|
||||
sprintf((char*)retdata,"%d", lua_tonumber(L,-1));
|
||||
printf("(check retval[lua]) Ret val (num): %d\n", (int)lua_tonumber(L, -1));
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
||||
if (send(sock, retdata, strlen((char*)retdata), MSG_NOSIGNAL) < 0) {
|
||||
perror("Write error");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void event0(lua_State * L, int sock, const char * uIp, uint16_t uPort, char* buf, ...) {
|
||||
puts("event0 - ");
|
||||
}
|
||||
|
||||
} // namespace opcode
|
||||
|
|
|
@ -51,7 +51,7 @@ class opcode{
|
|||
|
||||
|
||||
namespace opcode{
|
||||
void event1(lua_State * L, ...);
|
||||
void event0(lua_State * L, ...);
|
||||
void event1(lua_State * L, int sock, const char * uIp, uint16_t uPort, char* buf, ...);
|
||||
void event0(lua_State * L, int sock, const char * uIp, uint16_t uPort, char* buf, ...);
|
||||
};
|
||||
#endif
|
||||
|
|
|
@ -1,11 +1,18 @@
|
|||
#include "serv.h"
|
||||
|
||||
#include<iostream>
|
||||
#include"opcodes.h"
|
||||
#define doExit(...) \
|
||||
{ \
|
||||
eprintf(__VA_ARGS__); \
|
||||
exit(EXIT_FAILURE); \
|
||||
}
|
||||
|
||||
static opcode::opcode opcodes[] =
|
||||
{
|
||||
{{0x01, opcode::ignorebyte, opcode::ignorebyte, 0x01},
|
||||
opcode::event0},
|
||||
{{'a', 'b', 'c', 'd'},
|
||||
opcode::event1},
|
||||
};
|
||||
constexpr auto serv_status_lua_var = "server_inited";
|
||||
static bool serv_inited;
|
||||
void serv_thread(const char * host, const uint16_t port, lua_State * L) {
|
||||
|
@ -32,7 +39,7 @@ void serv_thread(const char * host, const uint16_t port, lua_State * L) {
|
|||
}
|
||||
if (listen(main_descriptor, MAX_LISTEN) == -1)
|
||||
doExit("Cant start listening\n");
|
||||
|
||||
printf("(serv) %s:%d listening\n", host, port);
|
||||
lua::pushval(L, true);
|
||||
lua_setglobal(L, serv_status_lua_var);
|
||||
lua_pop(L, 1);
|
||||
|
@ -108,17 +115,26 @@ void serv_thread(const char * host, const uint16_t port, lua_State * L) {
|
|||
puts("Stop server?");
|
||||
return;
|
||||
}
|
||||
printf("%s:%d -> %s\n",ip,port,buf);
|
||||
for (int z = sizeof(buf) - 1; z--;) {
|
||||
// printf("buf[%d] = %c\n", z, buf[z]);
|
||||
if (buf[z] != 0)
|
||||
buf[z] ^= rand() % 10;
|
||||
}
|
||||
if (send(events[n].data.fd, buf, sizeof(buf), MSG_NOSIGNAL) < 0) {
|
||||
perror("Write error");
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
opcode::opcode op1{{0x02, opcode::ignorebyte, opcode::ignorebyte, 0x01},
|
||||
NULL};
|
||||
opcode::opcode op2{{0x01, opcode::ignorebyte, opcode::ignorebyte, 0x01},
|
||||
NULL};
|
||||
std::cout << (op == op2) << std::endl;
|
||||
*/
|
||||
opcode::opcode_data data = {buf[0], buf[1], buf[2], buf[3]};
|
||||
for( auto op : opcodes ){
|
||||
if( op == data ){
|
||||
puts("Opcode found");
|
||||
op.getEvent().run(L, events[n].data.fd,
|
||||
ip, port, buf);
|
||||
break;
|
||||
}
|
||||
}//for
|
||||
// do_use_fd(events[n].data.fd);
|
||||
} // else client end
|
||||
} // for (events) end
|
||||
} // for ;; end
|
||||
}//for(?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue