Class: KRPC::Connection

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

Overview

A TCP Connection.

Direct Known Subclasses

RPCConnection, StreamConnection

Constant Summary collapse

DEFAULT_SERVER_HOST =
"127.0.0.1"
DEFAULT_SERVER_RPC_PORT =
50000
DEFAULT_SERVER_STREAM_PORT =
50001

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Connection

Returns a new instance of Connection.



16
17
18
# File 'lib/krpc/connection.rb', line 16

def initialize(host, port)
  @host, @port = host, port
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#socketObject (readonly)

Returns the value of attribute socket.



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

def socket
  @socket
end

Instance Method Details

#cleanupObject



50
# File 'lib/krpc/connection.rb', line 50

def cleanup; end

#closeObject

Close connection and clean up.



36
37
38
39
40
41
42
# File 'lib/krpc/connection.rb', line 36

def close
  if connected?
    socket.close
    cleanup
    true
  else false end
end

#connectObject

Connect and perform handshake.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/krpc/connection.rb', line 21

def connect
  if connected? then raise(ConnectionError, "Already connected")
  else
    @socket = TCPSocket.open(host, port)
    begin
      handshake
    rescue Exception => e
      close
      raise e
    end
  end
  self
end

#connected?Boolean

Return ‘true` if connected to a server, `false` otherwise.

Returns:



45
46
47
# File 'lib/krpc/connection.rb', line 45

def connected?
  !socket.nil? && !socket.closed?
end

#handshakeObject



49
# File 'lib/krpc/connection.rb', line 49

def handshake; end

#protobuf_handshake(type, **attrs) ⇒ Object

Raises:



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

def protobuf_handshake(type, **attrs)
  send_message PB::ConnectionRequest.new(type: type, **attrs)
  resp = receive_message PB::ConnectionResponse
  raise(ConnectionError, "#{resp.status} -- #{resp.message}") unless resp.status == :OK
  resp
end

#receive_message(msg_type) ⇒ Object



80
81
82
83
84
# File 'lib/krpc/connection.rb', line 80

def receive_message(msg_type)
  msg_length = recv_varint
  msg_data = recv(msg_length)
  msg_type.decode(msg_data)
end

#recv(maxlen = 1) ⇒ Object



66
67
68
# File 'lib/krpc/connection.rb', line 66

def recv(maxlen = 1)
  maxlen == 0 ? "" : @socket.read(maxlen)
end

#recv_varintObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/krpc/connection.rb', line 69

def recv_varint
  int_val = 0
  shift = 0
  loop do
    byte = recv.ord
    int_val |= (byte & 0b0111_1111) << shift
    return int_val if (byte & 0b1000_0000) == 0
    shift += 7
    raise(RuntimeError, "too many bytes when decoding varint") if shift >= 64
  end
end

#send(data) ⇒ Object



59
60
61
# File 'lib/krpc/connection.rb', line 59

def send(data)
  @socket.send(data, 0)
end

#send_message(msg) ⇒ Object



62
63
64
# File 'lib/krpc/connection.rb', line 62

def send_message(msg)
  send Encoder.encode_message_with_size(msg)
end