Serialize lua_State to send over the network

I need to send lua_state to the server using Sockets in C ++. How can I serialize lua_State so it can be sent over the network?

+4
source share
3 answers

Depending on your needs, you have several options. You can try using the Pluto Library . This is a "heavy" serialization library:

Pluto is a library that allows users to write arbitrarily large parts of the “Lua Universe” to a flat file, and then read them back into the same or different unit Lua. Object references are handled appropriately so that the file contains everything needed to recreate the objects in question.

You can also try lper , which uses Linux persistent memory .

Please note that you will have problems sending custom C functions and user data ...

If you really don't need to send whole lua_State (why do you need it?), You can take a look at TableSerialization in the Wiki Lua-users. Perhaps you can solve your problem by sending serialized (possibly large) Lua tables containing all the "state" you need.

+3
source

Serializing a full lua_State fundamentally impossible. After all, even if you can transfer memory stored in one, lua_State has many C functions associated with them. How can you serialize one of them over the network?

The best you can hope for is to try to remember what you did in one Lua state, and tell the program over the network to do the same. To do this, you need to write an abstraction of the Lua interface, which you call instead of the Lua interface. It will report all the actions you take for the network program. Downloading the file also had to transfer this file to the network program.

Basically, you need to take every Lua function and write a new version that calls the old one and tells the network program what you are doing.

+3
source

Well, I don't know how you will pass the actual lua_state through the socket. Perhaps you can extract the information contained in lua_state and then pass the extracted information through the socket?

 std::string name(lua_tostring(L,1)); int age = lua_tonumber(L,2); //then send name and string over the socket somehow... 

And if you have a response from the socket that you want to forward to lua, just do something like

 //get response from socket and push response to lua lua_pushnumber(L, response); return 1; //indicate how arguments you are returning. 

Hope this helps. Good luck

0
source

Source: https://habr.com/ru/post/1395647/


All Articles