mirror of
https://github.com/cyberarm/i-mic-rts.git
synced 2025-12-16 00:02:33 +00:00
Basic networking implemented, currently non functional
This commit is contained in:
@@ -1,7 +1,30 @@
|
||||
class IMICRTS
|
||||
# {IMICRTS::Connection} is the abstract middleman that Director sends/receives Orders from.
|
||||
# not to be confused with {IMICRTS::Networking::Connection}
|
||||
class Connection
|
||||
def initialize(*args)
|
||||
# Connection modes:
|
||||
# :virtual => emulates networking without sockets (used for solo play)
|
||||
# :host => starts server
|
||||
# :client => connects to server/host
|
||||
def initialize(director:, mode:, hostname: "localhost", port: 56789)
|
||||
@director = director
|
||||
@mode = mode
|
||||
@hostname = hostname
|
||||
@port = port
|
||||
|
||||
@pending_orders = []
|
||||
|
||||
case @mode
|
||||
when :virtual
|
||||
when :host
|
||||
@server = Networking::Server.new(director: @director, hostname: @hostname, port: @port)
|
||||
|
||||
@connection = Networking::Connection.new(director: @director, hostname: @hostname, port: @port)
|
||||
when :client
|
||||
@connection = Networking::Connection.new(director: @director, hostname: @hostname, port: @port)
|
||||
else
|
||||
raise RuntimeError, "Unable to process Connection of type: #{@mode.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
def add_order(order)
|
||||
@@ -9,15 +32,37 @@ class IMICRTS
|
||||
end
|
||||
|
||||
def update
|
||||
data = @pending_orders.sort_by { |order| order.tick_id }.map do |order|
|
||||
# data = @pending_orders.sort_by { |order| order.tick_id }.map do |order|
|
||||
|
||||
# Order serialized size in bytes + serialized order data
|
||||
[order.serialized_order.length].pack("n") + order.serialized_order
|
||||
end.join
|
||||
# # Order serialized size in bytes + serialized order data
|
||||
# pp order.serialized_order
|
||||
# [order.serialized_order.length].pack("n") + order.serialized_order
|
||||
# end.join
|
||||
|
||||
# p data if data.length > 0
|
||||
if @mode == :virtual
|
||||
else
|
||||
@pending_orders.each do |order|
|
||||
# TODO: make this include order_id and tick_id
|
||||
@server.broadcast(order.serialized_order) if @server
|
||||
@connection.write(order.serialized_order) unless @server
|
||||
|
||||
@pending_orders.clear
|
||||
@pending_orders.delete(order)
|
||||
end
|
||||
end
|
||||
|
||||
case @mode
|
||||
when :virtual
|
||||
when :host
|
||||
@server.update
|
||||
@connection.update
|
||||
when :client
|
||||
@connection.update
|
||||
end
|
||||
end
|
||||
|
||||
def finalize
|
||||
@server.stop if @server
|
||||
@connection.close if @connection
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user