Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add TCP and UDP server samples #243

Merged
merged 2 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions samples/tcpsrv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#! /usr/bin/env lua

--
-- Sample TCP echo server. Listens on 3333 port
-- Use netcat to send strings to server (ncat 127.0.0.1 3333)
--

local lgi = require 'lgi'
local Gio = lgi.Gio
local assert = lgi.assert

local app = Gio.Application{application_id = 'org.lgi.samples.tcptest',
flags = Gio.ApplicationFlags.NON_UNIQUE}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a Gio.Application? I have to admit, I do not really know what this is doing, but I would just remove the app.
(Especially with the NON_UNIQUE argument... what does Gio.Application do?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly everything should be using Gio.Application, manually dealing with the mainloop isn't helpful.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, what does GApplication do that GLib.MainLoop.new():run() does not do? I really have no idea.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most examples use Gio.Application. Probably because syntax is the same as for Gtk.Application.

Copy link
Contributor

@TingPing TingPing Apr 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the context of a demo, nothing. In real world applications, single instance support, daemonization, argument parsing, gresource loading, global menus, exporting the freedesktop application dbus interface, its just the API applications should use.


local service = Gio.SocketService.new()
service:add_address(
Gio.InetSocketAddress.new_from_string("127.0.0.1", 3333),
Gio.SocketType.STREAM,
Gio.SocketProtocol.TCP)

local function do_echo_connection(conn, istream, ostream)
local wrote, err = ostream:async_write_all("Connected\n")
assert(wrote >= 0, err)
while true do
local bytes = assert(istream:async_read_bytes(4096))
if bytes:get_size() == 0 then break end
local data = bytes.data:gsub("\n", "")
print(string.format("Data: %s", data))
wrote, err = ostream:async_write_all(bytes.data)
assert(wrote >= 0, err)
end
print("Closing connection")
conn:async_close()
end

function service:on_incoming(conn)
local istream = assert(conn:get_input_stream())
local ostream = assert(conn:get_output_stream())
local rc = conn:get_remote_address()
print(string.format("Incoming connection from %s:%s",
rc:get_address():to_string(),
rc:get_port()))
Gio.Async.start(do_echo_connection)(conn, istream, ostream)
return false
end

function app:on_activate()
app:hold()
end

app:run({arg[0]})
46 changes: 46 additions & 0 deletions samples/udpsrv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#! /usr/bin/env lua

--
-- Sample UDP server. Listens on 3333 port
-- Use netcat to send strings to server (ncat -u 127.0.0.1 3333)
--

local lgi = require 'lgi'
local core = require 'lgi.core'
local GLib = lgi.GLib
local Gio = lgi.Gio
local assert = lgi.assert

local app = Gio.Application{application_id = 'org.lgi.samples.udptest',
flags = Gio.ApplicationFlags.NON_UNIQUE}

local socket = Gio.Socket.new(Gio.SocketFamily.IPV4,
Gio.SocketType.DATAGRAM,
Gio.SocketProtocol.UDP)
local sa = Gio.InetSocketAddress.new_from_string("127.0.0.1", 3333)
assert(socket:bind(sa, true))

local buf = core.bytes.new(4096)
local source = assert(socket:create_source(GLib.IOCondition.IN))

source:set_callback(function()
local len, src = assert(socket:receive_from(buf))
if len > 0 then
local data = tostring(buf):sub(1, len):gsub("\n", "")
print(string.format("%s:%d %s",
src:get_address():to_string(),
src:get_port(),
data))
else
print('Failed to read data')
end
return true
end)

source:attach(GLib.MainContext.default())

function app:on_activate()
app:hold()
end

app:run({arg[0]})