lugre_net_L.cpp

Go to the documentation of this file.
00001 #include "lugre_prefix.h"
00002 #include "lugre_scripting.h"
00003 #include "lugre_fifo.h"
00004 #include "lugre_luabind.h"
00005 #include "lugre_net.h"
00006 
00007 extern "C" {
00008     #include "lua.h"
00009     #include "lauxlib.h"
00010     #include "lualib.h"
00011 }
00012 
00013 #ifdef WIN32
00014 #define snprintf _snprintf
00015 #endif
00016 
00017 namespace Lugre {
00018 
00019 class cConnection_L : public cLuaBind<cConnection> { public:
00020     // implementation of cLuaBind
00021 
00023         virtual void RegisterMethods    (lua_State *L) { PROFILE
00024             lua_register(L,"NetConnect",        &cConnection_L::NetConnect);
00025             lua_register(L,"NetLocalMaster",    &cConnection_L::NetLocalMaster);
00026             lua_register(L,"NetLocalSlave",     &cConnection_L::NetLocalSlave);
00027             lua_register(L,"NetReadAndWrite",   &cConnection_L::NetReadAndWrite);
00028             lua_register(L,"NtoA",              &cConnection_L::NtoA);
00029             lua_register(L,"AtoN",              &cConnection_L::AtoN);
00030             lua_register(L,"GetHostByName",     &cConnection_L::GetHostByName);
00031             
00032             #define REGISTER_METHOD(methodname) mlMethod.push_back(make_luaL_reg(#methodname,&cConnection_L::methodname));
00033             REGISTER_METHOD(Destroy);
00034             REGISTER_METHOD(GetRemoteAddress);
00035             REGISTER_METHOD(Push);
00036             REGISTER_METHOD(Pop);
00037             REGISTER_METHOD(IsConnected);
00038             
00039             #undef REGISTER_METHOD
00040         }
00041 
00042     // static methods exported to lua
00043         
00045         static int                  NetConnect      (lua_State *L) { PROFILE 
00046             cConnection* pConnection = cNet::GetSingleton().Connect(luaL_checkstring(L,1),luaL_checkint(L,2));
00047             return pConnection ? CreateUData(L,pConnection) : 0; 
00048         }
00049         
00051         static int                  NetLocalMaster      (lua_State *L) { PROFILE 
00052             cConnection* pConnection = new cConnection();
00053             return pConnection ? CreateUData(L,pConnection) : 0; 
00054         }
00055         
00057         static int                  NetLocalSlave       (lua_State *L) { PROFILE 
00058             cConnection* pConnection = new cConnection(checkudata_alive(L));
00059             return pConnection ? CreateUData(L,pConnection) : 0; 
00060         }
00061         
00063         static int      NetReadAndWrite     (lua_State *L) { PROFILE 
00064             cNet::GetSingleton().Step();
00065             return 0;
00066         }
00067         
00070         static int              NtoA    (lua_State *L) { PROFILE
00071             static char buffer[32];
00072             uint32 ip;
00073             unsigned char *h = (unsigned char *)&ip;
00074             if(lua_isnumber(L, 1)){
00075                 ip = (uint32)luaL_checkint(L,1);
00076                 snprintf(buffer,16,"%i.%i.%i.%i",(int)h[0],(int)h[1],(int)h[2],(int)h[3]);
00077             } else {
00078                 ip = (uint32)((long)lua_touserdata(L, 1));
00079                 snprintf(buffer,16,"%i.%i.%i.%i",(int)h[0],(int)h[1],(int)h[2],(int)h[3]);
00080             }
00081             lua_pushstring(L,buffer);
00082             return 1;
00083         }
00084 
00087         static int              AtoN    (lua_State *L) { PROFILE
00088             static char buffer[32];
00089             const char *str = luaL_checkstring(L, 1);
00090             uint32 ip = 0;
00091             unsigned char *h = (unsigned char *)&ip;
00092             int a=0,b=0,c=0,d=0;
00093             sscanf(str,"%d.%d.%d.%d",&a,&b,&c,&d);
00094             h[3] = a; h[2] = b; h[1] = c; h[0] = d;
00095             
00096             lua_pushlightuserdata(L,reinterpret_cast<void*>(ip));
00097             return 1; 
00098         }
00099 
00102         static int              GetHostByName   (lua_State *L) { PROFILE
00103             unsigned int ip = cNet::GetHostByName(luaL_checkstring(L,1));
00104             if(ip){
00105                 // TODO this could be system dependant, due to endianess
00106                 // probably use inet_ntop() ?
00107                 
00108                 static char buffer[32];
00109                 unsigned char *h = (unsigned char *)&ip;
00110                 snprintf(buffer,16,"%d.%d.%d.%d",(int)h[0],(int)h[1],(int)h[2],(int)h[3]);
00111                 lua_pushstring(L,buffer);
00112                 return 1;
00113             } else {
00114                 return 0;
00115             }
00116         }
00117 
00118     // object methods exported to lua
00119 
00120         static int  Destroy             (lua_State *L) { PROFILE 
00121             delete checkudata_alive(L);
00122             return 0; 
00123         }
00124 
00125         static int  GetRemoteAddress        (lua_State *L) { PROFILE 
00126             lua_pushlightuserdata(L,reinterpret_cast<void*>(checkudata_alive(L)->miRemoteAddr));
00127             return 1; 
00128         }
00129         
00130         static int  IsConnected             (lua_State *L) { PROFILE 
00131             cConnection*    target  = checkudata_alive(L);
00132             bool r = false;
00133             if(target)r = target->IsConnected();
00134             lua_pushboolean(L,r);
00135             return 1; 
00136         }
00137 
00139         static int          Push    (lua_State *L) { PROFILE 
00140             cConnection*    pCon    = checkudata_alive(L);
00141             cFIFO*          pFifo   = cLuaBind<cFIFO>::checkudata_alive(L,2);
00142             if (pFifo->size() > 0) {
00143                 //printf("cConnection_L::Push(%d)\n",pFifo->size());
00144                 pCon->mpOutBuffer->Push(*pFifo);
00145             }
00146             return 0; 
00147         }
00148         
00152         static int          Pop     (lua_State *L) { PROFILE 
00153             cConnection*    pCon    = checkudata_alive(L);
00154             cFIFO*          pFifo   = cLuaBind<cFIFO>::checkudata_alive(L,2);
00155             if (pCon->mpInBuffer->size() > 0) {
00156                 //printf("cConnection_L::Pop(%d)\n",pCon->mpInBuffer->size());
00157                 pFifo->Push(*pCon->mpInBuffer);
00158                 pCon->mpInBuffer->Clear();
00159             }
00160             return 0; 
00161         }
00162         
00163         virtual const char* GetLuaTypeName () { return "lugre.NetConnection"; }
00164 };
00165 
00166 
00167 class cNetListener_L : public cLuaBind<cNetListener> { public:
00168     // implementation of cLuaBind
00169 
00171         virtual void RegisterMethods    (lua_State *L) { PROFILE
00172             lua_register(L,"NetListen",     &cNetListener_L::NetListen);
00173             
00174             #define REGISTER_METHOD(methodname) mlMethod.push_back(make_luaL_reg(#methodname,&cNetListener_L::methodname));
00175             REGISTER_METHOD(Destroy);
00176             REGISTER_METHOD(PopAccepted);
00177             
00178             #undef REGISTER_METHOD
00179         }
00180 
00181     // static methods exported to lua
00182         
00184         static int                  NetListen       (lua_State *L) { PROFILE 
00185             cNetListener* pNetListener = cNet::GetSingleton().Listen(luaL_checkint(L,1));
00186             return pNetListener ? CreateUData(L,pNetListener) : 0; 
00187         }
00188 
00189     // object methods exported to lua
00190 
00191         static int  Destroy             (lua_State *L) { PROFILE 
00192             delete checkudata_alive(L);
00193             return 0; 
00194         }
00195         
00196         static int  PopAccepted         (lua_State *L) { PROFILE 
00197             cConnection* pConnection = checkudata_alive(L)->PopAccepted();
00198             return pConnection ? cLuaBind<cConnection>::CreateUData(L,pConnection) : 0; 
00199         }
00200         
00201         virtual const char* GetLuaTypeName () { return "lugre.NetListener"; }
00202 };
00203 
00204 class cUDP_ReceiveSocket_L : public cLuaBind<cUDP_ReceiveSocket> { public:
00205     // implementation of cLuaBind
00206 
00208         virtual void RegisterMethods    (lua_State *L) { PROFILE
00209             lua_register(L,"Create_UDP_ReceiveSocket",      &cUDP_ReceiveSocket_L::Create_UDP_ReceiveSocket);
00210             
00211             #define REGISTER_METHOD(methodname) mlMethod.push_back(make_luaL_reg(#methodname,&cUDP_ReceiveSocket_L::methodname));
00212             REGISTER_METHOD(Destroy);
00213             REGISTER_METHOD(Receive);
00214             
00215             #undef REGISTER_METHOD
00216         }
00217 
00218     // static methods exported to lua
00219         
00221         static int                          Create_UDP_ReceiveSocket        (lua_State *L) { PROFILE 
00222             cUDP_ReceiveSocket* pUDP_ReceiveSocket = new cUDP_ReceiveSocket(luaL_checkint(L,1));
00223             return pUDP_ReceiveSocket ? CreateUData(L,pUDP_ReceiveSocket) : 0; 
00224         }
00225 
00226     // object methods exported to lua
00227 
00228         static int  Destroy             (lua_State *L) { PROFILE 
00229             delete checkudata_alive(L);
00230             return 0; 
00231         }
00232         
00234         static int  Receive         (lua_State *L) { PROFILE 
00235             uint32 iRemoteAddr = 0;
00236             lua_pushnumber(L,checkudata_alive(L)->Receive(*cLuaBind<cFIFO>::checkudata_alive(L,2),iRemoteAddr));
00237             lua_pushlightuserdata(L,reinterpret_cast<void*>(iRemoteAddr));
00238             return 2; 
00239         }
00240         
00241         virtual const char* GetLuaTypeName () { return "lugre.UDP_ReceiveSocket"; }
00242 };
00243 
00244 class cUDP_SendSocket_L : public cLuaBind<cUDP_SendSocket> { public:
00245     // implementation of cLuaBind
00246 
00248         virtual void RegisterMethods    (lua_State *L) { PROFILE
00249             lua_register(L,"Create_UDP_SendSocket",     &cUDP_SendSocket_L::Create_UDP_SendSocket);
00250             
00251             #define REGISTER_METHOD(methodname) mlMethod.push_back(make_luaL_reg(#methodname,&cUDP_SendSocket_L::methodname));
00252             REGISTER_METHOD(Destroy);
00253             REGISTER_METHOD(Send);
00254             REGISTER_METHOD(SetBroadcast);
00255             
00256             #undef REGISTER_METHOD
00257         }
00258 
00259     // static methods exported to lua
00260         
00262         static int                          Create_UDP_SendSocket       (lua_State *L) { PROFILE 
00263             cUDP_SendSocket* pUDP_SendSocket = new cUDP_SendSocket();
00264             return pUDP_SendSocket ? CreateUData(L,pUDP_SendSocket) : 0; 
00265         }
00266 
00267     // object methods exported to lua
00268 
00269         static int  Destroy             (lua_State *L) { PROFILE 
00270             delete checkudata_alive(L);
00271             return 0; 
00272         }
00273         
00278         static int  Send            (lua_State *L) { PROFILE 
00279             
00280             uint32          iAddr = (uint32)(long)(lua_touserdata(L,2));
00281             // iAddr is not really a pointer, but lua has problems encoding full 32bit integers in it's number type (float)
00282             
00283             int             iPort = luaL_checkint(L,3);
00284             cFIFO*          pFIFO = cLuaBind<cFIFO>::checkudata_alive(L,4);
00285             int             iDataLen = (lua_gettop(L) >= 5 && !lua_isnil(L,5)) ? luaL_checkint(L,5) : pFIFO->size();
00286             lua_pushnumber(L,checkudata_alive(L)->Send(iAddr,iPort,*pFIFO,iDataLen));
00287             return 1; 
00288         }
00289         
00292         static int  SetBroadcast            (lua_State *L) { PROFILE 
00293             int             broadcast = luaL_checkint(L,2);
00294             checkudata_alive(L)->SetBroadcast(broadcast);
00295             return 0; 
00296         }
00297         
00298         virtual const char* GetLuaTypeName () { return "lugre.UDP_SendSocket"; }
00299 };
00300 
00302 void    LuaRegisterNet  (lua_State *L) { PROFILE
00303     cLuaBind<cConnection		>::GetSingletonPtr(new cConnection_L()         )->LuaRegister(L);
00304     cLuaBind<cNetListener		>::GetSingletonPtr(new cNetListener_L()       )->LuaRegister(L);
00305     cLuaBind<cUDP_ReceiveSocket	>::GetSingletonPtr(new cUDP_ReceiveSocket_L())->LuaRegister(L);
00306     cLuaBind<cUDP_SendSocket	>::GetSingletonPtr(new cUDP_SendSocket_L()  )->LuaRegister(L);
00307 }
00308 
00309 };

Generated on Wed May 23 06:00:15 2012 for cpp by  doxygen 1.5.6