Core/Battlegrounds: Move to scripts And a lot of misc commits i couldn't recover.

Port From (https://github.com/TrinityCore/TrinityCore/commit/d0d5d309bb5877dc2fcb27f6cb123707a31ec1e8)
This commit is contained in:
Hondacrx
2024-08-04 15:18:22 -04:00
parent bca02a24b0
commit e9b21a91be
139 changed files with 8322 additions and 8700 deletions
@@ -6,6 +6,7 @@ using Framework.Database;
using Framework.Web;
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Framework.Networking.Http
{
@@ -20,9 +21,21 @@ namespace Framework.Networking.Http
public Guid? GetSessionId();
}
public abstract class BaseSocket<Derived> : SSLSocket, IAbstractSocket
public abstract class BaseHttpSocket : SocketBase, IAbstractSocket
{
public BaseSocket(Socket socket) : base(socket) { }
public BaseHttpSocket(Socket socket, bool useSSL = false) : base(socket, useSSL) { }
public async override Task HandshakeHandler(Exception exception = null)
{
if (exception != null)
{
Log.outError(LogFilter.Http, $"{GetClientInfo()} SSL Handshake failed {exception.Message}");
CloseSocket();
return;
}
await AsyncRead();
}
public async override void ReadHandler(byte[] data, int receivedLength)
{
@@ -114,5 +127,6 @@ namespace Framework.Networking.Http
protected AsyncCallbackProcessor<QueryCallback> _queryProcessor = new();
protected SessionState _state;
protected ISocket socket;
}
}
@@ -1,39 +0,0 @@
// Copyright (c) CypherCore <http://github.com/CypherCore> All rights reserved.
// Licensed under the GNU GENERAL PUBLIC LICENSE. See LICENSE file in the project root for full license information.
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using Framework.Configuration;
namespace Framework.Networking.Http
{
public class SslSocket<Derived> : BaseSocket<Derived>
{
X509Certificate2 _certificate;
public SslSocket(Socket socket) : base(socket)
{
_certificate = new X509Certificate2(ConfigMgr.GetDefaultValue("CertificatesFile", "./BNetServer.pfx"));
}
public async override void Start()
{
await AsyncHandshake(_certificate);
}
public async override Task HandshakeHandler(Exception exception = null)
{
if (exception != null)
{
Log.outError(LogFilter.Http, $"{GetClientInfo()} SSL Handshake failed {exception.Message}");
CloseSocket();
return;
}
await AsyncRead();
}
}
}
-126
View File
@@ -1,126 +0,0 @@
// Copyright (c) CypherCore <http://github.com/CypherCore> All rights reserved.
// Licensed under the GNU GENERAL PUBLIC LICENSE. See LICENSE file in the project root for full license information.
using System;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace Framework.Networking
{
public abstract class SSLSocket : ISocket, IDisposable
{
Socket _socket;
internal SslStream _stream;
IPEndPoint _remoteEndPoint;
byte[] _receiveBuffer;
protected SSLSocket(Socket socket)
{
_socket = socket;
_remoteEndPoint = (IPEndPoint)_socket.RemoteEndPoint;
_receiveBuffer = new byte[ushort.MaxValue];
_stream = new SslStream(new NetworkStream(socket), false);
}
public virtual void Dispose()
{
_receiveBuffer = null;
_stream.Dispose();
}
public abstract void Start();
public virtual bool Update()
{
return _socket.Connected;
}
public IPAddress GetRemoteIpAddress()
{
return _remoteEndPoint.Address;
}
public async Task AsyncRead()
{
if (!IsOpen())
return;
try
{
var result = await _stream.ReadAsync(_receiveBuffer, 0, _receiveBuffer.Length);
if (result == 0)
{
CloseSocket();
return;
}
ReadHandler(_receiveBuffer, result);
}
catch (Exception ex)
{
Log.outDebug(LogFilter.Network, ex.Message);
}
}
public async Task AsyncHandshake(X509Certificate2 certificate)
{
try
{
await _stream.AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls12, false);
}
catch (Exception ex)
{
await HandshakeHandler(ex);
return;
}
await HandshakeHandler();
}
public abstract Task HandshakeHandler(Exception ex = null);
public abstract void ReadHandler(byte[] data, int receivedLength);
public async Task AsyncWrite(byte[] data)
{
if (!IsOpen())
return;
try
{
await _stream.WriteAsync(data, 0, data.Length);
}
catch (Exception ex)
{
Log.outException(ex);
}
}
public void CloseSocket()
{
try
{
_socket.Shutdown(SocketShutdown.Both);
_socket.Close();
}
catch (Exception ex)
{
Log.outDebug(LogFilter.Network, $"WorldSocket.CloseSocket: {GetRemoteIpAddress()} errored when shutting down socket: {ex.Message}");
}
}
public virtual void OnClose() { Dispose(); }
public bool IsOpen() { return _socket.Connected; }
public void SetNoDelay(bool enable)
{
_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, enable);
}
}
}
+88 -49
View File
@@ -2,8 +2,13 @@
// Licensed under the GNU GENERAL PUBLIC LICENSE. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace Framework.Networking
{
@@ -15,92 +20,126 @@ namespace Framework.Networking
void CloseSocket();
}
public delegate Task SocketReadCallback(byte[] data, int receivedLength);
public abstract class SocketBase : ISocket, IDisposable
{
Socket _socket;
IPEndPoint _remoteIPEndPoint;
Stream _stream;
IPEndPoint _remoteEndPoint;
byte[] _receiveBuffer;
SocketAsyncEventArgs receiveSocketAsyncEventArgsWithCallback;
SocketAsyncEventArgs receiveSocketAsyncEventArgs;
public delegate void SocketReadCallback(SocketAsyncEventArgs args);
protected SocketBase(Socket socket)
protected SocketBase(Socket socket, bool useSSL = false)
{
_socket = socket;
_remoteIPEndPoint = (IPEndPoint)_socket.RemoteEndPoint;
_remoteEndPoint = (IPEndPoint)_socket.RemoteEndPoint;
_receiveBuffer = new byte[ushort.MaxValue];
receiveSocketAsyncEventArgsWithCallback = new SocketAsyncEventArgs();
receiveSocketAsyncEventArgsWithCallback.SetBuffer(new byte[0x4000], 0, 0x4000);
receiveSocketAsyncEventArgs = new SocketAsyncEventArgs();
receiveSocketAsyncEventArgs.SetBuffer(new byte[0x4000], 0, 0x4000);
receiveSocketAsyncEventArgs.Completed += (sender, args) => ProcessReadAsync(args);
if (useSSL)
_stream = new SslStream(new NetworkStream(socket), false);
else
_stream = new NetworkStream(socket);
}
public virtual void Dispose()
{
_socket.Dispose();
_receiveBuffer = null;
_stream.Dispose();
}
public virtual void Start() { }
public abstract void Start();
public virtual bool Update()
{
return IsOpen();
}
public IPEndPoint GetRemoteIpAddress()
public IPAddress GetRemoteIpAddress()
{
return _remoteIPEndPoint;
return _remoteEndPoint.Address;
}
public void AsyncReadWithCallback(SocketReadCallback callback)
public IPEndPoint GetRemoteIpEndPoint()
{
return _remoteEndPoint;
}
public async void AsyncReadWithCallback(SocketReadCallback callback)
{
if (!IsOpen())
return;
receiveSocketAsyncEventArgsWithCallback.Completed += (sender, args) => callback(args);
receiveSocketAsyncEventArgsWithCallback.SetBuffer(0, 0x4000);
if (!_socket.ReceiveAsync(receiveSocketAsyncEventArgsWithCallback))
callback(receiveSocketAsyncEventArgsWithCallback);
}
public void AsyncRead()
{
if (!IsOpen())
return;
receiveSocketAsyncEventArgs.SetBuffer(0, 0x4000);
if (!_socket.ReceiveAsync(receiveSocketAsyncEventArgs))
ProcessReadAsync(receiveSocketAsyncEventArgs);
}
void ProcessReadAsync(SocketAsyncEventArgs args)
{
if (args.SocketError != SocketError.Success)
try
{
CloseSocket();
var result = await _stream.ReadAsync(_receiveBuffer, 0, _receiveBuffer.Length);
if (result == 0)
{
CloseSocket();
return;
}
await callback(_receiveBuffer, result);
}
catch (Exception ex)
{
Log.outDebug(LogFilter.Network, ex.Message);
}
}
public async Task AsyncRead()
{
if (!IsOpen())
return;
try
{
var result = await _stream.ReadAsync(_receiveBuffer, 0, _receiveBuffer.Length);
if (result == 0)
{
CloseSocket();
return;
}
ReadHandler(_receiveBuffer, result);
}
catch (Exception ex)
{
Log.outDebug(LogFilter.Network, ex.Message);
}
}
public async Task AsyncHandshake(X509Certificate2 certificate)
{
try
{
await (_stream as SslStream).AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls12, false);
}
catch (Exception ex)
{
await HandshakeHandler(ex);
return;
}
if (args.BytesTransferred == 0)
{
CloseSocket();
return;
}
ReadHandler(args);
await HandshakeHandler();
}
public abstract void ReadHandler(SocketAsyncEventArgs args);
public virtual Task HandshakeHandler(Exception exception = null) { return null; }
public void AsyncWrite(byte[] data)
public abstract void ReadHandler(byte[] data, int receivedLength);
public async Task AsyncWrite(byte[] data)
{
if (!IsOpen())
return;
_socket.Send(data);
try
{
await _stream.WriteAsync(data, 0, data.Length);
}
catch (Exception ex)
{
Log.outException(ex);
}
}
public void CloseSocket()