Class: Termfront::Network::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/termfront/network/server.rb

Constant Summary collapse

TEAM_SIZES =
[1, 2, 4].freeze
MAX_QUEUE_PER_MODE =
64
QUEUE_HANDSHAKE_TIMEOUT =
5
MAX_MSG_BYTES =
16 * 1024
MATCH_MAX_DURATION =
30 * 60
MATCH_IDLE_TIMEOUT =
5 * 60
ALLOWED_MP_WEAPONS =
%w[pistol ar shock_pistol shock_rifle].freeze
INITIAL_OBTAINED_WEAPONS =
%i[pistol ar].freeze
MAX_STATE_DT =
0.5
POSITION_DELTA_MARGIN =
1.5
RATE_LIMITS =
{
  "state"  => 60,
  "hit"    => 20,
  "fire"   => 20,
  "pickup" => 5,
  "ping"   => 5,
  "dead"   => 5
}.freeze
DEFAULT_RATE_LIMIT =
10
MAX_DROPPED_MSGS =
200
PVP_MAP =
[
  "####################",
  "#........##........#",
  "#........##........#",
  "#..................#",
  "#..##........##....#",
  "#..##........##....#",
  "#..................#",
  "#..................#",
  "#....##........##..#",
  "#....##........##..#",
  "#..................#",
  "#........##........#",
  "#........##........#",
  "####################"
].freeze
PVP_SPAWN_CANDIDATES =
[
  [2.5, 2.5, 0.0],
  [2.5, 11.5, 0.0],
  [5.5, 5.5, 0.0],
  [4.5, 9.5, 0.0],
  [17.5, 11.5, Math::PI],
  [17.5, 2.5, Math::PI],
  [14.5, 8.5, Math::PI],
  [15.5, 4.5, Math::PI]
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(port: Config::PVP_PORT) ⇒ Server

Returns a new instance of Server.



58
59
60
61
62
63
# File 'lib/termfront/network/server.rb', line 58

def initialize(port: Config::PVP_PORT)
  @port = port
  @queue_mutex = Mutex.new
  @queues = TEAM_SIZES.to_h { |team_size| [team_size, []] }
  @wavesfight_queues = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#runObject



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
# File 'lib/termfront/network/server.rb', line 65

def run
  cert, key, chain = load_cert

  ctx = OpenSSL::SSL::SSLContext.new
  ctx.cert = cert
  ctx.key  = key
  ctx.min_version = OpenSSL::SSL::TLS1_2_VERSION
  ctx.extra_chain_cert = chain unless chain.empty?

  tcp_server = TCPServer.new("0.0.0.0", @port)
  ssl_server = OpenSSL::SSL::SSLServer.new(tcp_server, ctx)
  ssl_server.start_immediately = true

  puts "Termfront PvP server listening on 0.0.0.0:#{@port}"

  loop do
    begin
      client = ssl_server.accept
      configure_client(client)
      Thread.new(client) do |c|
        enqueue_player(c)
      rescue StandardError => e
        puts "Connection handler error: #{e.class}"
        begin
          c.close
        rescue StandardError
          nil
        end
      end
    rescue OpenSSL::SSL::SSLError => e
      puts "SSL handshake failed: #{e.class}"
    rescue StandardError => e
      puts "Accept error: #{e.class}"
    end
  end
end