From bae6a6a332f76f0d98d11453dd401f9300181596 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Fri, 8 May 2020 21:42:14 -0500 Subject: [PATCH] Removed async-websocket, more work on netcode --- Gemfile | 1 - Gemfile.lock | 33 ---------------------- i-mic-fps.rb | 1 - lib/networking/client.rb | 21 ++++++++++++++ lib/networking/packet_handlers/move.rb | 0 lib/networking/protocol.rb | 39 ++++++++++++++++++++++++++ 6 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 lib/networking/packet_handlers/move.rb create mode 100644 lib/networking/protocol.rb diff --git a/Gemfile b/Gemfile index 4743343..933ba47 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,6 @@ gem "rake" gem "opengl-bindings", require: "opengl" gem "cyberarm_engine", git: "https://github.com/cyberarm/cyberarm_engine" gem "nokogiri", ">= 1.11.0.rc1" -gem "async-websocket" group(:packaging) do gem "releasy", github: "gosu/releasy" diff --git a/Gemfile.lock b/Gemfile.lock index 59d2c94..4b26fae 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -19,58 +19,25 @@ GIT GEM remote: https://rubygems.org/ specs: - async (1.26.0) - console (~> 1.0) - nio4r (~> 2.3) - timers (~> 4.1) - async-http (0.52.2) - async (~> 1.25) - async-io (~> 1.28) - async-pool (~> 0.2) - protocol-http (~> 0.19.0) - protocol-http1 (~> 0.13.0) - protocol-http2 (~> 0.14.0) - async-io (1.29.0) - async (~> 1.14) - async-pool (0.3.1) - async (~> 1.25) - async-websocket (0.14.0) - async-http (~> 0.51) - async-io (~> 1.23) - protocol-websocket (~> 0.7.0) - console (1.8.2) cri (2.1.0) excon (0.73.0) gosu (0.15.1) gosu (0.15.1-x64-mingw32) gosu_more_drawables (0.3.0) mini_portile2 (2.5.0) - nio4r (2.5.2) nokogiri (1.11.0.rc2) mini_portile2 (~> 2.5.0) nokogiri (1.11.0.rc2-x64-mingw32) ocra (1.3.11) opengl-bindings (1.6.10) - protocol-hpack (1.4.2) - protocol-http (0.19.0) - protocol-http1 (0.13.0) - protocol-http (~> 0.19) - protocol-http2 (0.14.0) - protocol-hpack (~> 1.4) - protocol-http (~> 0.18) - protocol-websocket (0.7.4) - protocol-http (~> 0.2) - protocol-http1 (~> 0.2) rake (13.0.1) rubyzip (2.3.0) - timers (4.3.0) PLATFORMS ruby x64-mingw32 DEPENDENCIES - async-websocket cyberarm_engine! excon nokogiri (>= 1.11.0.rc1) diff --git a/i-mic-fps.rb b/i-mic-fps.rb index adf7d3e..6376fd4 100644 --- a/i-mic-fps.rb +++ b/i-mic-fps.rb @@ -9,7 +9,6 @@ require "tmpdir" require "opengl" require "glu" require "nokogiri" -require "async/websocket" begin require_relative "../cyberarm_engine/lib/cyberarm_engine" diff --git a/lib/networking/client.rb b/lib/networking/client.rb index 7f9fc5a..1e9290c 100644 --- a/lib/networking/client.rb +++ b/lib/networking/client.rb @@ -1,8 +1,20 @@ class IMICFPS module Networking class Client + attr_reader :uuid, + :packets_sent, :packets_received, + :data_sent, :data_received def initialize(socket:) @socket = socket + @write_queue = [] + @read_queue = [] + + @uuid = "not_defined" + + @packets_sent = 0 + @packets_received = 0 + @data_sent = 0 + @data_received = 0 end def read @@ -11,7 +23,16 @@ class IMICFPS def write end + def puts(packet) + @write_queue << packet + end + + def gets + @socket.recvfrom_nonblock(Protocol::MAX_PACKET_SIZE) + end + def close + @socket.close if @socket end end end diff --git a/lib/networking/packet_handlers/move.rb b/lib/networking/packet_handlers/move.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/networking/protocol.rb b/lib/networking/protocol.rb new file mode 100644 index 0000000..4d6581b --- /dev/null +++ b/lib/networking/protocol.rb @@ -0,0 +1,39 @@ +class IMICFPS + module Networking + module Protocol + MAX_CLIENTS = 32 + MAX_PACKET_SIZE = 1024 + PROTOCOL_VERSION = 0 # int + HEARTBEAT_INTERVAL = 250 # ms + TIMEOUT_PERIOD = 30_000 # ms + + packet_types = %w{ + # protocol packets + reliable + multipart + acknowledgement + control + data + + # protocol control packets + connect + disconnect + authenticate + heartbeat + + # game data packets + client_connected + client_disconnected + entity_move + play_sound_effect + create_particle + } + + # emulate c-like enum + packet_types.each_with_index do |type, i| + next if type.start_with?("#") + self.const_set(:"#{type.upcase}", i) + end + end + end +end \ No newline at end of file