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;
|
||||
|
||||
connection.ConnectionClosed.Register(() =>
|
||||
{
|
||||
TcpServer.Instances.Remove(this);
|
||||
Dispose();
|
||||
});
|
||||
|
||||
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)
|
||||
{
|
||||
if (!SentFirstPacket && packetID != 0xF0 && packetID != 0xF1 && packetID != 0xCF && packetID != 0x80 &&
|
||||
|
|
@ -533,6 +574,8 @@ namespace Server.Network
|
|||
|
||||
try
|
||||
{
|
||||
Connection.Transport.Input.Complete();
|
||||
Connection.Transport.Output.Complete();
|
||||
Connection.Abort();
|
||||
Task.Run(Connection.DisposeAsync).Wait();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@
|
|||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Connections;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
|
@ -49,47 +50,9 @@ namespace Server.Network
|
|||
|
||||
var ns = new NetState(connection);
|
||||
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 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();
|
||||
await ns.ProcessIncoming(_messagePumpService).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static bool VerifySocket(ConnectionContext connection)
|
||||
|
|
|
|||
|
|
@ -37,6 +37,8 @@ namespace Server.Network
|
|||
// Make this thread safe
|
||||
public static List<NetState> Instances { get; } = new List<NetState>();
|
||||
|
||||
private static IPAddress[] m_ListeningAddresses;
|
||||
|
||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args = null) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseSetting(WebHostDefaults.SuppressStatusMessagesKey, "True")
|
||||
|
|
@ -46,6 +48,7 @@ namespace Server.Network
|
|||
foreach (var ipep in Listeners)
|
||||
{
|
||||
options.Listen(ipep, builder => { builder.UseConnectionHandler<ServerConnectionHandler>(); });
|
||||
m_ListeningAddresses = GetListeningAddresses(ipep);
|
||||
DisplayListener(ipep);
|
||||
}
|
||||
|
||||
|
|
@ -54,23 +57,30 @@ namespace Server.Network
|
|||
.UseLibuv()
|
||||
.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)
|
||||
{
|
||||
if (ipep.Address.Equals(IPAddress.Any) || ipep.Address.Equals(IPAddress.IPv6Any))
|
||||
{
|
||||
var adapters = NetworkInterface.GetAllNetworkInterfaces();
|
||||
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);
|
||||
}
|
||||
}
|
||||
foreach (var ip in m_ListeningAddresses)
|
||||
Console.WriteLine("Listening: {0}:{1}", ip, ipep.Port);
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Listening: {0}:{1}", ipep.Address, ipep.Port);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,11 +62,11 @@
|
|||
<CodeAnalysisRuleSet>$(SolutionDir)\Rules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Assemblies\libdrng.dll" Condition="'$(OS)' == 'Windows_NT'">
|
||||
<Content Include="Assemblies\libdrng.dll" Condition="'$(RuntimeIdentifier)' == 'win-x64'">
|
||||
<Link>libdrng.dll</Link>
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="Assemblies\zlib.dll" Condition="'$(OS)' == 'Windows_NT'">
|
||||
<Content Include="Assemblies\zlib.dll" Condition="'$(RuntimeIdentifier)' == 'win-x64'">
|
||||
<Link>zlib.dll</Link>
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue