From 1a017a52df6a3d5d8effb49b0c1826403eb782c8 Mon Sep 17 00:00:00 2001 From: Alina Lenk Date: Wed, 20 Jul 2022 14:48:21 +0200 Subject: [PATCH 01/12] generate_packets.py: Introduce enum for packet directions See osdn#45165 Signed-off-by: Alina Lenk --- common/generate_packets.py | 54 +++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/common/generate_packets.py b/common/generate_packets.py index 83ccc0635d..13e59359d4 100755 --- a/common/generate_packets.py +++ b/common/generate_packets.py @@ -23,6 +23,7 @@ from contextlib import contextmanager from functools import partial from itertools import chain, combinations from collections import deque +from enum import Enum try: from functools import cache @@ -1889,6 +1890,33 @@ if (NULL != *hash) { #endif /* FREECIV_DELTA_PROTOCOL */ """ + +class Directions(Enum): + """Describes the possible combinations of directions for which a packet + can be valid""" + + # Note: "sc" and "cs" are used to match the packet flags + + DOWN_ONLY = frozenset({"sc"}) + """Packet may only be sent from server to client""" + + UP_ONLY = frozenset({"cs"}) + """Packet may only be sent from client to server""" + + UNRESTRICTED = frozenset({"sc", "cs"}) + """Packet may be sent both ways""" + + @property + def down(self) -> bool: + """Whether a packet may be sent from server to client""" + return "sc" in self.value + + @property + def up(self) -> bool: + """Whether a packet may be sent from client to server""" + return "cs" in self.value + + # Class which represents a packet. A packet contains a list of fields. class Packet: # matches a packet's header line (packet type, number, flags) @@ -1914,18 +1942,20 @@ class Packet: arr=list(item.strip() for item in dummy.split(",") if item) - self.dirs=[] + dirs = set() if "sc" in arr: - self.dirs.append("sc") + dirs.add("sc") arr.remove("sc") if "cs" in arr: - self.dirs.append("cs") + dirs.add("cs") arr.remove("cs") - if not self.dirs: + if not dirs: raise ValueError("no directions defined for %s" % self.name) + self.dirs = Directions(dirs) + # "no" means normal packet # "yes" means is-info packet # "game" means is-game-info packet @@ -2378,9 +2408,9 @@ void packet_handlers_fill_initial(struct packet_handlers *phandlers) # handler at connecting time, because it would be anyway wrong # to use them before the network capability string would be # known. - if len(p.dirs)==1 and p.dirs[0]=="sc": + if p.dirs is Directions.DOWN_ONLY: sc_packets.append(p) - elif len(p.dirs)==1 and p.dirs[0]=="cs": + elif p.dirs is Directions.UP_ONLY: cs_packets.append(p) else: unrestricted.append(p) @@ -2425,9 +2455,9 @@ void packet_handlers_fill_capability(struct packet_handlers *phandlers, unrestricted=[] for p in packets: if len(p.variants)>1: - if len(p.dirs)==1 and p.dirs[0]=="sc": + if p.dirs is Directions.DOWN_ONLY: sc_packets.append(p) - elif len(p.dirs)==1 and p.dirs[0]=="cs": + elif p.dirs is Directions.UP_ONLY: cs_packets.append(p) else: unrestricted.append(p) @@ -2731,7 +2761,7 @@ bool server_handle_packet(enum packet_type type, const void *packet, """) for p in packets: - if "cs" in p.dirs and not p.no_handle: + if p.dirs.up and not p.no_handle: a=p.name[len("packet_"):] b = "".join( ", %s%s" % (field.get_handle_type(), field.name) @@ -2767,7 +2797,7 @@ bool client_handle_packet(enum packet_type type, const void *packet); """) for p in packets: - if "sc" not in p.dirs: continue + if not p.dirs.down: continue a=p.name[len("packet_"):] b = ", ".join( @@ -2805,7 +2835,7 @@ bool server_handle_packet(enum packet_type type, const void *packet, switch (type) { """) for p in packets: - if "cs" not in p.dirs: continue + if not p.dirs.up: continue if p.no_handle: continue a=p.name[len("packet_"):] c = "((const struct %s *)packet)->" % p.name @@ -2864,7 +2894,7 @@ bool client_handle_packet(enum packet_type type, const void *packet) switch (type) { """) for p in packets: - if "sc" not in p.dirs: continue + if not p.dirs.down: continue if p.no_handle: continue a=p.name[len("packet_"):] c = "((const struct %s *)packet)->" % p.name -- 2.34.1