Code cleanup

This commit is contained in:
Brett Hewitson 2021-06-08 21:34:43 +10:00
parent 94620c6cac
commit 027ba0918a

View file

@ -1,10 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@ -59,29 +57,28 @@ namespace QueryMaster
/// Retrieves information about the server /// Retrieves information about the server
/// </summary> /// </summary>
/// <returns>Instance of ServerInfo class</returns> /// <returns>Instance of ServerInfo class</returns>
public virtual ServerInfo GetInfo(byte[] data=null) public virtual ServerInfo GetInfo(byte[] data = null)
{ {
// If we already have pre-formed request in data var // If we already have pre-formed request in data parameter, we will populate Query with it.
// we will populate Query with it byte[] query;
// also let's declare Query earlier
byte[] Query;
if (data != null && data.Length > 0) if (data != null && data.Length > 0)
{ {
Query = data; query = data;
} }
else else
{ {
Query = QueryMsg.InfoQuery;
if (IsObsolete) if (IsObsolete)
Query = QueryMsg.ObsoleteInfoQuery; query = QueryMsg.ObsoleteInfoQuery;
else
query = QueryMsg.InfoQuery;
} }
byte[] recvData = new byte[socket.BufferSize]; var sw = Stopwatch.StartNew();
Stopwatch sw = Stopwatch.StartNew(); var recvData = socket.GetResponse(query, Type);
recvData = socket.GetResponse(Query, Type);
sw.Stop(); sw.Stop();
Latency = sw.ElapsedMilliseconds; Latency = sw.ElapsedMilliseconds;
try try
{ {
// first, let's check if we have a challenge received instead of S2A_INFO_SRC // first, let's check if we have a challenge received instead of S2A_INFO_SRC
@ -94,8 +91,9 @@ namespace QueryMaster
// we will get current Query and append recvData to its end. Skipping 0x41 and // we will get current Query and append recvData to its end. Skipping 0x41 and
// honoring the trailing 0x00 at the Query end // honoring the trailing 0x00 at the Query end
byte[] signedQuery = new byte[Query.Length + (recvData.Length - 1)]; var signedQuery = new byte[query.Length + (recvData.Length - 1)];
Query.CopyTo(signedQuery, 0); query.CopyTo(signedQuery, 0);
for (int bId = (recvData.Length - 1); bId > 0; bId--) for (int bId = (recvData.Length - 1); bId > 0; bId--)
{ {
signedQuery[signedQuery.Length - bId] = recvData[recvData.Length - bId]; signedQuery[signedQuery.Length - bId] = recvData[recvData.Length - bId];
@ -108,16 +106,14 @@ namespace QueryMaster
switch (recvData[0]) switch (recvData[0])
{ {
case 0x49: return Current(recvData); case 0x49:
case 0x6D: return Obsolete(recvData); return Current(recvData);
default: throw new InvalidHeaderException("packet header is not valid"); case 0x6D:
return Obsolete(recvData);
default:
throw new InvalidHeaderException("packet header is not valid");
} }
} }
// this patch must not have any impact in cases S2C_CHALLENGE never arrives
// hope, it will help those who are experiencing "stuck at initializing/waiting for publication"
// problem. ~FH
catch (Exception e) catch (Exception e)
{ {
e.Data.Add("ReceivedData", recvData); e.Data.Add("ReceivedData", recvData);