This commit is contained in:
Longfeng Qin 2024-09-11 19:17:42 +08:00
parent 1cfdf8ff4e
commit 8b27f7a090
5 changed files with 25 additions and 11 deletions

View File

@ -83,7 +83,7 @@ namespace PSO2SERVER.Network
internal void NotifyConnectionClosed(SocketClient client)
{
Console.WriteLine("连接关闭");
Logger.Write("断开客户端连接.");
_socketMap.Remove(client.Socket.Client);
_clients.Remove(client);

View File

@ -1,9 +1,6 @@
using System;
using System.IO;
using System.Security.Cryptography;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.OpenSsl;
using PSO2SERVER.Crypto;
namespace PSO2SERVER.Packets.Handlers

View File

@ -162,6 +162,8 @@ namespace PSO2SERVER
}
public async Task StartAsync()
{
var startTime = DateTime.Now; // 记录启动开始时间
Server = new Server();
await InitializeConfigurationAsync();
@ -169,6 +171,12 @@ namespace PSO2SERVER
InitializeQueryServers(); // Assuming this is synchronous
Logger.WriteInternal("服务器启动完成 " + DateTime.Now);
var endTime = DateTime.Now; // 记录启动结束时间
var duration = endTime - startTime; // 计算启动耗时
Logger.WriteInternal($"服务器启动耗时: {duration.TotalSeconds} 秒"); // 记录启动耗时
Server.Run();
}
@ -198,7 +206,7 @@ namespace PSO2SERVER
{
for (var i = 0; i < 10; i++)
{
QueryServers.Add(new QueryServer(QueryMode.ShipList, 12099 + (100 * i)));
QueryServers.Add(new QueryServer(QueryMode.ShipList, "舰船", 12099 + (100 * i)));
}
}

View File

@ -25,13 +25,13 @@ namespace PSO2SERVER
private readonly QueryMode _mode;
private readonly int _port;
public QueryServer(QueryMode mode, int port)
public QueryServer(QueryMode mode, string desc, int port)
{
_mode = mode;
_port = port;
var queryTask = Task.Run(() => RunAsync());
RunningServers.Add(queryTask);
Logger.WriteInternal("[QSP] 开始监听端口 " + port);
Logger.WriteInternal("[QSP] 开始监听" + desc + "端口 " + port);
}
private async Task RunAsync()
@ -70,21 +70,29 @@ namespace PSO2SERVER
var writer = new PacketWriter();
var entries = new List<ShipEntry>();
for (var i = 1; i < 11; i++)
for (var i = 1; i <= 10; i++)
{
var entry = new ShipEntry
{
order = (ushort)i,
number = (uint)i,
status = i == 2 ? ShipStatus.Online : ShipStatus.Offline, // Maybe move to Config?
//status = i == 2 ? ShipStatus.Online : ShipStatus.Full, // Maybe move to Config?
status = ShipStatus.Online, // Maybe move to Config?
name = String.Format("Ship{0:0#}", i),
ip = ServerApp.BindAddress.GetAddressBytes()
};
entries.Add(entry);
}
PacketHeader header = new PacketHeader(8 + Marshal.SizeOf(typeof(ShipEntry)) * entries.Count + 12, 0x11, 0x3D, 0x4, 0x0);
// Assuming header size: 8 bytes + (size of ShipEntry * number of entries) + 12 bytes
int headerSize = 8;
int shipEntrySize = Marshal.SizeOf(typeof(ShipEntry));
int totalSize = headerSize + (shipEntrySize * entries.Count) + 12;
PacketHeader header = new PacketHeader(totalSize, 0x11, 0x3D, 0x04, 0x00);
writer.WriteStruct(header);
writer.WriteMagic((uint)entries.Count, 0xE418, 81);
foreach (var entry in entries)
writer.WriteStruct(entry);
@ -92,6 +100,7 @@ namespace PSO2SERVER
writer.Write(1);
var buffer = writer.ToArray();
await Task.Factory.FromAsync(
(cb, state) => socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, cb, state),
socket.EndSend,

View File

@ -29,7 +29,7 @@ namespace PSO2SERVER
PingTimer.Elapsed += PingClients;
PingTimer.Start();
new QueryServer(QueryMode.Block, 12200); // Block
new QueryServer(QueryMode.Block, "认证", 12200); // Block
}
public void Run()