-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matsievskiy S.V
committed
Apr 6, 2020
1 parent
ff50e59
commit a997294
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#! /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 app = Gio.Application { application_id = 'org.test.tcptest', flags = 'NON_UNIQUE' } | ||
|
||
local service = Gio.SocketService.new() | ||
service:add_address( | ||
Gio.InetSocketAddress.new_from_string("127.0.0.1", 3333), | ||
"STREAM", "TCP") | ||
|
||
local function get_message(conn, istream, ostream) | ||
ostream:async_write("> ") | ||
local bytes = istream:async_read_bytes(4096) | ||
while bytes:get_size() > 0 do | ||
print(string.format("Data: %s", | ||
bytes.data:sub(1, bytes:get_size()-1))) | ||
ostream:async_write(bytes.data) | ||
ostream:async_write("> ") | ||
bytes = istream:async_read_bytes(4096) | ||
end | ||
print("Closing connection") | ||
conn:close() | ||
end | ||
|
||
function service:on_incoming(conn) | ||
local istream = conn:get_input_stream() | ||
local ostream = 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.call(function(ostream) | ||
ostream:async_write("Connected\n") | ||
end)(ostream) | ||
Gio.Async.start(get_message)(conn, istream, ostream) | ||
return false | ||
end | ||
|
||
service:start() | ||
|
||
function app:on_activate() | ||
app:hold() | ||
end | ||
|
||
app:run({arg[0]}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#! /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 Gio = lgi.Gio | ||
|
||
local app = Gio.Application { application_id = 'org.v1993.udptest', flags = 'NON_UNIQUE' } | ||
|
||
local socket = lgi.Gio.Socket.new('IPV4', 'DATAGRAM', 'UDP') | ||
local sa = lgi.Gio.InetSocketAddress.new(Gio.InetAddress.new_loopback('IPV4'), 3333) | ||
assert(socket:bind(sa, true)) | ||
|
||
do | ||
-- To avoid extra allocations | ||
local buf = require("lgi.core").bytes.new(4096) | ||
local source = socket:create_source('IN') | ||
source:set_callback(function() | ||
print('Data incoming') | ||
local len, src = socket:receive_from(buf) | ||
if len > 0 then | ||
print(('%s:%d %s'):format(src:get_address():to_string(), src:get_port(), tostring(buf):sub(1, len))) | ||
else | ||
print('Failed to read data') | ||
end | ||
return true | ||
end) | ||
|
||
source:attach(lgi.GLib.MainContext.default()) | ||
end | ||
|
||
function app:on_activate() | ||
app:hold() | ||
end | ||
|
||
app:run({arg[0]}) |