Make it possible to change the port number of the PacketServer

This commit is contained in:
zontreck 2024-06-03 15:19:28 -07:00
parent 043010aad3
commit 8d93157ac0
4 changed files with 10 additions and 8 deletions

View file

@ -8,7 +8,7 @@ void main() async {
reg.registerDefaults(); reg.registerDefaults();
PacketClient client = PacketClient(); PacketClient client = PacketClient();
await client.startConnect("127.0.0.1"); await client.startConnect("127.0.0.1", 25306);
S2CResponse response = await client.send(C2SPing(), true); S2CResponse response = await client.send(C2SPing(), true);
CompoundTag tag = response.contents; CompoundTag tag = response.contents;

View file

@ -1,5 +1,5 @@
import 'package:libac_dart/packets/packets.dart'; import 'package:libac_dart/packets/packets.dart';
void main() async { void main() async {
await PacketServer.start(); await PacketServer.start(25306);
} }

View file

@ -11,8 +11,8 @@ import '../nbt/impl/StringTag.dart';
class PacketServer { class PacketServer {
static ServerSocket? socket; static ServerSocket? socket;
static bool shouldRestart = true; static bool shouldRestart = true;
static Future<void> start() async { static Future<void> start(int port) async {
socket = await ServerSocket.bind(InternetAddress.anyIPv4, 25306); socket = await ServerSocket.bind(InternetAddress.anyIPv4, port);
print("Server now listening on port 25306"); print("Server now listening on port 25306");
await for (var sock in socket!) { await for (var sock in socket!) {
@ -74,14 +74,16 @@ class PacketClient {
Socket? socket; Socket? socket;
bool connected = false; bool connected = false;
String lastIP = ""; String lastIP = "";
int port = 25306;
PacketClient(); PacketClient();
Future<void> startConnect(String IPAddress) async { Future<void> startConnect(String IPAddress, int port) async {
try { try {
socket = await Socket.connect(IPAddress, 25306); socket = await Socket.connect(IPAddress, port);
connected = true; connected = true;
lastIP = IPAddress; lastIP = IPAddress;
this.port = port;
} catch (E, stack) { } catch (E, stack) {
connected = false; connected = false;
socket = null; socket = null;
@ -122,7 +124,7 @@ class PacketClient {
await onCompletion.future; await onCompletion.future;
await close(); await close();
await startConnect(lastIP); await startConnect(lastIP, port);
S2CResponse reply = S2CResponse(); S2CResponse reply = S2CResponse();
try { try {
reply.decodeTag(ct.get("result")!.asCompoundTag()); reply.decodeTag(ct.get("result")!.asCompoundTag());

View file

@ -1,6 +1,6 @@
name: libac_dart name: libac_dart
description: "Aria's Creations code library" description: "Aria's Creations code library"
version: 1.0.22 version: 1.0.23
homepage: "https://zontreck.com" homepage: "https://zontreck.com"