I’ve got something very odd happening. I think I made a mistake in the C code… I am trying to serialize a simple string in protobuffer format, and then pass that into a working deserialization program to determine if the serialization/variables were able to be handled properly in lua.
Lua:
[root@localhost amessage_lua]# cat amessage.lua
#!/usr/bin/lua
require("amsg")
result=dobf("123")
io.write(result)
--result=dobf("123")
--io.write(result)
Shared object code:
[root@localhost amessage_lua]# cat amsg.c
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "amessage.pb-c.h"
#define MAX_MSG_SIZE 1024
static void stackDump (lua_State *L) {
int i;
int top = lua_gettop(L);
for (i = 1; i <= top; i++) { /* repeat for each level */
int t = lua_type(L, i);
switch (t) {
case LUA_TSTRING: /* strings */
printf("LUA_TSTRING INDEX %d `%s'",i, lua_tostring(L, i));
break;
case LUA_TBOOLEAN: /* booleans */
printf("LUATBOOLEAN: %s", lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER: /* numbers */
printf("LUA_TNUMBER: %g", lua_tonumber(L, i));
break;
default: /* other values */
printf("DEFAULT: %s", lua_typename(L, t));
break;
}
printf(" || "); /* put a separator */
}
printf("\n"); /* end the listing */
}
static int dobf (lua_State *L){
AMessage msg = AMESSAGE__INIT; // AMessage
void *buf; // Buffer to store serialized data
unsigned len; // Length of serialized data
msg.a = lua_tonumber(L, -1);
lua_remove(L, 1);
len = amessage__get_packed_size(&msg);
buf = malloc(len);
amessage__pack(&msg,buf);
lua_pushstring(L, buf);//, len);
free(buf); // Free the allocated serialized dobfer
stackDump(L);
return 1;
}
int luaopen_amsg (lua_State *L){
lua_register(L,"dobf",dobf);
return 0;
}
[root@localhost amessage_lua]# ./amessage.lua
LUA_TSTRING INDEX 1 {▒&▒' ||
{▒&▒[root@localhost amessage_lua]# ./amessage.lua
LUA_TSTRING INDEX 1 {▒▒0' ||
{▒▒0[root@localhost amessage_lua]# ./amessage.lua
LUA_TSTRING INDEX 1 {lSb' ||
Removing the stackdump function so i can print to stdout and try to deserialize the data…(and recompiling of course)
gcc -Wall -shared -fPIC -o amsg.so -I/usr/include -llua $(pkg-config --cflags 'libprotobuf-c >= 1.0.0') $(pkg-config --libs 'libprotobuf-c >= 1.0.0') amsg.c amessage.pb-c.c -Wl,-E
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
error unpacking incoming message
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
error unpacking incoming message
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
error unpacking incoming message
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
error unpacking incoming message
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
error unpacking incoming message
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
error unpacking incoming message
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
error unpacking incoming message
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
error unpacking incoming message
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
error unpacking incoming message
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
error unpacking incoming message
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
error unpacking incoming message
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
Received: a=123
Why would it work so infrequently, or at all if something was wrong? Here’s where I feel like I messed something up in the C code… If I call the function TWICE before printing, it ALWAYS works.
[root@localhost amessage_lua]# cat amessage.lua
#!/usr/bin/lua
require("amsg")
result=dobf("123")
result=dobf("123")
io.write(result)
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
Received: a=123
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
Received: a=123
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
Received: a=123
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
Received: a=123
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
Received: a=123
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
Received: a=123
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
Received: a=123
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
Received: a=123
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
Received: a=123
[root@localhost amessage_lua]# ./amessage.lua | ./amessage_des
Received: a=123
Here’s the protobuf example I’m trying to follow Examples · protobuf-c/protobuf-c Wiki · GitHub . If this is compiled in just C binaries it works fine, but when it’s put into these shared objects and called by lua maybe something different has to happen? I get the feeling I’m not treating the luabuffer correctly.