Forum

> > CS2D > Scripts > Doubt with LuaSocket
Forums overviewCS2D overview Scripts overviewLog in to reply

English Doubt with LuaSocket

6 replies
To the start Previous 1 Next To the start

old Doubt with LuaSocket

Starkkz
Moderator Off Offline

Quote
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?

old Re: Doubt with LuaSocket

Apache uwu
User Off Offline

Quote
user Starkkz has written
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.

old Re: Doubt with LuaSocket

Lee
Moderator Off Offline

Quote
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.

old Re: Doubt with LuaSocket

Apache uwu
User Off Offline

Quote
user Lee has written
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)

old Re: Doubt with LuaSocket

Starkkz
Moderator Off Offline

Quote
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.

old Re: Doubt with LuaSocket

Lee
Moderator Off Offline

Quote
user Apache uwu has written
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.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview