document servermanager

This commit is contained in:
Michael 2017-05-20 08:40:26 -04:00
parent a1fa66f787
commit 038c819046

View file

@ -39,28 +39,50 @@ using System.Diagnostics;
namespace ShiftOS.Engine
{
/// <summary>
/// Digital Society connection management class.
/// </summary>
public static class ServerManager
{
/// <summary>
/// Print connection diagnostic information.
/// </summary>
public static void PrintDiagnostics()
{
Console.WriteLine($@"{{CLIENT_DIAGNOSTICS}}
{{GUID}}: {thisGuid}
Ping: {ServerManager.DigitalSocietyPing} ms
{{CLIENT_DATA}}:
{JsonConvert.SerializeObject(client, Formatting.Indented)}");
}
/// <summary>
/// Gets the unique identifier for this Digital Society connection. This can be used for peer-to-peer communication between two clients.
/// </summary>
public static Guid thisGuid { get; private set; }
private static NetObjectClient client { get; set; }
/// <summary>
/// Gets the underlying NetSockets client for this connection.
/// </summary>
public static NetObjectClient client { get; private set; }
private static bool UserDisconnect = false;
/// <summary>
/// Gets or sets the server response time for the last request made by this client.
/// </summary>
public static long DigitalSocietyPing
{
get;
private set;
}
/// <summary>
/// Disconnect from the digital society intentionally.
/// </summary>
public static void Disconnect()
{
UserDisconnect = true;
@ -72,36 +94,20 @@ namespace ShiftOS.Engine
}
/// <summary>
/// Occurs when you are disconnected from the Digital Society.
/// </summary>
public static event EmptyEventHandler Disconnected;
public static void InitiateMUDHack()
{
MessageReceived += ServerManager_MessageReceived;
SendMessage("mudhack_init", "");
}
public static event Action<string> ServerPasswordGenerated;
public static event EmptyEventHandler ServerAccessGranted;
public static event EmptyEventHandler ServerAccessDenied;
/// <summary>
/// Occurs when the unique ID for this client is sent by the server.
/// </summary>
public static event Action<string> GUIDReceived;
public static event Action<List<OnlineUser>> UsersReceived;
private static void ServerManager_MessageReceived(ServerMessage msg)
{
switch(msg.Name)
{
case "mudhack_users":
UsersReceived?.Invoke(JsonConvert.DeserializeObject<List<OnlineUser>>(msg.Contents));
break;
case "mudhack_init":
ServerPasswordGenerated?.Invoke(msg.Contents);
break;
case "mudhack_denied":
ServerAccessDenied?.Invoke();
break;
case "mudhack_granted":
ServerAccessGranted?.Invoke();
break;
case "getguid_fromserver":
if(SaveSystem.CurrentSave.Username == msg.Contents)
{
@ -124,6 +130,11 @@ namespace ShiftOS.Engine
MessageReceived -= new ServerMessageReceived(ServerManager_MessageReceived);
}
/// <summary>
/// Initiate a new Digital Society connection.
/// </summary>
/// <param name="mud_address">The IP address or hostname of the target server</param>
/// <param name="port">The target port.</param>
public static void Initiate(string mud_address, int port)
{
client = new NetObjectClient();
@ -222,6 +233,11 @@ namespace ShiftOS.Engine
private static Stopwatch PingTimer = new Stopwatch();
/// <summary>
/// Send a message to the server.
/// </summary>
/// <param name="name">The message name</param>
/// <param name="contents">The message body</param>
public static void SendMessage(string name, string contents)
{
var sMsg = new ServerMessage
@ -255,21 +271,33 @@ namespace ShiftOS.Engine
}
/// <summary>
/// Occurs when the server sends a message to this client.
/// </summary>
public static event ServerMessageReceived MessageReceived;
public static void Forward(string targetGUID, string v, string message)
/// <summary>
/// Send a message to another client.
/// </summary>
/// <param name="targetGUID">The target client GUID.</param>
/// <param name="title">The message title</param>
/// <param name="message">The message contents</param>
public static void Forward(string targetGUID, string title, string message)
{
var smsg = new ServerMessage
{
GUID = targetGUID,
Name = v,
Name = title,
Contents = message
};
ServerManager.SendMessage("mud_forward", JsonConvert.SerializeObject(smsg));
}
}
/// <summary>
/// Delegate for handling server messages
/// </summary>
/// <param name="msg">A server message containing the protocol message name, GUID of the sender, and the contents of the message.</param>
public delegate void ServerMessageReceived(ServerMessage msg);
public class MultiplayerOnlyAttribute : Attribute