From 61c0c9f53d3a57ee7fd5db5faa74c4b51e2da396 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sat, 28 Oct 2017 18:12:50 -0400 Subject: Lots of updates * Networking is finally working * Started moveing physics into the shared domain * Streams now have a readString() and writeString() method * streams are passed to the lua context for networking * Refactored cameras and physboxes to use metatables * Finally wrote the pushvector3* and popvector3* methods * Fixed a few crashes in ;main * Deleted a lot of code --- src/shared/lua_api/load_net.cpp | 88 +++++++++++++++++++++++++++++++++-------- 1 file changed, 71 insertions(+), 17 deletions(-) (limited to 'src/shared/lua_api/load_net.cpp') diff --git a/src/shared/lua_api/load_net.cpp b/src/shared/lua_api/load_net.cpp index cd155fb..5280d42 100644 --- a/src/shared/lua_api/load_net.cpp +++ b/src/shared/lua_api/load_net.cpp @@ -20,18 +20,45 @@ extern "C" { #include "load_net.hpp" -//#include #include "../util/hashmap.h" +#include "stream.hpp" std::map> netfuncs; +// stream:readInt() +int lstream_readInt(lua_State* L){//self + lua_getfield(L,-1,"data");//self,lightuserdata + if(!lua_islightuserdata(L,-1)){ + lua_pushstring(L,"data field was not light userdata"); + lua_error(L); + } + struct stream* s = (struct stream*)lua_touserdata(L,-1);//self,lightuserdata + printf("Stream in readint was %p\n",s); + stream_print(s); + int i = stream_readInt(s); + printf("In readint, int read from stream was %d\n",i); + lua_pop(L,2);// + lua_pushinteger(L,i);//int + return 1; +} +// stream:writeInt(number) +int lstream_writeInt(lua_State* L){//self,number + lua_getfield(L,-2,"data");//self,number,lightuserdata + struct stream* s = (struct stream*)lua_touserdata(L,-1);//self,number,lightuserdata + printf("Stream in writeInt was %p\n",s); + stream_print(s); + int num = lua_tointeger(L,-2);//self,number,lightuserdata + printf("Integer was %d\n",num); + stream_writeInt(s,num); + lua_pop(L,3);// + return 0; +} + void gameloop_net(lua_State* L){ //printf("Doing net of gameloop\n"); for(std::map>::iterator it = netfuncs.begin(); it != netfuncs.end(); ++it){ - //printf("Looking for new data for socket %d\n",it->first); char* buf = NULL; int bytes = nn_recv(it->first, &buf, NN_MSG,NN_DONTWAIT); - //printf("After recv\n"); if(bytes < 0 && nn_errno() != EAGAIN){ lua_pushstring(L,"Failed to receive"); lua_error(L); @@ -39,22 +66,27 @@ void gameloop_net(lua_State* L){ //do nothing }else{ //find how long until the first null character - int msglen = strlen(buf); - char msg[msglen]; - msg[msglen] = '\0'; - sprintf(msg,"%s",buf); + struct stream* stream = stream_create(); + stream->length = bytes; + stream->data = buf; + stream_print(stream); + char* msg = stream_readString(stream); std::map themap = it->second; std::string s = std::string(msg); std::map::iterator in = themap.find(s); if(in != themap.end()){ lua_rawgeti(L,LUA_REGISTRYINDEX,(*in).second); lua_newtable(L); + lua_pushlightuserdata(L,stream); + lua_setfield(L,-2,"data"); + luaL_getmetatable(L,"net.stream"); + lua_setmetatable(L,-2); lua_call(L,1,0); } nn_freemsg(buf); + free(stream);//We manually set stream->data so free_stream would crash here } } - //hashmap_iterate(netfuncs,check_socket,0); } int bindsocket(lua_State*L){ @@ -66,7 +98,7 @@ int bindsocket(lua_State*L){ int id = nn_bind(fd,s); if(id < 0){ const char* errstr = nn_strerror(nn_errno()); - char* failmsg = "Failed to bind socket: "; + char* failmsg = (char*)"Failed to bind socket: "; int faillen = strlen(failmsg + 2); char buf[faillen + strlen(errstr)]; sprintf(buf,"%s%s\n",failmsg,errstr); @@ -99,16 +131,28 @@ int connectsocket(lua_State* L){ return 0; } +//socket:send("name",function(stream)) int send(lua_State* L){ - const char* data = lua_tostring(L,-1); - lua_pushstring(L,"fd"); - lua_gettable(L,-2); - int fd = lua_tonumber(L,-1); + const char* data = lua_tostring(L,-2);//socket,"name",function(stream) + lua_pushstring(L,"fd");//socket,"name",function(stream),"fd" + lua_gettable(L,-4);//socket,"name",function(stream),int_socketdescriptor + int fd = lua_tonumber(L,-1);//socket,"name",function(stream),int_socketdescriptor + lua_pop(L,1);//socket,"name",function(stream) int dlen = strlen(data); - int err = nn_send(fd,(void*)data,dlen,0); + struct stream* s = stream_create(); + stream_writeString(s,data,dlen); + lua_newtable(L);//socket,"name",function(stream),streamtable + luaL_getmetatable(L,"net.stream");//socket,"name",function(stream),streamtable,net.stream + lua_setmetatable(L,-2);//socket,"name",function(stream),streamtable + lua_pushlightuserdata(L,s);//socket,"name",function(stream),streamtable,lightudata + lua_setfield(L,-2,"data");//socket,"name",function(stream),streamtable + lua_call(L,1,0);//socket,"name" + lua_pop(L,2);// + int err = nn_send(fd,s->data,s->length,0); + stream_free(s); if(err < 0){ const char* errstr = nn_strerror(nn_errno()); - char* failmsg = "Failed to bind socket: "; + char* failmsg = (char*)"Failed to bind socket: "; int faillen = strlen(failmsg + 2); char buf[faillen + strlen(errstr)]; sprintf(buf,"%s%s\n",failmsg,errstr); @@ -119,7 +163,7 @@ int send(lua_State* L){ } //LUA: -//socket:receive(s_name,function(socket)) +//socket:receive(s_name,function(stream)) int netreceive(lua_State* L){ const char* name = lua_tostring(L,-2); int slen = strlen(name); @@ -215,7 +259,17 @@ void loadNetLibs(lua_State* L){ //Set the table to gobal "net" lua_setglobal(L,"net"); - + + //Create the metatable for streams + luaL_newmetatable(L,"net.stream"); //net.stream + lua_newtable(L); + lua_pushcfunction(L,lstream_readInt);//net.stream,{},readInt() + lua_setfield(L,-2,"readInt");//net.stream,{} + lua_pushcfunction(L,lstream_writeInt);//net.stream,{},writeInt() + lua_setfield(L,-2,"writeInt");//net.stream,{} + lua_setfield(L,-2,"__index"); + + //Create the metatable for sockets luaL_newmetatable(L,"net.socket"); lua_pushvalue(L,-1); -- cgit v1.2.3-70-g09d2