lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

I need an advice from you guys...

I have made a server like application. This server handles multiple TCP connections. Each connection is part of an "application". Each "application" is a Lua script that must contain certain functions that I call from C++ when a message arrives over a TCP connection. The messages that I receive over the TCP connection have a relatively high rate. For each message that I receive I must do sequence of operations
like this:

lua_getglobal(state, "OnSomeMessage"); //get OnSomeMessage function from Lua script
lua_push*(state,...); //add parameter 1
lua_push*(state,...); //add parameter 2
...
lua_push*(state,...); //add parameter n
lua_pcall(state,...);

1. While lua_push* and lua_pcall are (obvious) mandatory, can I make something about lua_getglobal?
I mean cache the function "pointer" somehow...

2. I need a method of keeping some kind of "state" or "session" on each connection. This "session" is opaque from c++ point of view. Must be opaque because the user of the server must write his own Lua scripts and keep his own state particular for each application. For example, I have app1 and app2. App1 must keep an username and a passowrd in his "session" and app2 must keep a counter of some kind.

One approach (working but doesn't feel right...) is to spawn a Lua interpreter for each connection and kill it on connection shutdown. This way, some variables from Lua script never go out of scope and can be used like a session storage. Can this generate memory issues on a large number of connections due to large number of interpreter spawn?

Another method is to spawn an interpreter for each application and find a way to distinguish between connection 1's session and connection 2's session. Anyway, applications are few in number and i can afford to open a Lua interpreter
for each app.

P.S.
I simply love this scripting language! Very powerful, yet very simple to maintain and use :) I wanted to use python, but i was feeling like shooting a little fly with a big bazooka: BIG chance to miss, limited quantity of ammo, and each rocket is EXPENSIVE! :)


Thank you!!!