Class: ITerm2::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/iterm2/connection.rb

Constant Summary collapse

SOCKET_PATH =
File.expand_path("~/Library/Application Support/iTerm2/private/socket")
TCP_PORT =
1912

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name: "iterm2_ruby") ⇒ Connection

Returns a new instance of Connection.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/iterm2/connection.rb', line 16

def initialize(app_name: "iterm2_ruby")
  @app_name = app_name
  @id_counter = 0
  @connected = false
  @dispatch_active = false
  @mutex = Mutex.new
  @write_mutex = Mutex.new
  @pending_responses = {}
  @notification_callback = nil
  @reader_thread = nil
  connect!
end

Instance Attribute Details

#connectedObject (readonly)

Returns the value of attribute connected.



14
15
16
# File 'lib/iterm2/connection.rb', line 14

def connected
  @connected
end

Instance Method Details

#closeObject



77
78
79
80
81
82
83
84
85
# File 'lib/iterm2/connection.rb', line 77

def close
  if @dispatch_active
    stop_dispatch_loop!
  else
    send_close if @socket && !@socket.closed?
    @socket&.close rescue nil
  end
  @connected = false
end

#dispatch_active?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/iterm2/connection.rb', line 68

def dispatch_active?
  @dispatch_active
end

#next_idObject



38
39
40
# File 'lib/iterm2/connection.rb', line 38

def next_id
  @mutex.synchronize { @id_counter += 1 }
end

#on_notification(&block) ⇒ Object

Register callback for incoming notifications



73
74
75
# File 'lib/iterm2/connection.rb', line 73

def on_notification(&block)
  @notification_callback = block
end

#rpc(request) ⇒ Object

Send request, get response. Works in both sync and dispatch mode.



30
31
32
33
34
35
36
# File 'lib/iterm2/connection.rb', line 30

def rpc(request)
  if @dispatch_active
    rpc_dispatched(request)
  else
    rpc_sync(request)
  end
end

#send_request(request) ⇒ Object

Make send_binary accessible for client to send subscription requests while dispatch loop handles recv



89
90
91
92
# File 'lib/iterm2/connection.rb', line 89

def send_request(request)
  encoded = Proto::ClientOriginatedMessage.encode(request)
  @write_mutex.synchronize { send_binary(encoded) }
end

#start_dispatch_loop!Object

Start background reader thread for notification dispatch



43
44
45
46
47
48
49
# File 'lib/iterm2/connection.rb', line 43

def start_dispatch_loop!
  return if @dispatch_active

  @dispatch_active = true
  @reader_thread = Thread.new { dispatch_loop }
  @reader_thread.abort_on_exception = true
end

#stop_dispatch_loop!Object

Stop background reader thread cooperatively



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/iterm2/connection.rb', line 52

def stop_dispatch_loop!
  return unless @dispatch_active

  @dispatch_active = false
  # Close socket to unblock recv_binary — dispatch_loop rescues IOError and exits
  @socket&.close rescue nil
  @reader_thread&.join(2)
  @reader_thread = nil

  # Wake any blocked RPCs
  @mutex.synchronize do
    @pending_responses.each_value { |q| q.push(nil) }
    @pending_responses.clear
  end
end