Fixes disconnect, and publishing to windows. (#114)
This commit is contained in:
parent
cffb32d6ef
commit
63bf71ea47
6 changed files with 72 additions and 56 deletions
|
|
@ -423,6 +423,12 @@ namespace Server.Network
|
||||||
|
|
||||||
ConnectedOn = DateTime.UtcNow;
|
ConnectedOn = DateTime.UtcNow;
|
||||||
|
|
||||||
|
connection.ConnectionClosed.Register(() =>
|
||||||
|
{
|
||||||
|
TcpServer.Instances.Remove(this);
|
||||||
|
Dispose();
|
||||||
|
});
|
||||||
|
|
||||||
CreatedCallback?.Invoke(this);
|
CreatedCallback?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -482,6 +488,41 @@ namespace Server.Network
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task ProcessIncoming(IMessagePumpService messagePumpService)
|
||||||
|
{
|
||||||
|
var inPipe = Connection.Transport.Input;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (AsyncState.Paused)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await inPipe.ReadAsync();
|
||||||
|
if (result.IsCanceled || result.IsCompleted)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var seq = result.Buffer;
|
||||||
|
|
||||||
|
if (seq.IsEmpty)
|
||||||
|
break;
|
||||||
|
|
||||||
|
var pos = PacketHandlers.ProcessPacket(messagePumpService, this, seq);
|
||||||
|
|
||||||
|
if (pos <= 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
inPipe.AdvanceTo(seq.Slice(0, pos).End);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// ignored
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool CheckEncrypted(int packetID)
|
public bool CheckEncrypted(int packetID)
|
||||||
{
|
{
|
||||||
if (!SentFirstPacket && packetID != 0xF0 && packetID != 0xF1 && packetID != 0xCF && packetID != 0x80 &&
|
if (!SentFirstPacket && packetID != 0xF0 && packetID != 0xF1 && packetID != 0xCF && packetID != 0x80 &&
|
||||||
|
|
@ -533,6 +574,8 @@ namespace Server.Network
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
Connection.Transport.Input.Complete();
|
||||||
|
Connection.Transport.Output.Complete();
|
||||||
Connection.Abort();
|
Connection.Abort();
|
||||||
Task.Run(Connection.DisposeAsync).Wait();
|
Task.Run(Connection.DisposeAsync).Wait();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Connections;
|
using Microsoft.AspNetCore.Connections;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
@ -49,47 +50,9 @@ namespace Server.Network
|
||||||
|
|
||||||
var ns = new NetState(connection);
|
var ns = new NetState(connection);
|
||||||
TcpServer.Instances.Add(ns);
|
TcpServer.Instances.Add(ns);
|
||||||
_logger.LogInformation($"Client: {ns}: Connected. [{TcpServer.Instances.Count} Online]");
|
Console.WriteLine($"Client: {ns}: Connected. [{TcpServer.Instances.Count} Online]");
|
||||||
|
|
||||||
connection.ConnectionClosed.Register(() => { TcpServer.Instances.Remove(ns); });
|
await ns.ProcessIncoming(_messagePumpService).ConfigureAwait(false);
|
||||||
|
|
||||||
await ProcessIncoming(ns).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task ProcessIncoming(NetState ns)
|
|
||||||
{
|
|
||||||
var inPipe = ns.Connection.Transport.Input;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (NetState.AsyncState.Paused)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var result = await inPipe.ReadAsync();
|
|
||||||
if (result.IsCanceled || result.IsCompleted)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var seq = result.Buffer;
|
|
||||||
|
|
||||||
if (seq.IsEmpty)
|
|
||||||
break;
|
|
||||||
|
|
||||||
var pos = PacketHandlers.ProcessPacket(_messagePumpService, ns, seq);
|
|
||||||
|
|
||||||
if (pos <= 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
inPipe.AdvanceTo(seq.Slice(0, pos).End);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// ignored
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inPipe.Complete();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool VerifySocket(ConnectionContext connection)
|
private static bool VerifySocket(ConnectionContext connection)
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,8 @@ namespace Server.Network
|
||||||
// Make this thread safe
|
// Make this thread safe
|
||||||
public static List<NetState> Instances { get; } = new List<NetState>();
|
public static List<NetState> Instances { get; } = new List<NetState>();
|
||||||
|
|
||||||
|
private static IPAddress[] m_ListeningAddresses;
|
||||||
|
|
||||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args = null) =>
|
public static IWebHostBuilder CreateWebHostBuilder(string[] args = null) =>
|
||||||
WebHost.CreateDefaultBuilder(args)
|
WebHost.CreateDefaultBuilder(args)
|
||||||
.UseSetting(WebHostDefaults.SuppressStatusMessagesKey, "True")
|
.UseSetting(WebHostDefaults.SuppressStatusMessagesKey, "True")
|
||||||
|
|
@ -46,6 +48,7 @@ namespace Server.Network
|
||||||
foreach (var ipep in Listeners)
|
foreach (var ipep in Listeners)
|
||||||
{
|
{
|
||||||
options.Listen(ipep, builder => { builder.UseConnectionHandler<ServerConnectionHandler>(); });
|
options.Listen(ipep, builder => { builder.UseConnectionHandler<ServerConnectionHandler>(); });
|
||||||
|
m_ListeningAddresses = GetListeningAddresses(ipep);
|
||||||
DisplayListener(ipep);
|
DisplayListener(ipep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,23 +57,30 @@ namespace Server.Network
|
||||||
.UseLibuv()
|
.UseLibuv()
|
||||||
.UseStartup<ServerStartup>();
|
.UseStartup<ServerStartup>();
|
||||||
|
|
||||||
|
public static IPAddress[] GetListeningAddresses(IPEndPoint ipep)
|
||||||
|
{
|
||||||
|
if (m_ListeningAddresses != null)
|
||||||
|
return m_ListeningAddresses;
|
||||||
|
|
||||||
|
var list = new List<IPAddress>();
|
||||||
|
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
|
||||||
|
{
|
||||||
|
var properties = adapter.GetIPProperties();
|
||||||
|
foreach (var unicast in properties.UnicastAddresses)
|
||||||
|
if (ipep.AddressFamily == unicast.Address.AddressFamily)
|
||||||
|
list.Add(unicast.Address);
|
||||||
|
}
|
||||||
|
|
||||||
|
return list.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
private static void DisplayListener(IPEndPoint ipep)
|
private static void DisplayListener(IPEndPoint ipep)
|
||||||
{
|
{
|
||||||
if (ipep.Address.Equals(IPAddress.Any) || ipep.Address.Equals(IPAddress.IPv6Any))
|
if (ipep.Address.Equals(IPAddress.Any) || ipep.Address.Equals(IPAddress.IPv6Any))
|
||||||
{
|
foreach (var ip in m_ListeningAddresses)
|
||||||
var adapters = NetworkInterface.GetAllNetworkInterfaces();
|
Console.WriteLine("Listening: {0}:{1}", ip, ipep.Port);
|
||||||
foreach (var adapter in adapters)
|
|
||||||
{
|
|
||||||
var properties = adapter.GetIPProperties();
|
|
||||||
foreach (var unicast in properties.UnicastAddresses)
|
|
||||||
if (ipep.AddressFamily == unicast.Address.AddressFamily)
|
|
||||||
Console.WriteLine("Listening: {0}:{1}", unicast.Address, ipep.Port);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
Console.WriteLine("Listening: {0}:{1}", ipep.Address, ipep.Port);
|
Console.WriteLine("Listening: {0}:{1}", ipep.Address, ipep.Port);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -62,11 +62,11 @@
|
||||||
<CodeAnalysisRuleSet>$(SolutionDir)\Rules.ruleset</CodeAnalysisRuleSet>
|
<CodeAnalysisRuleSet>$(SolutionDir)\Rules.ruleset</CodeAnalysisRuleSet>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="Assemblies\libdrng.dll" Condition="'$(OS)' == 'Windows_NT'">
|
<Content Include="Assemblies\libdrng.dll" Condition="'$(RuntimeIdentifier)' == 'win-x64'">
|
||||||
<Link>libdrng.dll</Link>
|
<Link>libdrng.dll</Link>
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
<Content Include="Assemblies\zlib.dll" Condition="'$(OS)' == 'Windows_NT'">
|
<Content Include="Assemblies\zlib.dll" Condition="'$(RuntimeIdentifier)' == 'win-x64'">
|
||||||
<Link>zlib.dll</Link>
|
<Link>zlib.dll</Link>
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
dotnet -c Release Debug /p:PublishProfile=Windows
|
dotnet publish -c Debug /p:PublishProfile=Windows
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
dotnet -c Release Publish /p:PublishProfile=Windows
|
dotnet publish -c Release /p:PublishProfile=Windows
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue