Skip to content

Commit

Permalink
Add receive timeout parameter to client creation. Use longer timeout …
Browse files Browse the repository at this point in the history
…for bots in consideration of bcrypt hash in etheos. Add timestamp function 'time()'
  • Loading branch information
ethanmoffat committed Feb 1, 2022
1 parent e729a0e commit ddbaa44
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 9 deletions.
5 changes: 4 additions & 1 deletion EOBot/Interpreter/BuiltInIdentifierConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void SetupBuiltInFunctions()
_state.SymbolTable[PredefinedIdentifiers.LEN_FUNC] = Readonly(new Function<ArrayVariable, int>(PredefinedIdentifiers.LEN_FUNC, param1 => param1.Value.Count));
_state.SymbolTable[PredefinedIdentifiers.ARRAY_FUNC] = Readonly(new Function<int, List<IVariable>>(PredefinedIdentifiers.ARRAY_FUNC, param1 => Enumerable.Repeat(UndefinedVariable.Instance, param1).Cast<IVariable>().ToList()));
_state.SymbolTable[PredefinedIdentifiers.SLEEP] = Readonly(new VoidFunction<int>(PredefinedIdentifiers.SLEEP, param1 => Thread.Sleep(param1)));
_state.SymbolTable[PredefinedIdentifiers.TIME] = Readonly(new Function<string>(PredefinedIdentifiers.TIME, () => DateTime.Now.ToLongTimeString()));

BotDependencySetup();
_state.SymbolTable[PredefinedIdentifiers.CONNECT_FUNC] = Readonly(new AsyncVoidFunction<string, int>(PredefinedIdentifiers.CONNECT_FUNC, ConnectAsync));
Expand Down Expand Up @@ -78,7 +79,9 @@ private void BotDependencySetup()
var c = DependencyMaster.TypeRegistry[_botIndex];
var networkClientRepository = c.Resolve<INetworkClientRepository>();
var networkClientFactory = c.Resolve<INetworkClientFactory>();
networkClientRepository.NetworkClient = networkClientFactory.CreateNetworkClient();

const int LongReceiveTimeout = 15000;
networkClientRepository.NetworkClient = networkClientFactory.CreateNetworkClient(LongReceiveTimeout);
}

private async Task ConnectAsync(string host, int port)
Expand Down
1 change: 1 addition & 0 deletions EOBot/Interpreter/Variables/PredefinedIdentifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class PredefinedIdentifiers
public const string LEN_FUNC = "len";
public const string ARRAY_FUNC = "array";
public const string SLEEP = "sleep";
public const string TIME = "time";

// game functions
public const string CONNECT_FUNC = "Connect";
Expand Down
2 changes: 2 additions & 0 deletions EOLib/Net/Communication/INetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface INetworkClient : IDisposable

bool Started { get; }

TimeSpan ReceiveTimeout { get; }

Task<ConnectResult> ConnectToServer(string host, int port);

void Disconnect();
Expand Down
2 changes: 1 addition & 1 deletion EOLib/Net/Communication/INetworkClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface INetworkClientFactory
{
INetworkClient CreateNetworkClient();
INetworkClient CreateNetworkClient(int timeout = Constants.ResponseTimeout);
}
}
7 changes: 5 additions & 2 deletions EOLib/Net/Communication/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ public class NetworkClient : INetworkClient

public bool Started { get; private set; }

public TimeSpan ReceiveTimeout { get; }

public NetworkClient(IPacketProcessActions packetProcessActions,
IPacketHandlingActions packetHandlingActions,
INumberEncoderService numberEncoderService,
ILoggerProvider loggerProvider)
ILoggerProvider loggerProvider,
TimeSpan receiveTimeout)
{
_packetProcessActions = packetProcessActions;
_packetHandlingActions = packetHandlingActions;
_numberEncoderService = numberEncoderService;
_loggerProvider = loggerProvider;
ReceiveTimeout = receiveTimeout;

_socket = new AsyncSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

_backgroundReceiveCTS = new CancellationTokenSource();
}

Expand Down
5 changes: 3 additions & 2 deletions EOLib/Net/Communication/NetworkClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using EOLib.Logger;
using EOLib.Net.Handlers;
using EOLib.Net.PacketProcessing;
using System;

namespace EOLib.Net.Communication
{
Expand All @@ -25,9 +26,9 @@ public class NetworkClientFactory : INetworkClientFactory
_loggerProvider = loggerProvider;
}

public INetworkClient CreateNetworkClient()
public INetworkClient CreateNetworkClient(int timeout = Constants.ResponseTimeout)
{
return new NetworkClient(_packetProcessActions, _packetHandlingActions, _numberEncoderService, _loggerProvider);
return new NetworkClient(_packetProcessActions, _packetHandlingActions, _numberEncoderService, _loggerProvider, TimeSpan.FromMilliseconds(timeout));
}
}
}
6 changes: 3 additions & 3 deletions EOLib/Net/Communication/PacketSendService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class PacketSendService : IPacketSendService
private readonly IPacketQueueProvider _packetQueueProvider;

public PacketSendService(INetworkClientProvider networkClientProvider,
IPacketQueueProvider packetQueueProvider)
IPacketQueueProvider packetQueueProvider)
{
_networkClientProvider = networkClientProvider;
_packetQueueProvider = packetQueueProvider;
Expand All @@ -36,7 +36,7 @@ public async Task<IPacket> SendRawPacketAndWaitAsync(IPacket packet)
if (bytes == 0)
throw new NoDataSentException();

var responsePacket = await InBandQueue.WaitForPacketAndDequeue();
var responsePacket = await InBandQueue.WaitForPacketAndDequeue((int)Client.ReceiveTimeout.TotalMilliseconds);
if (responsePacket is EmptyPacket)
throw new EmptyPacketReceivedException();

Expand All @@ -49,7 +49,7 @@ public async Task<IPacket> SendEncodedPacketAndWaitAsync(IPacket packet)
if (bytes == 0)
throw new NoDataSentException();

var responsePacket = await InBandQueue.WaitForPacketAndDequeue();
var responsePacket = await InBandQueue.WaitForPacketAndDequeue((int)Client.ReceiveTimeout.TotalMilliseconds);
if (responsePacket is EmptyPacket)
throw new EmptyPacketReceivedException();

Expand Down

0 comments on commit ddbaa44

Please sign in to comment.