Class: RuneRb::Network::Session

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
Dispatcher, Handshake, Parser, System::Log
Defined in:
app/network/session.rb

Overview

A Session object encapsulates a connected TCPSocket object and provides functions for interacting with it.

Since:

  • 0.9.3

Instance Attribute Summary collapse

Attributes included from Handshake

#login

Instance Method Summary collapse

Methods included from Dispatcher

#write_message

Methods included from System::Log

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

Instance Attribute Details

#idInteger (readonly)

Returns the Session ID.

Returns:

  • (Integer)

    the Session ID.

Since:

  • 0.9.3


14
15
16
# File 'app/network/session.rb', line 14

def id
  @id
end

#ipString, ... (readonly)

Returns the IPV4 address that corresponds to the underlying <@socket> object.

Returns:

  • (String, IPAddr, Addrinfo)

    the IPV4 address that corresponds to the underlying <@socket> object.

Since:

  • 0.9.3


18
19
20
# File 'app/network/session.rb', line 18

def ip
  @ip
end

#startHash (readonly)

Returns a map of Time database for the session.

Returns:

  • (Hash)

    a map of Time database for the session.

Since:

  • 0.9.3


26
27
28
# File 'app/network/session.rb', line 26

def start
  @start
end

#statusHash (readonly)

Returns hash containing status information for the Session.

Returns:

  • (Hash)

    hash containing status information for the Session.

Since:

  • 0.9.3


22
23
24
# File 'app/network/session.rb', line 22

def status
  @status
end

Instance Method Details

#disconnect(reason = :manual) ⇒ Object

Ends the session closing it's socket and updating the <@status> to :LOGGED_OUT.

Parameters:

  • reason (Symbol) (defaults to: :manual)

    the reason for the disconnect. Exclusively used for logging purposes.

Since:

  • 0.9.3


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/network/session.rb', line 66

def disconnect(reason = :manual)
  case reason
  when :logout then log! RuneRb::GLOBAL[:COLOR].green.bold("Session ended by client.")
  when :authentication then err "Session disconnected during authentication."
  when :io_error then err "Session disconnected due to IO error."
  when :handshake then err "Session disconnected during handshake."
  when :manual then err "Session forcefully closed by Endpoint. This is typically caused by a closed socket."
  when :peer then err "Session forcefully closed by peer. This could be ECONNABORTED or ECONNRESET."
  else err "Session disconnected for reason: #{reason}"
  end
  @status[:auth] = :LOGGED_OUT
ensure
  @status[:active] = false
  log! inspect
  close_connection_after_writing
end

#inspectObject

Since:

  • 0.9.3


60
61
62
# File 'app/network/session.rb', line 60

def inspect
  RuneRb::GLOBAL[:COLOR].blue("[IP:] #{RuneRb::GLOBAL[:COLOR].cyan.bold(@ip)} || [ID:] #{RuneRb::GLOBAL[:COLOR].cyan.bold(@id)} || [UP:] #{RuneRb::GLOBAL[:COLOR].cyan.bold(up_time)} || [STATUS:] #{RuneRb::GLOBAL[:COLOR].cyan.bold(@status.inspect)}")
end

#post_initObject

Since:

  • 0.9.3


28
29
30
31
32
33
34
35
36
# File 'app/network/session.rb', line 28

def post_init
  @status = { auth: :PENDING_CONNECTION, active: true }
  _, @ip = Socket.unpack_sockaddr_in(get_peername)
  @start = { time: Process.clock_gettime(Process::CLOCK_MONOTONIC), stamp: Time.now }
  @id = Druuid.gen
  @buffer = String.new
  log! "Session initialized!"
  log! inspect
end

#receive_data(data) ⇒ Object

Since:

  • 0.9.3


38
39
40
41
42
43
44
45
46
# File 'app/network/session.rb', line 38

def receive_data(data)
  @buffer << data
  case @status[:auth]
  when :PENDING_CONNECTION then read_connection
  when :PENDING_BLOCK then read_block
  when :LOGGED_IN, :PENDING_WORLD, :TRANSFERRING then next_message
  else read_connection
  end
end

#register(context) ⇒ Object

Registers a Context to the Session.

Parameters:

Since:

  • 0.9.3


50
51
52
# File 'app/network/session.rb', line 50

def register(context)
  @context = context
end

#up_time(formatted: true) ⇒ Object

The current up-time for the session.

Since:

  • 0.9.3


55
56
57
58
# File 'app/network/session.rb', line 55

def up_time(formatted: true)
  up = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - (@start[:time] || Time.now)).round(3)).to_i
  formatted ? up.to_ftime : up
end