Skip to content

Commit

Permalink
Fixed sending of login packet to server if init response arrived befo…
Browse files Browse the repository at this point in the history
…re finishing initialization of Game instance
  • Loading branch information
Arcidev committed Aug 19, 2024
1 parent 1e0ab79 commit aff3ae3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
27 changes: 16 additions & 11 deletions Client.Logic/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,35 @@ private Game(ClientNetwork clientNetwork, IDataHolder dataHolder)
Chat = new Chat(this);
CombatLog = new CombatLog();

var packet = new Packet(CMSGPackets.Init);
using (var rsa = new RsaEncryptor(Security.RSAKey.Modulus, Security.RSAKey.Exponent))
{
aes = new AesEncryptor(AesEncryptionType.Aes256Bits) { PaddingMode = PaddingMode.PKCS7 };
network.Encryptor = aes;
packet.Write(rsa.Encrypt(aes.Encryptors));
}

SendPacket(packet, false);
aes = new AesEncryptor(AesEncryptionType.Aes256Bits) { PaddingMode = PaddingMode.PKCS7 };
network.Encryptor = aes;
networkConnectionTask = Task.Run(UpdateAsync, tokenSource.Token);
}

/// <summary>
/// Creates new instance of game
/// </summary>
/// <param name="server">Server url</param>
/// <param name="dataHolder">Data Holder</param>
/// <param name="packetCallback">Callback for processed packets</param>
/// <returns>Instance of game</returns>
public static async Task<Game> CreateAsync(string server, IDataHolder dataHolder)
public static async Task<Game> CreateAsync(string server, IDataHolder dataHolder, Action<UInt16> packetCallback)
{
var network = await ClientNetwork.CreateAsync(server, port);
if (network == null)
return null;

return new Game(network, dataHolder);
var game = new Game(network, dataHolder);
game.PacketProcessed += packetCallback;

var packet = new Packet(CMSGPackets.Init);
using (var rsa = new RsaEncryptor(Security.RSAKey.Modulus, Security.RSAKey.Exponent))
{
packet.Write(rsa.Encrypt(game.aes.Encryptors));
}

await game.SendPacketAsync(packet, false);
return game;
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions Client.UI/ViewModels/User/LoginViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ protected async Task<Game> UserOperation(Packet packet, Action<UInt16> callback)
var game = App.GetGame();
if (game == null)
{
game = await Game.CreateAsync(Server, new DataHolder());
game = await Game.CreateAsync(Server, new DataHolder(), callback);
if (game == null)
{
ErrorMessage = Texts.UnableToConnect;
return null;
}

game.MessageReceived += SetErrorMessage;
game.PacketProcessed += callback;
App.SetGame(game);
}

Expand Down

0 comments on commit aff3ae3

Please sign in to comment.