Build 0.21.6.0 (1.0 pre-patch)
This commit is contained in:
@@ -1,209 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
using Steamworks.Data;
|
||||
|
||||
namespace Steamworks
|
||||
{
|
||||
internal static class SourceServerQuery
|
||||
{
|
||||
private static readonly byte[] A2S_SERVERQUERY_GETCHALLENGE = { 0x55, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
// private static readonly byte A2S_PLAYER = 0x55;
|
||||
private const byte A2S_RULES = 0x56;
|
||||
|
||||
private static readonly Dictionary<IPEndPoint, Task<Dictionary<string, string>>> PendingQueries =
|
||||
new Dictionary<IPEndPoint, Task<Dictionary<string, string>>>();
|
||||
|
||||
private static HashSet<int> activeRequests = new HashSet<int>();
|
||||
private static int lastRequestId = 0;
|
||||
|
||||
internal static Task<Dictionary<string, string>> GetRules( ServerInfo server )
|
||||
{
|
||||
var endpoint = new IPEndPoint(server.Address, server.QueryPort);
|
||||
|
||||
lock (PendingQueries)
|
||||
{
|
||||
if (PendingQueries.TryGetValue(endpoint, out var pending))
|
||||
return pending;
|
||||
|
||||
var task = GetRulesImpl( endpoint )
|
||||
.ContinueWith(t =>
|
||||
{
|
||||
lock (PendingQueries)
|
||||
{
|
||||
PendingQueries.Remove(endpoint);
|
||||
}
|
||||
|
||||
return t;
|
||||
})
|
||||
.Unwrap();
|
||||
|
||||
PendingQueries.Add(endpoint, task);
|
||||
return task;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<Dictionary<string, string>> GetRulesImpl( IPEndPoint endpoint )
|
||||
{
|
||||
int currId;
|
||||
lock (activeRequests)
|
||||
{
|
||||
lastRequestId++;
|
||||
currId = lastRequestId;
|
||||
activeRequests.Add(currId);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await Task.Yield();
|
||||
while (true)
|
||||
{
|
||||
lock (activeRequests)
|
||||
{
|
||||
if (!activeRequests.Any() || (currId - activeRequests.Min()) < 25) { break; }
|
||||
}
|
||||
await Task.Delay(25);
|
||||
}
|
||||
|
||||
using (var client = new UdpClient())
|
||||
{
|
||||
client.Client.SendTimeout = 3000;
|
||||
client.Client.ReceiveTimeout = 3000;
|
||||
client.Connect(endpoint);
|
||||
|
||||
return await GetRules(client);
|
||||
}
|
||||
}
|
||||
catch (System.Exception)
|
||||
{
|
||||
//Console.Error.WriteLine( e.Message );
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
lock (activeRequests)
|
||||
{
|
||||
activeRequests.Remove(currId);
|
||||
}
|
||||
}
|
||||
private enum Status
|
||||
{
|
||||
Pending,
|
||||
Failure,
|
||||
Success
|
||||
}
|
||||
|
||||
static async Task<Dictionary<string, string>> GetRules( UdpClient client )
|
||||
private static readonly HashSet<SteamMatchmakingRulesResponse> ruleResponseHandlers
|
||||
= new HashSet<SteamMatchmakingRulesResponse>();
|
||||
|
||||
internal static async Task<Dictionary<string, string>> GetRules(Steamworks.Data.ServerInfo server)
|
||||
{
|
||||
var challengeBytes = await GetChallengeData( client );
|
||||
challengeBytes[0] = A2S_RULES;
|
||||
await Send( client, challengeBytes );
|
||||
var ruleData = await Receive( client );
|
||||
Status status = Status.Pending;
|
||||
|
||||
var rules = new Dictionary<string, string>();
|
||||
|
||||
using ( var br = new BinaryReader( new MemoryStream( ruleData ) ) )
|
||||
{
|
||||
if ( br.ReadByte() != 0x45 )
|
||||
throw new Exception( "Invalid data received in response to A2S_RULES request" );
|
||||
SteamMatchmakingRulesResponse responseHandler = null;
|
||||
|
||||
var numRules = br.ReadUInt16();
|
||||
for ( int index = 0; index < numRules; index++ )
|
||||
{
|
||||
rules.Add( br.ReadNullTerminatedUTF8String(), br.ReadNullTerminatedUTF8String() );
|
||||
}
|
||||
void onRulesResponded(string key, string value)
|
||||
=> rules.Add(key, value);
|
||||
|
||||
void onRulesFailToRespond()
|
||||
{
|
||||
finish(Status.Failure);
|
||||
}
|
||||
|
||||
return rules;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static async Task<byte[]> Receive( UdpClient client )
|
||||
{
|
||||
byte[][] packets = null;
|
||||
byte packetNumber = 0, packetCount = 1;
|
||||
|
||||
do
|
||||
void onRulesRefreshComplete()
|
||||
{
|
||||
Task<UdpReceiveResult> result = client.ReceiveAsync();
|
||||
await Task.WhenAny(result, Task.Delay(3000));
|
||||
if (!result.IsCompleted)
|
||||
{
|
||||
throw new Exception("Receive timed out");
|
||||
}
|
||||
var buffer = result.Result.Buffer;
|
||||
|
||||
using ( var br = new BinaryReader( new MemoryStream( buffer ) ) )
|
||||
{
|
||||
var header = br.ReadInt32();
|
||||
|
||||
if ( header == -1 )
|
||||
{
|
||||
var unsplitdata = new byte[buffer.Length - br.BaseStream.Position];
|
||||
Buffer.BlockCopy( buffer, (int)br.BaseStream.Position, unsplitdata, 0, unsplitdata.Length );
|
||||
return unsplitdata;
|
||||
}
|
||||
else if ( header == -2 )
|
||||
{
|
||||
int requestId = br.ReadInt32();
|
||||
packetNumber = br.ReadByte();
|
||||
packetCount = br.ReadByte();
|
||||
int splitSize = br.ReadInt32();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.Exception( "Invalid Header" );
|
||||
}
|
||||
|
||||
if ( packets == null ) packets = new byte[packetCount][];
|
||||
|
||||
var data = new byte[buffer.Length - br.BaseStream.Position];
|
||||
Buffer.BlockCopy( buffer, (int)br.BaseStream.Position, data, 0, data.Length );
|
||||
packets[packetNumber] = data;
|
||||
}
|
||||
finish(Status.Success);
|
||||
}
|
||||
while ( packets.Any( p => p == null ) );
|
||||
|
||||
var combinedData = Combine( packets );
|
||||
return combinedData;
|
||||
}
|
||||
|
||||
private static async Task<byte[]> GetChallengeData( UdpClient client )
|
||||
{
|
||||
await Send( client, A2S_SERVERQUERY_GETCHALLENGE );
|
||||
|
||||
var challengeData = await Receive( client );
|
||||
|
||||
if ( challengeData[0] != 0x41 )
|
||||
throw new Exception( "Invalid Challenge" );
|
||||
|
||||
return challengeData;
|
||||
}
|
||||
|
||||
static async Task Send( UdpClient client, byte[] message )
|
||||
{
|
||||
var sendBuffer = new byte[message.Length + 4];
|
||||
|
||||
sendBuffer[0] = 0xFF;
|
||||
sendBuffer[1] = 0xFF;
|
||||
sendBuffer[2] = 0xFF;
|
||||
sendBuffer[3] = 0xFF;
|
||||
|
||||
Buffer.BlockCopy( message, 0, sendBuffer, 4, message.Length );
|
||||
|
||||
await client.SendAsync( sendBuffer, message.Length + 4 );
|
||||
}
|
||||
|
||||
static byte[] Combine( byte[][] arrays )
|
||||
{
|
||||
var rv = new byte[arrays.Sum( a => a.Length )];
|
||||
int offset = 0;
|
||||
foreach ( byte[] array in arrays )
|
||||
void finish(Status stat)
|
||||
{
|
||||
Buffer.BlockCopy( array, 0, rv, offset, array.Length );
|
||||
offset += array.Length;
|
||||
if (status == Status.Pending) { status = stat; }
|
||||
|
||||
var handler = responseHandler;
|
||||
if (handler is null) { return; }
|
||||
|
||||
lock (ruleResponseHandlers)
|
||||
{
|
||||
ruleResponseHandlers.Remove(handler);
|
||||
}
|
||||
responseHandler = null;
|
||||
}
|
||||
return rv;
|
||||
|
||||
responseHandler = new SteamMatchmakingRulesResponse(
|
||||
onRulesResponded,
|
||||
onRulesFailToRespond,
|
||||
onRulesRefreshComplete);
|
||||
lock (ruleResponseHandlers)
|
||||
{
|
||||
ruleResponseHandlers.Add(responseHandler);
|
||||
}
|
||||
|
||||
var query = SteamMatchmakingServers.Internal.ServerRules(
|
||||
server.AddressRaw, (ushort)server.QueryPort, (IntPtr)responseHandler);
|
||||
|
||||
while (status == Status.Pending)
|
||||
{
|
||||
await Task.Delay(25);
|
||||
}
|
||||
|
||||
SteamMatchmakingServers.Internal.CancelServerQuery(query);
|
||||
|
||||
return status == Status.Success ? rules : null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user