Stubbed out files for networking

This commit is contained in:
2020-03-26 14:25:55 -05:00
parent bb21de2e2a
commit 75185425b6
14 changed files with 194 additions and 1 deletions

View File

@@ -61,7 +61,6 @@ class IMICFPS
@collision_manager.update
@entities.each(&:update)
# @lights.each(&:update)
end
end
end

View File

@@ -0,0 +1,6 @@
class IMICFPS
module Networking
class MemoryConnection < Connection
end
end
end

View File

@@ -0,0 +1,6 @@
class IMICFPS
module Networking
class MemoryServer < Server
end
end
end

18
lib/networking/client.rb Normal file
View File

@@ -0,0 +1,18 @@
class IMICFPS
module Networking
class Client
def initialize(socket:)
@socket = socket
end
def read
end
def write
end
def close
end
end
end
end

View File

@@ -0,0 +1,14 @@
class IMICFPS
module Networking
class Connection
def initialize(hostname:, port:)
end
def connect
end
def close
end
end
end
end

View File

@@ -0,0 +1,61 @@
class IMICFPS
module Networking
class Director
attr_reader :mode, :hostname, :port, :tick_rate, :storage
def initialize(mode:, hostname:, port:, interface:, state: nil, tick_rate: 2)
@mode = mode
@hostname = hostname
@port = port
@state = state
@tick_rate = (1000.0 / tick_rate) / 1000.0
case @mode
when :server
@server = interface.new(hostname: @hostname, port: @port)
when :connection
@connection = interface.new(hostname: @hostname, port: @port)
when :memory
@server = interface[:server].new(hostname: @hostname, port: @port)
@connection = interface[:connection].new(hostname: @hostname, port: @port)
else
raise ArgumentError, "Expected mode to be :server, :connection, or :memory, not #{mode.inspect}"
end
@last_tick_time = milliseconds
@directing = true
@storage = {}
end
def run
Thread.start do |thread|
while(@directing)
dt = milliseconds - @last_tick_time
tick(dt)
@server.update if @server
@connection.update if @connection
@last_tick_time = milliseconds
sleep(@tick_rate)
end
end
end
def tick(dt)
end
def shutdown
@directing = false
@clients.each(&:close)
@server.update if @server
@connection.update if @connection
end
def milliseconds
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
end
end
end

11
lib/networking/events.rb Normal file
View File

@@ -0,0 +1,11 @@
class IMICFPS
module Networking
module Events
def on_connect(client)
end
def on_disconnect(client)
end
end
end
end

18
lib/networking/packet.rb Normal file
View File

@@ -0,0 +1,18 @@
class IMICFPS
module Networking
class Packet
def initialize(type:, payload:)
end
def self.encode(packet)
"#{packet.type}|#{packet.payload}"
end
def self.decode(string)
split = string.split("|")
Packet.new(split.first, split.last)
end
end
end
end

View File

@@ -0,0 +1,6 @@
class IMICFPS
module Networking
class PacketHandler
end
end
end

26
lib/networking/server.rb Normal file
View File

@@ -0,0 +1,26 @@
class IMICFPS
module Networking
MAX_CLIENTS = 32
class Server
attr_reader :hostname, :port, :max_clients, :clients
def initialize(hostname:, port:, max_clients: MAX_CLIENTS)
@hostname = hostname
@port = port
@max_clients = max_clients
@clients = []
@socket = nil
end
def bind
end
def broadcast(packet)
end
def close
end
end
end
end

View File

@@ -9,6 +9,7 @@ class IMICFPS
@player = @map.find_entity_by(name: "character")
@camera = Camera.new(position: @player.position.clone)
@camera.attach_to(@player)
@director = Networking::Director.new(mode: :memory, hostname: "i-mic.rubyclan.org", port: 56789, interface: { server: Networking::MemoryServer, connection: Networking::MemoryConnection }, state: self)
@crosshair = Crosshair.new
@@ -39,6 +40,7 @@ class IMICFPS
control_player
@camera.update
@director.tick(window.dt)
if window.config.get(:debug_options, :stats)
@text.text = update_text