Class: Termfront::Network::Connection

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

Defined Under Namespace

Classes: PeerInfo

Constant Summary collapse

MAX_MSG_BYTES =
64 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



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

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.



23
24
25
# File 'lib/termfront/network/connection.rb', line 23

def peer_info
  @peer_info
end

#rttObject (readonly)

Returns the value of attribute rtt.



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

def rtt
  @rtt
end

Instance Method Details

#closeObject



97
98
99
100
101
102
103
104
105
# File 'lib/termfront/network/connection.rb', line 97

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

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



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

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)


107
108
109
# File 'lib/termfront/network/connection.rb', line 107

def connected?
  !@sock.nil?
end

#ping(now) ⇒ Object



111
112
113
114
# File 'lib/termfront/network/connection.rb', line 111

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

#receiveObject



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
89
90
91
92
93
94
95
# File 'lib/termfront/network/connection.rb', line 59

def receive
  return [] unless @sock

  messages = []

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

      if @buf.bytesize > MAX_MSG_BYTES
        close
        break
      end

      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



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

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



116
117
118
# File 'lib/termfront/network/connection.rb', line 116

def socket
  @sock
end