mirror of
https://github.com/TimeCrafters/timecrafters_configuration_tool_desktop.git
synced 2025-12-15 21:32:35 +00:00
Added socket error storage to client, TACNET UI now semi-functional
This commit is contained in:
@@ -1,26 +1,27 @@
|
||||
module TAC
|
||||
module Palette
|
||||
TACNET_CONNECTED = Gosu::Color.new(0xff008000)
|
||||
TACNET_CONNECTING = Gosu::Color.new(0xffff8800)
|
||||
TACNET_CONNECTION_ERROR = Gosu::Color.new(0xff800000)
|
||||
TACNET_CONNECTED = Gosu::Color.new(0xff_008000)
|
||||
TACNET_CONNECTING = Gosu::Color.new(0xff_ff8800)
|
||||
TACNET_CONNECTION_ERROR = Gosu::Color.new(0xff_800000)
|
||||
TACNET_NOT_CONNECTED = Gosu::Color.new(0xff_222222)
|
||||
|
||||
TIMECRAFTERS_PRIMARY = Gosu::Color.new(0xff008000)
|
||||
TIMECRAFTERS_SECONDARY = Gosu::Color.new(0xff006000)
|
||||
TIMECRAFTERS_TERTIARY = Gosu::Color.new(0xff00d000)
|
||||
TIMECRAFTERS_PRIMARY = Gosu::Color.new(0xff_008000)
|
||||
TIMECRAFTERS_SECONDARY = Gosu::Color.new(0xff_006000)
|
||||
TIMECRAFTERS_TERTIARY = Gosu::Color.new(0xff_00d000)
|
||||
|
||||
TACNET_PRIMARY = Gosu::Color.new(0xff000080)
|
||||
TACNET_SECONDARY = Gosu::Color.new(0xff000060)
|
||||
TACNET_PRIMARY = Gosu::Color.new(0xff_000080)
|
||||
TACNET_SECONDARY = Gosu::Color.new(0xff_000060)
|
||||
|
||||
GROUPS_PRIMARY = Gosu::Color.new(0xff444444)
|
||||
GROUPS_SECONDARY = Gosu::Color.new(0xff444444)
|
||||
GROUPS_PRIMARY = Gosu::Color.new(0xff_444444)
|
||||
GROUPS_SECONDARY = Gosu::Color.new(0xff_444444)
|
||||
|
||||
ACTIONS_PRIMARY = Gosu::Color.new(0xff4444aa)
|
||||
ACTIONS_SECONDARY = Gosu::Color.new(0xff040404)
|
||||
ACTIONS_PRIMARY = Gosu::Color.new(0xff_4444aa)
|
||||
ACTIONS_SECONDARY = Gosu::Color.new(0xff_040404)
|
||||
|
||||
VALUES_PRIMARY = Gosu::Color.new(0xff660066)
|
||||
VALUES_SECONDARY = Gosu::Color.new(0xff440044)
|
||||
VALUES_PRIMARY = Gosu::Color.new(0xff_660066)
|
||||
VALUES_SECONDARY = Gosu::Color.new(0xff_440044)
|
||||
|
||||
EDITOR_PRIMARY = Gosu::Color.new(0xff446688)
|
||||
EDITOR_SECONDARY = Gosu::Color.new(0xff224466)
|
||||
EDITOR_PRIMARY = Gosu::Color.new(0xff_446688)
|
||||
EDITOR_SECONDARY = Gosu::Color.new(0xff_224466)
|
||||
end
|
||||
end
|
||||
@@ -50,11 +50,21 @@ module TAC
|
||||
end
|
||||
|
||||
stack width: 0.499 do
|
||||
@tacnet_status = label "Connection Error", background: TAC::Palette::TACNET_CONNECTION_ERROR, text_size: 18, padding: 5, margin_top: 2
|
||||
@tacnet_connection_button = button "Connect", text_size: 18 do
|
||||
@tacnet_status = label "Not Connected", background: TAC::Palette::TACNET_NOT_CONNECTED, width: 1.0, text_size: 18, padding: 5, margin_top: 2, border_thickness: 1, border_color: Gosu::Color::GRAY
|
||||
flow width: 1.0 do
|
||||
@tacnet_connection_button = button "Connect", width: 0.475, text_size: 18 do
|
||||
case window.backend.tacnet.status
|
||||
when :connected, :connecting
|
||||
window.backend.tacnet.close
|
||||
when :not_connected, :connection_error
|
||||
window.backend.tacnet.connect("localhost")
|
||||
end
|
||||
end
|
||||
button "Status", text_size: 18, width: 0.475 do
|
||||
push_state(Dialog::AlertDialog, title: "TACNET Status", message: window.backend.tacnet.full_status)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -112,6 +122,37 @@ module TAC
|
||||
end
|
||||
|
||||
populate_groups_list
|
||||
|
||||
@tacnet_status_monitor = CyberarmEngine::Timer.new(250) do
|
||||
case window.backend.tacnet.status
|
||||
when :connected
|
||||
@tacnet_status.value = "Connected"
|
||||
@tacnet_status.background = TAC::Palette::TACNET_CONNECTED
|
||||
|
||||
@tacnet_connection_button.value = "Disconnect"
|
||||
when :connecting
|
||||
@tacnet_status.value = "Connecting..."
|
||||
@tacnet_status.background = TAC::Palette::TACNET_CONNECTING
|
||||
|
||||
@tacnet_connection_button.value = "Disconnect"
|
||||
when :connection_error
|
||||
@tacnet_status.value = "Connection Error"
|
||||
@tacnet_status.background = TAC::Palette::TACNET_CONNECTION_ERROR
|
||||
|
||||
@tacnet_connection_button.value = "Connect"
|
||||
when :not_connected
|
||||
@tacnet_status.value = "Not Connected"
|
||||
@tacnet_status.background = TAC::Palette::TACNET_NOT_CONNECTED
|
||||
|
||||
@tacnet_connection_button.value = "Connect"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
super
|
||||
|
||||
@tacnet_status_monitor.update
|
||||
end
|
||||
|
||||
def create_group(name)
|
||||
|
||||
@@ -10,33 +10,66 @@ module TAC
|
||||
@connection = nil
|
||||
end
|
||||
|
||||
def connect(hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT, error_callback = proc {})
|
||||
def connect(hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT)
|
||||
return if @connection && @connection.connected?
|
||||
|
||||
@connection = Connection.new(hostname, port)
|
||||
@connection.connect(error_callback)
|
||||
@connection.connect
|
||||
end
|
||||
|
||||
def status
|
||||
if connected?
|
||||
:connected
|
||||
elsif @connection && !@connection.client.socket_error?
|
||||
:connecting
|
||||
elsif @connection && @connection.client.socket_error?
|
||||
:connection_error
|
||||
else
|
||||
:not_connected
|
||||
end
|
||||
end
|
||||
|
||||
def full_status
|
||||
_status = status.to_s.split("_").map { |c| c.capitalize }.join(" ")
|
||||
|
||||
if connected?
|
||||
net_stats = ""
|
||||
net_stats += "<b>Connection Statistics:</b>\n"
|
||||
net_stats += "<b>Packets Sent:</b> #{client.packets_sent}\n"
|
||||
net_stats += "<b>Packets Received:</b> #{client.packets_received}\n\n"
|
||||
net_stats += "<b>Data Sent:</b> #{client.data_sent} bytes\n"
|
||||
net_stats += "<b>Data Received:</b> #{client.data_received} bytes\n"
|
||||
|
||||
"<b>Status:</b> #{_status}\n\n#{net_stats}"
|
||||
elsif @connection && @connection.client && @connection.client.socket_error?
|
||||
"<b>Status:</b> #{_status}\n\n#{@connection.client.last_socket_error.to_s.chars.each_slice(32).to_a.map { |c| c.join }.join("\n")}"
|
||||
else
|
||||
pp client
|
||||
"<b>Status:</b> #{_status}"
|
||||
end
|
||||
end
|
||||
|
||||
def connected?
|
||||
@connection && @connection.connected?
|
||||
end
|
||||
|
||||
def client
|
||||
def close
|
||||
if connected?
|
||||
@connection.client
|
||||
@connection.close
|
||||
@connection = nil
|
||||
end
|
||||
end
|
||||
|
||||
def client
|
||||
@connection.client if connected?
|
||||
end
|
||||
|
||||
def puts(packet)
|
||||
if connected?
|
||||
@connection.puts(packet)
|
||||
end
|
||||
@connection.puts(packet) if connected?
|
||||
end
|
||||
|
||||
def gets
|
||||
if connected?
|
||||
@connection.gets
|
||||
end
|
||||
@connection.gets if connected?
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -7,7 +7,7 @@ module TAC
|
||||
attr_reader :uuid, :read_queue, :write_queue, :socket,
|
||||
:packets_sent, :packets_received,
|
||||
:data_sent, :data_received
|
||||
attr_accessor :sync_interval
|
||||
attr_accessor :sync_interval, :last_socket_error, :socket_error
|
||||
def initialize
|
||||
@uuid = SecureRandom.uuid
|
||||
@read_queue = []
|
||||
@@ -15,6 +15,10 @@ module TAC
|
||||
|
||||
@sync_interval = 100
|
||||
|
||||
@last_socket_error = nil
|
||||
@socket_error = false
|
||||
@bound = false
|
||||
|
||||
@packets_sent, @packets_received = 0, 0
|
||||
@data_sent, @data_received = 0, 0
|
||||
end
|
||||
@@ -25,6 +29,7 @@ module TAC
|
||||
|
||||
def socket=(socket)
|
||||
@socket = socket
|
||||
@bound = true
|
||||
|
||||
listen
|
||||
end
|
||||
@@ -82,12 +87,16 @@ module TAC
|
||||
end
|
||||
end
|
||||
|
||||
def connected?
|
||||
!closed?
|
||||
def socket_error?
|
||||
@socket_error
|
||||
end
|
||||
|
||||
def bound?
|
||||
@socket.bound? if @socket
|
||||
def connected?
|
||||
if closed? == true || closed? == nil
|
||||
false
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def closed?
|
||||
@@ -97,7 +106,9 @@ module TAC
|
||||
def write(message)
|
||||
begin
|
||||
@socket.puts("#{message}\r\n\n")
|
||||
rescue Errno::EPIPE, IOError => error
|
||||
rescue => error
|
||||
@last_socket_error = error
|
||||
@socket_error = true
|
||||
log.e(TAG, error.message)
|
||||
close
|
||||
end
|
||||
@@ -109,7 +120,10 @@ module TAC
|
||||
begin
|
||||
data = @socket.readpartial(CHUNK_SIZE)
|
||||
message += data
|
||||
rescue Errno::EPIPE, EOFError
|
||||
rescue => error
|
||||
@last_socket_error = error
|
||||
@socket_error = true
|
||||
|
||||
message = ""
|
||||
break
|
||||
end until message.end_with?("\r\n\n")
|
||||
|
||||
@@ -7,6 +7,8 @@ module TAC
|
||||
@hostname = hostname
|
||||
@port = port
|
||||
|
||||
@client = nil
|
||||
|
||||
@last_sync_time = 0
|
||||
@sync_interval = SYNC_INTERVAL
|
||||
|
||||
@@ -20,7 +22,7 @@ module TAC
|
||||
@packet_handler = PacketHandler.new(host_is_a_connection: true)
|
||||
end
|
||||
|
||||
def connect(error_callback)
|
||||
def connect
|
||||
return if @client
|
||||
|
||||
@client = Client.new
|
||||
@@ -39,8 +41,13 @@ module TAC
|
||||
end
|
||||
|
||||
rescue => error
|
||||
p error
|
||||
error_callback.call(error)
|
||||
log.e(TAG, error)
|
||||
|
||||
if @client
|
||||
@client.close
|
||||
@client.last_socket_error = error
|
||||
@client.socket_error = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -68,12 +75,16 @@ module TAC
|
||||
end
|
||||
|
||||
def connected?
|
||||
!closed?
|
||||
@client.connected? if @client
|
||||
end
|
||||
|
||||
def closed?
|
||||
@client.closed? if @client
|
||||
end
|
||||
|
||||
def close
|
||||
@client.close if @client
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -35,7 +35,7 @@ module TAC
|
||||
begin
|
||||
log.i(TAG, "Starting server...")
|
||||
@socket = TCPServer.new(@port)
|
||||
rescue => error
|
||||
rescue IOError => error
|
||||
log.e(TAG, error)
|
||||
|
||||
@connection_attempts += 1
|
||||
@@ -46,8 +46,8 @@ module TAC
|
||||
while @socket && !@socket.closed?
|
||||
begin
|
||||
run_server
|
||||
rescue => error
|
||||
p error
|
||||
rescue IOError => error
|
||||
log.e(TAG, error)
|
||||
@socket.close if @socket
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user