fix kcp issue

This commit is contained in:
Naruse
2025-04-17 20:12:47 +08:00
parent 5434bab973
commit 2d6462c321

View File

@@ -9,6 +9,7 @@ from rail_proto import lib as protos
from game_server.game.player.player_manager import PlayerManager from game_server.game.player.player_manager import PlayerManager
from game_server.dummy import dummyprotolist from game_server.dummy import dummyprotolist
import traceback import traceback
from utils.time import cur_timestamp_ms
class PlayerSession: class PlayerSession:
def __init__(self, transport, session_id, client_addr, db): def __init__(self, transport, session_id, client_addr, db):
@@ -16,6 +17,7 @@ class PlayerSession:
self.session_id = session_id self.session_id = session_id
self.client_addr = client_addr self.client_addr = client_addr
self.kcp = Kcp(session_id, self.send_output) self.kcp = Kcp(session_id, self.send_output)
self.kcp.set_wndsize(sndwnd=256, rcvwnd=256)
self.kcp.set_nodelay(1, 5, 2, 0) self.kcp.set_nodelay(1, 5, 2, 0)
self.is_destroyed = False self.is_destroyed = False
self.db = db self.db = db
@@ -23,6 +25,7 @@ class PlayerSession:
self.player = PlayerManager() self.player = PlayerManager()
self.active = False self.active = False
self.last_received = asyncio.get_event_loop().time() self.last_received = asyncio.get_event_loop().time()
self.connect_time_ms = cur_timestamp_ms()
def update_last_received(self): def update_last_received(self):
self.last_received = asyncio.get_event_loop().time() self.last_received = asyncio.get_event_loop().time()
@@ -51,7 +54,8 @@ class PlayerSession:
async def consume(self, data): async def consume(self, data):
self.kcp.input(data) self.kcp.input(data)
self.kcp.update(asyncio.get_running_loop().time()) self.kcp.update(self.time())
self.kcp.flush()
while True: while True:
packet = self.kcp.recv() packet = self.kcp.recv()
@@ -59,7 +63,7 @@ class PlayerSession:
break break
await self.handle_packet(packet) await self.handle_packet(packet)
self.kcp.update(asyncio.get_running_loop().time()) #self.kcp.update(self.time())
async def handle_packet(self, packet): async def handle_packet(self, packet):
net_packet = NetPacket.from_bytes(packet) net_packet = NetPacket.from_bytes(packet)
@@ -130,4 +134,9 @@ class PlayerSession:
async def send(self, packet): async def send(self, packet):
self.kcp.send(packet.to_bytes()) self.kcp.send(packet.to_bytes())
self.kcp.flush() self.kcp.flush()
self.kcp.update(asyncio.get_running_loop().time()) cmd_id = packet.cmd_type
request_name = cmd.get_key_by_value(cmd_id)
Info(f"Sent cmd: {request_name}({cmd_id})")
def time(self):
return (cur_timestamp_ms()) - self.connect_time_ms