Class: NetBase

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

Overview

NetBase

Lowest network layer logic. Sends packets via udp. Also adding the teeworlds protocol packet header.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ NetBase

Returns a new instance of NetBase.



24
25
26
27
28
29
30
31
# File 'lib/net_base.rb', line 24

def initialize(opts = {})
  @verbose = opts[:verbose] || false
  @ip = nil
  @port = nil
  @s = nil
  @ack = 0
  @peer_token = [0xFF, 0xFF, 0xFF, 0xFF].map { |b| b.to_s(16).rjust(2, '0') }.join
end

Instance Attribute Details

#ackObject

Returns the value of attribute ack.



21
22
23
# File 'lib/net_base.rb', line 21

def ack
  @ack
end

#peer_tokenObject (readonly)

Returns the value of attribute peer_token.



22
23
24
# File 'lib/net_base.rb', line 22

def peer_token
  @peer_token
end

Instance Method Details

#bind(socket) ⇒ Object



33
34
35
# File 'lib/net_base.rb', line 33

def bind(socket)
  @s = socket
end

#connect(socket, ip, port) ⇒ Object



37
38
39
40
41
42
# File 'lib/net_base.rb', line 37

def connect(socket, ip, port)
  @s = socket
  @ip = ip
  @port = port
  @ack = 0
end

#send_packet(payload, opts = { chunks: 1, client: nil, addr: nil }) ⇒ Object

Sends a packing setting the proper header for you

Parameters:

  • payload (Array)

    The Integer list representing the data after the header

  • opts (Hash) (defaults to: { chunks: 1, client: nil, addr: nil })

    :chunks, :client and packet header flags for more details check the class PacketFlags



54
55
56
57
58
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/net_base.rb', line 54

def send_packet(payload, opts = { chunks: 1, client: nil, addr: nil })
  # unsigned char flags_ack;    // 6bit flags, 2bit ack
  # unsigned char ack;          // 8bit ack
  # unsigned char numchunks;    // 8bit chunks
  # unsigned char token[4];     // 32bit token
  # // ffffffaa
  # // aaaaaaaa
  # // NNNNNNNN
  # // TTTTTTTT
  # // TTTTTTTT
  # // TTTTTTTT
  # // TTTTTTTT
  if @s.nil?
    puts "Error: no active socket"
    return
  end
  flags_bits = PacketFlags.new(opts).bits
  ack = @ack
  ip = @ip
  port = @port
  token = @peer_token
  unless opts[:client].nil?
    ack = opts[:client].ack
    ip = opts[:client].addr.ip
    port = opts[:client].addr.port
    token = opts[:client].token
  end
  unless opts[:addr].nil?
    ip = opts[:addr].ip
    port = opts[:addr].port
  end
  #          unused flags       ack                             num chunks
  #              ff ffff        aa aaaa aaaa                    NNNN NNNN
  header_bits = "00#{flags_bits}#{ack.to_s(2).rjust(10, '0')}#{opts[:chunks].to_s(2).rjust(8, '0')}"

  header = header_bits.chars.groups_of(8).map do |eight_bits|
    eight_bits.join.to_i(2)
  end

  header += str_bytes(token)
  data = (header + payload).pack('C*')
  client = opts[:client]
  if @verbose
    if client
      puts "send to #{ip}:#{port} " \
           "client(id=#{client.id} " \
           "token=#{client.token} " \
           "name=#{client.player.name} port=#{client.addr.port})"
    else
      puts "send to #{ip}:#{port}"
    end
  end
  @s.send(data, 0, ip, port)

  puts Packet.new(data, '>').to_s if @verbose || opts[:test]
end

#set_peer_token(token) ⇒ Object



44
45
46
47
# File 'lib/net_base.rb', line 44

def set_peer_token(token)
  SecurityToken.validate(token)
  @peer_token = token
end