Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Commit

Permalink
Refactor client status messages
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxXor committed Sep 22, 2018
1 parent 9e9ed30 commit c1d4570
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 153 deletions.
113 changes: 113 additions & 0 deletions Server/Core/Commands/ClientStatusHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using Quasar.Common.Enums;
using Quasar.Common.Messages;
using Quasar.Common.Networking;
using xServer.Core.Networking;

namespace xServer.Core.Commands
{
public class ClientStatusHandler : MessageProcessorBase<object>
{
/// <summary>
/// Represents the method that will handle status updates.
/// </summary>
/// <param name="sender">The message handler which raised the event.</param>
/// <param name="client">The client which updated the status.</param>
/// <param name="statusMessage">The new status.</param>
public delegate void StatusUpdatedEventHandler(object sender, Client client, string statusMessage);

/// <summary>
/// Represents the method that will handle user status updates.
/// </summary>
/// <param name="sender">The message handler which raised the event.</param>
/// <param name="client">The client which updated the user status.</param>
/// <param name="userStatusMessage">The new user status.</param>
public delegate void UserStatusUpdatedEventHandler(object sender, Client client, UserStatus userStatusMessage);

/// <summary>
/// Raised when a client updated its status.
/// </summary>
/// <remarks>
/// Handlers registered with this event will be invoked on the
/// <see cref="System.Threading.SynchronizationContext"/> chosen when the instance was constructed.
/// </remarks>
public event StatusUpdatedEventHandler StatusUpdated;

/// <summary>
/// Raised when a client updated its user status.
/// </summary>
/// <remarks>
/// Handlers registered with this event will be invoked on the
/// <see cref="System.Threading.SynchronizationContext"/> chosen when the instance was constructed.
/// </remarks>
public event UserStatusUpdatedEventHandler UserStatusUpdated;

/// <summary>
/// Reports an updated status.
/// </summary>
/// <param name="client">The client which updated the status.</param>
/// <param name="statusMessage">The new status.</param>
private void OnStatusUpdated(Client client, string statusMessage)
{
SynchronizationContext.Post(c =>
{
var handler = StatusUpdated;
handler?.Invoke(this, (Client) c, statusMessage);
}, client);
}

/// <summary>
/// Reports an updated user status.
/// </summary>
/// <param name="client">The client which updated the user status.</param>
/// <param name="userStatusMessage">The new user status.</param>
private void OnUserStatusUpdated(Client client, UserStatus userStatusMessage)
{
SynchronizationContext.Post(c =>
{
var handler = UserStatusUpdated;
handler?.Invoke(this, (Client) c, userStatusMessage);
}, client);
}

/// <summary>
/// Initializes a new instance of the <see cref="ClientStatusHandler"/> class.
/// </summary>
public ClientStatusHandler() : base(true)
{
}

/// <inheritdoc />
public override bool CanExecute(IMessage message) => message is SetStatus || message is SetUserStatus;

/// <inheritdoc />
public override bool CanExecuteFrom(ISender sender) => true;

/// <inheritdoc />
public override void Execute(ISender sender, IMessage message)
{
switch (message)
{
case SetStatus status:
Execute((Client) sender, status);
break;
case SetUserStatus userStatus:
Execute((Client) sender, userStatus);
break;
}
}

private void Execute(Client client, SetStatus message)
{
OnStatusUpdated(client, message.Message);
}

private void Execute(Client client, SetUserStatus message)
{
OnUserStatusUpdated(client, message.Message);
}

protected override void Dispose(bool disposing)
{
}
}
}
7 changes: 0 additions & 7 deletions Server/Core/Commands/CommandHandler.cs

This file was deleted.

56 changes: 0 additions & 56 deletions Server/Core/Commands/ConnectionHandler.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Server/Core/Commands/PasswordRecoveryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void Execute(ISender client, GetPasswordsResponse message)
{
Client c = (Client) client;

string userAtPc = $"{c.Value.Username}@{c.Value.PCName}";
string userAtPc = $"{c.Value.Username}@{c.Value.PcName}";

OnAccountsRecovered(message.RecoveredAccounts, userAtPc);
}
Expand Down
2 changes: 1 addition & 1 deletion Server/Core/Helper/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class WindowHelper
{
public static string GetWindowTitle(string title, Client c)
{
return string.Format("{0} - {1}@{2} [{3}:{4}]", title, c.Value.Username, c.Value.PCName, c.EndPoint.Address.ToString(), c.EndPoint.Port.ToString());
return string.Format("{0} - {1}@{2} [{3}:{4}]", title, c.Value.Username, c.Value.PcName, c.EndPoint.Address.ToString(), c.EndPoint.Port.ToString());
}

public static string GetWindowTitle(string title, int count)
Expand Down
25 changes: 0 additions & 25 deletions Server/Core/Networking/PacketHandler.cs

This file was deleted.

57 changes: 40 additions & 17 deletions Server/Core/Networking/QuasarServer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq;
using Quasar.Common.Messages;
using xServer.Core.Commands;
using Quasar.Common.Messages;
using System.Linq;

namespace xServer.Core.Networking
{
Expand Down Expand Up @@ -33,10 +32,7 @@ private void OnClientConnected(Client client)
{
if (ProcessingDisconnect || !Listening) return;
var handler = ClientConnected;
if (handler != null)
{
handler(client);
}
handler?.Invoke(client);
}

/// <summary>
Expand All @@ -58,10 +54,7 @@ private void OnClientDisconnected(Client client)
{
if (ProcessingDisconnect || !Listening) return;
var handler = ClientDisconnected;
if (handler != null)
{
handler(client);
}
handler?.Invoke(client);
}

/// <summary>
Expand Down Expand Up @@ -109,11 +102,17 @@ private void OnClientRead(Server server, Client client, IMessage message)
{
if (type == typeof (GetAuthenticationResponse))
{
client.Authenticated = true;
client.Send(new SetAuthenticationSuccess()); // finish handshake
CommandHandler.HandleGetAuthenticationResponse(client,
(GetAuthenticationResponse)message);
OnClientConnected(client);
AuthenticateClient(client, (GetAuthenticationResponse) message);
client.Authenticated = AuthenticateClient(client, (GetAuthenticationResponse)message);
if (client.Authenticated)
{
client.Send(new SetAuthenticationSuccess()); // finish handshake
OnClientConnected(client);
}
else
{
client.Disconnect();
}
}
else
{
Expand All @@ -123,7 +122,31 @@ private void OnClientRead(Server server, Client client, IMessage message)
}

MessageHandler.Process(client, message);
PacketHandler.HandlePacket(client, message); // TODO: Remove this
}

private bool AuthenticateClient(Client client, GetAuthenticationResponse packet)
{
if (packet.Id.Length != 64)
return false;

client.Value.Version = packet.Version;
client.Value.OperatingSystem = packet.OperatingSystem;
client.Value.AccountType = packet.AccountType;
client.Value.Country = packet.Country;
client.Value.CountryCode = packet.CountryCode;
client.Value.Region = packet.Region;
client.Value.City = packet.City;
client.Value.Id = packet.Id;
client.Value.Username = packet.Username;
client.Value.PcName = packet.PcName;
client.Value.Tag = packet.Tag;
client.Value.ImageIndex = packet.ImageIndex;

// TODO: Refactor tooltip
//if (Settings.ShowToolTip)
// client.Send(new GetSystemInfo());

return true;
}
}
}
19 changes: 14 additions & 5 deletions Server/Core/Networking/UserState.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
namespace xServer.Core.Networking
using System.IO;
using System.Windows.Forms;
using xServer.Core.Helper;

namespace xServer.Core.Networking
{
public class UserState
{
private string _downloadDirectory;

public string Version { get; set; }
public string OperatingSystem { get; set; }
public string AccountType { get; set; }
Expand All @@ -12,10 +18,13 @@ public class UserState
public string City { get; set; }
public string Id { get; set; }
public string Username { get; set; }
public string PCName { get; set; }
public string UserAtPc { get { return string.Format("{0}@{1}", Username, PCName); } }
public string CountryWithCode { get { return string.Format("{0} [{1}]", Country, CountryCode); } }
public string PcName { get; set; }
public string UserAtPc { get { return $"{Username}@{PcName}"; } }
public string CountryWithCode { get { return $"{Country} [{CountryCode}]"; } }
public string Tag { get; set; }
public string DownloadDirectory { get; set; }

public string DownloadDirectory => _downloadDirectory ?? (_downloadDirectory = (!FileHelper.CheckPathForIllegalChars(UserAtPc))
? Path.Combine(Application.StartupPath, $"Clients\\{UserAtPc}_{Id.Substring(0, 7)}\\")
: Path.Combine(Application.StartupPath, $"Clients\\{Id}\\"));
}
}
Loading

0 comments on commit c1d4570

Please sign in to comment.