Class: Termfront::Network::Connection

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

Defined Under Namespace

Classes: PeerInfo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



23
24
25
26
27
28
29
# File 'lib/termfront/network/connection.rb', line 23

def initialize
  @sock = nil
  @buf = +""
  @ping_ts = 0
  @rtt = 0
  @peer_info = nil
end

Instance Attribute Details

#peer_infoObject (readonly)

Returns the value of attribute peer_info.



21
22
23
# File 'lib/termfront/network/connection.rb', line 21

def peer_info
  @peer_info
end

#rttObject (readonly)

Returns the value of attribute rtt.



20
21
22
# File 'lib/termfront/network/connection.rb', line 20

def rtt
  @rtt
end

Instance Method Details

#closeObject



90
91
92
93
94
95
96
97
98
# File 'lib/termfront/network/connection.rb', line 90

def close
  begin
    @sock&.close
  rescue StandardError
    nil
  end
  @sock = nil
  @peer_info = nil
end

#connect(host, port, ca_file: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/termfront/network/connection.rb', line 31

def connect(host, port, ca_file: nil)
  tcp = TCPSocket.new(host, port)
  tcp.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

  ctx = build_ssl_context(ca_file: ca_file)
  @sock = OpenSSL::SSL::SSLSocket.new(tcp, ctx)
  @sock.hostname = host if @sock.respond_to?(:hostname=)
  @sock.sync = true
  @sock.connect
  @sock.post_connection_check(host)
  @peer_info = build_peer_info(@sock.peer_cert)
rescue StandardError
  tcp&.close
  @sock = nil
  @peer_info = nil
  raise
end

#connected?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/termfront/network/connection.rb', line 100

def connected?
  !@sock.nil?
end

#ping(now) ⇒ Object



104
105
106
107
# File 'lib/termfront/network/connection.rb', line 104

def ping(now)
  @ping_ts = now
  send_msg({ t: "ping", ts: (now * 1000).to_i })
end

#receiveObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/termfront/network/connection.rb', line 57

def receive
  return [] unless @sock

  messages = []

  while IO.select([@sock], nil, nil, 0)
    begin
      data = @sock.read_nonblock(4096)
      @buf << data

      while (nl = @buf.index("\n"))
        line = @buf.slice!(0, nl + 1)
        begin
          msg = JSON.parse(line, symbolize_names: true)
          if msg[:t] == "pong"
            @rtt = ((clock - @ping_ts) * 1000).to_i if @ping_ts > 0
          else
            messages << msg
          end
        rescue JSON::ParserError
          next
        end
      end
    rescue IO::WaitReadable
      break
    rescue EOFError, Errno::ECONNRESET, IOError, OpenSSL::SSL::SSLError
      break
    end
  end

  messages
end

#send_msg(hash) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/termfront/network/connection.rb', line 49

def send_msg(hash)
  return unless @sock

  @sock.write(JSON.generate(hash) + "\n")
rescue Errno::EPIPE, Errno::ECONNRESET, IOError, OpenSSL::SSL::SSLError
  nil
end

#socketObject



109
110
111
# File 'lib/termfront/network/connection.rb', line 109

def socket
  @sock
end