Page 1 of 2
FFI: How to store a tcp client object in a more efficient way?
Posted: Tue Apr 20, 2021 11:07 pm
by kicknbritt
I would like to store a tcp client object returned by luasocket inside of a struct using ffi.
I tried to just store the variable in the struct like so:
Code: Select all
ffi = require 'ffi'
socket = require("socket")
ffi.cdef[[
typedef struct {
void *Tcp;
} ClientData;
void* malloc(size_t);
void free(void*);
]]
local Tcp = socket.bind("*", 12345)
local ptr = ffi.C.malloc(ffi.sizeof("void*"))
while true do
local NewClient, Error = Tcp:accept()
if NewClient ~= nil then
local ClientData = ffi.cast("ClientData(&)", ptr)
ClientData.Tcp = NewClient
ClientData.Tcp:send("Hello World\n")
end
end
But I would up with this error:
test2.lua:30: 'void *' has no member named 'send'
Can anyone help me?
And if there is no way to store a tcp client object in ffi, does anyone know of any other ways to use tcp with a lower amount of memory?
FYI, I plan on wrapping this tcp socket in a ssl connection with luasec or some other lua-based security.
Re: FFI: How to store a tcp client object in a more efficient way?
Posted: Wed Apr 21, 2021 1:57 am
by grump
You can't reference Lua objects through ffi pointers, or store them in ffi-allocated memory. Even if you could, the idea that the same object would then somehow require less memory makes no sense.
What exactly are you trying to do that their size becomes a concern?
Re: FFI: How to store a tcp client object in a more efficient way?
Posted: Wed Apr 21, 2021 2:40 am
by kicknbritt
I am trying to make a socket-based server that can handle alot of clients.
My concern is that the lua memory limit will not be enough in a 'worst case' scenario.
It seems like I have kind of hit a wall here.
I did some researching and it seems like I can set up my own socket lib in c, so I might just do that at some point in the near future.
Re: FFI: How to store a tcp client object in a more efficient way?
Posted: Wed Apr 21, 2021 10:27 am
by grump
kicknbritt wrote: ↑Wed Apr 21, 2021 2:40 am
I did some researching and it seems like I can set up my own socket lib in c, so I might just do that at some point in the near future.
But the socket lib in LÖVE
is a C library, and socket userdata are probably lightweight already. Doing better than that is going to be difficult. And I think you will face other limits long before memory consumption of sockets becomes a concern, depending on what this server is supposed to do.
Re: FFI: How to store a tcp client object in a more efficient way?
Posted: Wed Apr 21, 2021 10:35 am
by pgimeno
Wait for Löve 11.4, it will come with LuaJIT 2.1, which supports much more memory in 64-bit systems (I've seen 128 TB mentioned, not sure if that's accurate).
Re: FFI: How to store a tcp client object in a more efficient way?
Posted: Thu Apr 22, 2021 8:42 am
by kicknbritt
I mean i can get up to 500,000 tcp clients max which is far, far below what I was expecting ( I am shooting for 100,000,000 clients max which is way overkill) but I am expecting at least a few million max.
Wait I am using LuaJit 2.1 already... What
Re: FFI: How to store a tcp client object in a more efficient way?
Posted: Thu Apr 22, 2021 8:44 am
by kicknbritt
You know what I just realized that I was using zbstudio to prototype my script for creating client connections and completely forgot that it was running an older version of luajit lol
Re: FFI: How to store a tcp client object in a more efficient way?
Posted: Thu Apr 22, 2021 9:52 am
by grump
kicknbritt wrote: ↑Thu Apr 22, 2021 8:42 am
I am shooting for 100,000,000 clients max
lmao, that's an ambitious goal. Time to rent a server farm then, because a single machine is not going to handle that.
If all of these clients send just one measly byte per second, that's at least 50 Gb/s traffic. The memory required just for these sockets is in the hundreds of GBs. A single machine would only have a handful of CPU cycles to service all these clients. Entirely unrealistic. There is not a single server in the world that handles even 500,000 clients at once.
Divide your number by 10,000 and you might be able to pull it off, and even then Lua is not the language you want to use for this.
Re: FFI: How to store a tcp client object in a more efficient way?
Posted: Thu Apr 22, 2021 11:22 pm
by kicknbritt
Shoot for the stars, land on the moon.
I send updates every 100ms (max of 512 bytes (header included)), that is a maximum of 5120 a second, Plus other tcp updates that will be sent lets say 6000 a second.
6000 a second for 10 million clients is 60 Gbps. AWS supports instances up to 100 Gbps.
CPU wise I am perfectly fine. The amount of calculations I am doing basically amount to a bullet hell game with a small lobby of players.
All LOS is tile-based and precalculated, AI uses way points for movement (I might switch to precalculated paths). And I have up to 128 cores to throw at all these matches.
Again the only thing that is an issue is socket performance which I have not tested extensively. (500,000 sockets is about 2GB)
But client will hardly use their tcp connection in-game anyway, and apparently there are other more performant socket libs for lua as well.
Re: FFI: How to store a tcp client object in a more efficient way?
Posted: Fri Apr 23, 2021 5:26 am
by grump
kicknbritt wrote: ↑Thu Apr 22, 2021 11:22 pm
6000 a second for 10 million clients is 60 Gbps.
Nope, that's 480 Gbps. Gbps = Billion
bits per second.
I like your ambition. This game is gonna break several world records and will be one for the history books. Good luck!