Original Post
So reading through http://www.ozone3d.n...cket_lib_p4.php
Looking at the code for the server and had a few questions hoping someone has experience with:
In the article he states this method could be placed in the main game loop. That makes sense but I fail to see why this needs to be setup as a coroutine if that's the case. After each loop of this method it yields back out. How would that be any different than just running this function without the while loop and without it being a coroutine? It sets it's timeout to 10ms and no matter if it's a coroutine or not the program will end up waiting 10ms.
Also this code looks like it accepts a connection and reads the data. In TCP isn't the connection persistent? Here he is accepting and getting the data then closing. That doesn't seem logical for a real game. So does anyone know how to manage connected clients and how to determine disconnected clients with this library? I would think accept() gives you some sort of unique id that I could store off and receive() would give me that same id so I know who it came from and send() would allow me to either broadcast or send to a specific id.
Looking at the code for the server and had a few questions hoping someone has experience with:
In the article he states this method could be placed in the main game loop. That makes sense but I fail to see why this needs to be setup as a coroutine if that's the case. After each loop of this method it yields back out. How would that be any different than just running this function without the while loop and without it being a coroutine? It sets it's timeout to 10ms and no matter if it's a coroutine or not the program will end up waiting 10ms.
Also this code looks like it accepts a connection and reads the data. In TCP isn't the connection persistent? Here he is accepting and getting the data then closing. That doesn't seem logical for a real game. So does anyone know how to manage connected clients and how to determine disconnected clients with this library? I would think accept() gives you some sort of unique id that I could store off and receive() would give me that same id so I know who it came from and send() would allow me to either broadcast or send to a specific id.
function runTCPServer()
local stop_server = 0;
local num_cnx = 0;
while( stop_server==0 ) do
local err = "";
local tcp_client = 0;
-- Set a timeout of 10 ms for accept().
--
g_tcp_server:settimeout(0.01);
tcp_client, err = g_tcp_server:accept();
if tcp_client~=nil then
-- client message must be terminated by
-- terminated by a LF character (ASCII 10),
-- optionally preceded by a CR character (ASCII 13).
--
g_client_msg, err = tcp_client:receive('*l');
if( g_client_msg~=nil ) then
if(g_client_msg=="STOP") then
stop_server = 1;
else
-- tcp_client:send( "Demoniak3D TCP Server Echo: " .. g_client_msg);
end
end
if tcp_client~=nil then
tcp_client:close();
end
end
-- Yield execution.
--
coroutine.yield();
end
end