Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Converting Code  (Read 3965 times)

0 Members and 1 Guest are viewing this topic.

atomen

  • Newbie
  • *
  • Posts: 17
    • View Profile
Converting Code
« on: January 27, 2011, 09:46:17 pm »
Hi!

I am stuck at this piece of code which I am not quite sure on how I should
convert. For a little background; this script is used with Half Life (GoldSrc)
to retrieve the current map, number of players etc. on the server.

This is the code for the script in ruby:
Code: [Select]
# start querying servers and storing results in the "results" array
servers.each do |x|
x.chomp!
 case x.split(':')[2]
 when "hl"
   begin
    timeout(1) do
     begin
      socket = UDPSocket.new
      socket.connect(x.split(':')[0], x.split(':')[1].to_i)
      socket.send("\xff\xff\xff\xffinfostring \n", 0)
      a = socket.sysread(16384).delete("\xff").split("\\")
      results << "HL/CS: \x037 #{a[20]} -\x036 #{a[22]}\x0313
#{a[6]}/#{a[12]}\x033 IP: #{x.split(':')[0]}:#{x.split(':')[1]}"
     rescue Errno::ECONNREFUSED
      results << "HL/CS Server: #{x.split(':')[0]}:#{x.split(':')[1]} is
down at the moment (connection refused)"
     end
    end
   rescue TimeoutError
    results << "HL/CS Server: #{x.split(':')[0]}:#{x.split(':')[1]} is
down
at the moment (timed out)"
end
#End the HL query

If I understand everything correctly you are supposed to send a string of data
containing "...\xffinfostring..." to a specific address and a port. The problem lies
in the method that this script uses for retrieving this information.

I cannot interpret what socket.sysread means but is it equal to SocketUDP.Listen(port)?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Converting Code
« Reply #1 on: January 27, 2011, 09:51:49 pm »
are you using the old ruby bindings for SFML? Because the new ones doesn't include network. Or is this completely SFML unrelated? Anyway here's the inbuilt ruby UDPSocket class: http://ruby-doc.org/ruby-1.9/classes/UDPSocket.html

And here is sysread: http://ruby-doc.org/ruby-1.9/classes/IO.html#M000145
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

atomen

  • Newbie
  • *
  • Posts: 17
    • View Profile
Converting Code
« Reply #2 on: January 27, 2011, 10:19:58 pm »
No, I am writing this in c++ code and I want to translate this ruby code to
something resembling in sfml (c++). The difference with this script which
you can't do in SFML is listen to all ports (non-specified).

How am I supposed to do that? Or perhaps a better choice, how to find out
which port to listen to?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Converting Code
« Reply #3 on: January 27, 2011, 10:34:19 pm »
Code: [Select]
socket.connect(x.split(':')[0], x.split(':')[1].to_i)

THis code does set the port. Just some bad variable naming. x holds the adress to the server and the port. The method String#split returns an array of strings split at the specified character. Here it is ":" which defines where the port begins. In the first element of the array we got the actual address, on the second we have the port.

That "script" you gave is very poorly written :P Both from my perspective and from an overall ruby perspective. Too much junk in one place.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

 

anything