2
2
using System . Reflection ;
3
3
using System . Threading ;
4
4
using System . Net . Sockets ;
5
- using System . Windows . Forms ;
6
5
using NLog ;
7
6
using System . Collections . Generic ;
8
7
@@ -25,7 +24,16 @@ public class NetworkClient
25
24
26
25
public Client GetClient ( long id )
27
26
{
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
+ }
29
37
}
30
38
31
39
#region Delegates
@@ -73,7 +81,7 @@ public void Disconnect()
73
81
socket . Close ( ) ;
74
82
}
75
83
76
- public bool Connect ( string hostname , int port , string password )
84
+ public string Connect ( string hostname , int port , string password )
77
85
{
78
86
adminPassword = password ;
79
87
if ( adminPassword . Length == 0 || hostname . Length == 0 )
@@ -90,8 +98,8 @@ public bool Connect(string hostname, int port, string password)
90
98
if ( ( hostname . Length ) == 0 )
91
99
errorMessage += "password" ;
92
100
}
93
- MessageBox . Show ( errorMessage ) ;
94
- return false ;
101
+ logger . Log ( LogLevel . Error , errorMessage ) ;
102
+ return errorMessage ;
95
103
}
96
104
97
105
@@ -106,11 +114,11 @@ public bool Connect(string hostname, int port, string password)
106
114
catch ( Exception ex )
107
115
{
108
116
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 ;
111
119
}
112
120
Start ( ) ;
113
- return true ;
121
+ return null ;
114
122
}
115
123
116
124
public void chatPublic ( string msg )
@@ -173,6 +181,7 @@ public void delegatePacket(Packet p)
173
181
#region Polls
174
182
public void pollCmdNames ( )
175
183
{
184
+ logger . Log ( LogLevel . Trace , "pollCmdNames" ) ;
176
185
sendAdminPoll ( AdminUpdateType . ADMIN_UPDATE_CMD_NAMES ) ;
177
186
}
178
187
@@ -182,6 +191,7 @@ public void pollCmdNames()
182
191
/// <param name="clientId">Optional parameter specifying the Client ID to get info on</param>
183
192
public void pollClientInfos ( long clientId = long . MaxValue )
184
193
{
194
+ logger . Log ( LogLevel . Trace , "pollClientInfos" ) ;
185
195
sendAdminPoll ( AdminUpdateType . ADMIN_UPDATE_CLIENT_INFO , clientId ) ;
186
196
}
187
197
@@ -191,21 +201,25 @@ public void pollClientInfos(long clientId = long.MaxValue)
191
201
/// <param name="companyId">Optional parameter specifying the Company ID to get info on</param>
192
202
public void pollCompanyInfos ( long companyId = long . MaxValue )
193
203
{
204
+ logger . Log ( LogLevel . Trace , "pollCompanyInfos" ) ;
194
205
sendAdminPoll ( AdminUpdateType . ADMIN_UPDATE_CLIENT_INFO , companyId ) ;
195
206
}
196
207
197
208
public void pollCompanyStats ( )
198
209
{
210
+ logger . Log ( LogLevel . Trace , "pollCompanyStats" ) ;
199
211
sendAdminPoll ( AdminUpdateType . ADMIN_UPDATE_COMPANY_STATS ) ;
200
212
}
201
213
202
214
public void pollCompanyEconomy ( )
203
215
{
216
+ logger . Log ( LogLevel . Trace , "pollCompanyEconomy" ) ;
204
217
sendAdminPoll ( AdminUpdateType . ADMIN_UPDATE_COMPANY_ECONOMY ) ;
205
218
}
206
219
207
220
public void pollDate ( )
208
221
{
222
+ logger . Log ( LogLevel . Trace , "pollDate" ) ;
209
223
sendAdminPoll ( AdminUpdateType . ADMIN_UPDATE_DATE ) ;
210
224
}
211
225
#endregion
@@ -226,6 +240,7 @@ public void sendAdminJoin()
226
240
227
241
public void sendAdminChat ( NetworkAction action , DestType type , long dest , String msg , long data )
228
242
{
243
+ logger . Log ( LogLevel . Trace , "sendAdminChat" ) ;
229
244
Packet p = new Packet ( getSocket ( ) , PacketType . ADMIN_PACKET_ADMIN_CHAT ) ;
230
245
p . writeUint8 ( ( short ) action ) ;
231
246
p . writeUint8 ( ( short ) type ) ;
@@ -241,15 +256,15 @@ public void sendAdminChat(NetworkAction action, DestType type, long dest, String
241
256
242
257
public void sendAdminGameScript ( string command )
243
258
{
259
+ logger . Log ( LogLevel . Trace , string . Format ( "sendAdminGameScript - command: {0}" , command ) ) ;
244
260
Packet p = new Packet ( getSocket ( ) , PacketType . ADMIN_PACKET_ADMIN_GAMESCRIPT ) ;
245
-
246
-
247
261
p . WriteString ( command ) ; // JSON encode
248
262
NetworkOutputThread . append ( p ) ;
249
263
}
250
264
251
265
public void sendAdminUpdateFrequency ( AdminUpdateType type , AdminUpdateFrequency freq )
252
266
{
267
+ logger . Log ( LogLevel . Trace , "sendAdminUpdateFrequency" ) ;
253
268
if ( getProtocol ( ) . isSupported ( type , freq ) == false )
254
269
throw new ArgumentException ( "The server does not support " + freq + " for " + type ) ;
255
270
@@ -262,6 +277,7 @@ public void sendAdminUpdateFrequency(AdminUpdateType type, AdminUpdateFrequency
262
277
263
278
public void sendAdminPoll ( AdminUpdateType type , long data = 0 )
264
279
{
280
+ logger . Log ( LogLevel . Trace , "AdminUpdateType" ) ;
265
281
if ( getProtocol ( ) . isSupported ( type , AdminUpdateFrequency . ADMIN_FREQUENCY_POLL ) == false )
266
282
throw new ArgumentException ( "The server does not support polling for " + type ) ;
267
283
@@ -272,11 +288,12 @@ public void sendAdminPoll(AdminUpdateType type, long data = 0)
272
288
NetworkOutputThread . append ( p ) ;
273
289
}
274
290
275
- #endregion
291
+ #endregion
276
292
277
- #region Receive Packets
293
+ #region Receive Packets
278
294
public void receiveServerClientInfo ( Packet p )
279
295
{
296
+ logger . Log ( LogLevel . Trace , "receiveServerClientInfo" ) ;
280
297
Client client = new Client ( p . readUint32 ( ) )
281
298
{
282
299
address = p . readString ( ) ,
@@ -287,13 +304,15 @@ public void receiveServerClientInfo(Packet p)
287
304
client . joindate = new GameDate ( p . readUint32 ( ) ) ;
288
305
client . companyId = p . readUint8 ( ) ;
289
306
290
- clientPool . Add ( client . clientId , client ) ;
307
+ if ( GetClient ( client . clientId ) == null )
308
+ clientPool . Add ( client . clientId , client ) ;
291
309
292
310
OnClientInfo ? . Invoke ( client ) ;
293
311
}
294
312
295
313
public void receiveServerProtocol ( Packet p )
296
314
{
315
+ logger . Log ( LogLevel . Trace , "receiveServerProtocol" ) ;
297
316
Protocol protocol = getProtocol ( ) ;
298
317
299
318
protocol . version = p . readUint8 ( ) ;
@@ -319,7 +338,7 @@ public void receiveServerProtocol(Packet p)
319
338
320
339
public void receiveServerWelcome ( Packet p )
321
340
{
322
-
341
+ logger . Log ( LogLevel . Trace , "receiveServerWelcome" ) ;
323
342
Map map = new Map
324
343
{
325
344
name = p . readString ( ) ,
@@ -344,6 +363,7 @@ public void receiveServerWelcome(Packet p)
344
363
345
364
public void receiveServerChat ( Packet p )
346
365
{
366
+ logger . Log ( LogLevel . Trace , "receiveServerChat" ) ;
347
367
NetworkAction action = ( NetworkAction ) p . readUint8 ( ) ;
348
368
DestType dest = ( DestType ) p . readUint8 ( ) ;
349
369
long clientId = p . readUint32 ( ) ;
@@ -356,6 +376,7 @@ public void receiveServerChat(Packet p)
356
376
357
377
public void receiveServerCmdNames ( Packet p )
358
378
{
379
+ logger . Log ( LogLevel . Trace , "receiveServerCmdNames" ) ;
359
380
while ( p . readBool ( ) )
360
381
{
361
382
int cmdId = p . readUint16 ( ) ;
@@ -367,7 +388,7 @@ public void receiveServerCmdNames(Packet p)
367
388
368
389
public void receiveServerCmdLogging ( Packet p )
369
390
{
370
-
391
+ logger . Log ( LogLevel . Trace , "receiveServerCmdLogging" ) ;
371
392
}
372
393
#endregion
373
394
0 commit comments