Changeset 736
- Timestamp:
- 06/21/09 05:07:51 (15 months ago)
- Location:
- trunk/lugre
- Files:
-
- 7 modified
-
include/lugre_scripting.h (modified) (1 diff)
-
include/lugre_thread.h (modified) (2 diffs)
-
lua/lib.irc.lua (modified) (4 diffs)
-
src/lugre_scripting.cpp (modified) (2 diffs)
-
src/lugre_scripting.general.cpp (modified) (2 diffs)
-
src/lugre_thread.cpp (modified) (2 diffs)
-
src/lugre_thread_L.cpp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lugre/include/lugre_scripting.h
r565 r736 68 68 bool LuaCall (const char *func, const char *sig = "", ...); 69 69 70 71 void InitLugreLuaEnvironment (lua_State* L); 72 70 73 // cInputListener methods 71 74 virtual void Notify_KeyPress (const unsigned char iKey,const int iLetter); -
trunk/lugre/include/lugre_thread.h
r120 r736 36 36 class cThread_LoadFileImpl; 37 37 38 int MyThreadSleepMilliSeconds (int iSleepTimeMilliSeconds); 39 void LuaRegisterThreading (lua_State* L); 38 40 39 41 /// connects to host:port , sends senddata and collects answer until the other side closes the connection … … 80 82 81 83 84 85 86 87 88 89 90 91 82 92 }; 83 93 -
trunk/lugre/lua/lib.irc.lua
r83 r736 31 31 if not self:IsConnected() then return end 32 32 33 print("DEBUG:IRC:send",">"..tostring(line).."<") 34 33 35 local fifo = CreateFIFO() 34 36 FIFO_PushPlainText(fifo,line .. "\r\n") … … 58 60 local message = "" 59 61 62 60 63 if string.find(line," ",pos) then 61 64 sender = trim(string.sub(line,pos,string.find(line," ",pos))) … … 78 81 end 79 82 80 print("DEBUG",tostring(sender) .. "|" .. tostring(command) .. "|" .. tostring(param) .. "|" .. tostring(message))81 82 83 83 84 local nick = trim(sender,":") … … 93 94 self:OnMessage(nick,param,message) 94 95 end 96 97 98 local a,b 99 local msg_prefix 100 local msg_command 101 local msg_params 102 if (string.find(line,"^:")) then 103 a,b,msg_prefix,msg_command,msg_params = string.find(line,"^:([^ ]+) ([^ ]+) (.*)\r\n$") 104 else 105 a,b,msg_command,msg_params = string.find(line,"([^ ]+) (.*)\r\n$") 106 end 107 108 local RPL_MOTD = "372" 109 local RPL_UMODEIS = "221" 110 111 112 --~ print("DEBUG",tostring(sender) .. "|" .. tostring(command) .. "|" .. tostring(param) .. "|" .. tostring(message)) 113 if (msg_command ~= RPL_MOTD) then 114 --~ print("DEBUG:IRC",">"..tostring(msg_command).."<",">"..tostring(msg_params).."<",">"..tostring(string.gsub(line,"\r\n$","")).."<") 115 print("DEBUG:IRC:recv",">"..tostring(string.gsub(line,"\r\n$","")).."<") 116 end 117 118 if (msg_command == RPL_UMODEIS) then -- final login message on quakenet 119 if (self.on_login_complete) then self:on_login_complete() end 120 end 121 if (msg_command == "PING") then 122 local a,b,daemon = string.find(msg_params,"^:(.*)") 123 if (not daemon) then daemon = tostring(msg_params) end 124 -- quakenet sends >PING :424867537< so we just remove the : 125 -- zw sends >PING :zwischenwelt.org< 126 print("IRC: got ping, sending pong.. daemon="..daemon) 127 self:SendLine("PONG "..daemon) 128 end 129 -- DEBUG PING|||zwischenwelt.org 95 130 96 131 -- read out next line -
trunk/lugre/src/lugre_scripting.cpp
r723 r736 376 376 L = lua_open(); 377 377 assert(L); 378 InitLugreLuaEnvironment(L); 379 380 cInput::RegisterListener(this); 381 382 int res = luaL_dofile(L,sLuaMainPath.c_str()); 383 if (res) { 384 fprintf(stderr,"%s\n",lua_tostring(L,-1)); 385 MyCrash("error in main script-initialisation\n"); 386 exit(-1); 387 } 388 } 389 390 391 392 void cScripting::InitLugreLuaEnvironment (lua_State* L) { 378 393 luaL_openlibs(L); 379 394 //~ luaopen_base(L); … … 447 462 { for (std::list<cScriptingPlugin*>::iterator itor=mlPlugins.begin();itor!=mlPlugins.end();++itor) 448 463 (*itor)->RegisterLua_Classes(L); } 449 450 cInput::RegisterListener(this);451 452 res = luaL_dofile(L,sLuaMainPath.c_str());453 if (res) {454 fprintf(stderr,"%s\n",lua_tostring(L,-1));455 MyCrash("error in main script-initialisation\n");456 exit(-1);457 }458 464 } 459 465 -
trunk/lugre/src/lugre_scripting.general.cpp
r715 r736 471 471 472 472 473 473 474 void RegisterLua_General_Classes (lua_State* L) { 474 475 RegisterLuaXML(L); 475 476 LuaRegisterFIFO(L); 476 477 LuaRegisterNet(L); 478 LuaRegisterThreading(L); 477 479 cGfx3D::LuaRegister(L); 478 480 cGfx2D::LuaRegister(L); … … 482 484 cSoundSource::LuaRegister(L); 483 485 cRandom::LuaRegister(L); 484 cThread_NetRequest::LuaRegister(L);485 cThread_LoadFile::LuaRegister(L);486 486 cTexAtlas::LuaRegister(L); 487 487 cImage::LuaRegister(L); -
trunk/lugre/src/lugre_thread.cpp
r120 r736 17 17 // warning ! starting a thread makes a COPY of the passed in functor-object, so you cannot access the original 18 18 // see http://boost.org/doc/html/boost/thread.html#id1291385-bb for details 19 // http://www.boost.org/doc/libs/1_39_0/doc/html/thread.html 19 20 20 21 // see also http://engineering.meta-comm.com/resources/cs-win32_1_30_2_metacomm/libs/thread/doc/thread.html … … 188 189 189 190 191 190 192 }; -
trunk/lugre/src/lugre_thread_L.cpp
r120 r736 3 3 #include "lugre_fifo.h" 4 4 #include "lugre_luabind.h" 5 #include "lugre_scripting.h" 6 7 #ifdef ENABLE_THREADS 8 #include <boost/thread/xtime.hpp> 9 #include <boost/thread/thread.hpp> 10 #include <boost/thread/mutex.hpp> 11 #endif 5 12 6 13 namespace Lugre { … … 111 118 }; 112 119 120 121 // result : 0:not supported, 1:success 2:interrupted 122 int MyThreadSleepMilliSeconds (int iSleepTimeMilliSeconds) { 123 #ifdef ENABLE_THREADS 124 125 126 /* 127 ancient boost version : 103401 128 boost::xtime xt; 129 boost::xtime_get(&xt, boost::TIME_UTC); 130 int big = 1000*1000*1000; 131 xt.sec += (iSleepTimeMilliSeconds / 1000); 132 while (xt.nsec > big) { xt.nsec -= big; xt.sec += 1; } 133 xt.nsec += (iSleepTimeMilliSeconds % 1000)*1000*1000; 134 while (xt.nsec > big) { xt.nsec -= big; xt.sec += 1; } 135 boost::thread::sleep(xt); 136 */ 137 138 139 //~ #else 140 //~ #define BOOST_VERSION 103401 -- ghoul : old : linux 141 //~ #define BOOST_VERSION 103700 -- ghoul : new 142 //~ #define BOOST_VERSION 103800 -- hagish:win 143 // #define BOOST_LIB_VERSION "1_34_1" 144 145 try { 146 // boost::this_thread::sleep(system_time const& abs_time); 147 // boost::this_thread::sleep(TimeDuration const& rel_time); 148 boost::this_thread::sleep(boost::posix_time::milliseconds(iSleepTimeMilliSeconds)); 149 } catch (...) { 150 return 2; 151 } 152 return 1; 153 #else 154 return 0; 155 #endif 156 } 157 158 #ifdef ENABLE_THREADS 159 160 class cLuaThread : public cSmartPointable { public: 161 cFIFO mFIFO; 162 boost::mutex mMutex; 163 boost::thread* mThread; 164 std::string msFilePath; 165 166 cLuaThread (std::string sFilePath); 167 virtual ~cLuaThread () { if (mThread) { delete mThread; mThread = 0; } } 168 169 cFIFO& GetFIFO () { return mFIFO; } 170 void LockMutex () { mMutex.lock(); } 171 void UnLockMutex () { mMutex.unlock(); } 172 void Interrupt () { if (mThread) mThread->interrupt(); } 173 }; 174 175 class cLuaThread_Callable { public: // must be copyable 176 cLuaThread* mSelfThreadHandle; 177 std::string msFilePath; 178 cLuaThread_Callable (cLuaThread* mSelfThreadHandle,std::string msFilePath) : mSelfThreadHandle(mSelfThreadHandle),msFilePath(msFilePath) {} 179 180 static int DontUseWarning_Client_Sleep (lua_State *L) { printf("DontUseWarning_Client_Sleep\n"); return 0; } 181 182 // bool Thread_Sleep (iSleepTimeMilliSeconds) // returns true if it was interrupted 183 static int Thread_Sleep (lua_State *L) { 184 int iSleepTimeMilliSeconds = luaL_checkint(L,1); 185 if (MyThreadSleepMilliSeconds(iSleepTimeMilliSeconds) == 2) { lua_pushboolean(L,true); return 1; } 186 return 0; 187 } 188 189 void operator()() { 190 //~ msFilePath 191 // boost::thread::sleep() 192 193 194 lua_State* L = lua_open(); 195 if (!L) { printf("cLuaThread_Callable: failed to init lua state\n"); return; } 196 cScripting::GetSingletonPtr()->InitLugreLuaEnvironment(L); 197 198 199 lua_register(L,"Client_Sleep", &cLuaThread_Callable::DontUseWarning_Client_Sleep); 200 lua_register(L,"Thread_Sleep", &cLuaThread_Callable::Thread_Sleep); 201 202 203 cLuaBind<cLuaThread>::CreateUData(L,mSelfThreadHandle); 204 lua_setglobal(L,"this_thread"); 205 206 207 int res = luaL_dofile(L,msFilePath.c_str()); 208 if (res) { 209 fprintf(stderr,"%s\n",lua_tostring(L,-1)); 210 MyCrash("error during cLuaThread_Callable run\n"); 211 exit(-1); 212 } 213 } 214 }; 215 216 217 218 cLuaThread::cLuaThread (std::string sFilePath) : mThread(0),msFilePath(sFilePath) { 219 cLuaThread_Callable myImpl(this,sFilePath); 220 mThread = new boost::thread(myImpl); // warning ! this COPIES the impl object 221 } 222 223 224 class cLuaThread_L : public cLuaBind<cLuaThread> { public: 225 // implementation of cLuaBind 226 227 /// called by Register(), registers object-methods (see cLuaBind constructor for examples) 228 virtual void RegisterMethods (lua_State *L) { PROFILE 229 #define REGISTER_METHOD(methodname) mlMethod.push_back(make_luaL_reg(#methodname,&cLuaThread_L::methodname)); 230 231 REGISTER_METHOD(Destroy); 232 REGISTER_METHOD(CreateFifoHandle); 233 REGISTER_METHOD(LockMutex); 234 REGISTER_METHOD(UnLockMutex); 235 REGISTER_METHOD(Interrupt); 236 237 #undef REGISTER_METHOD 238 239 lua_register(L,"CreateLuaThread", &cLuaThread_L::CreateLuaThread); 240 lua_register(L,"Threads_GetHardwareConcurrency", &cLuaThread_L::Threads_GetHardwareConcurrency); 241 } 242 243 // static methods exported to lua 244 245 /// luathread CreateLuaThread (sFilePath) 246 static int CreateLuaThread (lua_State *L) { PROFILE 247 std::string sFilePath = luaL_checkstring(L,1); 248 return CreateUData(L,new cLuaThread(sFilePath)); 249 } 250 251 /// int Threads_GetHardwareConcurrency () 252 // The number of hardware threads available on the current system (e.g. number of CPUs or cores or hyperthreading units), or 0 if this information is not available. 253 static int Threads_GetHardwareConcurrency (lua_State *L) { PROFILE 254 lua_pushnumber(L,boost::thread::hardware_concurrency()); //~ unsigned boost::thread::hardware_concurrency(); 255 return 1; 256 } 257 258 259 260 // object methods exported to lua 261 262 // use LockMutex -- UnLockMutex around access to this fifo ! 263 /// for lua : fifo CreateFifoHandle () 264 static int CreateFifoHandle (lua_State *L) { PROFILE 265 cFIFO& pFIFO = checkudata_alive(L)->GetFIFO(); 266 return cLuaBind<cFIFO>::CreateUData(L,&pFIFO); 267 } 268 269 /// for lua : void LockMutex () 270 static int LockMutex (lua_State *L) { PROFILE checkudata_alive(L)->LockMutex(); return 0; } 271 /// for lua : void UnLockMutex () 272 static int UnLockMutex (lua_State *L) { PROFILE checkudata_alive(L)->UnLockMutex(); return 0; } 273 /// for lua : void Interrupt () 274 static int Interrupt (lua_State *L) { PROFILE checkudata_alive(L)->Interrupt(); return 0; } 275 276 /// Destroy() 277 static int Destroy (lua_State *L) { PROFILE 278 delete checkudata_alive(L); 279 return 0; 280 } 281 282 virtual const char* GetLuaTypeName () { return "lugre.luathread"; } 283 }; 284 285 286 287 288 289 #endif 113 290 114 291 /// lua binding … … 120 297 } 121 298 122 }; 299 void LuaRegisterThreading (lua_State* L) { 300 cThread_NetRequest::LuaRegister(L); 301 cThread_LoadFile::LuaRegister(L); 302 #ifdef ENABLE_THREADS 303 cLuaBind<cLuaThread>::GetSingletonPtr(new cLuaThread_L())->LuaRegister(L); 304 #endif 305 } 306 307 308 };
