Fixed some bad memory leaks on sockets

This commit is contained in:
hondacrx
2017-11-06 17:59:59 -05:00
parent 71d913107d
commit 45a394cda7
3 changed files with 52 additions and 38 deletions
+1 -1
View File
@@ -58,7 +58,7 @@ namespace Framework.Networking
if (_newSockets.Empty()) if (_newSockets.Empty())
return; return;
foreach (var socket in _newSockets) foreach (var socket in _newSockets.ToArray())
{ {
if (!socket.IsOpen()) if (!socket.IsOpen())
{ {
+32 -27
View File
@@ -31,7 +31,10 @@ namespace Framework.Networking
_receiveBuffer = new byte[ushort.MaxValue]; _receiveBuffer = new byte[ushort.MaxValue];
} }
public virtual void Dispose() { } public virtual void Dispose()
{
_receiveBuffer = null;
}
public abstract void Start(); public abstract void Start();
@@ -57,16 +60,16 @@ namespace Framework.Networking
try try
{ {
var socketEventargs = new SocketAsyncEventArgs(); using (var socketEventargs = new SocketAsyncEventArgs())
{
socketEventargs.SetBuffer(_receiveBuffer, 0, _receiveBuffer.Length);
socketEventargs.Completed += (sender, args) => ReadHandlerInternal(args);
socketEventargs.SocketFlags = SocketFlags.None;
socketEventargs.RemoteEndPoint = _socket.RemoteEndPoint;
socketEventargs.SetBuffer(_receiveBuffer, 0, _receiveBuffer.Length); if (!_socket.ReceiveAsync(socketEventargs))
ReadHandlerInternal(socketEventargs);
socketEventargs.Completed += (sender, args) => ReadHandlerInternal(args); }
socketEventargs.SocketFlags = SocketFlags.None;
socketEventargs.RemoteEndPoint = _socket.RemoteEndPoint;
if (!_socket.ReceiveAsync(socketEventargs))
ReadHandlerInternal(socketEventargs);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -82,15 +85,15 @@ namespace Framework.Networking
try try
{ {
var socketEventargs = new SocketAsyncEventArgs(); using (var socketEventargs = new SocketAsyncEventArgs())
{
socketEventargs.SetBuffer(_receiveBuffer, 0, _receiveBuffer.Length); socketEventargs.SetBuffer(_receiveBuffer, 0, _receiveBuffer.Length);
socketEventargs.Completed += (sender, args) => callback(args);
socketEventargs.Completed += (sender, args) => callback(args); socketEventargs.UserToken = _socket;
socketEventargs.UserToken = _socket; socketEventargs.SocketFlags = SocketFlags.None;
socketEventargs.SocketFlags = SocketFlags.None; if (!_socket.ReceiveAsync(socketEventargs))
if (!_socket.ReceiveAsync(socketEventargs)) callback(socketEventargs);
callback(socketEventargs); }
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -122,14 +125,16 @@ namespace Framework.Networking
if (!IsOpen()) if (!IsOpen())
return; return;
var socketEventargs = new SocketAsyncEventArgs(); using (var socketEventargs = new SocketAsyncEventArgs())
socketEventargs.SetBuffer(data, 0, data.Length); {
socketEventargs.Completed += WriteHandlerInternal; socketEventargs.SetBuffer(data, 0, data.Length);
socketEventargs.RemoteEndPoint = _socket.RemoteEndPoint; socketEventargs.Completed += WriteHandlerInternal;
socketEventargs.UserToken = _socket; socketEventargs.RemoteEndPoint = _socket.RemoteEndPoint;
socketEventargs.SocketFlags = SocketFlags.None; socketEventargs.UserToken = _socket;
socketEventargs.SocketFlags = SocketFlags.None;
_socket.SendAsync(socketEventargs); _socket.SendAsync(socketEventargs);
}
} }
void WriteHandlerInternal(object sender, SocketAsyncEventArgs args) void WriteHandlerInternal(object sender, SocketAsyncEventArgs args)
@@ -158,7 +163,7 @@ namespace Framework.Networking
_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, enable); _socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, enable);
} }
public virtual void OnClose() { } public virtual void OnClose() { Dispose(); }
public bool IsOpen() { return !_closed; } public bool IsOpen() { return !_closed; }
+19 -10
View File
@@ -29,12 +29,12 @@ namespace Game.Network
{ {
public class WorldSocket : SocketBase public class WorldSocket : SocketBase
{ {
string ClientConnectionInitialize = "WORLD OF WARCRAFT CONNECTION - CLIENT TO SERVER"; static string ClientConnectionInitialize = "WORLD OF WARCRAFT CONNECTION - CLIENT TO SERVER";
string ServerConnectionInitialize = "WORLD OF WARCRAFT CONNECTION - SERVER TO CLIENT"; static string ServerConnectionInitialize = "WORLD OF WARCRAFT CONNECTION - SERVER TO CLIENT";
byte[] AuthCheckSeed = { 0xC5, 0xC6, 0x98, 0x95, 0x76, 0x3F, 0x1D, 0xCD, 0xB6, 0xA1, 0x37, 0x28, 0xB3, 0x12, 0xFF, 0x8A }; static byte[] AuthCheckSeed = { 0xC5, 0xC6, 0x98, 0x95, 0x76, 0x3F, 0x1D, 0xCD, 0xB6, 0xA1, 0x37, 0x28, 0xB3, 0x12, 0xFF, 0x8A };
byte[] SessionKeySeed = { 0x58, 0xCB, 0xCF, 0x40, 0xFE, 0x2E, 0xCE, 0xA6, 0x5A, 0x90, 0xB8, 0x01, 0x68, 0x6C, 0x28, 0x0B }; static byte[] SessionKeySeed = { 0x58, 0xCB, 0xCF, 0x40, 0xFE, 0x2E, 0xCE, 0xA6, 0x5A, 0x90, 0xB8, 0x01, 0x68, 0x6C, 0x28, 0x0B };
byte[] ContinuedSessionSeed = { 0x16, 0xAD, 0x0C, 0xD4, 0x46, 0xF9, 0x4F, 0xB2, 0xEF, 0x7D, 0xEA, 0x2A, 0x17, 0x66, 0x4D, 0x2F }; static byte[] ContinuedSessionSeed = { 0x16, 0xAD, 0x0C, 0xD4, 0x46, 0xF9, 0x4F, 0xB2, 0xEF, 0x7D, 0xEA, 0x2A, 0x17, 0x66, 0x4D, 0x2F };
public WorldSocket(Socket socket) : base(socket) public WorldSocket(Socket socket) : base(socket)
{ {
@@ -43,6 +43,20 @@ namespace Game.Network
worldCrypt = new WorldCrypt(); worldCrypt = new WorldCrypt();
} }
public override void Dispose()
{
_worldSession = null;
_queryProcessor = null;
_serverChallenge = null;
worldCrypt = null;
_encryptSeed = null;
_decryptSeed = null;
_sessionKey = null;
_compressionStream = null;
base.Dispose();
}
public override void Start() public override void Start()
{ {
string ip_address = GetRemoteIpAddress().ToString(); string ip_address = GetRemoteIpAddress().ToString();
@@ -317,11 +331,6 @@ namespace Game.Network
return compressedSize; return compressedSize;
} }
public override void OnClose()
{
_worldSession = null;
}
public override bool Update() public override bool Update()
{ {
if (!base.Update()) if (!base.Update())