diff --git a/const.py b/const.py index 9ee52e5a3cbea0468f4b06b46b8562b333551c86..2bee9594f6cff24cdd3008bd79390ba1cefebe2c 100644 --- a/const.py +++ b/const.py @@ -6,7 +6,8 @@ class FLAGS: ACK = 0b0001_0000 WINDOWS_SIZE = 16 -SERVER_TIMEOUT = CLIENT_TIMEOUT = 5 +CLIENT_TIMEOUT = 5 +SERVER_TIMEOUT = CLIENT_TIMEOUT / 2 HEADER_SIZE = 12 # bytes MAX_PAYLOAD_SIZE = 32768 # bytes diff --git a/server.py b/server.py index a7e23c9b98c391a4f428d1b8bc10529ca0cbd4ff..80a267b263e2e69b2a457bc9ce5adf8c662ebd09 100644 --- a/server.py +++ b/server.py @@ -21,6 +21,7 @@ def send_handler( sequence_base = 0 sequence_max = WINDOWS_SIZE + 1 ack_number = -1 + go_back = False def update_ack(): nonlocal ack_number @@ -41,10 +42,8 @@ def send_handler( # terima ACK update_ack() - can_send = sequence_base <= sequence_current <= sequence_max + can_send = sequence_base <= sequence_current < sequence_max init_time = perf_counter() - i = 0 - max_try = 3 while not can_send: # Nunggu dapet ACK sampe timeout # Kalo timeout belom go_back_n @@ -54,16 +53,17 @@ def send_handler( update_ack() # Tandai request udah di-consume event.clear() - can_send = sequence_base <= sequence_current <= sequence_max + can_send = sequence_base <= sequence_current < sequence_max # masih ga bisa ngirim dan waktu habis if not can_send and perf_counter() - init_time >= SERVER_TIMEOUT: - if i >= max_try: - print(f'Connection with client {client[0]}:{client[1]} has timed out after {max_try} tries. Giving up.') - return + packets.go_back_n(WINDOWS_SIZE + 1) + go_back = True + break - packets.go_back_n(WINDOWS_SIZE) - i += 1 + if go_back: + go_back = False + continue print(f'Sending packet {sequence_current} to {client[0]}:{client[1]}') sock.sendto(packet, client)