diff options
Diffstat (limited to 'src/shared')
| -rw-r--r-- | src/shared/lua_api/load_net.cpp | 35 | ||||
| -rw-r--r-- | src/shared/lua_api/stream.cpp | 1 | ||||
| -rw-r--r-- | src/shared/lua_api/stream.hpp | 2 |
3 files changed, 35 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} }; diff --git a/src/shared/lua_api/stream.cpp b/src/shared/lua_api/stream.cpp index 9f7200c..fcdc3e3 100644 --- a/src/shared/lua_api/stream.cpp +++ b/src/shared/lua_api/stream.cpp @@ -11,6 +11,7 @@ struct stream* stream_create(){ s->length = 0; s->data = (byte*)malloc(sizeof(byte)*0); s->read = 0; + s->pipe = NULL; return s; } void stream_writeInt(struct stream* s, int number){ diff --git a/src/shared/lua_api/stream.hpp b/src/shared/lua_api/stream.hpp index bf44ed4..74a0e69 100644 --- a/src/shared/lua_api/stream.hpp +++ b/src/shared/lua_api/stream.hpp @@ -1,4 +1,5 @@ #include <stdlib.h> +#include <nng.h> //Char is not definitvely a byte, read the fucking standard #define byte char @@ -7,6 +8,7 @@ typedef struct stream { long length; byte* data; long read; + nng_pipe *pipe; } stream; struct stream* stream_create(); |
