Forum

> > CS2D > Scripts > Doubt with LuaSocket
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Doubt with LuaSocket

6 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Doubt with LuaSocket

Starkkz
Moderator Off Offline

Zitieren
I'm trying to make a connection system but i have a problem with the socket.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mySocket = socket.tcp()
mySocket:bind("*",3784)
mySocket:settimeout(0) -- I SET THE TIMEOUT
mySocket:listen(0)

Client = {}
addhook("always","receiveClients")
function receiveClients()
	local cl = mySocket.accept()
	if cl then
		table.insert(Client,cl)
	end
	for id, c in pairs(Client) do
		-- This part blocks the socket
		local Line,err = c.socket:receive("*l") -- We get a line respecting the timeout
		if Line then
		elseif err then
			error(err)
		end
		Client[id] = c
	end
end

The socket get my external client, but my problem is that it blocks the system (gets frozen) instead of wait the timeout (What should be 0 - means do not wait). And i have to close the external connection to let the game continue working. Some help?

alt Re: Doubt with LuaSocket

Apache uwu
User Off Offline

Zitieren
user Starkkz hat geschrieben
I'm trying to make a connection system but i have a problem with the socket.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mySocket = socket.tcp()
mySocket:bind("*",3784)
mySocket:settimeout(0) -- I SET THE TIMEOUT
mySocket:listen(0)

Client = {}
addhook("always","receiveClients")
function receiveClients()
	local cl = mySocket.accept()
	if cl then
		table.insert(Client,cl)
	end
	for id, c in pairs(Client) do
		-- This part blocks the socket
		local Line,err = c.socket:receive("*l") -- We get a line respecting the timeout
		if Line then
		elseif err then
			error(err)
		end
		Client[id] = c
	end
end

The socket get my external client, but my problem is that it blocks the system (gets frozen) instead of wait the timeout (What should be 0 - means do not wait). And i have to close the external connection to let the game continue working. Some help?


You're placing LuaSocket on the same state cs2d's lua is running on. If hook_always is blocked then cs2d will be blocked.

Instead you must place LuaSocket on another lua state. Of course this is very risky and I don't recommend manipulating states like this, but if you want to try you can go and download it here.

alt Re: Doubt with LuaSocket

Lee
Moderator Off Offline

Zitieren
Sockets are managed by the operating system, not lua. If you try to bind two sockets from the same state, it'd be perfectly legal as lua only holds a pointer to the file descriptor from which the socket operates from.

Your script is blocking because by specification, a 0 timeout means no timeout, which means that a socket will block forever if it never receives data.

alt Re: Doubt with LuaSocket

Apache uwu
User Off Offline

Zitieren
user Lee hat geschrieben
Sockets are managed by the operating system, not lua. If you try to bind two sockets from the same state, it'd be perfectly legal as lua only holds a pointer to the file descriptor from which the socket operates from.

Your script is blocking because by specification, a 0 timeout means no timeout, which means that a socket will block forever if it never receives data.


LuaSocket IS managed by the os however lua waits until the process is completed. (Similar os.execute,io.open,etc)

alt Re: Doubt with LuaSocket

Starkkz
Moderator Off Offline

Zitieren
Thank you guys, but i found my mistake. I should put the settimeout to the client and not to the server, that was what blocked the server.

alt Re: Doubt with LuaSocket

Lee
Moderator Off Offline

Zitieren
user Apache uwu hat geschrieben
LuaSocket IS managed by the os however lua waits until the process is completed. (Similar os.execute,io.open,etc)


Similarly, opening another lua state via lua (even if through the API) requires an execution reference in the original state. Assuming that rings use the thread paradigm, querying for information from slave threads/states must be blocking as well. Hence, if socket IO blocks in the slave, the slave.join operation will also block in the parent thread, else querying the slave will not be meaningful.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht