From f43f20522f1d4b7e46db89e9f09a9a96b17c2b61 Mon Sep 17 00:00:00 2001 From: icedream Date: Thu, 15 May 2014 06:27:27 +0200 Subject: [PATCH] Some code cleanup --- src/libnpsharp/NPServer.cs | 70 +++++++++---------- src/libnpsharp/NPServerClient.cs | 3 +- src/libnpsharp/RPC/Messages/RPCMessage.cs | 7 +- src/libnpsharp/RPC/RPCStream.cs | 4 +- .../BrightstarDatabaseFileServingHandler.cs | 21 ++++-- src/npserv/Database/IFriend.cs | 9 +-- src/npserv/Database/IUser.cs | 1 - src/npserv/Program.cs | 21 ++---- 8 files changed, 65 insertions(+), 71 deletions(-) diff --git a/src/libnpsharp/NPServer.cs b/src/libnpsharp/NPServer.cs index 75b54ed..c2a5007 100644 --- a/src/libnpsharp/NPServer.cs +++ b/src/libnpsharp/NPServer.cs @@ -16,8 +16,8 @@ namespace NPSharp { private readonly List _clients; private readonly ILog _log; - private readonly Socket _socket; private readonly ushort _port; + private readonly Socket _socket; /// /// Constructs a new NP server. @@ -31,6 +31,34 @@ namespace NPSharp _port = port; } + /// + /// The handler to use for file requests to this NP server. + /// + public IFileServingHandler FileServingHandler { get; set; } + + /// + /// The handler to use for user avatar requests to this NP server. + /// + public IUserAvatarHandler UserAvatarHandler { get; set; } + + /// + /// Returns all currently connected clients + /// + public NPServerClient[] Clients + { + get { return _clients.ToArray(); } // Avoid race condition by IEnum changes + } + + /// + /// The handler to use for authentication requests to this NP server. + /// + public IAuthenticationHandler AuthenticationHandler { get; set; } + + /// + /// The handler to use for friends-related requests to this NP server. + /// + public IFriendsHandler FriendsHandler { get; set; } + /// /// Starts up the NP server. /// @@ -71,8 +99,8 @@ namespace NPSharp var npsc = new NPServerClient(this, new RPCServerStream(clientSocket)); - _log.Debug("Async accept client end"); - + _log.Debug("Async accept client end"); + _handleClient(npsc); }, _socket); allDone.WaitOne(); @@ -89,37 +117,9 @@ namespace NPSharp _socket.Shutdown(SocketShutdown.Both); } - /// - /// The handler to use for file requests to this NP server. - /// - public IFileServingHandler FileServingHandler { get; set; } - - /// - /// The handler to use for user avatar requests to this NP server. - /// - public IUserAvatarHandler UserAvatarHandler { get; set; } - - /// - /// Returns all currently connected clients - /// - public NPServerClient[] Clients - { - get { return _clients.ToArray(); } // Avoid race condition by IEnum changes - } - - /// - /// The handler to use for authentication requests to this NP server. - /// - public IAuthenticationHandler AuthenticationHandler { get; set; } - - /// - /// The handler to use for friends-related requests to this NP server. - /// - public IFriendsHandler FriendsHandler { get; set; } - internal void _handleClient(NPServerClient client) { - _log.Debug("Client now being handled"); + _log.Debug("Client now being handled"); #region RPC authentication message handlers @@ -233,7 +233,7 @@ namespace NPSharp // Send authentication result directly to client 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, SessionToken = msg.Token }); @@ -568,8 +568,8 @@ namespace NPSharp try { #endif - _log.Debug("Client connected"); - OnClientConnected(client); + _log.Debug("Client connected"); + OnClientConnected(client); #if !DEBUG try #endif diff --git a/src/libnpsharp/NPServerClient.cs b/src/libnpsharp/NPServerClient.cs index 63ce923..0934453 100644 --- a/src/libnpsharp/NPServerClient.cs +++ b/src/libnpsharp/NPServerClient.cs @@ -26,7 +26,8 @@ namespace NPSharp public IEnumerable Friends { - get { + get + { return NP.FriendsHandler == null ? new FriendDetails[0] : NP.FriendsHandler.GetFriends(this).ToArray(); diff --git a/src/libnpsharp/RPC/Messages/RPCMessage.cs b/src/libnpsharp/RPC/Messages/RPCMessage.cs index 48becbf..845a036 100644 --- a/src/libnpsharp/RPC/Messages/RPCMessage.cs +++ b/src/libnpsharp/RPC/Messages/RPCMessage.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.IO; using System.Linq; 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, typeof (T).Name); foreach ( - PropertyInfo prop in + var prop in message.GetType().GetProperties().Where(p => !(p.DeclaringType == typeof (RPCServerMessage)))) { Log.DebugFormat("\t{0} = {1}", prop.Name, prop.GetValue(message)); @@ -168,7 +167,7 @@ namespace NPSharp.RPC.Messages #if DEBUG Log.DebugFormat("{3}[ID={0},Type={1},TypeName={2}] {{", MessageId, GetTypeId(), 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)); } diff --git a/src/libnpsharp/RPC/RPCStream.cs b/src/libnpsharp/RPC/RPCStream.cs index ad3af04..e3c8ac9 100644 --- a/src/libnpsharp/RPC/RPCStream.cs +++ b/src/libnpsharp/RPC/RPCStream.cs @@ -100,7 +100,7 @@ namespace NPSharp.RPC TypeCallbacks.Add( new KeyValuePair>( ((PacketAttribute) typeof (T).GetCustomAttributes(typeof (PacketAttribute), false).Single()).Type, - msg => callback.Invoke((T)msg))); + msg => callback.Invoke((T) msg))); } /// @@ -134,7 +134,7 @@ namespace NPSharp.RPC if (message.MessageId == default(uint)) message.MessageId = MessageID; - byte[] buffer = message.Serialize(); + var buffer = message.Serialize(); _sock.Send(buffer); diff --git a/src/npserv/BrightstarDatabaseFileServingHandler.cs b/src/npserv/BrightstarDatabaseFileServingHandler.cs index 7010325..23f53dc 100644 --- a/src/npserv/BrightstarDatabaseFileServingHandler.cs +++ b/src/npserv/BrightstarDatabaseFileServingHandler.cs @@ -15,15 +15,13 @@ namespace NPSharp.CommandLine.Server _db = database; } - ~BrightstarDatabaseFileServingHandler() - { - _db.Dispose(); - } - public byte[] ReadUserFile(NPServerClient client, string file) { 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; } @@ -39,7 +37,11 @@ namespace NPSharp.CommandLine.Server public void WriteUserFile(NPServerClient client, string file, byte[] data) { 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(); userFile.FileName = file; @@ -49,6 +51,11 @@ namespace NPSharp.CommandLine.Server _db.SaveChanges(); } + ~BrightstarDatabaseFileServingHandler() + { + _db.Dispose(); + } + protected byte[] GetDefaultUserFile(string file) { switch (file) diff --git a/src/npserv/Database/IFriend.cs b/src/npserv/Database/IFriend.cs index 52faadc..c87ef9d 100644 --- a/src/npserv/Database/IFriend.cs +++ b/src/npserv/Database/IFriend.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography.X509Certificates; -using System.Text; -using BrightstarDB.EntityFramework; +using BrightstarDB.EntityFramework; namespace NPSharp.CommandLine.Server.Database { @@ -18,4 +13,4 @@ namespace NPSharp.CommandLine.Server.Database string FriendName { get; set; } } -} +} \ No newline at end of file diff --git a/src/npserv/Database/IUser.cs b/src/npserv/Database/IUser.cs index 4a4c1f8..ad2315e 100644 --- a/src/npserv/Database/IUser.cs +++ b/src/npserv/Database/IUser.cs @@ -7,7 +7,6 @@ namespace NPSharp.CommandLine.Server.Database [Entity] public interface IUser { - [Identifier("http://npserver.icedream.kthx.at/users/")] string Id { get; } string UserName { get; set; } diff --git a/src/npserv/Program.cs b/src/npserv/Program.cs index 7053bd5..5e8dc34 100644 --- a/src/npserv/Program.cs +++ b/src/npserv/Program.cs @@ -1,5 +1,4 @@ using System; -using System.Globalization; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -89,9 +88,8 @@ namespace NPSharp.CommandLine.Server _log.DebugFormat("Ban {0} became invalid", ban.Id); dbForCleanup.DeleteObject(ban); } - foreach ( - var cheatDetection in - dbForCleanup.CheatDetections.Where(s => s.ExpiryTime < DateTime.Now).ToArray()) + + foreach (var cheatDetection in dbForCleanup.CheatDetections.Where(s => s.ExpiryTime < DateTime.Now).ToArray()) { _log.DebugFormat("Cheat detection {0} became invalid", cheatDetection.Id); dbForCleanup.DeleteObject(cheatDetection); @@ -212,8 +210,7 @@ namespace NPSharp.CommandLine.Server { appender, new DebugAppender {Layout = appender.Layout, Threshold = Level.All} - } - ); + }); } else { @@ -232,8 +229,7 @@ namespace NPSharp.CommandLine.Server { Level = Level.Debug, ForeColor = ColoredConsoleAppender.Colors.Cyan | ColoredConsoleAppender.Colors.HighIntensity - } - ); + }); appender.AddMapping( new ColoredConsoleAppender.LevelColors { @@ -249,8 +245,7 @@ namespace NPSharp.CommandLine.Server Level = Level.Warn, ForeColor = ColoredConsoleAppender.Colors.Purple | ColoredConsoleAppender.Colors.HighIntensity - } - ); + }); appender.AddMapping( new ColoredConsoleAppender.LevelColors @@ -266,8 +261,7 @@ namespace NPSharp.CommandLine.Server ForeColor = ColoredConsoleAppender.Colors.White | ColoredConsoleAppender.Colors.HighIntensity, BackColor = ColoredConsoleAppender.Colors.Red - } - ); + }); appender.ActivateOptions(); BasicConfigurator.Configure( @@ -275,8 +269,7 @@ namespace NPSharp.CommandLine.Server { appender, new DebugAppender {Layout = appender.Layout, Threshold = Level.All} - } - ); + }); } _log = LogManager.GetLogger("Main");