Skip to content

Instantly share code, notes, and snippets.

View laheadle's full-sized avatar

Lyn Headley laheadle

View GitHub Profile
local sendpacketbuf = ffi.new('uint8_t[?]', maxpacketlen)
local recvpacketbuf = ffi.new('uint8_t[?]', maxpacketlen)
local maxpacketlen=512
local headerlen = versionsize + filenamelensize +
contentlensize + fromsize + seqsize
local bodylen = maxpacketlen - headerlen
-- header field sizes in bytes
local versionsize = 1
local filenamelensize = 2
local contentlensize = 4
local fromsize = 4
local seqsize = 4
function putb(buf, type, where, what)
local bp = ffi.cast('uint8_t*', buf)
local ptr = ffi.cast(type..'*', bp + where)
ptr[0] = what
end
function getb(buf, type, where)
local bp = ffi.cast('uint8_t*', buf)
return ffi.cast(type..'*', bp + where)[0]
end
function run(hInstance, cmdline, nCmdShow)
val, err = pcall(function() _run(hInstance, nCmdShow) end)
end
typedef struct speer {
SOCKET socket;
struct sockaddr_in addr;
} speer;
#define mkspeer(tpeer) ((speer *) tpeer)
int peer_broadcast(tpeer peerIn, char* msg, unsigned int size)
{
speer* p = mkspeer(peerIn);
int val = sendto(p->socket, msg, size, 0,
(struct sockaddr *)&p->addr, sizeof p->addr);
if (val == -1) {
char buf[32];
sprintf(buf, "broadcast %d", WSAGetLastError());
ulog(buf);
}
tpeer peer_create(int isServer)
{
init();
speer *p = (speer *)malloc(sizeof(speer));
sockopt_t broadcast=1;
if ((p->socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
ffi.cdef[[
typedef void* tpeer;
tpeer peer_create(int isServer);
int peer_broadcast(tpeer peerIn, char* msg, unsigned int size);
void peer_destroy(tpeer p);
@laheadle
laheadle / gist:4431714
Last active December 10, 2015 12:08
Winmain Function
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luaL_dostring(L, "require \"ezshare\"");
lua_getfield(L, LUA_GLOBALSINDEX, "run");
lua_pushlightuserdata (L, hInstance);
lua_pushlstring(L, lpCmdLine, strlen(lpCmdLine));
lua_pushinteger(L, nCmdShow);