Class: Nova::Starbound::Protocol

Inherits:
Object
  • Object
show all
Includes:
Encryption, Messages, Socket
Defined in:
lib/nova/starbound/protocol.rb,
lib/nova/starbound/protocol/packet.rb,
lib/nova/starbound/protocol/socket.rb,
lib/nova/starbound/protocol/messages.rb,
lib/nova/starbound/protocol/encryption.rb

Overview

TODO:

More testing.

The basic Starbound protocol.

Defined Under Namespace

Modules: Encryption, Messages, Socket Classes: Packet

Instance Attribute Summary collapse

Attributes included from Encryption

#encryption_provider

Attributes included from Socket

#current_packet_id, #queue, #socket

Instance Method Summary collapse

Methods included from Messages

#check_versions, #handle_encryption, #handle_server_encryption, #wait_for_protocol_version

Methods included from Socket

#callbacks, #loop, #on, #read, #respond_to, #response_to, #run?, #run_callback, #send, #thread, #threaded?, #write_packet

Constructor Details

#initialize(options = {}) ⇒ Protocol

Initialize the protocol.

Parameters:

  • options (Hash) (defaults to: {})

    the options to initialize the protocol with.



58
59
60
61
62
63
# File 'lib/nova/starbound/protocol.rb', line 58

def initialize(options = {})
  @options = options
  @state   = :offline

  super()
end

Instance Attribute Details

#optionsHash<Symbol, Object> (readonly)

The options that was passed to this protocol on initialization.

Returns:

  • (Hash<Symbol, Object>)


24
25
26
# File 'lib/nova/starbound/protocol.rb', line 24

def options
  @options
end

#stateSymbol (readonly)

The current state of the protocol. Known values: :offline (default), :handshake, :online, :closing.

Returns:

  • (Symbol)


30
31
32
# File 'lib/nova/starbound/protocol.rb', line 30

def state
  @state
end

Instance Method Details

#close(code = :none) ⇒ void

This method returns an undefined value.

Closes the connection.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/nova/starbound/protocol.rb', line 68

def close(code = :none)
  @state = :closing

  if code
    send :close, Packet::CloseReasons[code].to_s
  end

  super()

  @state = :offline
end

#default_behaviorDefaultBehavior

Sets up default behaviors within this protocol.

Returns:



83
84
85
# File 'lib/nova/starbound/protocol.rb', line 83

def default_behavior
  @_default_behavior ||= DefaultBehavior.new(self)
end

#handshakevoid

This method returns an undefined value.

Perform a handshake with the server. First sets the state to :handshake.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nova/starbound/protocol.rb', line 36

def handshake
  @state = :handshake
  thread

  if options[:type] == :client
    message  = send :protocol_version, Nova::VERSION
    response = response_to message
    check_versions response
    handle_encryption
  else

    wait_for_protocol_version
    handle_server_encryption
  end

  @state = :online
end