From 6c005497d475a16692b7693626b3299ba49d99ed Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Wed, 9 Nov 2022 08:40:17 +0100 Subject: [PATCH] Layout server control msg structure Thus closed #5 By deciding to support server as well. This is rebranding from teeworlds-client to teeworlds_network And thus also including a bunch of file renames --- README.md | 6 +++--- sample.rb => client_sample.rb | 2 +- examples/01_chat_logger.rb | 2 +- examples/02_packet_debugger.rb | 2 +- examples/03_send_chat.rb | 2 +- examples/04_player_infos.rb | 2 +- examples/05_chatbot.rb | 2 +- examples/06_join_leave_messages.rb | 2 +- examples/07_rcon.rb | 2 +- integration_test/rcon_shutdown.rb | 2 +- integration_test/reconnect.rb | 2 +- integration_test/send_chat_hello.rb | 2 +- lib/net_addr.rb | 4 ++++ lib/{teeworlds-client.rb => teeworlds_client.rb} | 1 - lib/teeworlds_network.rb | 4 ++++ lib/{srv.rb => teeworlds_server.rb} | 16 +++++++++++++++- server.rb => server_sample.rb | 2 +- teeworlds-client.gemspec | 8 ++++---- 18 files changed, 42 insertions(+), 21 deletions(-) rename sample.rb => client_sample.rb (96%) rename lib/{teeworlds-client.rb => teeworlds_client.rb} (99%) mode change 100755 => 100644 create mode 100644 lib/teeworlds_network.rb rename lib/{srv.rb => teeworlds_server.rb} (82%) rename server.rb => server_sample.rb (75%) diff --git a/README.md b/README.md index dc62acb..fa575f3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# teeworlds-client -A teeworlds 0.7 client library written in ruby +# teeworlds_network +A teeworlds 0.7 client & server library written in ruby ## Sample @@ -11,7 +11,7 @@ Also properly disconnect when the program is killed gracefully. For more sample usages checkout the [examples/](examples/) folder. ```ruby -require_relative 'lib/teeworlds-client' +require_relative 'lib/teeworlds_client' client = TeeworldsClient.new(verbose: false) diff --git a/sample.rb b/client_sample.rb similarity index 96% rename from sample.rb rename to client_sample.rb index 761b727..fd50b11 100755 --- a/sample.rb +++ b/client_sample.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require_relative 'lib/teeworlds-client' +require_relative 'lib/teeworlds_client' args = { verbose: false, ip: nil, port: nil } diff --git a/examples/01_chat_logger.rb b/examples/01_chat_logger.rb index 428d7d6..983a2f2 100755 --- a/examples/01_chat_logger.rb +++ b/examples/01_chat_logger.rb @@ -3,7 +3,7 @@ # Print all incoming chat messages to stdout -require_relative '../lib/teeworlds-client' +require_relative '../lib/teeworlds_client' client = TeeworldsClient.new(verbose: false) diff --git a/examples/02_packet_debugger.rb b/examples/02_packet_debugger.rb index 365e402..bcc4407 100755 --- a/examples/02_packet_debugger.rb +++ b/examples/02_packet_debugger.rb @@ -3,7 +3,7 @@ # Prints out all network traffic -require_relative '../lib/teeworlds-client' +require_relative '../lib/teeworlds_client' client = TeeworldsClient.new(verbose: true) diff --git a/examples/03_send_chat.rb b/examples/03_send_chat.rb index 3b2232e..3fd6be2 100755 --- a/examples/03_send_chat.rb +++ b/examples/03_send_chat.rb @@ -3,7 +3,7 @@ # Chat spamming client -require_relative '../lib/teeworlds-client' +require_relative '../lib/teeworlds_client' client = TeeworldsClient.new(verbose: true) diff --git a/examples/04_player_infos.rb b/examples/04_player_infos.rb index c4672f9..fefbf36 100755 --- a/examples/04_player_infos.rb +++ b/examples/04_player_infos.rb @@ -3,7 +3,7 @@ # Set custom skin and other player infos -require_relative '../lib/teeworlds-client' +require_relative '../lib/teeworlds_client' client = TeeworldsClient.new(verbose: true) diff --git a/examples/05_chatbot.rb b/examples/05_chatbot.rb index 8b32ba4..69ecec8 100755 --- a/examples/05_chatbot.rb +++ b/examples/05_chatbot.rb @@ -7,7 +7,7 @@ # # Then connect to localhost and write !ping in the chat -require_relative '../lib/teeworlds-client' +require_relative '../lib/teeworlds_client' client = TeeworldsClient.new(verbose: false) diff --git a/examples/06_join_leave_messages.rb b/examples/06_join_leave_messages.rb index 10d1006..2891923 100755 --- a/examples/06_join_leave_messages.rb +++ b/examples/06_join_leave_messages.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require_relative '../lib/teeworlds-client' +require_relative '../lib/teeworlds_client' client = TeeworldsClient.new diff --git a/examples/07_rcon.rb b/examples/07_rcon.rb index 09f8257..428e1f7 100755 --- a/examples/07_rcon.rb +++ b/examples/07_rcon.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require_relative '../lib/teeworlds-client' +require_relative '../lib/teeworlds_client' client = TeeworldsClient.new diff --git a/integration_test/rcon_shutdown.rb b/integration_test/rcon_shutdown.rb index a8df950..14f4a43 100755 --- a/integration_test/rcon_shutdown.rb +++ b/integration_test/rcon_shutdown.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require_relative '../lib/teeworlds-client' +require_relative '../lib/teeworlds_client' client = TeeworldsClient.new diff --git a/integration_test/reconnect.rb b/integration_test/reconnect.rb index 13bbf18..f101f56 100755 --- a/integration_test/reconnect.rb +++ b/integration_test/reconnect.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require_relative '../lib/teeworlds-client' +require_relative '../lib/teeworlds_client' client = TeeworldsClient.new(verbose: false) diff --git a/integration_test/send_chat_hello.rb b/integration_test/send_chat_hello.rb index ec3c12e..5067dc3 100755 --- a/integration_test/send_chat_hello.rb +++ b/integration_test/send_chat_hello.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require_relative '../lib/teeworlds-client' +require_relative '../lib/teeworlds_client' client = TeeworldsClient.new(verbose: false) diff --git a/lib/net_addr.rb b/lib/net_addr.rb index 3651679..15d8fcd 100644 --- a/lib/net_addr.rb +++ b/lib/net_addr.rb @@ -7,4 +7,8 @@ class NetAddr @ip = ip @port = port end + + def to_s + "#{@ip}:#{@port}" + end end diff --git a/lib/teeworlds-client.rb b/lib/teeworlds_client.rb old mode 100755 new mode 100644 similarity index 99% rename from lib/teeworlds-client.rb rename to lib/teeworlds_client.rb index 822f9c7..a7e80d8 --- a/lib/teeworlds-client.rb +++ b/lib/teeworlds_client.rb @@ -1,4 +1,3 @@ -#!/usr/bin/env ruby # frozen_string_literal: true require 'socket' diff --git a/lib/teeworlds_network.rb b/lib/teeworlds_network.rb new file mode 100644 index 0000000..9daf2c9 --- /dev/null +++ b/lib/teeworlds_network.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +require_relative 'teeworlds_server' +require_relative 'teeworlds_client' diff --git a/lib/srv.rb b/lib/teeworlds_server.rb similarity index 82% rename from lib/srv.rb rename to lib/teeworlds_server.rb index c3a59bf..4c719b8 100755 --- a/lib/srv.rb +++ b/lib/teeworlds_server.rb @@ -1,4 +1,3 @@ -#!/usr/bin/env ruby # frozen_string_literal: true require 'socket' @@ -47,6 +46,9 @@ class TeeworldsServer puts "got ctrl msg: #{msg}" case msg when NET_CTRLMSG_TOKEN then on_ctrl_token(packet) + when NET_CTRLMSG_CONNECT then on_ctrl_connect(packet) + when NET_CTRLMSG_KEEPALIVE then on_ctrl_keep_alive(packet) + when NET_CTRLMSG_CLOSE then on_ctrl_close(packet) else puts "Uknown control message #{msg}" exit(1) @@ -65,6 +67,18 @@ class TeeworldsServer send_ctrl_with_token(packet.addr, token) end + def on_ctrl_keep_alive(packet) + puts "Got keep alive from #{packet.addr}" if @verbose + end + + def on_ctrl_close(packet) + puts "Client closed the connection #{packet.addr}" + end + + def on_ctrl_connect(packet) + puts "Got connect from #{packet.addr}" + end + def on_packet(packet) # process connless packets data if packet.flags_control diff --git a/server.rb b/server_sample.rb similarity index 75% rename from server.rb rename to server_sample.rb index a4a3f7b..1abe7f2 100755 --- a/server.rb +++ b/server_sample.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require_relative 'lib/srv' +require_relative 'lib/teeworlds_server' srv = TeeworldsServer.new(verbose: false) srv.run('127.0.0.1', 8303) diff --git a/teeworlds-client.gemspec b/teeworlds-client.gemspec index 12fd80b..2028f97 100644 --- a/teeworlds-client.gemspec +++ b/teeworlds-client.gemspec @@ -3,12 +3,12 @@ require 'rake' Gem::Specification.new do |s| - s.name = 'teeworlds-client' + s.name = 'teeworlds_network' s.version = '0.0.1' - s.summary = 'teeworlds 0.7 network protocol (client)' + s.summary = 'teeworlds 0.7 network protocol (client and server)' s.description = <<-DESC A library wrapping the network protocol of the game teeworlds. - Supported protocol version 0.7 and only the client side. + Only supporting the version 0.7 of the teeworlds protocol. DESC s.authors = ['ChillerDragon'] s.email = 'ChillerDragon@gmail.com' @@ -18,7 +18,7 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 3.1.2' s.add_dependency 'huffman_tw', '~> 0.0.1' s.add_dependency 'rspec', '~> 3.9.0' - s.homepage = 'https://github.com/ChillerDragon/teeworlds-client' + s.homepage = 'https://github.com/ChillerDragon/teeworlds_network' s.license = 'Unlicense' s.metadata['rubygems_mfa_required'] = 'true' end