Cuberite Forum
Network API - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Cuberite (https://forum.cuberite.org/forum-4.html)
+--- Forum: Development (https://forum.cuberite.org/forum-13.html)
+--- Thread: Network API (/thread-1700.html)

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15


RE: Network API - NiLSPACE - 02-14-2015

I was playing with the network API, but I can't find a way to close a TCPLink's connection.


RE: Network API - xoft - 02-14-2015

Yes, I forgot about that. Will fix tonight on train back to prague.


RE: Network API - NiLSPACE - 02-14-2015

Thanks in advance Smile


RE: Network API - xoft - 02-14-2015

Close() is implemented; Shutdown() is somewhat problematic - LibEvent keeps the data in a buffer, so when you shut down a socket, you lose whatever LibEvent hasn't sent yet. I'm trying to find a solution to this, because it affects all connections a lot.


RE: Network API - NiLSPACE - 02-15-2015

I really like the network API Smile I now have a small webserver where you can use <?lua LuaCode ?> to add code. It even detects if it's a POST or GET request in the code ;D

I even found a use for Lua Coroutines ;D


RE: Network API - NiLSPACE - 02-15-2015

Made a simple login/logout page:
<?lua
Client, Link = ...


local Message = ""

-- User send a post, so he probably wants to login or logout
if (Client.RequestMethod == "POST") then
	if (Client.POST['logout'] ~= nil) then
		-- The user wants to log out
		Client.SESSION['loggedin'] = false;
		Message = "You logged out"
	elseif (Client.POST['login'] ~= nil) then
		-- The user tries to log in.
		local UserName = Client.POST['username']
		local Password = Client.POST['password']
		
		if (UserName == "admin" and Password == "admin") then
			-- password and username is correct
			Client.SESSION['loggedin'] = true
			Message = "You logged in"
		else
			-- Wrong username or password
			Message = "You used the wrong username or password"
		end
	end
end


local IsLoggedIn = Client.SESSION['loggedin'];
local Form = [[
<input type="text" name="username" placeholder="username"><br>
<input type="password" name="password" placeholder="password"><br>
<input type="submit" name="login" value="login" >]]

if (IsLoggedIn) then
	Form = '<input type="submit" name="logout" value="logout">'
end

?>
<!DOCTYPE html>
<html>
<head>
</head>

<body>
	<?lua
do -- Do - end isn't needed, but it makes it easier to notice where small bits of lua code are.
	Link:Send(Message) 
end
	?>
	<form method="POST">
<?lua
do -- Do - end isn't needed, but it makes it easier to notice where small bits of lua code are.
	Link:Send(Form)
end
?>
	</form>
</body
</html>



RE: Network API - NiLSPACE - 02-15-2015

Is it possible that the cTCPLink:Close function closes the connection before some data that was send is received? I now have to use
cRoot:Get():GetDefaultWorld():QueueTask(
	function()
		a_TCPLink:Close()
	end
)

Because otherwise the browser won't receive anything.

Oh I see in the APIDesc file that I have to use cTCPLink:Shutdown because cTCPLink:Close could do exacly what I just mentioned Wink


RE: Network API - xoft - 02-15-2015

Exactly Smile


RE: Network API - xoft - 02-16-2015

I'm thinking about UDP support. Since UDP doesn't have the concept of a persistent connection, I think the only interface needed would be an "Endpoint", which can be attached to a port number and either receive or send UDP datagrams.

The interface:
- cNetwork:CreateUDPEndpoint(Port, UDPCallbacks) -> cUDPEndpoint object

cUDPEndpoint methods:
- IsOpen()
- GetPort()
- Send(Data, Host, Port) - sends a datagram with the specified data payload to the specified host/port
- EnableBroadcasts() - enables the endpoint to send broadcasts (using special host/port combinations)
- Close()

UDPCallbacks:
- OnError(ErrorCode, ErrorMsg)
- OnReceivedData(Data, Host, Port) - A datagram with the specified Data payload has been received from the specified host/port


RE: Network API - jan64 - 02-16-2015

Sounds good.