Skip to content

Commit

Permalink
Remove logging from NetworkClient
Browse files Browse the repository at this point in the history
Fixes crash bug when attempting to re-serialize packets with invalid incoming data for logging purposes.
  • Loading branch information
ethanmoffat committed May 31, 2024
1 parent 341cad2 commit 7840e9a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 69 deletions.
75 changes: 11 additions & 64 deletions EOLib/Net/Communication/NetworkClient.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using System;
using System.Diagnostics;
using System.Linq;
using EOLib.IO.Services;
using EOLib.Net.Handlers;
using EOLib.Net.PacketProcessing;
using Moffat.EndlessOnline.SDK.Protocol.Net;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using EOLib.IO.Services;
using EOLib.Logger;
using EOLib.Net.Handlers;
using EOLib.Net.PacketProcessing;
using Moffat.EndlessOnline.SDK.Data;
using Moffat.EndlessOnline.SDK.Protocol.Net;

namespace EOLib.Net.Communication
{
Expand All @@ -19,7 +15,6 @@ public class NetworkClient : INetworkClient
private readonly IPacketProcessActions _packetProcessActions;
private readonly IPacketHandlingActions _packetHandlingActions;
private readonly INumberEncoderService _numberEncoderService;
private readonly ILoggerProvider _loggerProvider;

private readonly IAsyncSocket _socket;

Expand All @@ -32,13 +27,11 @@ public class NetworkClient : INetworkClient
public NetworkClient(IPacketProcessActions packetProcessActions,
IPacketHandlingActions packetHandlingActions,
INumberEncoderService numberEncoderService,
ILoggerProvider loggerProvider,
TimeSpan receiveTimeout)
{
_packetProcessActions = packetProcessActions;
_packetHandlingActions = packetHandlingActions;
_numberEncoderService = numberEncoderService;
_loggerProvider = loggerProvider;
ReceiveTimeout = receiveTimeout;

_socket = new AsyncSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Expand Down Expand Up @@ -86,35 +79,18 @@ public async Task RunReceiveLoopAsync(CancellationToken cancellationToken)
while (!cancellationToken.IsCancellationRequested)
{
var lengthData = await _socket.ReceiveAsync(2, cancellationToken);
if (cancellationToken.IsCancellationRequested)
if (cancellationToken.IsCancellationRequested || lengthData.Length != 2)
{
_loggerProvider.Logger.Log("RECV thread: Cancellation was requested when receiving length");
break;
}

if (lengthData.Length != 2)
{
_loggerProvider.Logger.Log("RECV thread: Did not receive two bytes of data");
break;
}

var length = _numberEncoderService.DecodeNumber(lengthData);

var packetData = await _socket.ReceiveAsync(length, cancellationToken);
if (cancellationToken.IsCancellationRequested)
if (cancellationToken.IsCancellationRequested || packetData.Length != length)
{
_loggerProvider.Logger.Log("RECV thread: Cancellation was requested when receiving data");
break;
}

if (packetData.Length != length)
{
_loggerProvider.Logger.Log("RECV thread: Did not receive expected {0} bytes of data", length);
break;
}

var packet = _packetProcessActions.DecodeData(packetData);
LogReceivedPacket(packet);

_packetHandlingActions.EnqueuePacketForHandling(packet);
}
Expand All @@ -128,45 +104,16 @@ public int Send(IPacket packet)

public async Task<int> SendAsync(IPacket packet, int timeout = 1500)
{
LogSentPacket(packet, true);
var bytesToSend = _packetProcessActions.EncodePacket(packet);
using (var cts = new CancellationTokenSource(timeout))
return await _socket.SendAsync(bytesToSend, cts.Token).ConfigureAwait(false);
using var cts = new CancellationTokenSource(timeout);
return await _socket.SendAsync(bytesToSend, cts.Token).ConfigureAwait(false);
}

public async Task<int> SendRawPacketAsync(IPacket packet, int timeout = 1500)
{
LogSentPacket(packet, false);
var bytesToSend = _packetProcessActions.EncodeRawPacket(packet);
using (var cts = new CancellationTokenSource(timeout))
return await _socket.SendAsync(bytesToSend, cts.Token).ConfigureAwait(false);
}

[Conditional("DEBUG")]
private void LogReceivedPacket(IPacket packet)
{
var writer = new EoWriter();
packet.Serialize(writer);

_loggerProvider.Logger.Log("RECV thread: Received packet Family={0,-13} Action={1,-8} sz={2,-5} data={3}",
Enum.GetName(typeof(PacketFamily), packet.Family),
Enum.GetName(typeof(PacketAction), packet.Action),
writer.Length,
string.Join(":", writer.ToByteArray().Select(b => $"{b:x2}")));
}

[Conditional("DEBUG")]
private void LogSentPacket(IPacket packet, bool encoded)
{
var writer = new EoWriter();
packet.Serialize(writer);

_loggerProvider.Logger.Log("SEND thread: Processing {0,-3} packet Family={1,-13} Action={2,-8} sz={3,-5} data={4}",
encoded ? "ENC" : "RAW",
Enum.GetName(typeof(PacketFamily), packet.Family),
Enum.GetName(typeof(PacketAction), packet.Action),
writer.Length,
string.Join(":", writer.ToByteArray().Select(b => $"{b:x2}")));
using var cts = new CancellationTokenSource(timeout);
return await _socket.SendAsync(bytesToSend, cts.Token).ConfigureAwait(false);
}

public void Dispose()
Expand Down
7 changes: 2 additions & 5 deletions EOLib/Net/Communication/NetworkClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ public class NetworkClientFactory : INetworkClientFactory
private readonly IPacketProcessActions _packetProcessActions;
private readonly IPacketHandlingActions _packetHandlingActions;
private readonly INumberEncoderService _numberEncoderService;
private readonly ILoggerProvider _loggerProvider;

public NetworkClientFactory(IPacketProcessActions packetProcessActions,
IPacketHandlingActions packetHandlingActions,
INumberEncoderService numberEncoderService,
ILoggerProvider loggerProvider)
INumberEncoderService numberEncoderService)
{
_packetProcessActions = packetProcessActions;
_packetHandlingActions = packetHandlingActions;
_numberEncoderService = numberEncoderService;
_loggerProvider = loggerProvider;
}

public INetworkClient CreateNetworkClient(int timeout = Constants.ResponseTimeout)
{
return new NetworkClient(_packetProcessActions, _packetHandlingActions, _numberEncoderService, _loggerProvider, TimeSpan.FromMilliseconds(timeout));
return new NetworkClient(_packetProcessActions, _packetHandlingActions, _numberEncoderService, TimeSpan.FromMilliseconds(timeout));
}
}
}

0 comments on commit 7840e9a

Please sign in to comment.