-
Notifications
You must be signed in to change notification settings - Fork 211
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
mpd: Allow mpd connection via UNIX socket #349
base: master
Are you sure you want to change the base?
Changes from all commits
69a3eb9
9d6ad74
c20eae8
d1332d9
f416604
c4903a9
a697b94
61bdee5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ local io = { lines = io.lines, | |
local rawget = rawget | ||
local table = { sort = table.sort } | ||
|
||
local Gio = require("lgi").Gio | ||
|
||
-- Lain helper functions for internal use | ||
-- lain.helpers | ||
local helpers = {} | ||
|
@@ -134,6 +136,61 @@ end | |
|
||
-- }}} | ||
|
||
|
||
-- {{{ Network functions | ||
|
||
-- Create a recieving buffer | ||
-- NOTE: Not sure whether to export this with helpers or not | ||
-- Probably just gonna leave it local for now | ||
function generate_buffer(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_buffer .. " " | ||
end | ||
|
||
return recv_buffer | ||
end | ||
|
||
-- Wrapper function to send to any sort of address | ||
function helpers.send_to_address(host, port, data, buffer_length, callback) | ||
-- Have a default buffer length of 1000 | ||
if not buffer_length then | ||
buffer_length = 1000 | ||
end | ||
|
||
-- Generate a buffer to store our result in | ||
local recv_buffer = generate_buffer(buffer_length) | ||
|
||
-- Check if we should be sending to a socket or an IP | ||
local is_socket = (string.sub(host, 1, 1) == "/") | ||
|
||
-- Create a client to listen and send with | ||
local client = Gio.SocketClient() | ||
|
||
local addr | ||
|
||
if is_socket then | ||
addr = Gio.UnixSocketAddress.new(host) | ||
else | ||
local inet_addr = gio.InetAddress.new_from_string(host) | ||
addr = Gio.InetSocketAddress.new(inet_addr, port) | ||
end | ||
|
||
local conn = client:connect(addr) | ||
|
||
local input_stream = conn:get_output_stream() | ||
local output_stream = conn:get_input_stream() | ||
|
||
input_stream:write(data) | ||
output_stream:read(recv_buffer) | ||
output_stream:read(recv_buffer) | ||
|
||
callback(recv_buffer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm... this will actually "break Lua". Lua assumes that strings are immutable. So, e.g., if any other piece of code constructed the same number of blanks before, the received data will also end up in there. Also, you should (Note that I just looked over this in a drive-by fashion, @copycat-killer gets to decide things and I have no say in anything here) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @psychon I appreciate any review of yours, for me you are free to come by as you like. |
||
end | ||
-- }}} | ||
|
||
-- {{{ Misc | ||
|
||
-- check if an element exist on a table | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return string.rep(" ", buffer_length + 1)
(the+1
is there to match the behaviour of your code, but I don't know if your code really should do that)