189 lines
3.7 KiB
C
189 lines
3.7 KiB
C
#include "IPC_msg.h"
|
|
#include <string.h>
|
|
#include "../debug.h"
|
|
#include <sys/ksys.h>
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
static int syscalls_indexIPC_msg(lua_State* L)
|
|
{
|
|
ksys_ipc_msg* r = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name);
|
|
const char* index = luaL_checkstring(L, 2);
|
|
|
|
if (strcmp(index, "size") == 0)
|
|
{
|
|
lua_pushinteger(L, r->datalen);
|
|
}
|
|
else if (strcmp(index, "pid") == 0)
|
|
{
|
|
lua_pushboolean(L, r->pid);
|
|
}
|
|
else
|
|
{
|
|
lua_pushnil(L);
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
static int syscalls_newindexIPC_msg(lua_State* L)
|
|
{
|
|
ksys_ipc_msg* r = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name);
|
|
lua_Integer index_i = lua_isnumber(L, 2);
|
|
uint32_t val = luaL_checkinteger(L, 3);
|
|
|
|
if (!index_i)
|
|
{
|
|
const char* index = luaL_checkstring(L, 2);
|
|
|
|
if (strcmp(index, "size") == 0)
|
|
{
|
|
r->datalen = val;
|
|
}
|
|
else if (strcmp(index, "pid") == 0)
|
|
{
|
|
r->pid = val;
|
|
}
|
|
else
|
|
{
|
|
luaL_error(L, "wrong index: %s", index);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (index_i < r->data)
|
|
{
|
|
((lua_Integer*)r + (2 * sizeof(unsigned)))[index_i] = val;
|
|
}
|
|
else
|
|
{
|
|
luaL_error(L, "out of range. size: %d index: %d", r->datalen, index_i);
|
|
}
|
|
}
|
|
|
|
lua_pushboolean(L, true);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static bool compare_ipc_msg(ksys_ipc_msg* a, ksys_ipc_msg* b)
|
|
{
|
|
if (a->datalen == b->datalen)
|
|
{
|
|
return memcmp(
|
|
a + (2 * sizeof(unsigned)),
|
|
b + (2 * sizeof(unsigned)),
|
|
a->datalen
|
|
);
|
|
}
|
|
else // длина сообщений не совпадает, занчитт они зразу не могут быть равны
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static int syscalls_eqIPC_msg(lua_State* L)
|
|
{
|
|
lua_pushboolean(
|
|
L,
|
|
compare_ipc_msg(
|
|
luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name),
|
|
luaL_checkudata(L, 2, syscalls_IPC_msg_metatable_name)
|
|
)
|
|
);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static int syscalls_IPC_msg_ReadByte(lua_State* L)
|
|
{
|
|
lua_pushinteger(
|
|
L,
|
|
((uint8_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)]
|
|
);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static int syscalls_IPC_msg_ReadWord(lua_State* L)
|
|
{
|
|
lua_pushinteger(
|
|
L,
|
|
((uint16_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)]
|
|
);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static int syscalls_IPC_msg_ReadDword(lua_State* L)
|
|
{
|
|
lua_pushinteger(
|
|
L,
|
|
((uint32_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)]
|
|
);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static int syscalls_IPC_msg_ReadInteger(lua_State* L)
|
|
{
|
|
lua_pushinteger(
|
|
L,
|
|
((lua_Integer*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)]
|
|
);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static const luaL_Reg syscalls_IPC_msg_m[] = {
|
|
{"__index", syscalls_indexIPC_msg},
|
|
{"__newindex", syscalls_newindexIPC_msg},
|
|
{"__eq", syscalls_eqIPC_msg},
|
|
{"ReadByte", syscalls_IPC_msg_ReadByte},
|
|
{"ReadWord", syscalls_IPC_msg_ReadWord},
|
|
{"ReadDword", syscalls_IPC_msg_ReadDword},
|
|
{"ReadInteger", syscalls_IPC_msg_ReadInteger},
|
|
{NULL, NULL}
|
|
};
|
|
|
|
ksys_ipc_msg* syscalls_pushIPC_msg(lua_State* L, size_t dataLen)
|
|
{
|
|
DEBUG_LINE("push IPC_msg entry");
|
|
|
|
ksys_ipc_msg* entry = lua_newuserdata(L, sizeof(ksys_ipc_msg) + dataLen);
|
|
|
|
luaL_setmetatable(L, syscalls_IPC_msg_metatable_name);
|
|
|
|
return entry;
|
|
}
|
|
|
|
static int syscalls_newIPC_msg(lua_State* L)
|
|
{
|
|
size_t dataLen = luaL_checkinteger(L, 1);
|
|
ksys_ipc_msg* r = syscalls_pushIPC_msg(L, dataLen);
|
|
r->datalen = dataLen;
|
|
|
|
return 1;
|
|
}
|
|
|
|
static const luaL_Reg syscalls_IPC_msg_lib[] = {
|
|
{"new", syscalls_newIPC_msg},
|
|
{NULL, NULL}
|
|
};
|
|
|
|
void syscalls_register_IPC_msg(lua_State* L)
|
|
{
|
|
DEBUG_LINE("register IPC_msg entry");
|
|
|
|
luaL_newlib(L, syscalls_IPC_msg_lib);
|
|
|
|
lua_setfield(L, -2, syscalls_IPC_msg_name);
|
|
|
|
|
|
luaL_newmetatable(L, syscalls_IPC_msg_metatable_name);
|
|
luaL_setfuncs(L, syscalls_IPC_msg_m, 0);
|
|
|
|
lua_pop(L, 1);
|
|
}
|