Some code cleanup

feature-npv2
Icedream 2014-05-15 06:27:27 +02:00
parent 301281737c
commit f43f20522f
8 changed files with 65 additions and 71 deletions

View File

@ -16,8 +16,8 @@ namespace NPSharp
{ {
private readonly List<NPServerClient> _clients; private readonly List<NPServerClient> _clients;
private readonly ILog _log; private readonly ILog _log;
private readonly Socket _socket;
private readonly ushort _port; private readonly ushort _port;
private readonly Socket _socket;
/// <summary> /// <summary>
/// Constructs a new NP server. /// Constructs a new NP server.
@ -31,6 +31,34 @@ namespace NPSharp
_port = port; _port = port;
} }
/// <summary>
/// The handler to use for file requests to this NP server.
/// </summary>
public IFileServingHandler FileServingHandler { get; set; }
/// <summary>
/// The handler to use for user avatar requests to this NP server.
/// </summary>
public IUserAvatarHandler UserAvatarHandler { get; set; }
/// <summary>
/// Returns all currently connected clients
/// </summary>
public NPServerClient[] Clients
{
get { return _clients.ToArray(); } // Avoid race condition by IEnum changes
}
/// <summary>
/// The handler to use for authentication requests to this NP server.
/// </summary>
public IAuthenticationHandler AuthenticationHandler { get; set; }
/// <summary>
/// The handler to use for friends-related requests to this NP server.
/// </summary>
public IFriendsHandler FriendsHandler { get; set; }
/// <summary> /// <summary>
/// Starts up the NP server. /// Starts up the NP server.
/// </summary> /// </summary>
@ -89,34 +117,6 @@ namespace NPSharp
_socket.Shutdown(SocketShutdown.Both); _socket.Shutdown(SocketShutdown.Both);
} }
/// <summary>
/// The handler to use for file requests to this NP server.
/// </summary>
public IFileServingHandler FileServingHandler { get; set; }
/// <summary>
/// The handler to use for user avatar requests to this NP server.
/// </summary>
public IUserAvatarHandler UserAvatarHandler { get; set; }
/// <summary>
/// Returns all currently connected clients
/// </summary>
public NPServerClient[] Clients
{
get { return _clients.ToArray(); } // Avoid race condition by IEnum changes
}
/// <summary>
/// The handler to use for authentication requests to this NP server.
/// </summary>
public IAuthenticationHandler AuthenticationHandler { get; set; }
/// <summary>
/// The handler to use for friends-related requests to this NP server.
/// </summary>
public IFriendsHandler FriendsHandler { get; set; }
internal void _handleClient(NPServerClient client) internal void _handleClient(NPServerClient client)
{ {
_log.Debug("Client now being handled"); _log.Debug("Client now being handled");
@ -233,7 +233,7 @@ namespace NPSharp
// Send authentication result directly to client // Send authentication result directly to client
client.RPC.Send(new AuthenticateResultMessage client.RPC.Send(new AuthenticateResultMessage
{ {
NPID = result.UserID == null ? 0 : result.UserID.AccountID, NPID = result.UserID == null ? 0 : result.UserID.ConvertToUint64(),
Result = result.Result ? 0 : 1, Result = result.Result ? 0 : 1,
SessionToken = msg.Token SessionToken = msg.Token
}); });
@ -568,8 +568,8 @@ namespace NPSharp
try try
{ {
#endif #endif
_log.Debug("Client connected"); _log.Debug("Client connected");
OnClientConnected(client); OnClientConnected(client);
#if !DEBUG #if !DEBUG
try try
#endif #endif

View File

@ -26,7 +26,8 @@ namespace NPSharp
public IEnumerable<FriendDetails> Friends public IEnumerable<FriendDetails> Friends
{ {
get { get
{
return NP.FriendsHandler == null return NP.FriendsHandler == null
? new FriendDetails[0] ? new FriendDetails[0]
: NP.FriendsHandler.GetFriends(this).ToArray(); : NP.FriendsHandler.GetFriends(this).ToArray();

View File

@ -1,5 +1,4 @@
using System; using System.Diagnostics;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@ -127,7 +126,7 @@ namespace NPSharp.RPC.Messages
Log.DebugFormat("{3}[ID={0},Type={1},TypeName={2}] {{", mid, message.GetTypeId(), message.GetType().Name, Log.DebugFormat("{3}[ID={0},Type={1},TypeName={2}] {{", mid, message.GetTypeId(), message.GetType().Name,
typeof (T).Name); typeof (T).Name);
foreach ( foreach (
PropertyInfo prop in var prop in
message.GetType().GetProperties().Where(p => !(p.DeclaringType == typeof (RPCServerMessage)))) message.GetType().GetProperties().Where(p => !(p.DeclaringType == typeof (RPCServerMessage))))
{ {
Log.DebugFormat("\t{0} = {1}", prop.Name, prop.GetValue(message)); Log.DebugFormat("\t{0} = {1}", prop.Name, prop.GetValue(message));
@ -168,7 +167,7 @@ namespace NPSharp.RPC.Messages
#if DEBUG #if DEBUG
Log.DebugFormat("{3}[ID={0},Type={1},TypeName={2}] {{", MessageId, GetTypeId(), GetType().Name, Log.DebugFormat("{3}[ID={0},Type={1},TypeName={2}] {{", MessageId, GetTypeId(), GetType().Name,
GetType().Name); GetType().Name);
foreach (PropertyInfo prop in GetType().GetProperties()) foreach (var prop in GetType().GetProperties())
{ {
Log.DebugFormat("\t{0} = {1}", prop.Name, prop.GetValue(this)); Log.DebugFormat("\t{0} = {1}", prop.Name, prop.GetValue(this));
} }

View File

@ -100,7 +100,7 @@ namespace NPSharp.RPC
TypeCallbacks.Add( TypeCallbacks.Add(
new KeyValuePair<uint, Action<TRecv>>( new KeyValuePair<uint, Action<TRecv>>(
((PacketAttribute) typeof (T).GetCustomAttributes(typeof (PacketAttribute), false).Single()).Type, ((PacketAttribute) typeof (T).GetCustomAttributes(typeof (PacketAttribute), false).Single()).Type,
msg => callback.Invoke((T)msg))); msg => callback.Invoke((T) msg)));
} }
/// <summary> /// <summary>
@ -134,7 +134,7 @@ namespace NPSharp.RPC
if (message.MessageId == default(uint)) if (message.MessageId == default(uint))
message.MessageId = MessageID; message.MessageId = MessageID;
byte[] buffer = message.Serialize(); var buffer = message.Serialize();
_sock.Send(buffer); _sock.Send(buffer);

View File

@ -15,15 +15,13 @@ namespace NPSharp.CommandLine.Server
_db = database; _db = database;
} }
~BrightstarDatabaseFileServingHandler()
{
_db.Dispose();
}
public byte[] ReadUserFile(NPServerClient client, string file) public byte[] ReadUserFile(NPServerClient client, string file)
{ {
var resultEnum = var resultEnum =
_db.UserFiles.Where(uf => uf.User.Id == client.UserID.AccountID.ToString(CultureInfo.InvariantCulture) && uf.FileName == file); _db.UserFiles.Where(
uf =>
uf.User.Id == client.UserID.AccountID.ToString(CultureInfo.InvariantCulture) &&
uf.FileName == file);
return resultEnum.Any() ? resultEnum.Single().FileData : null; return resultEnum.Any() ? resultEnum.Single().FileData : null;
} }
@ -39,7 +37,11 @@ namespace NPSharp.CommandLine.Server
public void WriteUserFile(NPServerClient client, string file, byte[] data) public void WriteUserFile(NPServerClient client, string file, byte[] data)
{ {
var resultEnum = var resultEnum =
_db.UserFiles.Where(uf => uf.User.Id == client.UserID.AccountID.ToString(CultureInfo.InvariantCulture) && uf.FileName == file); _db.UserFiles.Where(
uf =>
uf.User.Id == client.UserID.AccountID.ToString(CultureInfo.InvariantCulture) &&
uf.FileName == file)
.ToArray();
var userFile = resultEnum.Any() ? resultEnum.Single() : _db.UserFiles.Create(); var userFile = resultEnum.Any() ? resultEnum.Single() : _db.UserFiles.Create();
userFile.FileName = file; userFile.FileName = file;
@ -49,6 +51,11 @@ namespace NPSharp.CommandLine.Server
_db.SaveChanges(); _db.SaveChanges();
} }
~BrightstarDatabaseFileServingHandler()
{
_db.Dispose();
}
protected byte[] GetDefaultUserFile(string file) protected byte[] GetDefaultUserFile(string file)
{ {
switch (file) switch (file)

View File

@ -1,9 +1,4 @@
using System; using BrightstarDB.EntityFramework;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using BrightstarDB.EntityFramework;
namespace NPSharp.CommandLine.Server.Database namespace NPSharp.CommandLine.Server.Database
{ {

View File

@ -7,7 +7,6 @@ namespace NPSharp.CommandLine.Server.Database
[Entity] [Entity]
public interface IUser public interface IUser
{ {
[Identifier("http://npserver.icedream.kthx.at/users/")]
string Id { get; } string Id { get; }
string UserName { get; set; } string UserName { get; set; }

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -89,9 +88,8 @@ namespace NPSharp.CommandLine.Server
_log.DebugFormat("Ban {0} became invalid", ban.Id); _log.DebugFormat("Ban {0} became invalid", ban.Id);
dbForCleanup.DeleteObject(ban); dbForCleanup.DeleteObject(ban);
} }
foreach (
var cheatDetection in foreach (var cheatDetection in dbForCleanup.CheatDetections.Where(s => s.ExpiryTime < DateTime.Now).ToArray())
dbForCleanup.CheatDetections.Where(s => s.ExpiryTime < DateTime.Now).ToArray())
{ {
_log.DebugFormat("Cheat detection {0} became invalid", cheatDetection.Id); _log.DebugFormat("Cheat detection {0} became invalid", cheatDetection.Id);
dbForCleanup.DeleteObject(cheatDetection); dbForCleanup.DeleteObject(cheatDetection);
@ -212,8 +210,7 @@ namespace NPSharp.CommandLine.Server
{ {
appender, appender,
new DebugAppender {Layout = appender.Layout, Threshold = Level.All} new DebugAppender {Layout = appender.Layout, Threshold = Level.All}
} });
);
} }
else else
{ {
@ -232,8 +229,7 @@ namespace NPSharp.CommandLine.Server
{ {
Level = Level.Debug, Level = Level.Debug,
ForeColor = ColoredConsoleAppender.Colors.Cyan | ColoredConsoleAppender.Colors.HighIntensity ForeColor = ColoredConsoleAppender.Colors.Cyan | ColoredConsoleAppender.Colors.HighIntensity
} });
);
appender.AddMapping( appender.AddMapping(
new ColoredConsoleAppender.LevelColors new ColoredConsoleAppender.LevelColors
{ {
@ -249,8 +245,7 @@ namespace NPSharp.CommandLine.Server
Level = Level.Warn, Level = Level.Warn,
ForeColor = ForeColor =
ColoredConsoleAppender.Colors.Purple | ColoredConsoleAppender.Colors.HighIntensity ColoredConsoleAppender.Colors.Purple | ColoredConsoleAppender.Colors.HighIntensity
} });
);
appender.AddMapping( appender.AddMapping(
new ColoredConsoleAppender.LevelColors new ColoredConsoleAppender.LevelColors
@ -266,8 +261,7 @@ namespace NPSharp.CommandLine.Server
ForeColor = ForeColor =
ColoredConsoleAppender.Colors.White | ColoredConsoleAppender.Colors.HighIntensity, ColoredConsoleAppender.Colors.White | ColoredConsoleAppender.Colors.HighIntensity,
BackColor = ColoredConsoleAppender.Colors.Red BackColor = ColoredConsoleAppender.Colors.Red
} });
);
appender.ActivateOptions(); appender.ActivateOptions();
BasicConfigurator.Configure( BasicConfigurator.Configure(
@ -275,8 +269,7 @@ namespace NPSharp.CommandLine.Server
{ {
appender, appender,
new DebugAppender {Layout = appender.Layout, Threshold = Level.All} new DebugAppender {Layout = appender.Layout, Threshold = Level.All}
} });
);
} }
_log = LogManager.GetLogger("Main"); _log = LogManager.GetLogger("Main");