From 0da4f7eaa274e33065c5a9ec031af5d971112d66 Mon Sep 17 00:00:00 2001 From: faketruth Date: Wed, 22 Aug 2012 12:24:29 +0000 Subject: Fixed cWebPlugin_Lua being not thread safe. And I don't know why, but it still crashes in Lua sometimes o_O WebAdmin chat now supports infinite number of chat messages (only client side). The client requests only NEW chat messages from the server. git-svn-id: http://mc-server.googlecode.com/svn/trunk@776 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- MCServer/Plugins/Core/web_chat.lua | 58 ++++++++++++++++++++++++++------------ source/cPlugin_NewLua.h | 2 ++ source/cWebPlugin_Lua.cpp | 1 + 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/MCServer/Plugins/Core/web_chat.lua b/MCServer/Plugins/Core/web_chat.lua index c8b5ded59..4ee679729 100644 --- a/MCServer/Plugins/Core/web_chat.lua +++ b/MCServer/Plugins/Core/web_chat.lua @@ -1,4 +1,5 @@ local CHAT_HISTORY = 50 +local LastMessageID = 0 local JavaScript = [[ ]] local ChatLogMessages = {} function AddMessage( PlayerName, Message ) - table.insert( ChatLogMessages, { name = PlayerName, message = Message } ) + LastMessageID = LastMessageID + 1 + table.insert( ChatLogMessages, { name = PlayerName, message = Message, id = LastMessageID } ) while( #ChatLogMessages > CHAT_HISTORY ) do table.remove( ChatLogMessages, 1 ) end @@ -93,25 +111,29 @@ function OnChat( Player, Message ) end function HandleRequest_Chat( Request ) - if( Request.Params["JustChat"] ~= nil ) then + if( Request.PostParams["JustChat"] ~= nil ) then + local LastIdx = 0 + if( Request.PostParams["LastMessageID"] ~= nil ) then LastIdx = tonumber( Request.PostParams["LastMessageID"] ) end local Content = "" for key, value in pairs(ChatLogMessages) do - Content = Content .. "[" .. value.name .. "]: " .. value.message .. "
" + if( value.id > LastIdx ) then + Content = Content .. "[" .. value.name .. "]: " .. value.message .. "
" + end end + Content = Content .. "<>" .. LastMessageID .. "<>" .. LastIdx return Content end - if( Request.Params["ChatMessage"] ~= nil ) then - LOG("Chat msg: " .. Request.Params["ChatMessage"] ) - local Message = "[WebAdmin]: " .. Request.Params["ChatMessage"] + if( Request.PostParams["ChatMessage"] ~= nil ) then + local Message = "[WebAdmin]: " .. Request.PostParams["ChatMessage"] cRoot:Get():GetServer():SendMessage( Message ) - AddMessage("WebAdmin", Request.Params["ChatMessage"] ) + AddMessage("WebAdmin", Request.PostParams["ChatMessage"] ) return "" end local Content = JavaScript Content = Content .. [[ -
Chat messageessss
+
]] return Content diff --git a/source/cPlugin_NewLua.h b/source/cPlugin_NewLua.h index 1d931530b..20b74b490 100644 --- a/source/cPlugin_NewLua.h +++ b/source/cPlugin_NewLua.h @@ -49,6 +49,8 @@ public: //tolua_export lua_State* GetLuaState() { return m_LuaState; } cWebPlugin_Lua* CreateWebPlugin(lua_State* a_LuaState); //tolua_export + + cCriticalSection & GetCriticalSection() { return m_CriticalSection; } private: bool PushFunction( const char* a_FunctionName, bool a_bLogError = true ); bool CallFunction( int a_NumArgs, int a_NumResults, const char* a_FunctionName ); // a_FunctionName is only used for error messages, nothing else diff --git a/source/cWebPlugin_Lua.cpp b/source/cWebPlugin_Lua.cpp index e22c59961..7d17378f9 100644 --- a/source/cWebPlugin_Lua.cpp +++ b/source/cWebPlugin_Lua.cpp @@ -79,6 +79,7 @@ bool cWebPlugin_Lua::AddTab( const char* a_Title, lua_State * a_LuaState, int a_ std::string cWebPlugin_Lua::HandleRequest( HTTPRequest* a_Request ) { + cCSLock( m_Plugin->GetCriticalSection() ); lua_State* LuaState = m_Plugin->GetLuaState(); std::string RetVal = ""; -- cgit v1.2.3