Skip to content

Commit b7001b4

Browse files
committed
Added IsConnected property
1 parent 99301fb commit b7001b4

File tree

5 files changed

+30
-50
lines changed

5 files changed

+30
-50
lines changed

MCQuery/Connection.cs

+9-13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ protected Connection(string address, int port)
1919
_port = port;
2020
}
2121

22+
public abstract bool IsConnected { get; }
23+
2224
protected byte[] SendByUdp(string address, int port, byte[] data)
2325
{
2426
try
@@ -86,14 +88,14 @@ protected bool IsDomainAddress(string address)
8688

8789
protected byte[] SendByTcp(string address, int port, byte[] data)
8890
{
89-
//TODO: Implement sending packet by TCP.
90-
9191
try
9292
{
9393
if (_tcpClient == null)
9494
{
95-
_tcpClient = new TcpClient(address, port);
96-
_tcpClient.ReceiveTimeout = 5000;
95+
_tcpClient = new TcpClient(address, port)
96+
{
97+
ReceiveTimeout = 5000
98+
};
9799
}
98100

99101
NetworkStream networkStream = _tcpClient.GetStream();
@@ -114,16 +116,10 @@ protected byte[] SendByTcp(string address, int port, byte[] data)
114116

115117
public virtual void Close()
116118
{
117-
if(_udpClient != null)
118-
{
119-
_udpClient.Close();
120-
}
121-
if (_tcpClient != null)
122-
{
123-
_tcpClient.Close();
124-
}
119+
_udpClient?.Close();
120+
_tcpClient?.Close();
125121

126-
Dispose();
122+
Dispose();
127123
}
128124

129125
#region IDisposable Support

MCQuery/MCQuery.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
<Compile Include="Properties\AssemblyInfo.cs" />
4646
<Compile Include="Query.cs" />
4747
<Compile Include="Rcon.cs" />
48-
<Compile Include="Request.cs" />
4948
<Compile Include="Server.cs" />
5049
</ItemGroup>
5150
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

MCQuery/Query.cs

+13-20
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public class Query : Connection
2121

2222
//Byte[] - but written as byte array - Challenge Token
2323
private byte[] _challengeToken;
24-
25-
private readonly Timer _challengeTimer = new Timer();
24+
private readonly Timer _challengeTimer = new Timer();
2625

2726
public Query(string address, int port) : base(address, port)
2827
{
@@ -72,12 +71,9 @@ private byte[] GetBasicStat(string address, int port)
7271
byte[] tcpResponse = SendByTcp(address, port, basicStatMessage);
7372

7473
if (tcpResponse.Length == 0) return new byte[] { };
75-
else return tcpResponse;
76-
}
77-
else
78-
{
79-
return udpResponse;
74+
return tcpResponse;
8075
}
76+
return udpResponse;
8177
}
8278

8379
private byte[] GetFullStat(string address, int port)
@@ -97,12 +93,9 @@ private byte[] GetFullStat(string address, int port)
9793
byte[] tcpResponse = SendByTcp(address, port, fullStatMessage);
9894

9995
if (tcpResponse.Length == 0) return new byte[] { };
100-
else return tcpResponse;
101-
}
102-
else
103-
{
104-
return udpResponse;
96+
return tcpResponse;
10597
}
98+
return udpResponse;
10699
}
107100

108101
public Server GetBasicServerInfo()
@@ -205,7 +198,7 @@ private byte[] GetChallengeToken(byte[] message)
205198
//Index 1 - 4 = SessionId
206199
//Index 5 and further is a challenge token which we need to extract.
207200

208-
byte[] challengeToken = new byte[message.Length - 5];
201+
//byte[] challengeToken = new byte[message.Length - 5];
209202

210203
string response = "";
211204

@@ -214,13 +207,12 @@ private byte[] GetChallengeToken(byte[] message)
214207
if (i >= 5)
215208
{
216209
byte item = message[i];
217-
response += Encoding.ASCII.GetString(new byte[] { item });
210+
response += Encoding.ASCII.GetString(new[] { item });
218211
}
219212
}
220213
Int32 tokenInt32 = Int32.Parse(response);
221214

222-
byte[] challenge = new byte[]
223-
{
215+
byte[] challenge = {
224216
(byte)(tokenInt32 >> 24 & 0xFF),
225217
(byte)(tokenInt32 >> 16 & 0xFF),
226218
(byte)(tokenInt32 >> 8 & 0xFF),
@@ -236,9 +228,10 @@ private void RegenerateChallengeToken(Object sender, ElapsedEventArgs e)
236228
Handshake(_address, _port);
237229
}
238230

239-
public override void Close()
240-
{
241-
base.Close();
242-
}
231+
public override bool IsConnected
232+
{
233+
get => _challengeToken != null || _challengeToken.Length > 0;
234+
set => _isAuthenticated = value;
235+
}
243236
}
244237
}

MCQuery/Rcon.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Rcon : Connection
1414
private readonly byte[] _twoBytePad = { 0x00, 0x00 };
1515

1616
private string _password;
17+
private bool _isAuthenticated;
1718

1819
public Rcon(string address, int port, string password) : base(address, port)
1920
{
@@ -41,18 +42,17 @@ public bool Login()
4142
message.AddRange(_twoBytePad);
4243

4344
byte[] response = SendByTcp(_address, _port, message.ToArray());
44-
bool authenticated = false;
4545

4646
foreach (byte item in response)
4747
{
4848
if (reminder[0] == item)
4949
{
50-
authenticated = true;
50+
_isAuthenticated = true;
5151
break;
5252
}
5353
}
5454

55-
return authenticated;
55+
return _isAuthenticated;
5656
}
5757

5858
public bool SendCommand(string command)
@@ -81,5 +81,10 @@ public bool SendCommand(string command)
8181

8282
return didSucceeed;
8383
}
84+
85+
public override bool IsConnected
86+
{
87+
get => _isAuthenticated;
88+
}
8489
}
8590
}

MCQuery/Request.cs

-13
This file was deleted.

0 commit comments

Comments
 (0)