Skip to content
Prev Previous commit
Next Next commit
helpers: Add UNIX socket connection method to helpers
  • Loading branch information
FloatingGhost committed Jul 17, 2017
commit c20eae85658be33c13e799a596afcf69cf81861c
38 changes: 38 additions & 0 deletions helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ local io = { lines = io.lines,
local rawget = rawget
local table = { sort = table.sort }

local gio = require("lgi").Gio
Copy link
Owner

@lcpz lcpz Jul 17, 2017

Choose a reason for hiding this comment

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

Please, rename the variable to Gio, for consistency.


-- Lain helper functions for internal use
-- lain.helpers
local helpers = {}
Expand Down Expand Up @@ -134,6 +136,42 @@ end

-- }}}


-- {{{ Network functions

-- Send some data to a UNIX socket
function helpers.send_to_unix_socket(unix_path, data, buffer_length)

-- First, create an output buffer to recieve from
local recv_buffer = ""
-- We'll need to allocate `buffer_length` bytes to it
for i=1,buffer_length do
recv_buffer = recv_bufer .. " "
end

-- Create a socket to send some data from
local sock = gio.Socket.new(gio.SocketFamily.UNIX,
gio.SocketType.STREAM,
gio.SocketProtocol.DEFAULT)

-- Create a socket address to connect to
local addr = gio.UnixSocketAddress.new(path)

-- Connect across
sock:connect(addr)

-- Send our message
sock:send(data)

-- Pull back anything it sends back
sock:receive(recv_buffer)

return recv_buffer
end


-- }}}

-- {{{ Misc

-- check if an element exist on a table
Expand Down