Skip to content

Commit

Permalink
Merge pull request #197 from ethanmoffat/reduce_async
Browse files Browse the repository at this point in the history
Reduce async calls and disable vsync and fixed timestep. Fixes performance issues transitioning to in-game state.
  • Loading branch information
ethanmoffat authored Jun 9, 2022
2 parents b54ad98 + a0ea151 commit ffd50a1
Show file tree
Hide file tree
Showing 23 changed files with 255 additions and 336 deletions.
5 changes: 2 additions & 3 deletions EOLib/Net/Communication/INetworkClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace EOLib.Net.Communication
Expand All @@ -15,9 +16,7 @@ public interface INetworkClient : IDisposable

void Disconnect();

Task RunReceiveLoopAsync();

void CancelBackgroundReceiveLoop();
Task RunReceiveLoopAsync(CancellationToken cancellationToken);

int Send(IPacket packet);

Expand Down
22 changes: 6 additions & 16 deletions EOLib/Net/Communication/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public class NetworkClient : INetworkClient
private readonly ILoggerProvider _loggerProvider;

private readonly IAsyncSocket _socket;

private readonly CancellationTokenSource _backgroundReceiveCTS;

public bool Connected => _socket.Connected;

Expand All @@ -42,7 +40,6 @@ public NetworkClient(IPacketProcessActions packetProcessActions,
ReceiveTimeout = receiveTimeout;

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

public async Task<ConnectResult> ConnectToServer(string host, int port)
Expand Down Expand Up @@ -82,12 +79,12 @@ public void Disconnect()
Started = false;
}

public async Task RunReceiveLoopAsync()
public async Task RunReceiveLoopAsync(CancellationToken cancellationToken)
{
while (!_backgroundReceiveCTS.IsCancellationRequested)
while (!cancellationToken.IsCancellationRequested)
{
var lengthData = await _socket.ReceiveAsync(2, _backgroundReceiveCTS.Token);
if (_backgroundReceiveCTS.IsCancellationRequested)
var lengthData = await _socket.ReceiveAsync(2, cancellationToken);
if (cancellationToken.IsCancellationRequested)
{
_loggerProvider.Logger.Log("RECV thread: Cancellation was requested when receiving length");
break;
Expand All @@ -101,8 +98,8 @@ public async Task RunReceiveLoopAsync()

var length = _numberEncoderService.DecodeNumber(lengthData);

var packetData = await _socket.ReceiveAsync(length, _backgroundReceiveCTS.Token);
if (_backgroundReceiveCTS.IsCancellationRequested)
var packetData = await _socket.ReceiveAsync(length, cancellationToken);
if (cancellationToken.IsCancellationRequested)
{
_loggerProvider.Logger.Log("RECV thread: Cancellation was requested when receiving data");
break;
Expand All @@ -121,11 +118,6 @@ public async Task RunReceiveLoopAsync()
}
}

public void CancelBackgroundReceiveLoop()
{
_backgroundReceiveCTS.Cancel();
}

public int Send(IPacket packet)
{
var sendTask = SendAsync(packet);
Expand Down Expand Up @@ -186,11 +178,9 @@ protected virtual void Dispose(bool disposing)
{
if (Connected)
{
CancelBackgroundReceiveLoop();
Disconnect();
}

_backgroundReceiveCTS.Dispose();
_socket.Dispose();
}
}
Expand Down
35 changes: 14 additions & 21 deletions EOLib/Net/Connection/BackgroundReceiveActions.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
using System;
using System.Threading;
using AutomaticTypeMapper;
using EOLib.Net.Communication;
using System.Threading;
using System.Threading.Tasks;

namespace EOLib.Net.Connection
{
[AutoMappedType]
public class BackgroundReceiveActions : IBackgroundReceiveActions
{
private readonly INetworkClientProvider _clientProvider;
private readonly IBackgroundReceiveThreadRepository _backgroundReceiveThreadRepository;
private readonly IBackgroundReceiveTaskRepository _backgroundReceiveTaskRepository;

public BackgroundReceiveActions(INetworkClientProvider clientProvider,
IBackgroundReceiveThreadRepository backgroundReceiveThreadRepository)
IBackgroundReceiveTaskRepository backgroundReceiveThreadRepository)
{
_clientProvider = clientProvider;
_backgroundReceiveThreadRepository = backgroundReceiveThreadRepository;
_backgroundReceiveThreadRepository.BackgroundThreadObject = new Thread(BackgroundLoop);
_backgroundReceiveTaskRepository = backgroundReceiveThreadRepository;
}

public void RunBackgroundReceiveLoop()
{
if (_backgroundReceiveThreadRepository.BackgroundThreadRunning)
if (_backgroundReceiveTaskRepository.Task != null)
return;

_backgroundReceiveThreadRepository.BackgroundThreadObject.Start();
_backgroundReceiveThreadRepository.BackgroundThreadRunning = true;
_backgroundReceiveTaskRepository.BackgroundCancellationTokenSource = new CancellationTokenSource();
_backgroundReceiveTaskRepository.Task = Task.Run(RunLoop, _backgroundReceiveTaskRepository.BackgroundCancellationTokenSource.Token);
}

public void CancelBackgroundReceiveLoop()
{
if (!_backgroundReceiveThreadRepository.BackgroundThreadRunning)
return;
_backgroundReceiveTaskRepository.BackgroundCancellationTokenSource?.Cancel();
_backgroundReceiveTaskRepository.BackgroundCancellationTokenSource?.Dispose();
_backgroundReceiveTaskRepository.BackgroundCancellationTokenSource = null;

Client.CancelBackgroundReceiveLoop();

if (_backgroundReceiveThreadRepository.BackgroundThreadObject.ThreadState == ThreadState.Running)
_backgroundReceiveThreadRepository.BackgroundThreadObject.Join();
_backgroundReceiveThreadRepository.BackgroundThreadObject = new Thread(BackgroundLoop);
_backgroundReceiveThreadRepository.BackgroundThreadRunning = false;
_backgroundReceiveTaskRepository.Task = null;
}

private async void BackgroundLoop()
private Task RunLoop()
{
await Client.RunReceiveLoopAsync().ConfigureAwait(false);
return _clientProvider.NetworkClient.RunReceiveLoopAsync(_backgroundReceiveTaskRepository.BackgroundCancellationTokenSource.Token);
}

private INetworkClient Client => _clientProvider.NetworkClient;
}
}
21 changes: 21 additions & 0 deletions EOLib/Net/Connection/IBackgroundReceiveTaskRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using AutomaticTypeMapper;
using System.Threading;
using System.Threading.Tasks;

namespace EOLib.Net.Connection
{
public interface IBackgroundReceiveTaskRepository
{
Task Task { get; set; }

CancellationTokenSource BackgroundCancellationTokenSource { get; set; }
}

[AutoMappedType(IsSingleton = true)]
public class BackgroundReceiveThreadRepository : IBackgroundReceiveTaskRepository
{
public Task Task { get; set; }

public CancellationTokenSource BackgroundCancellationTokenSource { get; set; }
}
}
20 changes: 0 additions & 20 deletions EOLib/Net/Connection/IBackgroundReceiveThreadRepository.cs

This file was deleted.

20 changes: 0 additions & 20 deletions EOLib/Net/Connection/IConnectionStateRepository.cs

This file was deleted.

2 changes: 0 additions & 2 deletions EOLib/Net/Connection/INetworkConnectionActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public interface INetworkConnectionActions
{
Task<ConnectResult> ConnectToServer();

Task<ConnectResult> ReconnectToServer();

void DisconnectFromServer();

Task<IInitializationData> BeginHandshake();
Expand Down
31 changes: 9 additions & 22 deletions EOLib/Net/Connection/NetworkConnectionActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace EOLib.Net.Connection
public class NetworkConnectionActions : INetworkConnectionActions
{
private readonly INetworkClientRepository _networkClientRepository;
private readonly IConnectionStateRepository _connectionStateRepository;
private readonly ISequenceRepository _sequenceRepository;
private readonly IConfigurationProvider _configurationProvider;
private readonly IHashService _hashService;
Expand All @@ -26,7 +25,6 @@ public class NetworkConnectionActions : INetworkConnectionActions


public NetworkConnectionActions(INetworkClientRepository networkClientRepository,
IConnectionStateRepository connectionStateRepository,
ISequenceRepository sequenceRepository,
IConfigurationProvider configurationProvider,
IHashService hashService,
Expand All @@ -37,7 +35,6 @@ public NetworkConnectionActions(INetworkClientRepository networkClientRepository
IPlayerInfoRepository playerInfoRepository)
{
_networkClientRepository = networkClientRepository;
_connectionStateRepository = connectionStateRepository;
_sequenceRepository = sequenceRepository;
_configurationProvider = configurationProvider;
_hashService = hashService;
Expand All @@ -50,39 +47,29 @@ public NetworkConnectionActions(INetworkClientRepository networkClientRepository

public async Task<ConnectResult> ConnectToServer()
{
if (Client.Connected)
if (Client != null && Client.Connected)
return ConnectResult.AlreadyConnected;

_networkClientRepository.NetworkClient?.Dispose();
_networkClientRepository.NetworkClient = _networkClientFactory.CreateNetworkClient();

var host = _configurationProvider.Host;
var port = _configurationProvider.Port;

var result = await Client.ConnectToServer(host, port);
if (result != ConnectResult.AlreadyConnected && result != ConnectResult.Success)
_connectionStateRepository.NeedsReconnect = true;

return result;
return await Client.ConnectToServer(host, port);
}

public async Task<ConnectResult> ReconnectToServer()
public void DisconnectFromServer()
{
if (Client.Connected)
if (Client != null)
{
Client.CancelBackgroundReceiveLoop();
Client.Disconnect();
Client.Dispose();
_networkClientRepository.NetworkClient = null;
}
Client.Dispose();

_networkClientRepository.NetworkClient = _networkClientFactory.CreateNetworkClient();
return await ConnectToServer();
}

public void DisconnectFromServer()
{
Client.Disconnect();

_sequenceRepository.SequenceIncrement = 0;
_sequenceRepository.SequenceStart = 0;
_connectionStateRepository.NeedsReconnect = true;
}

public async Task<IInitializationData> BeginHandshake()
Expand Down
27 changes: 17 additions & 10 deletions EndlessClient/ControlSets/CreateAccountControlSet.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Linq;
using EndlessClient.Content;
using EndlessClient.Content;
using EndlessClient.Controllers;
using EndlessClient.GameExecution;
using EndlessClient.Input;
Expand All @@ -9,6 +7,9 @@
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Linq;
using System.Threading.Tasks;
using XNAControls;

namespace EndlessClient.ControlSets
Expand All @@ -31,6 +32,8 @@ public class CreateAccountControlSet : IntermediateControlSet
private TextBoxClickEventHandler _clickHandler;
private TextBoxTabEventHandler _tabHandler;

private Task _createAccountTask;

public override GameStates GameState => GameStates.CreateAccount;

public CreateAccountControlSet(KeyboardDispatcher dispatcher,
Expand Down Expand Up @@ -201,13 +204,17 @@ protected override IXNAButton GetCreateButton()

private void DoCreateAccount(object sender, EventArgs e)
{
_accountController.CreateAccount(new CreateAccountParameters(
_tbAccountName.Text,
_tbPassword.Text,
_tbConfirm.Text,
_tbRealName.Text,
_tbLocation.Text,
_tbEmail.Text));
if (_createAccountTask == null)
{
_createAccountTask = _accountController.CreateAccount(
new CreateAccountParameters(_tbAccountName.Text,
_tbPassword.Text,
_tbConfirm.Text,
_tbRealName.Text,
_tbLocation.Text,
_tbEmail.Text));
_createAccountTask.ContinueWith(_ => _createAccountTask = null);
}
}

protected override void Dispose(bool disposing)
Expand Down
Loading

0 comments on commit ffd50a1

Please sign in to comment.