02-17-2012, 12:23 AM
Hello peoples.
Last 2 days I have been making my own query library for Source.
Already got the query to the "normal" servers working but can't seem to get the master server query working, it wont allow me to get the second set of IP's my current code looks like this:
Any one got an idea?
Last 2 days I have been making my own query library for Source.
Already got the query to the "normal" servers working but can't seem to get the master server query working, it wont allow me to get the second set of IP's my current code looks like this:
Code:
namespace Nisd.ServerQuery.ConsoleTest
{
using System;
using Nisd.ServerQuery.Source;
class Program
{
static void Main(string[] args)
{
MasterServerQuery masterServer = new MasterServerQuery();
masterServer.Progress += new EventHandler<MasterServerProgress>(masterServer_Progress);
masterServer.FethcServerList(0xFF, null);
Console.WriteLine("Done");
Console.ReadLine();
masterServer.Dispose();
}
static void masterServer_Progress(object sender, MasterServerProgress e)
{
Console.WriteLine("************************");
foreach (var item in e.ServerAddresses)
{
Console.WriteLine(item);
}
}
}
}
Code:
namespace Nisd.ServerQuery.Source
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class MasterServerQuery : IDisposable
{
public event EventHandler<MasterServerProgress> Progress;
protected bool disposed;
protected UdpClient socket;
IPEndPoint masterServer;
public MasterServerQuery()
{
masterServer = new IPEndPoint(Dns.GetHostEntry("hl2master.steampowered.com").AddressList[1], 27011);
socket = new UdpClient(new IPEndPoint(IPAddress.Any, 6666));
socket.Connect(masterServer);
}
public void FethcServerList(byte region, string filter)
{
FethcServerList(region, new IPEndPoint(new IPAddress(0), 0), filter);
}
private void FethcServerList(byte region, IPEndPoint ipAddress, string filter)
{
byte[] ipAddressBuffer = Encoding.Default.GetBytes(ipAddress.ToString());
byte[] filterBuffer = filter != null ? Encoding.Default.GetBytes(filter) : new byte[0];
byte[] buffer = new byte[4 + ipAddressBuffer.Length + filterBuffer.Length];
buffer[0] = 0x31; // Message Type
buffer[1] = region; // Region Code
Array.Copy(ipAddressBuffer, 0, buffer, 2, ipAddressBuffer.Length);
Array.Copy(filterBuffer, 0, buffer, 3 + ipAddressBuffer.Length, filterBuffer.Length);
string hex = ToHexString(buffer); // This is for debug
socket.BeginReceive(EndReceive, new { region = region, filter = filter });
socket.Send(buffer, buffer.Length);
}
protected void EndReceive(IAsyncResult result)
{
if (disposed)
return;
byte[] buffer = socket.EndReceive(result, ref masterServer);
bool theEnd = false;
if (Progress != null)
{
List<IPEndPoint> listResult = new List<IPEndPoint>();
using (MemoryStream ms = new MemoryStream(buffer))
{
byte[] tempBuff = new byte[4];
ms.Seek(6, SeekOrigin.Current);
while (ms.Length > ms.Position)
{
ms.Read(tempBuff, 0, 4);
IPAddress ipAddress = new IPAddress(tempBuff);
ms.Read(tempBuff, 0, 2);
ushort port = BitConverter.ToUInt16(tempBuff, 0);
if (port == 0)
theEnd = true;
else
listResult.Add(new IPEndPoint(ipAddress, port));
}
}
dynamic userState = result.AsyncState;
if (!theEnd)
FethcServerList(userState.region, listResult.Last(), userState.filter);
Progress(this, new MasterServerProgress()
{
ServerAddresses = listResult.ToArray()
});
}
}
protected string ToHexString(byte[] buffer)
{
StringBuilder sb = new StringBuilder();
foreach (var item in buffer)
{
sb.Append(item.ToString("X2"));
sb.Append(" ");
}
return sb.ToString();
}
public void Dispose()
{
if (!disposed)
{
disposed = true;
socket.Close();
}
}
}
}
Any one got an idea?