PSO2SERVER/Server/QueryServer.cs

139 lines
4.3 KiB
C#
Raw Normal View History

2024-12-19 01:21:00 +08:00
using PSO2SERVER.Models;
using PSO2SERVER.Protocol.Packets;
using System;
2024-09-10 00:31:40 +08:00
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
2024-09-11 16:33:10 +08:00
using System.Threading.Tasks;
2024-09-10 00:31:40 +08:00
2024-09-10 01:13:20 +08:00
namespace PSO2SERVER
2024-09-10 00:31:40 +08:00
{
public enum QueryMode
{
ShipList,/*12100 - 12900*/
2024-09-12 10:22:58 +08:00
AuthList
2024-09-10 00:31:40 +08:00
}
public class QueryServer
{
2024-09-11 16:33:10 +08:00
public static List<Task> RunningServers = new List<Task>();
2024-09-10 00:31:40 +08:00
private readonly QueryMode _mode;
private readonly int _port;
2024-09-11 19:17:42 +08:00
public QueryServer(QueryMode mode, string desc, int port)
2024-09-10 00:31:40 +08:00
{
_mode = mode;
_port = port;
2024-12-19 01:21:00 +08:00
2024-09-11 16:33:10 +08:00
var queryTask = Task.Run(() => RunAsync());
RunningServers.Add(queryTask);
2024-12-19 01:21:00 +08:00
Logger.WriteInternal("[监听] 监听" + desc + "端口 " + port);
}
private bool IsPortInUse(int port)
{
try
{
// 创建一个 TcpListener 实例
TcpListener listener = new TcpListener(IPAddress.Any, port);
// 尝试启动监听
listener.Start();
listener.Stop();
return false; // 如果没有异常,说明端口没有被占用
}
catch (SocketException)
{
return true; // 捕获异常,说明端口已被占用
}
2024-09-10 00:31:40 +08:00
}
2024-09-11 16:33:10 +08:00
private async Task RunAsync()
2024-09-10 00:31:40 +08:00
{
2024-09-11 16:33:10 +08:00
Func<Socket, Task> connectionHandler;
2024-09-10 00:31:40 +08:00
switch (_mode)
{
2024-09-12 10:22:58 +08:00
case QueryMode.AuthList:
2024-09-11 16:33:10 +08:00
connectionHandler = DoBlockBalanceAsync;
2024-09-10 00:31:40 +08:00
break;
case QueryMode.ShipList:
2024-09-11 16:33:10 +08:00
connectionHandler = DoShipListAsync;
break;
default:
connectionHandler = DoShipListAsync;
2024-09-10 00:31:40 +08:00
break;
}
var serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
{
Blocking = true
};
2024-12-19 01:21:00 +08:00
if (IsPortInUse(_port))
{
Logger.WriteError($"端口 {_port} 已被占用.");
}
2024-09-10 00:31:40 +08:00
var ep = new IPEndPoint(IPAddress.Any, _port);
serverSocket.Bind(ep); // TODO: Custom bind address.
serverSocket.Listen(5);
2024-09-11 16:33:10 +08:00
2024-09-10 00:31:40 +08:00
while (true)
{
2024-09-11 16:33:10 +08:00
var newConnection = await Task.Factory.FromAsync(serverSocket.BeginAccept, serverSocket.EndAccept, null);
_ = connectionHandler(newConnection); // Fire-and-forget pattern for handling connections
2024-09-10 00:31:40 +08:00
}
}
2024-09-12 10:22:58 +08:00
public ShipStatus CheckShipStatus(int port)
{
//TODO 还有其他状态要判断
if (PortChecker.IsPortListening(port))
{
return ShipStatus.Online;
}
return ShipStatus.Offline;
}
2024-09-11 16:33:10 +08:00
private async Task DoShipListAsync(Socket socket)
2024-09-10 00:31:40 +08:00
{
var entries = new List<ShipEntry>();
2024-09-11 19:17:42 +08:00
for (var i = 1; i <= 10; i++)
2024-09-10 00:31:40 +08:00
{
var entry = new ShipEntry
{
order = (ushort)i,
number = (uint)i,
2024-09-12 10:22:58 +08:00
status = CheckShipStatus(ServerApp.ServerShipProt + (100 * (i - 1))), // Maybe move to Config?
2024-09-10 00:31:40 +08:00
name = String.Format("Ship{0:0#}", i),
2024-09-10 01:13:20 +08:00
ip = ServerApp.BindAddress.GetAddressBytes()
2024-09-10 00:31:40 +08:00
};
entries.Add(entry);
}
2024-09-11 19:17:42 +08:00
2024-12-06 19:47:18 +08:00
var shiplistpacket = new ShipListPacket(entries, (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds, 1).GetPacketBytes();
2024-09-11 19:17:42 +08:00
2024-09-11 16:33:10 +08:00
await Task.Factory.FromAsync(
2024-12-06 19:47:18 +08:00
(cb, state) => socket.BeginSend(shiplistpacket, 0, shiplistpacket.Length, SocketFlags.None, cb, state),
2024-09-11 16:33:10 +08:00
socket.EndSend,
null);
2024-09-10 00:31:40 +08:00
socket.Close();
}
2024-09-11 16:33:10 +08:00
private async Task DoBlockBalanceAsync(Socket socket)
2024-09-10 00:31:40 +08:00
{
2024-12-11 04:49:09 +08:00
var balancepacket = new BlockBalancePacket(ServerApp.ServerBlockBalanceName, ServerApp.ServerBlockBalanceProt).GetPacketBytes();
2024-12-06 19:47:18 +08:00
2024-09-11 16:33:10 +08:00
await Task.Factory.FromAsync(
2024-12-06 19:47:18 +08:00
(cb, state) => socket.BeginSend(balancepacket, 0, balancepacket.Length, SocketFlags.None, cb, state),
2024-09-11 16:33:10 +08:00
socket.EndSend,
null);
2024-09-10 00:31:40 +08:00
socket.Close();
}
}
}