aboutsummaryrefslogtreecommitdiff
path: root/src/shared/lua_api/load_net.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/lua_api/load_net.cpp')
-rw-r--r--src/shared/lua_api/load_net.cpp35
1 files changed, 32 insertions, 3 deletions
diff --git a/src/shared/lua_api/load_net.cpp b/src/shared/lua_api/load_net.cpp
index 48bdc84..cc2db02 100644
--- a/src/shared/lua_api/load_net.cpp
+++ b/src/shared/lua_api/load_net.cpp
@@ -201,6 +201,29 @@ int lstream_writedouble(lua_State* L){
return 0;
}
+//stream:getpipe() :: pipe
+int lstream_getpipe(lua_State* L){
+ lua_getfield(L,-1,"data");//{stream},ud_stream
+ struct stream* s = (struct stream*)lua_touserdata(L,-1);//{stream},ud_stream
+ lua_pop(L,2);//
+ lua_newtable(L);//{}
+ lua_pushlightuserdata(L,s->pipe);//{},ud_pipe
+ lua_setfield(L,-2,"pipe");//{pipe}
+ return 1;
+}
+
+//stream:setpipe(pipe)
+int lstream_setpipe(lua_State* L){
+ lua_getfield(L,-1,"pipe");//{stream},{pipe},ud_pipe
+ nng_pipe *pipe = (nng_pipe*)lua_touserdata(L,-1);//{stream},{pipe},ud_pipe
+ lua_pop(L,2);//{stream}
+ lua_getfield(L,-1,"data");//{stream},ud_stream
+ struct stream *s = (struct stream*)lua_touserdata(L,-1);
+ lua_pop(L,2);
+ s->pipe = pipe;
+ return 0;
+}
+
/***
Write some data to the stream
@function stream:writedata()
@@ -245,9 +268,13 @@ void gameloop_net(lua_State* L){
//printf("Doing net of gameloop\n");
for(std::map<nng_socket*,int>::iterator it = netfuncs.begin(); it != netfuncs.end(); ++it){
//printf("In for socket %d\n");
- char* buf = NULL;
- size_t size;
- int err = nng_recv(*(it->first), &buf, &size, NNG_FLAG_NONBLOCK | NNG_FLAG_ALLOC);
+ //char* buf = NULL;
+ //size_t size;
+ //int err = nng_recv(*(it->first), &buf, &size, NNG_FLAG_NONBLOCK | NNG_FLAG_ALLOC);
+ nng_msg *msgp;
+ int err = nng_recvmsg(*(it->first), &msgp, NNG_FLAG_NONBLOCK);
+ char* buf = (char*)nng_msg_body(msgp);
+ size_t size = nng_msg_len(msgp);
//printf("Got bytes: %d EAGAIN:%d\n",bytes,EAGAIN);
if(err != 0 && err != NNG_EAGAIN){
printf("Net error: %s\n\t err: %d\n\teagain:%d\n",nng_strerror(err),err,NNG_EAGAIN);
@@ -467,6 +494,8 @@ static const struct luaL_Reg stream_m[] = {
{"readdata",lstream_readdata},
{"readstring",lstream_readstring},
+ {"getpipe",lstream_getpipe},
+ {"setpipe",lstream_setpipe},
{NULL,NULL}
};