Core/Refactor: Part 1
This commit is contained in:
@@ -21,7 +21,7 @@ using System.Threading;
|
||||
|
||||
namespace Framework.Networking
|
||||
{
|
||||
public class NetworkThread<SocketType> where SocketType : ISocket
|
||||
public class NetworkThread<TSocketType> where TSocketType : ISocket
|
||||
{
|
||||
public void Stop()
|
||||
{
|
||||
@@ -43,15 +43,15 @@ namespace Framework.Networking
|
||||
return _connections;
|
||||
}
|
||||
|
||||
public virtual void AddSocket(SocketType sock)
|
||||
public virtual void AddSocket(TSocketType sock)
|
||||
{
|
||||
Interlocked.Increment(ref _connections);
|
||||
_newSockets.Add(sock);
|
||||
SocketAdded(sock);
|
||||
}
|
||||
|
||||
public virtual void SocketAdded(SocketType sock) { }
|
||||
public virtual void SocketRemoved(SocketType sock) { }
|
||||
protected virtual void SocketAdded(TSocketType sock) { }
|
||||
protected virtual void SocketRemoved(TSocketType sock) { }
|
||||
|
||||
void AddNewSockets()
|
||||
{
|
||||
@@ -78,12 +78,11 @@ namespace Framework.Networking
|
||||
Log.outDebug(LogFilter.Network, "Network Thread Starting");
|
||||
|
||||
int sleepTime = 10;
|
||||
uint tickStart = 0, diff = 0;
|
||||
while (!_stopped)
|
||||
{
|
||||
Thread.Sleep(sleepTime);
|
||||
|
||||
tickStart = Time.GetMSTime();
|
||||
uint tickStart = Time.GetMSTime();
|
||||
|
||||
AddNewSockets();
|
||||
|
||||
@@ -101,7 +100,7 @@ namespace Framework.Networking
|
||||
}
|
||||
}
|
||||
|
||||
diff = Time.GetMSTimeDiffToNow(tickStart);
|
||||
uint diff = Time.GetMSTimeDiffToNow(tickStart);
|
||||
sleepTime = (int)(diff > 10 ? 0 : 10 - diff);
|
||||
}
|
||||
|
||||
@@ -110,12 +109,12 @@ namespace Framework.Networking
|
||||
_Sockets.Clear();
|
||||
}
|
||||
|
||||
volatile int _connections;
|
||||
int _connections;
|
||||
volatile bool _stopped;
|
||||
|
||||
Thread _thread;
|
||||
|
||||
List<SocketType> _Sockets = new List<SocketType>();
|
||||
List<SocketType> _newSockets = new List<SocketType>();
|
||||
List<TSocketType> _Sockets = new List<TSocketType>();
|
||||
List<TSocketType> _newSockets = new List<TSocketType>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ using System.Net.Sockets;
|
||||
|
||||
namespace Framework.Networking
|
||||
{
|
||||
public class SocketManager<SocketType> where SocketType : ISocket
|
||||
public class SocketManager<TSocketType> where TSocketType : ISocket
|
||||
{
|
||||
public virtual bool StartNetwork(string bindIp, int port, int threadCount = 1)
|
||||
{
|
||||
@@ -30,11 +30,11 @@ namespace Framework.Networking
|
||||
Acceptor = new AsyncAcceptor(bindIp, port);
|
||||
|
||||
_threadCount = threadCount;
|
||||
_threads = new NetworkThread<SocketType>[GetNetworkThreadCount()];
|
||||
_threads = new NetworkThread<TSocketType>[GetNetworkThreadCount()];
|
||||
|
||||
for (int i = 0; i < _threadCount; ++i)
|
||||
{
|
||||
_threads[i] = new NetworkThread<SocketType>();
|
||||
_threads[i] = new NetworkThread<TSocketType>();
|
||||
_threads[i].Start();
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ namespace Framework.Networking
|
||||
{
|
||||
try
|
||||
{
|
||||
SocketType newSocket = (SocketType)Activator.CreateInstance(typeof(SocketType), sock);
|
||||
TSocketType newSocket = (TSocketType)Activator.CreateInstance(typeof(TSocketType), sock);
|
||||
newSocket.Start();
|
||||
|
||||
_threads[SelectThreadWithMinConnections()].AddSocket(newSocket);
|
||||
@@ -94,7 +94,7 @@ namespace Framework.Networking
|
||||
}
|
||||
|
||||
public AsyncAcceptor Acceptor;
|
||||
NetworkThread<SocketType>[] _threads;
|
||||
NetworkThread<TSocketType>[] _threads;
|
||||
int _threadCount;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user