Ported .Net Core commits:
hondacrx: - Initial commit: Switch to .Net Core 2.0 - Fix build and removed not needed files Fabi: - Updated solution platforms. - Changed folder structure. - Change library target framework to netstandard2.0. - Updated solution platforms again... - Removed windows specific kernel32 function usage (Ctrl-C handler).
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (C) 2012-2017 CypherCore <http://github.com/CypherCore>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Framework.Networking
|
||||
{
|
||||
public delegate void SocketAcceptDelegate(Socket newSocket);
|
||||
|
||||
public class AsyncAcceptor
|
||||
{
|
||||
public AsyncAcceptor(string ip, int port)
|
||||
{
|
||||
var bindIP = IPAddress.None;
|
||||
if (!IPAddress.TryParse(ip, out bindIP))
|
||||
{
|
||||
Log.outError(LogFilter.Network, "Server can't be started: Invalid IP-Address ({0})", ip);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_listener = new TcpListener(bindIP, port);
|
||||
_listener.Start();
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
Log.outException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async void AsyncAcceptSocket(SocketAcceptDelegate mgrHandler)
|
||||
{
|
||||
try
|
||||
{
|
||||
var _socket = await _listener.AcceptSocketAsync();
|
||||
if (_socket != null)
|
||||
{
|
||||
mgrHandler(_socket);
|
||||
|
||||
if (!_closed)
|
||||
AsyncAcceptSocket(mgrHandler);
|
||||
}
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{ }
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
if (_closed)
|
||||
return;
|
||||
|
||||
_closed = true;
|
||||
_listener.Stop();
|
||||
}
|
||||
|
||||
TcpListener _listener;
|
||||
volatile bool _closed;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright (C) 2012-2017 CypherCore <http://github.com/CypherCore>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace Framework.Networking
|
||||
{
|
||||
public class NetworkThread<SocketType> where SocketType : ISocket
|
||||
{
|
||||
public void Stop()
|
||||
{
|
||||
_stopped = true;
|
||||
}
|
||||
|
||||
public bool Start()
|
||||
{
|
||||
if (_thread != null)
|
||||
return false;
|
||||
|
||||
_thread = new Thread(Run);
|
||||
_thread.Start();
|
||||
return true;
|
||||
}
|
||||
|
||||
public int GetConnectionCount()
|
||||
{
|
||||
return _connections;
|
||||
}
|
||||
|
||||
public virtual void AddSocket(SocketType sock)
|
||||
{
|
||||
Interlocked.Increment(ref _connections);
|
||||
_newSockets.Add(sock);
|
||||
SocketAdded(sock);
|
||||
}
|
||||
|
||||
public virtual void SocketAdded(SocketType sock) { }
|
||||
public virtual void SocketRemoved(SocketType sock) { }
|
||||
|
||||
void AddNewSockets()
|
||||
{
|
||||
if (_newSockets.Empty())
|
||||
return;
|
||||
|
||||
foreach (var socket in _newSockets)
|
||||
{
|
||||
if (!socket.IsOpen())
|
||||
{
|
||||
SocketRemoved(socket);
|
||||
|
||||
Interlocked.Decrement(ref _connections);
|
||||
}
|
||||
else
|
||||
_Sockets.Add(socket);
|
||||
}
|
||||
|
||||
_newSockets.Clear();
|
||||
}
|
||||
|
||||
void Run()
|
||||
{
|
||||
Log.outDebug(LogFilter.Network, "Network Thread Starting");
|
||||
|
||||
int sleepTime = 10;
|
||||
uint tickStart = 0, diff = 0;
|
||||
while (!_stopped)
|
||||
{
|
||||
Thread.Sleep(sleepTime);
|
||||
|
||||
tickStart = Time.GetMSTime();
|
||||
|
||||
AddNewSockets();
|
||||
|
||||
foreach (var socket in _Sockets.ToList())
|
||||
{
|
||||
if (!socket.Update())
|
||||
{
|
||||
if (socket.IsOpen())
|
||||
socket.CloseSocket();
|
||||
|
||||
SocketRemoved(socket);
|
||||
|
||||
--_connections;
|
||||
_Sockets.Remove(socket);
|
||||
}
|
||||
}
|
||||
|
||||
diff = Time.GetMSTimeDiffToNow(tickStart);
|
||||
sleepTime = (int)(diff > 10 ? 0 : 10 - diff);
|
||||
}
|
||||
|
||||
Log.outDebug(LogFilter.Misc, "Network Thread exits");
|
||||
_newSockets.Clear();
|
||||
_Sockets.Clear();
|
||||
}
|
||||
|
||||
volatile int _connections;
|
||||
volatile bool _stopped;
|
||||
|
||||
Thread _thread;
|
||||
|
||||
List<SocketType> _Sockets = new List<SocketType>();
|
||||
List<SocketType> _newSockets = new List<SocketType>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright (C) 2012-2017 CypherCore <http://github.com/CypherCore>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Security;
|
||||
using System.Net.Sockets;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace Framework.Networking
|
||||
{
|
||||
public abstract class SSLSocket : ISocket
|
||||
{
|
||||
public SSLSocket(Socket socket)
|
||||
{
|
||||
_socket = socket;
|
||||
_remoteAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address;
|
||||
_remotePort = (ushort)((IPEndPoint)_socket.RemoteEndPoint).Port;
|
||||
_receiveBuffer = new byte[ushort.MaxValue];
|
||||
|
||||
_stream = new SslStream(new NetworkStream(socket), false);
|
||||
}
|
||||
|
||||
public abstract void Start();
|
||||
|
||||
public virtual bool Update()
|
||||
{
|
||||
return IsOpen();
|
||||
}
|
||||
|
||||
public IPAddress GetRemoteIpAddress()
|
||||
{
|
||||
return _remoteAddress;
|
||||
}
|
||||
|
||||
public ushort GetRemotePort()
|
||||
{
|
||||
return _remotePort;
|
||||
}
|
||||
|
||||
public void AsyncRead()
|
||||
{
|
||||
if (!IsOpen())
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
_stream.BeginRead(_receiveBuffer, 0, _receiveBuffer.Length, ReadHandlerInternal, _stream);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.outException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
void ReadHandlerInternal(IAsyncResult result)
|
||||
{
|
||||
int bytes = 0;
|
||||
try
|
||||
{
|
||||
bytes = _stream.EndRead(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.outException(ex);
|
||||
}
|
||||
|
||||
ReadHandler(bytes);
|
||||
}
|
||||
|
||||
public abstract void ReadHandler(int transferredBytes);
|
||||
|
||||
public void AsyncHandshake(X509Certificate2 certificate)
|
||||
{
|
||||
try
|
||||
{
|
||||
_stream.AuthenticateAsServer(certificate, false, System.Security.Authentication.SslProtocols.Tls12, false);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Log.outException(ex);
|
||||
CloseSocket();
|
||||
return;
|
||||
}
|
||||
AsyncRead();
|
||||
}
|
||||
|
||||
public void AsyncWrite(byte[] data)
|
||||
{
|
||||
if (!IsOpen())
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
_stream.Write(data, 0, data.Length);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.outException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void CloseSocket()
|
||||
{
|
||||
try
|
||||
{
|
||||
_closed = true;
|
||||
_socket.Shutdown(SocketShutdown.Both);
|
||||
_socket.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.outDebug(LogFilter.Network, "WorldSocket.CloseSocket: {0} errored when shutting down socket: {1}", GetRemoteIpAddress().ToString(), ex.Message);
|
||||
}
|
||||
|
||||
OnClose();
|
||||
}
|
||||
|
||||
public void SetNoDelay(bool enable)
|
||||
{
|
||||
_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, enable);
|
||||
}
|
||||
|
||||
public virtual void OnClose() { }
|
||||
|
||||
public bool IsOpen() { return !_closed; }
|
||||
|
||||
public byte[] GetReceiveBuffer()
|
||||
{
|
||||
return _receiveBuffer;
|
||||
}
|
||||
|
||||
Socket _socket;
|
||||
internal SslStream _stream;
|
||||
byte[] _receiveBuffer;
|
||||
|
||||
volatile bool _closed;
|
||||
|
||||
IPAddress _remoteAddress;
|
||||
ushort _remotePort;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
* Copyright (C) 2012-2017 CypherCore <http://github.com/CypherCore>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Framework.Networking
|
||||
{
|
||||
public abstract class SocketBase : ISocket, IDisposable
|
||||
{
|
||||
public SocketBase(Socket socket)
|
||||
{
|
||||
_socket = socket;
|
||||
_remoteAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address;
|
||||
_remotePort = (ushort)((IPEndPoint)_socket.RemoteEndPoint).Port;
|
||||
_receiveBuffer = new byte[ushort.MaxValue];
|
||||
}
|
||||
|
||||
public virtual void Dispose() { }
|
||||
|
||||
public abstract void Start();
|
||||
|
||||
public virtual bool Update()
|
||||
{
|
||||
return IsOpen();
|
||||
}
|
||||
|
||||
public IPAddress GetRemoteIpAddress()
|
||||
{
|
||||
return _remoteAddress;
|
||||
}
|
||||
|
||||
public ushort GetRemotePort()
|
||||
{
|
||||
return _remotePort;
|
||||
}
|
||||
|
||||
public void AsyncRead()
|
||||
{
|
||||
if (!IsOpen())
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var socketEventargs = new SocketAsyncEventArgs();
|
||||
|
||||
socketEventargs.SetBuffer(_receiveBuffer, 0, _receiveBuffer.Length);
|
||||
|
||||
socketEventargs.Completed += ReadHandlerInternal;
|
||||
socketEventargs.UserToken = _socket;
|
||||
socketEventargs.SocketFlags = SocketFlags.None;
|
||||
|
||||
_socket.ReceiveAsync(socketEventargs);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.outException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public delegate void SocketReadCallback(object sender, SocketAsyncEventArgs args);
|
||||
public void AsyncReadWithCallback(SocketReadCallback callback)
|
||||
{
|
||||
if (!IsOpen())
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var socketEventargs = new SocketAsyncEventArgs();
|
||||
|
||||
socketEventargs.SetBuffer(_receiveBuffer, 0, _receiveBuffer.Length);
|
||||
|
||||
socketEventargs.Completed += new EventHandler<SocketAsyncEventArgs>(callback);
|
||||
socketEventargs.UserToken = _socket;
|
||||
socketEventargs.SocketFlags = SocketFlags.None;
|
||||
|
||||
_socket.ReceiveAsync(socketEventargs);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.outException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
void ReadHandlerInternal(object sender, SocketAsyncEventArgs args)
|
||||
{
|
||||
args.Completed -= ReadHandlerInternal;
|
||||
if (args.SocketError != SocketError.Success)
|
||||
{
|
||||
CloseSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.BytesTransferred == 0)
|
||||
{
|
||||
CloseSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
ReadHandler(args.BytesTransferred);
|
||||
}
|
||||
|
||||
public abstract void ReadHandler(int transferredBytes);
|
||||
|
||||
public void AsyncWrite(byte[] data)
|
||||
{
|
||||
if (!IsOpen())
|
||||
return;
|
||||
|
||||
var socketEventargs = new SocketAsyncEventArgs();
|
||||
socketEventargs.SetBuffer(data, 0, data.Length);
|
||||
socketEventargs.Completed += WriteHandlerInternal;
|
||||
socketEventargs.RemoteEndPoint = _socket.RemoteEndPoint;
|
||||
socketEventargs.SocketFlags = SocketFlags.None;
|
||||
|
||||
_socket.SendAsync(socketEventargs);
|
||||
}
|
||||
|
||||
void WriteHandlerInternal(object sender, SocketAsyncEventArgs args)
|
||||
{
|
||||
args.Completed -= WriteHandlerInternal;
|
||||
}
|
||||
|
||||
public void CloseSocket()
|
||||
{
|
||||
try
|
||||
{
|
||||
_closed = true;
|
||||
_socket.Shutdown(SocketShutdown.Both);
|
||||
_socket.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.outDebug(LogFilter.Network, "WorldSocket.CloseSocket: {0} errored when shutting down socket: {1}", GetRemoteIpAddress().ToString(), ex.Message);
|
||||
}
|
||||
|
||||
OnClose();
|
||||
}
|
||||
|
||||
public void SetNoDelay(bool enable)
|
||||
{
|
||||
_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, enable);
|
||||
}
|
||||
|
||||
public virtual void OnClose() { }
|
||||
|
||||
public bool IsOpen() { return !_closed; }
|
||||
|
||||
public byte[] GetReceiveBuffer()
|
||||
{
|
||||
return _receiveBuffer;
|
||||
}
|
||||
|
||||
Socket _socket;
|
||||
byte[] _receiveBuffer;
|
||||
|
||||
volatile bool _closed;
|
||||
|
||||
IPAddress _remoteAddress;
|
||||
ushort _remotePort;
|
||||
}
|
||||
|
||||
public interface ISocket
|
||||
{
|
||||
void Start();
|
||||
bool Update();
|
||||
bool IsOpen();
|
||||
void CloseSocket();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (C) 2012-2017 CypherCore <http://github.com/CypherCore>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Framework.Networking
|
||||
{
|
||||
public class SocketManager<SocketType> where SocketType : ISocket
|
||||
{
|
||||
public virtual bool StartNetwork(string bindIp, int port, int threadCount = 1)
|
||||
{
|
||||
Contract.Assert(threadCount > 0);
|
||||
|
||||
Acceptor = new AsyncAcceptor(bindIp, port);
|
||||
|
||||
_threadCount = threadCount;
|
||||
_threads = new NetworkThread<SocketType>[GetNetworkThreadCount()];
|
||||
|
||||
for (int i = 0; i < _threadCount; ++i)
|
||||
{
|
||||
_threads[i] = new NetworkThread<SocketType>();
|
||||
_threads[i].Start();
|
||||
}
|
||||
|
||||
Acceptor.AsyncAcceptSocket(OnSocketOpen);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void StopNetwork()
|
||||
{
|
||||
Acceptor.Close();
|
||||
|
||||
if (_threadCount != 0)
|
||||
for (int i = 0; i < _threadCount; ++i)
|
||||
_threads[i].Stop();
|
||||
|
||||
Wait();
|
||||
|
||||
Acceptor = null;
|
||||
_threads = null;
|
||||
_threadCount = 0;
|
||||
}
|
||||
|
||||
void Wait()
|
||||
{
|
||||
//if (_threadCount != 0)
|
||||
//for (int i = 0; i < _threadCount; ++i)
|
||||
//_threads[i].Wait();
|
||||
}
|
||||
|
||||
public virtual void OnSocketOpen(Socket sock)
|
||||
{
|
||||
try
|
||||
{
|
||||
SocketType newSocket = (SocketType)Activator.CreateInstance(typeof(SocketType), sock);
|
||||
newSocket.Start();
|
||||
|
||||
_threads[SelectThreadWithMinConnections()].AddSocket(newSocket);
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
Log.outException(err);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetNetworkThreadCount() { return _threadCount; }
|
||||
|
||||
uint SelectThreadWithMinConnections()
|
||||
{
|
||||
uint min = 0;
|
||||
|
||||
for (uint i = 1; i < _threadCount; ++i)
|
||||
if (_threads[i].GetConnectionCount() < _threads[min].GetConnectionCount())
|
||||
min = i;
|
||||
|
||||
return min;
|
||||
}
|
||||
|
||||
public AsyncAcceptor Acceptor;
|
||||
NetworkThread<SocketType>[] _threads;
|
||||
int _threadCount;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user