class MessageBlock{ ... /// int write_char(const char value); int write_short(const short value); int write_int(const int value); int write_string(const char* ss); char read_char(); short read_short(); int read_int(); char* read_string();}int MessageBlock::write_char(const char value){(*this) << value;return 0;}int MessageBlock::write_short(const short value){(*this) << value;return 0;}int MessageBlock::write_int(const int value){(*this) << value;return 0;}int MessageBlock::write_string(const char* ss){(*this) << ss;return 0;}char MessageBlock::read_char(){char a;(*this) >> a;return a;}short MessageBlock::read_short(){short s=0;(*this) >> s;return s;}int MessageBlock::read_int(){int i=0;(*this) >> i;return i;}char* MessageBlock::read_string(){static char ss[150];(*this) >> ss;return ss;}
Extern "C" {# include "lua. h "# include" lualib. h "# include" lauxlib. h "};# include" lua_tinker.h "int GameMonitor: lua_init () {this-> L = lua_open (); luaopen_base (this-> L ); // luaopen_string (this-> L); return 0;} int GameMonitor: lua_class () {// register MessageBlock to luareturn 0;} int GameMonitor: lua_method (){
Return 0;} int GameMonitor: lua_member () {// register some members as needed: return 0;} int GameMonitor: lua_batfile () {// run the script to enter the main logical loop lua_tinker: dofile (this-> L, "LuaScript/msuplc. lua "); return 0 ;}
-- Msg = MessageBlock (1024) ---- msg: write_int (10) ---- print (msg: read_int () ---- a = "sdsdcds" ---- msg: write_string (); ---- print (msg: read_string () local s_msghead = {} local msg
-- Main logic, endless loop, used to process the message local function run_logic () while true domsg = peek_msg () if msg = nil thensleep (1) elseprocess_msg (msg) endendendlocal function process_msg (msg) msg_head = msg: read_int () if s_msghead [msg_head] thens_msghead [msg_head] (msg) endend
-- Register the message processing logic local function init_msghead () s_msghead [10001] = process_10001s_msghead [10002] = process_10002endlocal function process_10002 (msg) msg here. read_int (a); msg. read_string (B); -- processremsg = MessageBlock (50); remsg. write_int (100) remsg. write_string ("OK"); respond_to_client (remsg); end -- process_10001init_msghead () run_logic ()