Skip to content

Commit a18cb33

Browse files
committed
Add trace logs
Add a check to only add to clientPool if not exist
1 parent c0f38cd commit a18cb33

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

COAN/Config.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ private Poco.Config ReadConfig
3030
{
3131
var configContent = File.ReadAllText(GetAppConfig);
3232
if (!string.IsNullOrWhiteSpace(configContent))
33+
{
34+
logger.Log(LogLevel.Trace, "Config found");
3335
return JsonConvert.DeserializeObject<Poco.Config>(configContent);
36+
}
3437
}
3538
return null;
3639
}

COAN/Network/NetworkClient.cs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Reflection;
33
using System.Threading;
44
using System.Net.Sockets;
5-
using System.Windows.Forms;
65
using NLog;
76
using System.Collections.Generic;
87

@@ -25,7 +24,16 @@ public class NetworkClient
2524

2625
public Client GetClient(long id)
2726
{
28-
return clientPool[id];
27+
logger.Log(LogLevel.Trace, string.Format("GetClient - {0}", id));
28+
try
29+
{
30+
return clientPool[id];
31+
}
32+
catch (KeyNotFoundException)
33+
{
34+
logger.Log(LogLevel.Trace, string.Format("GetClient - Key not found - {0}", id));
35+
return null;
36+
}
2937
}
3038

3139
#region Delegates
@@ -73,7 +81,7 @@ public void Disconnect()
7381
socket.Close();
7482
}
7583

76-
public bool Connect(string hostname, int port, string password)
84+
public string Connect(string hostname, int port, string password)
7785
{
7886
adminPassword = password;
7987
if (adminPassword.Length == 0 || hostname.Length == 0)
@@ -90,8 +98,8 @@ public bool Connect(string hostname, int port, string password)
9098
if ((hostname.Length) == 0)
9199
errorMessage += "password";
92100
}
93-
MessageBox.Show(errorMessage);
94-
return false;
101+
logger.Log(LogLevel.Error, errorMessage);
102+
return errorMessage;
95103
}
96104

97105

@@ -106,11 +114,11 @@ public bool Connect(string hostname, int port, string password)
106114
catch (Exception ex)
107115
{
108116
var errorMessage = string.Format("An error occurred while trying to connect to: {0} - Error message: {1)", hostname, ex.Message);
109-
MessageBox.Show(errorMessage);
110-
return false;
117+
logger.Log(LogLevel.Error, errorMessage);
118+
return errorMessage;
111119
}
112120
Start();
113-
return true;
121+
return null;
114122
}
115123

116124
public void chatPublic(string msg)
@@ -173,6 +181,7 @@ public void delegatePacket(Packet p)
173181
#region Polls
174182
public void pollCmdNames()
175183
{
184+
logger.Log(LogLevel.Trace, "pollCmdNames");
176185
sendAdminPoll(AdminUpdateType.ADMIN_UPDATE_CMD_NAMES);
177186
}
178187

@@ -182,6 +191,7 @@ public void pollCmdNames()
182191
/// <param name="clientId">Optional parameter specifying the Client ID to get info on</param>
183192
public void pollClientInfos(long clientId = long.MaxValue)
184193
{
194+
logger.Log(LogLevel.Trace, "pollClientInfos");
185195
sendAdminPoll(AdminUpdateType.ADMIN_UPDATE_CLIENT_INFO, clientId);
186196
}
187197

@@ -191,21 +201,25 @@ public void pollClientInfos(long clientId = long.MaxValue)
191201
/// <param name="companyId">Optional parameter specifying the Company ID to get info on</param>
192202
public void pollCompanyInfos(long companyId = long.MaxValue)
193203
{
204+
logger.Log(LogLevel.Trace, "pollCompanyInfos");
194205
sendAdminPoll(AdminUpdateType.ADMIN_UPDATE_CLIENT_INFO, companyId);
195206
}
196207

197208
public void pollCompanyStats()
198209
{
210+
logger.Log(LogLevel.Trace, "pollCompanyStats");
199211
sendAdminPoll(AdminUpdateType.ADMIN_UPDATE_COMPANY_STATS);
200212
}
201213

202214
public void pollCompanyEconomy()
203215
{
216+
logger.Log(LogLevel.Trace, "pollCompanyEconomy");
204217
sendAdminPoll(AdminUpdateType.ADMIN_UPDATE_COMPANY_ECONOMY);
205218
}
206219

207220
public void pollDate()
208221
{
222+
logger.Log(LogLevel.Trace, "pollDate");
209223
sendAdminPoll(AdminUpdateType.ADMIN_UPDATE_DATE);
210224
}
211225
#endregion
@@ -226,6 +240,7 @@ public void sendAdminJoin()
226240

227241
public void sendAdminChat(NetworkAction action, DestType type, long dest, String msg, long data)
228242
{
243+
logger.Log(LogLevel.Trace, "sendAdminChat");
229244
Packet p = new Packet(getSocket(), PacketType.ADMIN_PACKET_ADMIN_CHAT);
230245
p.writeUint8((short)action);
231246
p.writeUint8((short)type);
@@ -241,15 +256,15 @@ public void sendAdminChat(NetworkAction action, DestType type, long dest, String
241256

242257
public void sendAdminGameScript(string command)
243258
{
259+
logger.Log(LogLevel.Trace, string.Format("sendAdminGameScript - command: {0}", command));
244260
Packet p = new Packet(getSocket(), PacketType.ADMIN_PACKET_ADMIN_GAMESCRIPT);
245-
246-
247261
p.WriteString(command); // JSON encode
248262
NetworkOutputThread.append(p);
249263
}
250264

251265
public void sendAdminUpdateFrequency(AdminUpdateType type, AdminUpdateFrequency freq)
252266
{
267+
logger.Log(LogLevel.Trace, "sendAdminUpdateFrequency");
253268
if (getProtocol().isSupported(type, freq) == false)
254269
throw new ArgumentException("The server does not support " + freq + " for " + type);
255270

@@ -262,6 +277,7 @@ public void sendAdminUpdateFrequency(AdminUpdateType type, AdminUpdateFrequency
262277

263278
public void sendAdminPoll(AdminUpdateType type, long data = 0)
264279
{
280+
logger.Log(LogLevel.Trace, "AdminUpdateType");
265281
if (getProtocol().isSupported(type, AdminUpdateFrequency.ADMIN_FREQUENCY_POLL) == false)
266282
throw new ArgumentException("The server does not support polling for " + type);
267283

@@ -272,11 +288,12 @@ public void sendAdminPoll(AdminUpdateType type, long data = 0)
272288
NetworkOutputThread.append(p);
273289
}
274290

275-
#endregion
291+
#endregion
276292

277-
#region Receive Packets
293+
#region Receive Packets
278294
public void receiveServerClientInfo(Packet p)
279295
{
296+
logger.Log(LogLevel.Trace, "receiveServerClientInfo");
280297
Client client = new Client(p.readUint32())
281298
{
282299
address = p.readString(),
@@ -287,13 +304,15 @@ public void receiveServerClientInfo(Packet p)
287304
client.joindate = new GameDate(p.readUint32());
288305
client.companyId = p.readUint8();
289306

290-
clientPool.Add(client.clientId, client);
307+
if (GetClient(client.clientId) == null)
308+
clientPool.Add(client.clientId, client);
291309

292310
OnClientInfo?.Invoke(client);
293311
}
294312

295313
public void receiveServerProtocol(Packet p)
296314
{
315+
logger.Log(LogLevel.Trace, "receiveServerProtocol");
297316
Protocol protocol = getProtocol();
298317

299318
protocol.version = p.readUint8();
@@ -319,7 +338,7 @@ public void receiveServerProtocol(Packet p)
319338

320339
public void receiveServerWelcome(Packet p)
321340
{
322-
341+
logger.Log(LogLevel.Trace, "receiveServerWelcome");
323342
Map map = new Map
324343
{
325344
name = p.readString(),
@@ -344,6 +363,7 @@ public void receiveServerWelcome(Packet p)
344363

345364
public void receiveServerChat(Packet p)
346365
{
366+
logger.Log(LogLevel.Trace, "receiveServerChat");
347367
NetworkAction action = (NetworkAction) p.readUint8();
348368
DestType dest = (DestType) p.readUint8();
349369
long clientId = p.readUint32();
@@ -356,6 +376,7 @@ public void receiveServerChat(Packet p)
356376

357377
public void receiveServerCmdNames(Packet p)
358378
{
379+
logger.Log(LogLevel.Trace, "receiveServerCmdNames");
359380
while (p.readBool())
360381
{
361382
int cmdId = p.readUint16();
@@ -367,7 +388,7 @@ public void receiveServerCmdNames(Packet p)
367388

368389
public void receiveServerCmdLogging(Packet p)
369390
{
370-
391+
logger.Log(LogLevel.Trace, "receiveServerCmdLogging");
371392
}
372393
#endregion
373394

COAN/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
using System;
22
using System.Windows.Forms;
3+
using NLog;
34

45
namespace COAN
56
{
67
public static class Program
78
{
9+
public static Logger logger = LogManager.GetCurrentClassLogger();
10+
811
/// <summary>
912
/// The main entry point for the application.
1013
/// </summary>
1114
[STAThread]
1215
public static void Main()
1316
{
17+
logger.Log(LogLevel.Trace, "App open");
18+
1419
Application.EnableVisualStyles();
1520
Application.SetCompatibleTextRenderingDefault(false);
1621
Application.Run(new Form1());

0 commit comments

Comments
 (0)