Facebook
From a.pvp, 9 Years ago, written in Visual Basic.
Embed
Download Paste or View Raw
Hits: 675
  1. Imports System.IO
  2. Imports System.Net
  3. Imports System.Net.Sockets
  4.  
  5. Namespace Query
  6.         Class RCONQuery
  7.                 Private qSocket As Socket
  8.                 Private address As IPAddress
  9.                 Private _port As Integer = 0
  10.                 Private _password As String = Nothing
  11.  
  12.                 Private results As String() = New String(49) {}
  13.                 Private _count As Integer = 0
  14.  
  15.                 Public Sub New(IP As String, port As Integer, password As String)
  16.                         qSocket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
  17.  
  18.                         qSocket.SendTimeout = 5000
  19.                         qSocket.ReceiveTimeout = 5000
  20.  
  21.                         Try
  22.                                 address = Dns.GetHostAddresses(IP)(0)
  23.  
  24.  
  25.                         Catch
  26.                         End Try
  27.  
  28.                         _port = port
  29.                         _password = password
  30.                 End Sub
  31.  
  32.                 Public Function Send(command As String) As Boolean
  33.                         Try
  34.                                 Dim endpoint As New IPEndPoint(address, _port)
  35.  
  36.                                 Using stream As New MemoryStream()
  37.                                         Using writer As New BinaryWriter(stream)
  38.                                                 writer.Write("SAMP".ToCharArray())
  39.  
  40.                                                 Dim SplitIP As String() = address.ToString().Split("."C)
  41.  
  42.                                                 writer.Write(Convert.ToByte(Convert.ToInt32(SplitIP(0))))
  43.                                                 writer.Write(Convert.ToByte(Convert.ToInt32(SplitIP(1))))
  44.                                                 writer.Write(Convert.ToByte(Convert.ToInt32(SplitIP(2))))
  45.                                                 writer.Write(Convert.ToByte(Convert.ToInt32(SplitIP(3))))
  46.  
  47.                                                 writer.Write(CUShort(_port))
  48.  
  49.                                                 writer.Write("x"C)
  50.  
  51.                                                 writer.Write(CUShort(_password.Length))
  52.                                                 writer.Write(_password.ToCharArray())
  53.  
  54.                                                 writer.Write(CUShort(command.Length))
  55.                                                 writer.Write(command.ToCharArray())
  56.                                         End Using
  57.  
  58.                                         If qSocket.SendTo(stream.ToArray(), endpoint) > 0 Then
  59.                                                 Return True
  60.                                         End If
  61.                                 End Using
  62.  
  63.                         Catch
  64.                                 Return False
  65.                         End Try
  66.  
  67.                         Return False
  68.                 End Function
  69.  
  70.                 Public Function Rceive() As Integer
  71.                         Try
  72.                                 For i As Integer = 0 To results.GetLength(0) - 1
  73.                                         results.SetValue(Nothing, i)
  74.                                 Next
  75.  
  76.                                 _count = 0
  77.  
  78.                                 Dim endpoint As EndPoint = New IPEndPoint(address, _port)
  79.  
  80.                                 Dim rBuffer As Byte() = New Byte(499) {}
  81.  
  82.                                 Dim count As Integer = qSocket.ReceiveFrom(rBuffer, endpoint)
  83.  
  84.                                 Using stream As New MemoryStream(rBuffer)
  85.                                         Using reader As New BinaryReader(stream)
  86.                                                 If stream.Length <= 11 Then
  87.                                                         Return _count
  88.                                                 End If
  89.  
  90.                                                 reader.ReadBytes(11)
  91.                                                 Dim len As Short
  92.  
  93.                                                 Try
  94.                                                         While (InlineAssignHelper(len, reader.ReadInt16())) <> 0
  95.                                                                 results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = New String(reader.ReadChars(Convert.ToInt32(len)))
  96.                                                         End While
  97.  
  98.                                                 Catch
  99.                                                         Return _count
  100.                                                 End Try
  101.                                         End Using
  102.                                 End Using
  103.  
  104.                         Catch
  105.                                 Return _count
  106.                         End Try
  107.  
  108.                         Return _count
  109.                 End Function
  110.  
  111.                 Public Function Store(count As Integer) As String()
  112.                         Dim rString As String() = New String(count - 1) {}
  113.  
  114.                         Dim i As Integer = 0
  115.                         While i < count AndAlso i < _count
  116.                                 rString(i) = results(i)
  117.                                 i += 1
  118.                         End While
  119.  
  120.                         _count = 0
  121.  
  122.                         Return rString
  123.                 End Function
  124.                 Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
  125.                         target = value
  126.                         Return value
  127.                 End Function
  128.         End Class
  129.  
  130.         Class Query
  131.                 Private qSocket As Socket
  132.                 Private address As IPAddress
  133.                 Private _port As Integer = 0
  134.  
  135.                 Private results As String()
  136.                 Private _count As Integer = 0
  137.  
  138.                 Private timestamp As DateTime() = New DateTime(1) {}
  139.  
  140.                 Public Sub New(IP As String, port As Integer)
  141.                         qSocket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
  142.  
  143.                         qSocket.SendTimeout = 5000
  144.                         qSocket.ReceiveTimeout = 5000
  145.  
  146.                         Try
  147.                                 address = Dns.GetHostAddresses(IP)(0)
  148.  
  149.  
  150.                         Catch
  151.                         End Try
  152.  
  153.                         _port = port
  154.                 End Sub
  155.  
  156.                 Public Function Send(opcode As Char) As Boolean
  157.                         Try
  158.                                 Dim endpoint As EndPoint = New IPEndPoint(address, _port)
  159.  
  160.                                 Using stream As New MemoryStream()
  161.                                         Using writer As New BinaryWriter(stream)
  162.                                                 writer.Write("SAMP".ToCharArray())
  163.  
  164.                                                 Dim SplitIP As String() = address.ToString().Split("."C)
  165.  
  166.                                                 writer.Write(Convert.ToByte(Convert.ToInt32(SplitIP(0))))
  167.                                                 writer.Write(Convert.ToByte(Convert.ToInt32(SplitIP(1))))
  168.                                                 writer.Write(Convert.ToByte(Convert.ToInt32(SplitIP(2))))
  169.                                                 writer.Write(Convert.ToByte(Convert.ToInt32(SplitIP(3))))
  170.  
  171.                                                 writer.Write(CUShort(_port))
  172.  
  173.                                                 writer.Write(opcode)
  174.  
  175.                                                 If opcode = "p"C Then
  176.                                                         writer.Write("8493".ToCharArray())
  177.                                                 End If
  178.  
  179.                                                 timestamp(0) = DateTime.Now
  180.                                         End Using
  181.  
  182.                                         If qSocket.SendTo(stream.ToArray(), endpoint) > 0 Then
  183.                                                 Return True
  184.                                         End If
  185.                                 End Using
  186.  
  187.                         Catch
  188.                                 Return False
  189.                         End Try
  190.  
  191.                         Return False
  192.                 End Function
  193.  
  194.                 Public Function Receive() As Integer
  195.                         Try
  196.                                 _count = 0
  197.  
  198.                                 Dim endpoint As EndPoint = New IPEndPoint(address, _port)
  199.  
  200.                                 Dim rBuffer As Byte() = New Byte(3401) {}
  201.                                 qSocket.ReceiveFrom(rBuffer, endpoint)
  202.  
  203.                                 timestamp(1) = DateTime.Now
  204.  
  205.                                 Using stream As New MemoryStream(rBuffer)
  206.                                         Using reader As New BinaryReader(stream)
  207.                                                 If stream.Length <= 10 Then
  208.                                                         Return _count
  209.                                                 End If
  210.  
  211.                                                 reader.ReadBytes(10)
  212.  
  213.                                                 Select Case reader.ReadChar()
  214.                                                         Case "i"C
  215.                                                                 ' Information
  216.                                                                 If True Then
  217.                                                                         results = New String(5) {}
  218.  
  219.                                                                         results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = Convert.ToString(reader.ReadByte())
  220.  
  221.                                                                         results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = Convert.ToString(reader.ReadInt16())
  222.  
  223.                                                                         results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = Convert.ToString(reader.ReadInt16())
  224.  
  225.                                                                         Dim hostnamelen As Integer = reader.ReadInt32()
  226.                                                                         results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = New String(reader.ReadChars(hostnamelen))
  227.  
  228.                                                                         Dim gamemodelen As Integer = reader.ReadInt32()
  229.                                                                         results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = New String(reader.ReadChars(gamemodelen))
  230.  
  231.                                                                         Dim mapnamelen As Integer = reader.ReadInt32()
  232.                                                                         results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = New String(reader.ReadChars(mapnamelen))
  233.  
  234.                                                                         Return _count
  235.                                                                 End If
  236.  
  237.                                                         Case "r"C
  238.                                                                 ' Rules
  239.                                                                 If True Then
  240.                                                                         Dim rulecount As Integer = reader.ReadInt16()
  241.  
  242.                                                                         results = New String(rulecount * 2 - 1) {}
  243.  
  244.                                                                         For i As Integer = 0 To rulecount - 1
  245.                                                                                 Dim rulelen As Integer = reader.ReadByte()
  246.                                                                                 results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = New String(reader.ReadChars(rulelen))
  247.  
  248.                                                                                 Dim valuelen As Integer = reader.ReadByte()
  249.                                                                                 results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = New String(reader.ReadChars(valuelen))
  250.                                                                         Next
  251.  
  252.                                                                         Return _count
  253.                                                                 End If
  254.  
  255.                                                         Case "c"C
  256.                                                                 ' Client list
  257.                                                                 If True Then
  258.                                                                         Dim playercount As Integer = reader.ReadInt16()
  259.  
  260.                                                                         results = New String(playercount * 2 - 1) {}
  261.  
  262.                                                                         For i As Integer = 0 To playercount - 1
  263.                                                                                 Dim namelen As Integer = reader.ReadByte()
  264.                                                                                 results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = New String(reader.ReadChars(namelen))
  265.  
  266.                                                                                 results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = Convert.ToString(reader.ReadInt32())
  267.                                                                         Next
  268.  
  269.                                                                         Return _count
  270.                                                                 End If
  271.  
  272.                                                         Case "d"C
  273.                                                                 ' Detailed player information
  274.                                                                 If True Then
  275.                                                                         Dim playercount As Integer = reader.ReadInt16()
  276.  
  277.                                                                         results = New String(playercount * 4 - 1) {}
  278.  
  279.                                                                         For i As Integer = 0 To playercount - 1
  280.                                                                                 results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = Convert.ToString(reader.ReadByte())
  281.  
  282.                                                                                 Dim namelen As Integer = reader.ReadByte()
  283.                                                                                 results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = New String(reader.ReadChars(namelen))
  284.  
  285.                                                                                 results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = Convert.ToString(reader.ReadInt32())
  286.                                                                                 results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = Convert.ToString(reader.ReadInt32())
  287.                                                                         Next
  288.  
  289.                                                                         Return _count
  290.                                                                 End If
  291.  
  292.                                                         Case "p"C
  293.                                                                 ' Ping
  294.                                                                 If True Then
  295.                                                                         results = New String(0) {}
  296.  
  297.                                                                         results(System.Math.Max(System.Threading.Interlocked.Increment(_count),_count - 1)) = timestamp(1).Subtract(timestamp(0)).Milliseconds.ToString()
  298.  
  299.                                                                         Return _count
  300.                                                                 End If
  301.                                                         Case Else
  302.  
  303.                                                                 Return _count
  304.                                                 End Select
  305.                                         End Using
  306.                                 End Using
  307.  
  308.                         Catch
  309.                                 Return _count
  310.                         End Try
  311.                 End Function
  312.  
  313.                 Public Function Store(count As Integer) As String()
  314.                         Dim rString As String() = New String(count - 1) {}
  315.  
  316.                         Dim i As Integer = 0
  317.                         While i < count AndAlso i < _count
  318.                                 rString(i) = results(i)
  319.                                 i += 1
  320.                         End While
  321.  
  322.                         _count = 0
  323.  
  324.                         Return rString
  325.                 End Function
  326.         End Class
  327. End Namespace