Skip to content

Commit 6c14fa7

Browse files
authored
Add: return the length of the data for every send function (#25)
This allows callers to track how much bandwidth was used for sending replies.
1 parent 77d52d3 commit 6c14fa7

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

openttd_protocol/protocol/content.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ async def send_PACKET_CONTENT_SERVER_INFO(
225225
write_string(data, tag)
226226

227227
write_presend(data, SEND_TCP_COMPAT_MTU)
228-
await self.send_packet(data)
228+
return await self.send_packet(data)
229229

230230
async def send_PACKET_CONTENT_SERVER_CONTENT(self, content_type, content_id, filesize, filename, stream):
231231
# First, send a packet to tell the client it will be receiving a file
@@ -238,15 +238,16 @@ async def send_PACKET_CONTENT_SERVER_CONTENT(self, content_type, content_id, fil
238238
write_string(data, filename)
239239

240240
write_presend(data, SEND_TCP_COMPAT_MTU)
241-
await self.send_packet(data)
241+
length = await self.send_packet(data)
242242

243243
# Next, send the content of the file over
244244
while not stream.eof():
245245
data = write_init(PacketContentType.PACKET_CONTENT_SERVER_CONTENT)
246246
data += stream.read(SEND_TCP_COMPAT_MTU - 3)
247247
write_presend(data, SEND_TCP_COMPAT_MTU)
248-
await self.send_packet(data)
248+
length += await self.send_packet(data)
249249

250250
data = write_init(PacketContentType.PACKET_CONTENT_SERVER_CONTENT)
251251
write_presend(data, SEND_TCP_COMPAT_MTU)
252-
await self.send_packet(data)
252+
length += await self.send_packet(data)
253+
return length

openttd_protocol/protocol/coordinator.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ async def send_PACKET_COORDINATOR_GC_ERROR(self, protocol_version, error_no, err
356356
write_string(data, error_detail)
357357

358358
write_presend(data, SEND_TCP_MTU)
359-
await self.send_packet(data)
359+
return await self.send_packet(data)
360360

361361
async def send_PACKET_COORDINATOR_GC_REGISTER_ACK(
362362
self, protocol_version, connection_type, invite_code, invite_code_secret
@@ -369,7 +369,7 @@ async def send_PACKET_COORDINATOR_GC_REGISTER_ACK(
369369
write_uint8(data, connection_type.value)
370370

371371
write_presend(data, SEND_TCP_MTU)
372-
await self.send_packet(data)
372+
return await self.send_packet(data)
373373

374374
def _fill_NEWGRF_LOOKUP_PACKET(self, newgrf_lookup_table_cursor, newgrf_lookup_table):
375375
data = bytearray()
@@ -401,6 +401,7 @@ async def send_PACKET_COORDINATOR_GC_NEWGRF_LOOKUP(
401401
):
402402
cursor = max(newgrf_lookup_table.keys())
403403

404+
length = 0
404405
for count, body in self._fill_NEWGRF_LOOKUP_PACKET(newgrf_lookup_table_cursor, newgrf_lookup_table):
405406
data = write_init(PacketCoordinatorType.PACKET_COORDINATOR_GC_NEWGRF_LOOKUP)
406407

@@ -411,11 +412,14 @@ async def send_PACKET_COORDINATOR_GC_NEWGRF_LOOKUP(
411412
write_bytes(data, body)
412413

413414
write_presend(data, SEND_TCP_MTU)
414-
await self.send_packet(data)
415+
length += await self.send_packet(data)
416+
417+
return length
415418

416419
async def send_PACKET_COORDINATOR_GC_LISTING(
417420
self, protocol_version, game_info_version, servers, newgrf_lookup_table
418421
):
422+
length = 0
419423
for server in servers:
420424
if server.game_type != ServerGameType.SERVER_GAME_TYPE_PUBLIC:
421425
continue
@@ -483,13 +487,14 @@ async def send_PACKET_COORDINATOR_GC_LISTING(
483487
write_uint8(data, server.info["is_dedicated"])
484488

485489
write_presend(data, SEND_TCP_MTU)
486-
await self.send_packet(data)
490+
length += await self.send_packet(data)
487491

488492
# Send a final packet with 0 servers to indicate end-of-list.
489493
data = write_init(PacketCoordinatorType.PACKET_COORDINATOR_GC_LISTING)
490494
write_uint16(data, 0)
491495
write_presend(data, SEND_TCP_MTU)
492-
await self.send_packet(data)
496+
length += await self.send_packet(data)
497+
return length
493498

494499
async def send_PACKET_COORDINATOR_GC_CONNECTING(self, protocol_version, token, invite_code):
495500
data = write_init(PacketCoordinatorType.PACKET_COORDINATOR_GC_CONNECTING)
@@ -498,15 +503,15 @@ async def send_PACKET_COORDINATOR_GC_CONNECTING(self, protocol_version, token, i
498503
write_string(data, invite_code)
499504

500505
write_presend(data, SEND_TCP_MTU)
501-
await self.send_packet(data)
506+
return await self.send_packet(data)
502507

503508
async def send_PACKET_COORDINATOR_GC_CONNECT_FAILED(self, protocol_version, token):
504509
data = write_init(PacketCoordinatorType.PACKET_COORDINATOR_GC_CONNECT_FAILED)
505510

506511
write_string(data, token)
507512

508513
write_presend(data, SEND_TCP_MTU)
509-
await self.send_packet(data)
514+
return await self.send_packet(data)
510515

511516
async def send_PACKET_COORDINATOR_GC_DIRECT_CONNECT(self, protocol_version, token, tracking_number, hostname, port):
512517
data = write_init(PacketCoordinatorType.PACKET_COORDINATOR_GC_DIRECT_CONNECT)
@@ -517,15 +522,15 @@ async def send_PACKET_COORDINATOR_GC_DIRECT_CONNECT(self, protocol_version, toke
517522
write_uint16(data, port)
518523

519524
write_presend(data, SEND_TCP_MTU)
520-
await self.send_packet(data)
525+
return await self.send_packet(data)
521526

522527
async def send_PACKET_COORDINATOR_GC_STUN_REQUEST(self, protocol_version, token):
523528
data = write_init(PacketCoordinatorType.PACKET_COORDINATOR_GC_STUN_REQUEST)
524529

525530
write_string(data, token)
526531

527532
write_presend(data, SEND_TCP_MTU)
528-
await self.send_packet(data)
533+
return await self.send_packet(data)
529534

530535
async def send_PACKET_COORDINATOR_GC_STUN_CONNECT(
531536
self, protocol_version, token, tracking_number, interface_number, hostname, port
@@ -539,7 +544,7 @@ async def send_PACKET_COORDINATOR_GC_STUN_CONNECT(
539544
write_uint16(data, port)
540545

541546
write_presend(data, SEND_TCP_MTU)
542-
await self.send_packet(data)
547+
return await self.send_packet(data)
543548

544549
async def send_PACKET_COORDINATOR_GC_TURN_CONNECT(
545550
self, protocol_version, token, tracking_number, ticket, connection_string
@@ -552,4 +557,4 @@ async def send_PACKET_COORDINATOR_GC_TURN_CONNECT(
552557
write_string(data, connection_string)
553558

554559
write_presend(data, SEND_TCP_MTU)
555-
await self.send_packet(data)
560+
return await self.send_packet(data)

openttd_protocol/protocol/game.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,4 @@ def receive_PACKET_SERVER_SHUTDOWN(source, data):
167167
async def send_PACKET_CLIENT_GAME_INFO(self):
168168
data = write_init(PacketGameType.PACKET_CLIENT_GAME_INFO)
169169
write_presend(data, SEND_TCP_MTU)
170-
await self.send_packet(data)
170+
return await self.send_packet(data)

openttd_protocol/protocol/turn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ async def send_PACKET_TURN_TURN_CONNECTED(self, protocol_version, hostname):
4848
write_string(data, hostname)
4949

5050
write_presend(data, SEND_TCP_MTU)
51-
await self.send_packet(data)
51+
return await self.send_packet(data)

openttd_protocol/wire/tcp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,5 @@ async def send_packet(self, data):
217217
# everything else, it is a non-blocking normal function.
218218
if iscoroutine(res):
219219
await res
220+
221+
return len(data)

0 commit comments

Comments
 (0)