Skip to content

Commit fcf1613

Browse files
author
yorickvanpelt
committed
- Codechange: add receive_bytes to receive possibly segmented data
- Fix: use gethostbyname instead of gethostbyaddr, gethostbyaddr fails on *.openttd.org
1 parent 38e84d1 commit fcf1613

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

newgrfs.grflist

1.66 KB
Binary file not shown.

ottd_lib.py

+40-41
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
M_NONE = 0
2626
M_TCP = 1
2727
M_UDP = 2
28-
M_BOTH = 3
28+
M_BOTH = M_TCP | M_UDP
2929

3030
class DataStorageClass(object):
3131
def __init__(self, dict={}):
@@ -129,14 +129,14 @@ def connect(self, mode=M_BOTH):
129129
self.socket_udp.settimeout(5)
130130

131131
try:
132-
self.ip = socket.gethostbyaddr(self.ip)[2][0]
132+
self.ip = socket.gethostbyname(self.ip)
133133
except Exception, e:
134134
LOG.error('gethost error on ip %s: %s'%(str(self.ip), str(e)))
135135
if not str(e) in self.errors:
136136
self.errors.append(str(e))
137137
return False
138138

139-
LOG.info("connecting to %s:%d" % (self.ip, self.port))
139+
LOG.debug("connecting to %s:%d" % (self.ip, self.port))
140140

141141
if mode & M_TCP:
142142
self.socket_tcp.connect((self.ip, self.port))
@@ -182,9 +182,8 @@ def run(self):
182182
def getServerList(self):
183183
payload = struct.pack("B", NETWORK_MASTER_SERVER_VERSION)
184184
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:
188187
protocol_version = p.recv_uint8()
189188

190189
if protocol_version == 1:
@@ -208,12 +207,11 @@ def getGRFInfo(self, grfs):
208207
for grf in grfs:
209208
p.send_something('4s16s', grf)
210209
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:
213212
LOG.debug("unable to receive UDP packet")
214213
return None
215214
newgrfs = []
216-
p = DataPacket(*res)
217215
if p.command == PACKET_UDP_SERVER_NEWGRFS:
218216
reply_count = p.recv_uint8()
219217
for i in range(0, reply_count):
@@ -231,10 +229,9 @@ def getGRFInfo(self, grfs):
231229

232230
def getCompanyInfo(self):
233231
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:
236234
return None
237-
p = DataPacket(*res)
238235
if p.command == PACKET_UDP_SERVER_DETAIL_INFO:
239236
info_version = p.recv_uint8()
240237
player_count = p.recv_uint8()
@@ -292,22 +289,19 @@ def getCompanyInfo(self):
292289
LOG.error("unexpected reply on PACKET_UDP_CLIENT_DETAIL_INFO: %d" % (command))
293290
def getTCPCompanyInfo(self):
294291
self.sendMsg_TCP(PACKET_CLIENT_COMPANY_INFO)
295-
res = self.receiveMsg_TCP()
292+
p = self.receiveMsg_TCP(True)
296293
if res is None:
297294
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:
301296
[info_version, player_count] = p.recv_something('BB')
302297
if info_version == NETWORK_COMPANY_INFO_VERSION or info_version == 4: #4 and 5 are the same:
303298
companies = []
304299
firsttime = True
305300
for i in range(0, player_count):
306301
if not firsttime:
307-
res2 = self.receiveMsg_TCP()
308-
if res2 is None:
302+
p = self.receiveMsg_TCP(True)
303+
if p is None:
309304
return None
310-
p = DataPacket(*res2)
311305
if p.command != PACKET_SERVER_COMPANY_INFO:
312306
LOG.error("unexpectged reply on PACKET_CLIENT_COMPANY_INFO: %d" % p.command)
313307
return None
@@ -409,21 +403,21 @@ def throwRandomData(self):
409403

410404
def sendRaw(self, data, type=M_NONE):
411405
if type == M_TCP:
412-
socket = self.socket_tcp
406+
s = self.socket_tcp
413407
socketname = "TCP" # for errors
414408
elif type == M_UDP:
415-
socket = self.socket_udp
409+
s = self.socket_udp
416410
socketname = "UDP" # for errors
417411
else:
418412
LOG.error("cannot send: unknown type")
419413
return False
420-
if socket is None:
414+
if s is None:
421415
# not connected
422416
LOG.error("cannot send: " + socketname + " not connected!")
423417
return False
424418
try:
425419
# send the data
426-
socket.send(data)
420+
s.send(data)
427421
except socket.error, e:
428422
LOG.error("send error: %d (%s)" % (e[0], e[1]))
429423
return True
@@ -457,7 +451,14 @@ def sendMsg_TCP(self, *args, **kwargs):
457451
return self.sendMsg(M_TCP, *args, **kwargs)
458452
def sendMsg_UDP(self, *args, **kwargs):
459453
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):
461462
try:
462463
if self.socket_udp is None:
463464
return None
@@ -466,8 +467,10 @@ def receiveMsg_UDP(self):
466467
[size, command], osize = unpackFromExt(self.header_format, data, 0)
467468
LOG.debug("received size: %d/%d, command: %d"% (size, osize, command))
468469
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
471474
except Exception, e:
472475
LOG.error('receiveMsg_UDP error: '+str(e))
473476
errorMsg = StringIO.StringIO()
@@ -476,47 +479,43 @@ def receiveMsg_UDP(self):
476479
if not str(e) in self.errors:
477480
self.errors.append(str(e))
478481

479-
def receiveMsg_TCP(self):
482+
def receiveMsg_TCP(self, datapacket = False):
480483
if self.socket_tcp is None:
481484
return None
482485
note = ""
483-
#print "headersize: ", headersize
484-
data = ""
485-
readcounter = 0
486486
#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)
490488
if readcounter > 1:
491489
note += "HEADER SEGMENTED INTO %s SEGMENTS!" % readcounter
492490

493491
(size, command) = struct.unpack(self.header_format, data)
494492
if not command in [PACKET_SERVER_FRAME, PACKET_SERVER_SYNC]:
495-
if command in packet_names.keys():
493+
if command in packet_names:
496494
LOG.debug("received size: %d, command: %s (%d)"% (size, packet_names[command], command))
497495
else:
498496
LOG.debug("received size: %d, command: %d"% (size, command))
499497
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)
506499
if readcounter > 1:
507500
note += "DATA SEGMENTED INTO %s SEGMENTS!" % readcounter
508501

509502
if not self.running:
510503
return None
511504
if len(note) > 0:
512505
LOG.info(note)
506+
content = data
513507

514-
return size, command, data
508+
if datapacket:
509+
return DataPacket(size, command, content)
510+
else:
511+
return size, command, content
515512
#content = struct.unpack(str(size) + 's', data)
516513
#content = content[0]
517514

518515
#LOG.debug(size, command, content)
519516
def dateToYMD(self, date):
517+
if date == 0:
518+
return (0, 0, 0)
520519
ymddate = datetime.date.fromordinal(date - 365)
521520
return (ymddate.year, ymddate.month, ymddate.day)
522521

0 commit comments

Comments
 (0)