FFI: How to store a tcp client object in a more efficient way?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
kicknbritt
Citizen
Posts: 71
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

FFI: How to store a tcp client object in a more efficient way?

Post 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.
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
grump
Party member
Posts: 727
Joined: Sat Jul 22, 2017 7:43 pm

Re: FFI: How to store a tcp client object in a more efficient way?

Post 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?
User avatar
kicknbritt
Citizen
Posts: 71
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: FFI: How to store a tcp client object in a more efficient way?

Post 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.
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
grump
Party member
Posts: 727
Joined: Sat Jul 22, 2017 7:43 pm

Re: FFI: How to store a tcp client object in a more efficient way?

Post 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.
User avatar
pgimeno
Party member
Posts: 2614
Joined: Sun Oct 18, 2015 2:58 pm

Re: FFI: How to store a tcp client object in a more efficient way?

Post 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).
User avatar
kicknbritt
Citizen
Posts: 71
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: FFI: How to store a tcp client object in a more efficient way?

Post 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
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
User avatar
kicknbritt
Citizen
Posts: 71
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: FFI: How to store a tcp client object in a more efficient way?

Post 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
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
grump
Party member
Posts: 727
Joined: Sat Jul 22, 2017 7:43 pm

Re: FFI: How to store a tcp client object in a more efficient way?

Post 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.
User avatar
kicknbritt
Citizen
Posts: 71
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: FFI: How to store a tcp client object in a more efficient way?

Post 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.
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
grump
Party member
Posts: 727
Joined: Sat Jul 22, 2017 7:43 pm

Re: FFI: How to store a tcp client object in a more efficient way?

Post 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!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 30 guests