25
25
M_NONE = 0
26
26
M_TCP = 1
27
27
M_UDP = 2
28
- M_BOTH = 3
28
+ M_BOTH = M_TCP | M_UDP
29
29
30
30
class DataStorageClass (object ):
31
31
def __init__ (self , dict = {}):
@@ -129,14 +129,14 @@ def connect(self, mode=M_BOTH):
129
129
self .socket_udp .settimeout (5 )
130
130
131
131
try :
132
- self .ip = socket .gethostbyaddr (self .ip )[ 2 ][ 0 ]
132
+ self .ip = socket .gethostbyname (self .ip )
133
133
except Exception , e :
134
134
LOG .error ('gethost error on ip %s: %s' % (str (self .ip ), str (e )))
135
135
if not str (e ) in self .errors :
136
136
self .errors .append (str (e ))
137
137
return False
138
138
139
- LOG .info ("connecting to %s:%d" % (self .ip , self .port ))
139
+ LOG .debug ("connecting to %s:%d" % (self .ip , self .port ))
140
140
141
141
if mode & M_TCP :
142
142
self .socket_tcp .connect ((self .ip , self .port ))
@@ -182,9 +182,8 @@ def run(self):
182
182
def getServerList (self ):
183
183
payload = struct .pack ("B" , NETWORK_MASTER_SERVER_VERSION )
184
184
self .sendMsg_UDP (PACKET_UDP_CLIENT_GET_LIST , payload )
185
- size , command , content = self .receiveMsg_UDP ()
186
- if command == PACKET_UDP_MASTER_RESPONSE_LIST :
187
- p = DataPacket (size , command , content )
185
+ p = self .receiveMsg_UDP (datapacket = True )
186
+ if p .command == PACKET_UDP_MASTER_RESPONSE_LIST :
188
187
protocol_version = p .recv_uint8 ()
189
188
190
189
if protocol_version == 1 :
@@ -208,12 +207,11 @@ def getGRFInfo(self, grfs):
208
207
for grf in grfs :
209
208
p .send_something ('4s16s' , grf )
210
209
self .sendMsg_UDP (p .command , p .data )
211
- res = self .receiveMsg_UDP ()
212
- if res is None :
210
+ p = self .receiveMsg_UDP (True )
211
+ if p is None :
213
212
LOG .debug ("unable to receive UDP packet" )
214
213
return None
215
214
newgrfs = []
216
- p = DataPacket (* res )
217
215
if p .command == PACKET_UDP_SERVER_NEWGRFS :
218
216
reply_count = p .recv_uint8 ()
219
217
for i in range (0 , reply_count ):
@@ -231,10 +229,9 @@ def getGRFInfo(self, grfs):
231
229
232
230
def getCompanyInfo (self ):
233
231
self .sendMsg_UDP (PACKET_UDP_CLIENT_DETAIL_INFO )
234
- res = self .receiveMsg_UDP ()
235
- if res is None :
232
+ p = self .receiveMsg_UDP (True )
233
+ if p is None :
236
234
return None
237
- p = DataPacket (* res )
238
235
if p .command == PACKET_UDP_SERVER_DETAIL_INFO :
239
236
info_version = p .recv_uint8 ()
240
237
player_count = p .recv_uint8 ()
@@ -292,22 +289,19 @@ def getCompanyInfo(self):
292
289
LOG .error ("unexpected reply on PACKET_UDP_CLIENT_DETAIL_INFO: %d" % (command ))
293
290
def getTCPCompanyInfo (self ):
294
291
self .sendMsg_TCP (PACKET_CLIENT_COMPANY_INFO )
295
- res = self .receiveMsg_TCP ()
292
+ p = self .receiveMsg_TCP (True )
296
293
if res is None :
297
294
return None
298
- size , command , content = res
299
- if command == PACKET_SERVER_COMPANY_INFO :
300
- p = DataPacket (size , command , content )
295
+ if p .command == PACKET_SERVER_COMPANY_INFO :
301
296
[info_version , player_count ] = p .recv_something ('BB' )
302
297
if info_version == NETWORK_COMPANY_INFO_VERSION or info_version == 4 : #4 and 5 are the same:
303
298
companies = []
304
299
firsttime = True
305
300
for i in range (0 , player_count ):
306
301
if not firsttime :
307
- res2 = self .receiveMsg_TCP ()
308
- if res2 is None :
302
+ p = self .receiveMsg_TCP (True )
303
+ if p is None :
309
304
return None
310
- p = DataPacket (* res2 )
311
305
if p .command != PACKET_SERVER_COMPANY_INFO :
312
306
LOG .error ("unexpectged reply on PACKET_CLIENT_COMPANY_INFO: %d" % p .command )
313
307
return None
@@ -409,21 +403,21 @@ def throwRandomData(self):
409
403
410
404
def sendRaw (self , data , type = M_NONE ):
411
405
if type == M_TCP :
412
- socket = self .socket_tcp
406
+ s = self .socket_tcp
413
407
socketname = "TCP" # for errors
414
408
elif type == M_UDP :
415
- socket = self .socket_udp
409
+ s = self .socket_udp
416
410
socketname = "UDP" # for errors
417
411
else :
418
412
LOG .error ("cannot send: unknown type" )
419
413
return False
420
- if socket is None :
414
+ if s is None :
421
415
# not connected
422
416
LOG .error ("cannot send: " + socketname + " not connected!" )
423
417
return False
424
418
try :
425
419
# send the data
426
- socket .send (data )
420
+ s .send (data )
427
421
except socket .error , e :
428
422
LOG .error ("send error: %d (%s)" % (e [0 ], e [1 ]))
429
423
return True
@@ -457,7 +451,14 @@ def sendMsg_TCP(self, *args, **kwargs):
457
451
return self .sendMsg (M_TCP , * args , ** kwargs )
458
452
def sendMsg_UDP (self , * args , ** kwargs ):
459
453
return self .sendMsg (M_UDP , * args , ** kwargs )
460
- def receiveMsg_UDP (self ):
454
+ def receive_bytes (self , socket , bytes ):
455
+ data = ""
456
+ readcounter = 0
457
+ while len (data ) < bytes and self .running :
458
+ data += socket .recv (bytes - len (data ))
459
+ readcounter += 1
460
+ return data , readcounter
461
+ def receiveMsg_UDP (self , datapacket = False ):
461
462
try :
462
463
if self .socket_udp is None :
463
464
return None
@@ -466,8 +467,10 @@ def receiveMsg_UDP(self):
466
467
[size , command ], osize = unpackFromExt (self .header_format , data , 0 )
467
468
LOG .debug ("received size: %d/%d, command: %d" % (size , osize , command ))
468
469
content = data [self .header_size :]
469
-
470
- return size , command , content
470
+ if datapacket :
471
+ return DataPacket (size , command , content )
472
+ else :
473
+ return size , command , content
471
474
except Exception , e :
472
475
LOG .error ('receiveMsg_UDP error: ' + str (e ))
473
476
errorMsg = StringIO .StringIO ()
@@ -476,47 +479,43 @@ def receiveMsg_UDP(self):
476
479
if not str (e ) in self .errors :
477
480
self .errors .append (str (e ))
478
481
479
- def receiveMsg_TCP (self ):
482
+ def receiveMsg_TCP (self , datapacket = False ):
480
483
if self .socket_tcp is None :
481
484
return None
482
485
note = ""
483
- #print "headersize: ", headersize
484
- data = ""
485
- readcounter = 0
486
486
#LOG.debug( "receiving...")
487
- while len (data ) < self .header_size :
488
- data += self .socket_tcp .recv (self .header_size - len (data ))
489
- readcounter += 1
487
+ data , readcounter = self .receive_bytes (self .socket_tcp , self .header_size )
490
488
if readcounter > 1 :
491
489
note += "HEADER SEGMENTED INTO %s SEGMENTS!" % readcounter
492
490
493
491
(size , command ) = struct .unpack (self .header_format , data )
494
492
if not command in [PACKET_SERVER_FRAME , PACKET_SERVER_SYNC ]:
495
- if command in packet_names . keys () :
493
+ if command in packet_names :
496
494
LOG .debug ("received size: %d, command: %s (%d)" % (size , packet_names [command ], command ))
497
495
else :
498
496
LOG .debug ("received size: %d, command: %d" % (size , command ))
499
497
size -= self .header_size # remove size of the header ...
500
- data = ""
501
- readcounter = 0
502
- while len (data ) < size and self .running :
503
- readcounter += 1
504
- data += self .socket_tcp .recv (size - len (data ))
505
- #print "waiting on ", size - len(data)
498
+ data , readcounter = self .receive_bytes (self .socket_tcp , size )
506
499
if readcounter > 1 :
507
500
note += "DATA SEGMENTED INTO %s SEGMENTS!" % readcounter
508
501
509
502
if not self .running :
510
503
return None
511
504
if len (note ) > 0 :
512
505
LOG .info (note )
506
+ content = data
513
507
514
- return size , command , data
508
+ if datapacket :
509
+ return DataPacket (size , command , content )
510
+ else :
511
+ return size , command , content
515
512
#content = struct.unpack(str(size) + 's', data)
516
513
#content = content[0]
517
514
518
515
#LOG.debug(size, command, content)
519
516
def dateToYMD (self , date ):
517
+ if date == 0 :
518
+ return (0 , 0 , 0 )
520
519
ymddate = datetime .date .fromordinal (date - 365 )
521
520
return (ymddate .year , ymddate .month , ymddate .day )
522
521
0 commit comments