Skip to content

SVCD::subscribe [OK] #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion natlib/svcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@


#define SVCD_SYMBOLS \
{ LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) },
{ LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) }, \
{ LSTRKEY( "svcd_subscribe"), LFUNCVAL ( svcd_subscribe ) },


//If this file is defining only specific functions, or if it
Expand Down Expand Up @@ -185,3 +186,52 @@ static int svcd_init( lua_State *L )

return 0;
}

static int svcd_subscribe(lua_State* L)
{
if (lua_gettop(L) != 4) return luaL_error(L, "Expected (targetip, svcid, attrid, on_notify)");

// arg 1 is targetip, to be passed along to to libstorm_net_sendto
uint16_t svcid = luaL_checknumber(L, 2);
uint16_t attrid = luaL_checknumber(L, 3);
// arg 4 is on_notify

lua_getglobal(L, "SVCD"); // position 5 on stack
lua_getfield(L, 5, "ivkid");

uint16_t ivkid = lua_tointeger(L, -1);

uint16_t new_ivkid = ivkid + 1;
if (new_ivkid > 65535) {
new_ivkid = 0;
}

lua_pushnumber(L, new_ivkid);
lua_setfield(L, 5, "ivkid");

lua_getfield(L, 5, "oursubs");
// put value on the stack
lua_pushinteger(L, ivkid);
lua_pushvalue(L, 4); // on_notify
lua_settable(L, -3);

lua_pushlightfunction(L, libstorm_net_sendto);

lua_getfield(L, 5, "ncsock");

char msg[8] __attribute__((aligned(2)));

// don't use the msg[0] byte
msg[1] = 1;
((uint16_t*) msg)[1] = svcid;
((uint16_t*) msg)[2] = attrid;
((uint16_t*) msg)[3] = ivkid;
lua_pushlstring(L, &(msg[1]), 7);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for groups wanting to solve the alignment problem, this is what I was suggesting. The 16 bit writes will still be word aligned because byte 0 is used as padding. Good job.


lua_pushvalue(L, 1); // targetip
lua_pushnumber(L, 2530);
lua_call(L, 4, 0);

lua_pushnumber(L, ivkid);
return 1;
}