-
This is a follow up to #11863 which I'm making a (very basic) Python client to fetch content from the Bananas content server. And specifically in this case, I would like to fetch an AI by its name. Nosing around sources in https://github.com/OpenTTD/py-protocol, https://github.com/OpenTTD/bananas-server and https://github.com/OpenTTD/OpenTTD/blob/master/src/network/network_content.cpp, I've figured out how to make a request for the list of published AIs. Very initial code is at michalc/OpenTTDLab#116, but to have it here: def bananas_file(ai):
def recv_iter(s, length):
while length:
chunk = s.recv(length)
if not chunk:
raise Exception("Connection ended")
length -= len(chunk)
yield chunk
def recv_bytes(s, length):
return b''.join(recv_iter(s, length))
def recv_packet(s):
length = struct.unpack("<H", recv_bytes(s, 2))[0]
return recv_bytes(s, length - 2)
# Connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
s.settimeout(10.0)
s.connect(("content.openttd.org", 3978))
# Send request for AI list
PACKET_CONTENT_CLIENT_INFO_LIST = 0
CONTENT_TYPE_AI = 3
packet_body = \
struct.pack("<B", PACKET_CONTENT_CLIENT_INFO_LIST) + \
struct.pack("<B", CONTENT_TYPE_AI) + \
struct.pack("<I", 0xffffffff) + \
struct.pack("<B", 1) + \
"vanilla".encode('utf-8') + b'\0' + \
"13.4".encode('utf-8') + b'\0'
s.sendall(struct.pack("<H", len(packet_body) + 2) + packet_body)
# Receive AI list
while True:
print(recv_packet(s)) and it prints the binary/text details of each AI. But I'm a bit stuck on working out if there are more AIs to come back from the server. Nothing leaps out at me as the total number of AIs, or something that indicates the final AI. Is there a way of knowing when the client has received the full list of AIs? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Nope. (I can spend more words on this, but it is the answer to your question :D ) But as you already know what AI you want to fetch (right?), you can also just request the specific AI, and only get that returned. |
Beta Was this translation helpful? Give feedback.
Nope.
(I can spend more words on this, but it is the answer to your question :D )
But as you already know what AI you want to fetch (right?), you can also just request the specific AI, and only get that returned.
PACKET_CONTENT_CLIENT_INFO_EXTID
and friends basically.