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
This commit is contained in:
ChillerDragon 2022-11-09 08:40:17 +01:00
parent 3a39d3f9f8
commit 6c005497d4
18 changed files with 42 additions and 21 deletions

View file

@ -1,5 +1,5 @@
# teeworlds-client # teeworlds_network
A teeworlds 0.7 client library written in ruby A teeworlds 0.7 client & server library written in ruby
## Sample ## Sample
@ -11,7 +11,7 @@ Also properly disconnect when the program is killed gracefully.
For more sample usages checkout the [examples/](examples/) folder. For more sample usages checkout the [examples/](examples/) folder.
```ruby ```ruby
require_relative 'lib/teeworlds-client' require_relative 'lib/teeworlds_client'
client = TeeworldsClient.new(verbose: false) client = TeeworldsClient.new(verbose: false)

View file

@ -1,7 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require_relative 'lib/teeworlds-client' require_relative 'lib/teeworlds_client'
args = { verbose: false, ip: nil, port: nil } args = { verbose: false, ip: nil, port: nil }

View file

@ -3,7 +3,7 @@
# Print all incoming chat messages to stdout # Print all incoming chat messages to stdout
require_relative '../lib/teeworlds-client' require_relative '../lib/teeworlds_client'
client = TeeworldsClient.new(verbose: false) client = TeeworldsClient.new(verbose: false)

View file

@ -3,7 +3,7 @@
# Prints out all network traffic # Prints out all network traffic
require_relative '../lib/teeworlds-client' require_relative '../lib/teeworlds_client'
client = TeeworldsClient.new(verbose: true) client = TeeworldsClient.new(verbose: true)

View file

@ -3,7 +3,7 @@
# Chat spamming client # Chat spamming client
require_relative '../lib/teeworlds-client' require_relative '../lib/teeworlds_client'
client = TeeworldsClient.new(verbose: true) client = TeeworldsClient.new(verbose: true)

View file

@ -3,7 +3,7 @@
# Set custom skin and other player infos # Set custom skin and other player infos
require_relative '../lib/teeworlds-client' require_relative '../lib/teeworlds_client'
client = TeeworldsClient.new(verbose: true) client = TeeworldsClient.new(verbose: true)

View file

@ -7,7 +7,7 @@
# #
# Then connect to localhost and write !ping in the chat # 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) client = TeeworldsClient.new(verbose: false)

View file

@ -1,7 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require_relative '../lib/teeworlds-client' require_relative '../lib/teeworlds_client'
client = TeeworldsClient.new client = TeeworldsClient.new

View file

@ -1,7 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require_relative '../lib/teeworlds-client' require_relative '../lib/teeworlds_client'
client = TeeworldsClient.new client = TeeworldsClient.new

View file

@ -1,7 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require_relative '../lib/teeworlds-client' require_relative '../lib/teeworlds_client'
client = TeeworldsClient.new client = TeeworldsClient.new

View file

@ -1,7 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require_relative '../lib/teeworlds-client' require_relative '../lib/teeworlds_client'
client = TeeworldsClient.new(verbose: false) client = TeeworldsClient.new(verbose: false)

View file

@ -1,7 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require_relative '../lib/teeworlds-client' require_relative '../lib/teeworlds_client'
client = TeeworldsClient.new(verbose: false) client = TeeworldsClient.new(verbose: false)

View file

@ -7,4 +7,8 @@ class NetAddr
@ip = ip @ip = ip
@port = port @port = port
end end
def to_s
"#{@ip}:#{@port}"
end
end end

1
lib/teeworlds-client.rb → lib/teeworlds_client.rb Executable file → Normal file
View file

@ -1,4 +1,3 @@
#!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require 'socket' require 'socket'

4
lib/teeworlds_network.rb Normal file
View file

@ -0,0 +1,4 @@
# frozen_string_literal: true
require_relative 'teeworlds_server'
require_relative 'teeworlds_client'

View file

@ -1,4 +1,3 @@
#!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require 'socket' require 'socket'
@ -47,6 +46,9 @@ class TeeworldsServer
puts "got ctrl msg: #{msg}" puts "got ctrl msg: #{msg}"
case msg case msg
when NET_CTRLMSG_TOKEN then on_ctrl_token(packet) 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 else
puts "Uknown control message #{msg}" puts "Uknown control message #{msg}"
exit(1) exit(1)
@ -65,6 +67,18 @@ class TeeworldsServer
send_ctrl_with_token(packet.addr, token) send_ctrl_with_token(packet.addr, token)
end 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) def on_packet(packet)
# process connless packets data # process connless packets data
if packet.flags_control if packet.flags_control

View file

@ -1,7 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require_relative 'lib/srv' require_relative 'lib/teeworlds_server'
srv = TeeworldsServer.new(verbose: false) srv = TeeworldsServer.new(verbose: false)
srv.run('127.0.0.1', 8303) srv.run('127.0.0.1', 8303)

View file

@ -3,12 +3,12 @@
require 'rake' require 'rake'
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = 'teeworlds-client' s.name = 'teeworlds_network'
s.version = '0.0.1' 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 s.description = <<-DESC
A library wrapping the network protocol of the game teeworlds. 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 DESC
s.authors = ['ChillerDragon'] s.authors = ['ChillerDragon']
s.email = 'ChillerDragon@gmail.com' s.email = 'ChillerDragon@gmail.com'
@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.1.2' s.required_ruby_version = '>= 3.1.2'
s.add_dependency 'huffman_tw', '~> 0.0.1' s.add_dependency 'huffman_tw', '~> 0.0.1'
s.add_dependency 'rspec', '~> 3.9.0' 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.license = 'Unlicense'
s.metadata['rubygems_mfa_required'] = 'true' s.metadata['rubygems_mfa_required'] = 'true'
end end