diff options
Diffstat (limited to 'network/map_server.gd')
-rw-r--r-- | network/map_server.gd | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/network/map_server.gd b/network/map_server.gd index af46c6c..f4dacc1 100644 --- a/network/map_server.gd +++ b/network/map_server.gd @@ -1,6 +1,9 @@ class_name MapServer +signal received_packet(packet: Packet) +signal logged_in(packet: MapServerLoginSuccessPacket) + var host: String var port: int = 5121 var stream: PacketPeerStream = PacketPeerStream.new() @@ -17,7 +20,24 @@ func _init(host: String, port: int = 5121) -> void: peer.poll() -func login(account_id: int, character_id: int, login_id1: int, gender: Constants.Gender) -> MapServerLoginSuccessPacket: +## Emits [signal received_packet]. +func listen() -> void: + if peer.get_status() == StreamPeerTCP.Status.STATUS_CONNECTED: + var raw_packet: PackedByteArray = peer.get_partial_data(2)[1] + + if raw_packet.size() > 0: + var header = raw_packet.decode_u16(0) + var packet_type = Constants.PacketDB[header] + if packet_type.BYTE_LENGTH > 0: + raw_packet += peer.get_data(packet_type.BYTE_LENGTH - 2)[1] + else: + raw_packet += peer.get_data(peer.get_available_bytes())[1] + var packet = packet_type.from_bytes(raw_packet) + + received_packet.emit(packet) + + +func login(account_id: int, character_id: int, login_id1: int, gender: Constants.Gender): var map_server_login_packet = MapServerLoginPacket.new() map_server_login_packet.account_id = account_id map_server_login_packet.character_id = character_id @@ -29,7 +49,7 @@ func login(account_id: int, character_id: int, login_id1: int, gender: Constants peer.get_data(6) # in-between packet peer.get_data(4) # in-between packet - var success = MapServerLoginSuccessPacket.from_bytes_via_peer(peer) - print(inst_to_dict(success)) - - return success + var packet = await received_packet + if packet is MapServerLoginSuccessPacket: + print(inst_to_dict(packet)) + logged_in.emit(packet) |