1
1
#!/bin/env python
2
2
# made by thomas in 5 hours - no guarantees ;)
3
3
import sys , struct
4
- import logging , logging .config
5
4
import threading
6
5
import socket
7
6
import random
12
11
import copy
13
12
import traceback
14
13
import StringIO
15
- import datetime
16
14
from operator import itemgetter
17
15
from struct_zerostrings import *
18
- from ottd_constants import *
19
16
import signal
20
17
from log import LOG
21
-
22
- sys . path . append ( os . path . join ( os . path . dirname ( os . path . abspath ( __file__ )), "lib" ))
18
+ import constants as const
19
+ from datastorageclass import DataStorageClass
23
20
24
21
#connection modes
25
22
M_NONE = 0
26
- M_TCP = 1
27
- M_UDP = 2
23
+ M_TCP = 1 << 0
24
+ M_UDP = 1 << 1
28
25
M_BOTH = M_TCP | M_UDP
29
26
30
- class DataStorageClass (object ):
31
- def __init__ (self , dict = {}):
32
- self .__dict__ = dict
33
- def __getitem__ (self , key ):
34
- return self .__dict__ [key ]
35
- def __getattr__ (self , key ):
36
- if key in self .__dict__ :
37
- return self .__dict__ [key ]
38
- else :
39
- raise AttributeError
40
- def __setattr__ (self , key , value ):
41
- if not key == "__dict__" :
42
- self .__dict__ [key ] = value
43
- def __delattr__ (self , key ):
44
- del self .__dict__ [key ]
45
- def getDict (self ):
46
- return self .__dict__
47
-
48
27
class DataPacket :
49
28
size = 0
50
29
command = 0
@@ -112,7 +91,6 @@ def __init__(self, ip, port, debugLevel=0, uid=None):
112
91
self .debuglevel = debugLevel
113
92
self .uid = uid
114
93
115
- LOG .debug ('__init__' )
116
94
self .running = True # sighandler will change this value
117
95
self .lock = threading .Lock ()
118
96
threading .Thread .__init__ (self )
@@ -149,7 +127,7 @@ def connect(self, mode=M_BOTH):
149
127
150
128
#self.throwRandomData()
151
129
#self.packetTest()
152
- #self.sendMsg_UDP(PACKET_UDP_CLIENT_FIND_SERVER)
130
+ #self.sendMsg_UDP(const. PACKET_UDP_CLIENT_FIND_SERVER)
153
131
#self.sendRaw(self.packetTest())
154
132
#data=self.receiveMsg_UDP()
155
133
@@ -180,10 +158,10 @@ def run(self):
180
158
pass
181
159
182
160
def getServerList (self ):
183
- payload = struct .pack ("B" , NETWORK_MASTER_SERVER_VERSION )
184
- self .sendMsg_UDP (PACKET_UDP_CLIENT_GET_LIST , payload )
161
+ payload = struct .pack ("B" , const . NETWORK_MASTER_SERVER_VERSION )
162
+ self .sendMsg_UDP (const . PACKET_UDP_CLIENT_GET_LIST , payload )
185
163
p = self .receiveMsg_UDP (datapacket = True )
186
- if p .command == PACKET_UDP_MASTER_RESPONSE_LIST :
164
+ if p .command == const . PACKET_UDP_MASTER_RESPONSE_LIST :
187
165
protocol_version = p .recv_uint8 ()
188
166
189
167
if protocol_version == 1 :
@@ -202,7 +180,7 @@ def getServerList(self):
202
180
203
181
204
182
def getGRFInfo (self , grfs ):
205
- p = DataPacket (0 , PACKET_UDP_CLIENT_GET_NEWGRFS )
183
+ p = DataPacket (0 , const . PACKET_UDP_CLIENT_GET_NEWGRFS )
206
184
p .send_uint8 (len (grfs ))
207
185
for grf in grfs :
208
186
p .send_something ('4s16s' , grf )
@@ -212,7 +190,7 @@ def getGRFInfo(self, grfs):
212
190
LOG .debug ("unable to receive UDP packet" )
213
191
return None
214
192
newgrfs = []
215
- if p .command == PACKET_UDP_SERVER_NEWGRFS :
193
+ if p .command == const . PACKET_UDP_SERVER_NEWGRFS :
216
194
reply_count = p .recv_uint8 ()
217
195
for i in range (0 , reply_count ):
218
196
[grfid , md5sum ] = p .recv_something ('4s16s' )
@@ -225,18 +203,18 @@ def getGRFInfo(self, grfs):
225
203
226
204
return newgrfs
227
205
else :
228
- LOG .error ("unexpected reply on PACKET_UDP_CLIENT_GET_NEWGRFS: %d" % (p .command ))
206
+ LOG .error ("unexpected reply on const. PACKET_UDP_CLIENT_GET_NEWGRFS: %d" % (p .command ))
229
207
230
208
def getCompanyInfo (self ):
231
- self .sendMsg_UDP (PACKET_UDP_CLIENT_DETAIL_INFO )
209
+ self .sendMsg_UDP (const . PACKET_UDP_CLIENT_DETAIL_INFO )
232
210
p = self .receiveMsg_UDP (True )
233
211
if p is None :
234
212
return None
235
- if p .command == PACKET_UDP_SERVER_DETAIL_INFO :
213
+ if p .command == const . PACKET_UDP_SERVER_DETAIL_INFO :
236
214
info_version = p .recv_uint8 ()
237
215
player_count = p .recv_uint8 ()
238
216
239
- if info_version == NETWORK_COMPANY_INFO_VERSION or info_version == 4 :
217
+ if info_version == const . NETWORK_COMPANY_INFO_VERSION or info_version == 4 :
240
218
companies = []
241
219
242
220
for i in range (0 , player_count ):
@@ -284,26 +262,26 @@ def getCompanyInfo(self):
284
262
ret .spectators = players
285
263
return ret
286
264
else :
287
- LOG .error ("unsupported NETWORK_COMPANY_INFO_VERSION: %d. supported version: %d" % (info_version , NETWORK_COMPANY_INFO_VERSION ))
265
+ LOG .error ("unsupported NETWORK_COMPANY_INFO_VERSION: %d. supported version: %d" % (info_version , const . NETWORK_COMPANY_INFO_VERSION ))
288
266
else :
289
- LOG .error ("unexpected reply on PACKET_UDP_CLIENT_DETAIL_INFO: %d" % (command ))
267
+ LOG .error ("unexpected reply on const. PACKET_UDP_CLIENT_DETAIL_INFO: %d" % (command ))
290
268
def getTCPCompanyInfo (self ):
291
- self .sendMsg_TCP (PACKET_CLIENT_COMPANY_INFO )
269
+ self .sendMsg_TCP (const . PACKET_CLIENT_COMPANY_INFO )
292
270
p = self .receiveMsg_TCP (True )
293
271
if res is None :
294
272
return None
295
- if p .command == PACKET_SERVER_COMPANY_INFO :
273
+ if p .command == const . PACKET_SERVER_COMPANY_INFO :
296
274
[info_version , player_count ] = p .recv_something ('BB' )
297
- if info_version == NETWORK_COMPANY_INFO_VERSION or info_version == 4 : #4 and 5 are the same:
275
+ if info_version == const . NETWORK_COMPANY_INFO_VERSION or info_version == 4 : #4 and 5 are the same:
298
276
companies = []
299
277
firsttime = True
300
278
for i in range (0 , player_count ):
301
279
if not firsttime :
302
280
p = self .receiveMsg_TCP (True )
303
281
if p is None :
304
282
return None
305
- if p .command != PACKET_SERVER_COMPANY_INFO :
306
- LOG .error ("unexpectged reply on PACKET_CLIENT_COMPANY_INFO: %d" % p .command )
283
+ if p .command != const . PACKET_SERVER_COMPANY_INFO :
284
+ LOG .error ("unexpectged reply on const. PACKET_CLIENT_COMPANY_INFO: %d" % p .command )
307
285
return None
308
286
[info_version , player_count ] = p .recv_something ('BB' )
309
287
firsttime = False
@@ -323,12 +301,12 @@ def getTCPCompanyInfo(self):
323
301
companies .append (company )
324
302
return companies
325
303
else :
326
- LOG .error ("unknown company info version %d, supported: %d" % (info_version , NETWORK_COMPANY_INFO_VERSION ))
304
+ LOG .error ("unknown company info version %d, supported: %d" % (info_version , const . NETWORK_COMPANY_INFO_VERSION ))
327
305
else :
328
- LOG .error ("unexpected reply on PACKET_CLIENT_COMPANY_INFO: %d" % (command ))
306
+ LOG .error ("unexpected reply on const. PACKET_CLIENT_COMPANY_INFO: %d" % (command ))
329
307
330
308
def getGameInfo (self , encode_grfs = False , short = False ):
331
- self .sendMsg_UDP (PACKET_UDP_CLIENT_FIND_SERVER )
309
+ self .sendMsg_UDP (const . PACKET_UDP_CLIENT_FIND_SERVER )
332
310
result = self .receiveMsg_UDP ()
333
311
if result is None :
334
312
LOG .debug ("unable to receive UDP packet" )
@@ -441,7 +419,7 @@ def packetTest(self):
441
419
format_payload = '15s80sbb33s'
442
420
payload = struct .pack (format_payload , serverversion , playername , playas , netlang , uniqueid )
443
421
444
- self .sendMsg_TCP (PACKET_CLIENT_JOIN , payload )
422
+ self .sendMsg_TCP (const . PACKET_CLIENT_JOIN , payload )
445
423
446
424
def createPacketHeader (self , command , payload ):
447
425
return struct .pack (self .header_format , self .header_size + len (payload ), command )
@@ -478,7 +456,7 @@ def receiveMsg_UDP(self, datapacket = False):
478
456
LOG .error ('receiveMsg_UDP error: ' + str (e ))
479
457
errorMsg = StringIO .StringIO ()
480
458
traceback .print_exc (file = errorMsg )
481
- logging .error (errorMsg .getvalue ())
459
+ LOG .error (errorMsg .getvalue ())
482
460
if not str (e ) in self .errors :
483
461
self .errors .append (str (e ))
484
462
@@ -492,9 +470,9 @@ def receiveMsg_TCP(self, datapacket = False):
492
470
note += "HEADER SEGMENTED INTO %s SEGMENTS!" % readcounter
493
471
494
472
size , command = self .parsePacketHeader (data )
495
- if not command in (PACKET_SERVER_FRAME , PACKET_SERVER_SYNC ):
496
- if command in packet_names :
497
- LOG .debug ("received size: %d, command: %s (%d)" % (size , packet_names [command ], command ))
473
+ if not command in (const . PACKET_SERVER_FRAME , const . PACKET_SERVER_SYNC ):
474
+ if command in const . packet_names :
475
+ LOG .debug ("received size: %d, command: %s (%d)" % (size , const . packet_names [command ], command ))
498
476
else :
499
477
LOG .debug ("received size: %d, command: %d" % (size , command ))
500
478
size -= self .header_size # remove size of the header ...
@@ -516,10 +494,5 @@ def receiveMsg_TCP(self, datapacket = False):
516
494
#content = content[0]
517
495
518
496
#LOG.debug(size, command, content)
519
- def dateToYMD (self , date ):
520
- if date == 0 :
521
- return (0 , 0 , 0 )
522
- ymddate = datetime .date .fromordinal (date - 365 )
523
- return (ymddate .year , ymddate .month , ymddate .day )
524
497
525
498
0 commit comments