Implementing getservers and getserversExt messages.

feature-npv2
Icedream 2014-05-19 19:28:17 +02:00
parent 898aeb63c0
commit 4c25de4677
5 changed files with 224 additions and 0 deletions

View File

@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using NPSharp.Master.Messages.Data;
namespace NPSharp.Master.Messages.Client
{
/// <summary>
/// Represents a request message for the master server for an extended dedicated server list.
/// </summary>
[MasterClientMessage("getserversExt")]
public class MasterGetServersExtendedMessage : MasterClientMessage
{
private static readonly ILog Log;
static MasterGetServersExtendedMessage()
{
Log = LogManager.GetLogger(typeof (MasterGetServersExtendedMessage));
}
protected override string Serialize()
{
// I wonder if an extra useless space char at the end is okay in this case
return string.Format("{0} {1} {2} {3}", Name, GameName, ProtocolVersion, string.Join(" ", Keywords.Select(k => k.ToString())));
}
protected override void Deserialize(string[] arguments)
{
GameName = arguments[0];
ProtocolVersion = uint.Parse(arguments[1]);
foreach (var kw in arguments.Skip(2))
{
switch (kw.ToLower())
{
case "empty":
Keywords.Add(MasterGetServersExtendedKeywords.Empty);
break;
case "full":
Keywords.Add(MasterGetServersExtendedKeywords.Full);
break;
case "ipv4":
Keywords.Add(MasterGetServersExtendedKeywords.InternetProtocolVersion4);
break;
case "ipv6":
Keywords.Add(MasterGetServersExtendedKeywords.InternetProtocolVersion6);
break;
default:
Log.WarnFormat("{0}: weird keyword {1}", Name, kw);
break;
}
}
}
/// <summary>
/// The game for which servers should be fetched
/// </summary>
public string GameName { get; set; }
/// <summary>
/// The protocol version of the dedicated servers to search for
/// </summary>
public uint ProtocolVersion { get; set; }
/// <summary>
/// Extra keywords to take care of when generating the server list
/// </summary>
public List<MasterGetServersExtendedKeywords> Keywords { get; set; }
}
}
namespace NPSharp.Master.Messages.Data
{
/// <summary>
/// Represents keywords for a master server standard serverlist request.
/// </summary>
public enum MasterGetServersExtendedKeywords
{
Full = 0x01,
Empty = 0x02,
InternetProtocolVersion4 = 0x04,
InternetProtocolVersion6 = 0x08
}
}

View File

@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using NPSharp.Master.Messages.Data;
namespace NPSharp.Master.Messages.Client
{
/// <summary>
/// Represents a request message for the master server for a standard dedicated server list.
/// </summary>
[MasterClientMessage("getservers")]
public class MasterGetServersMessage : MasterClientMessage
{
private static readonly ILog Log;
static MasterGetServersMessage()
{
Log = LogManager.GetLogger(typeof (MasterGetServersMessage));
}
protected override string Serialize()
{
// I wonder if an extra useless space char at the end is okay in this case
return string.Format("{0} {1} {2} {3}", Name, GameName, ProtocolVersion, string.Join(" ", Keywords.Select(k => k.ToString())));
}
protected override void Deserialize(string[] arguments)
{
GameName = arguments[0];
ProtocolVersion = uint.Parse(arguments[1]);
foreach (var kw in arguments.Skip(2))
{
switch (kw.ToLower())
{
case "empty":
Keywords.Add(MasterGetServersKeywords.Empty);
break;
case "full":
Keywords.Add(MasterGetServersKeywords.Full);
break;
default:
Log.WarnFormat("{0}: weird keyword {1}", Name, kw);
break;
}
}
}
/// <summary>
/// The game for which servers should be fetched
/// </summary>
public string GameName { get; set; }
/// <summary>
/// The protocol version of the dedicated servers to search for
/// </summary>
public uint ProtocolVersion { get; set; }
/// <summary>
/// Extra keywords to take care of when generating the server list
/// </summary>
public List<MasterGetServersKeywords> Keywords { get; set; }
}
}
namespace NPSharp.Master.Messages.Data
{
/// <summary>
/// Represents keywords for a master server standard serverlist request.
/// </summary>
public enum MasterGetServersKeywords
{
Full = 0x01,
Empty = 0x02
}
}

View File

@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
namespace NPSharp.Master.Messages
{
public abstract class MasterClientMessage
{
protected List<string> Properties { get; set; }
internal MasterClientMessage Deserialize(Socket sock)
{
// TODO
}
internal byte[] SerializeInternal()
{
var buffer = new List<byte>();
buffer.AddRange(Header);
buffer.AddRange(Encoding.ASCII.GetBytes(Serialize()));
buffer.Add(0x0a); // end of command
return buffer.ToArray();
}
protected virtual string Serialize()
{
return Name;
}
public virtual byte[] Header { get { return new byte[] {0xFF, 0xFF, 0xFF, 0xFF}; } }
internal string Name
{
get { return GetType().GetCustomAttribute<MasterClientMessageAttribute>().Name; }
}
protected abstract void Deserialize(string[] arguments);
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NPSharp.Master.Messages
{
public class MasterClientMessageAttribute : Attribute
{
public MasterClientMessageAttribute(string name)
{
Name = name;
}
internal string Name { get; private set; }
}
}

View File

@ -63,6 +63,10 @@
<Reference Include="System.Xml.Serialization" />
</ItemGroup>
<ItemGroup>
<Compile Include="Master\Messages\Client\MasterGetServersExtendedMessage.cs" />
<Compile Include="Master\Messages\Client\MasterGetServersMessage.cs" />
<Compile Include="Master\Messages\MasterClientMessage.cs" />
<Compile Include="Master\Messages\MasterClientMessageAttribute.cs" />
<Compile Include="NP\NPAuthenticationResult.cs" />
<Compile Include="Authentication\SessionAuthenticationResult.cs" />
<Compile Include="Authentication\SessionAuthenticationServer.cs" />