Sha256: 3ae26fe775bb0d3a35de3d838f0dc99d1dcdc2ca9576187b286faba8f242cad5

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require_relative '../dtas'
require_relative 'xs'
require 'socket'
require 'io/wait'
require 'shellwords'

class DTAS::UNIXClient # :nodoc:
  attr_reader :to_io

  include DTAS::XS

  def self.default_path
    (ENV["DTAS_PLAYER_SOCK"] || File.expand_path("~/.dtas/player.sock")).b
  end

  def initialize(path = self.class.default_path)
    @to_io = Socket.new(:AF_UNIX, :SOCK_SEQPACKET, 0)
    @to_io.connect(Socket.pack_sockaddr_un(path))
  end

  def req_start(args)
    args = xs(args) if Array === args
    @to_io.send(args, Socket::MSG_EOR)
  end

  def req_ok(args, timeout = nil)
    res = req(args, timeout)
    res == "OK" or raise "Unexpected response: #{res}"
    res
  end

  def req(args, timeout = nil)
    req_start(args)
    res_wait(timeout)
  end

  def res_wait(timeout = nil)
    @to_io.wait(timeout)
    nr = @to_io.nread
    nr > 0 or raise EOFError, "unexpected EOF from server"
    @to_io.recvmsg[0]
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dtas-0.5.0 lib/dtas/unix_client.rb
dtas-0.4.0 lib/dtas/unix_client.rb
dtas-0.3.0 lib/dtas/unix_client.rb