Class: RuneRb::Network::Endpoint

Inherits:
Object
  • Object
show all
Includes:
System::Log
Defined in:
app/network/endpoint.rb

Overview

An Endpoint object accepts Socket connections via TCP/IP, creates RuneRb::Network::Session objects from the accepted TCPSockets, and then transfers context to the RuneRb::Network::Sessions.

Author:

  • Patrick W.

Since:

  • 0.9.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from System::Log

#class_name, #err, #err!, #log, #log!, #symbolize_file

Constructor Details

#initialize(target: nil) ⇒ Endpoint

Constructs a new Endpoint object

Parameters:

target:
  The target which will receive sessions from the Endpoint.

Since:

  • 0.9.3


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/network/endpoint.rb', line 21

def initialize(target: nil)
  parse_config
  setup_pipeline
  @server = TCPServer.new(@host, @port)
  @sessions = {}
  @node = Fiber.new do
    loop do
      break if @closed

      @pipeline[:process].resume
    end
  end
  @start = { time: Process.clock_gettime(Process::CLOCK_MONOTONIC), stamp: Time.now }
  log RuneRb::GLOBAL[:COLOR].blue("Endpoint constructed: #{RuneRb::GLOBAL[:COLOR].cyan(@host)}:#{RuneRb::GLOBAL[:COLOR].cyan(@port)} @ #{RuneRb::GLOBAL[:COLOR].cyan(@start[:stamp])}")
end

Instance Attribute Details

#idInteger (readonly)

Returns the id for the Endpoint.

Returns:

  • (Integer)

    the id for the Endpoint.

Since:

  • 0.9.3


11
12
13
# File 'app/network/endpoint.rb', line 11

def id
  @id
end

#nodeFiber (readonly)

Returns the Fiber executing the main logic of the Endpoint.

Returns:

  • (Fiber)

    the Fiber executing the main logic of the Endpoint.

Since:

  • 0.9.3


15
16
17
# File 'app/network/endpoint.rb', line 15

def node
  @node
end

Instance Method Details

#shutdown(graceful: true) ⇒ Object

Closes the Endpoint.

Parameters:

graceful:
  Should a graceful shutdown be attempted? This will attempt to cleanly close all attached Session objects before closing the <@server> and <@selector> objects.

Parameters:

  • graceful (Boolean) (defaults to: true)

    Should a graceful shutdown be attempted?

Since:

  • 0.9.3


51
52
53
54
55
56
57
# File 'app/network/endpoint.rb', line 51

def shutdown(graceful: true)
  @sessions.each_key(&:close) if graceful
  log! "Up-time: #{up_time.to_i.to_ftime}"
ensure
  @server.close
  @closed = true
end

#up_timeInteger, Float

The current up-time for the Endpoint.

Returns:

  • (Integer, Float)

    the current up-time for the Endpoint.

Since:

  • 0.9.3


40
41
42
# File 'app/network/endpoint.rb', line 40

def up_time
  (Process.clock_gettime(Process::CLOCK_MONOTONIC) - (@start[:time] || Time.now)).round(3)
end