From 1abacc81482c6e6d5a3da5371aed967bdd632d80 Mon Sep 17 00:00:00 2001 From: sheychen Date: Mon, 10 Oct 2016 13:41:14 +0200 Subject: [PATCH] Documentation du Server --- .../Commands/Manager.cs | 13 ++++++ Galactic Colors Control Server/Config.cs | 11 +++++ Galactic Colors Control Server/Logger.cs | 17 ++++++++ Galactic Colors Control Server/Program.cs | 28 ++++++++++--- Galactic Colors Control Server/Utilities.cs | 42 +++++++++++++++++-- 5 files changed, 103 insertions(+), 8 deletions(-) diff --git a/Galactic Colors Control Server/Commands/Manager.cs b/Galactic Colors Control Server/Commands/Manager.cs index 701cb1b..674e173 100644 --- a/Galactic Colors Control Server/Commands/Manager.cs +++ b/Galactic Colors Control Server/Commands/Manager.cs @@ -10,8 +10,12 @@ namespace Galactic_Colors_Control_Server.Commands { public static Dictionary commands { get; private set; } = new Dictionary(); + /// + /// Find all ICommand and add them to commands + /// public static void Load() { + //C# is magic IEnumerable coms = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.GetInterfaces().Contains(typeof(ICommand)) && x.GetConstructor(Type.EmptyTypes) != null).Select(x => Activator.CreateInstance(x) as ICommand); foreach (ICommand com in coms) { @@ -20,6 +24,12 @@ namespace Galactic_Colors_Control_Server.Commands } } + /// + /// Execute sended command + /// + /// command with args + /// Sender socket + /// Is server? public static void Execute(string[] args, Socket soc = null, bool server = false) { if (commands.ContainsKey(args[0])) @@ -54,6 +64,9 @@ namespace Galactic_Colors_Control_Server.Commands } } + /// + /// Check is socket can use specified command + /// public static bool CanAccess(ICommand command, Socket soc = null, bool server = false) { if (server) diff --git a/Galactic Colors Control Server/Config.cs b/Galactic Colors Control Server/Config.cs index 45c993f..af16ae7 100644 --- a/Galactic Colors Control Server/Config.cs +++ b/Galactic Colors Control Server/Config.cs @@ -19,6 +19,11 @@ namespace Galactic_Colors_Control_Server public ConsoleColor[] logForeColor = new ConsoleColor[5] {ConsoleColor.Gray , ConsoleColor.White, ConsoleColor.Yellow, ConsoleColor.Red, ConsoleColor.White}; public ConsoleColor[] logBackColor = new ConsoleColor[5] { ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Red }; + /// + /// Load config from xml file + /// App.config is too easy + /// + /// Loaded config public Config Load() { Logger.Write("Loading config", Logger.logType.info); @@ -49,6 +54,9 @@ namespace Galactic_Colors_Control_Server return config; } + /// + /// Write actual config in xml file + /// public void Save() { XmlSerializer xs = new XmlSerializer(typeof(Config)); @@ -60,6 +68,9 @@ namespace Galactic_Colors_Control_Server if (Program._debug) { logLevel = Logger.logType.debug; } } + /// + /// Check config format using Schema + /// public bool CorrectConfig() { bool isCorrect = false; diff --git a/Galactic Colors Control Server/Logger.cs b/Galactic Colors Control Server/Logger.cs index d692eb3..fb66c4b 100644 --- a/Galactic Colors Control Server/Logger.cs +++ b/Galactic Colors Control Server/Logger.cs @@ -29,6 +29,9 @@ namespace Galactic_Colors_Control_Server } } + /// + /// Create log file and start logger thread + /// public static void Initialise() { if (!Directory.Exists(Program.config.logPath)) { @@ -37,6 +40,7 @@ namespace Galactic_Colors_Control_Server } else { + //Sort old logs string[] files = Directory.GetFiles(Program.config.logPath); foreach(string file in files) { @@ -72,23 +76,36 @@ namespace Galactic_Colors_Control_Server Updater.Start(); } + /// + /// Add log to log pile + /// + /// Log text + /// Log status public static void Write(string text, logType type) { Write(new Log(text, type)); } + /// + /// Add log to log pile + /// + /// Log struct public static void Write(Log log) { if (log.type != logType.debug || Program.config.logLevel == logType.debug) { if(Program._debug) { + //Add Source Method log.text = "[" + new StackTrace().GetFrame(2).GetMethod().Name + "]: " + log.text; } toWriteLogs.Add(log); } } + /// + /// Write log pile to logfile and console + /// public static void UpdaterLoop() { while (_run || toWriteLogs.Count > 0) diff --git a/Galactic Colors Control Server/Program.cs b/Galactic Colors Control Server/Program.cs index ffea75b..9b77f51 100644 --- a/Galactic Colors Control Server/Program.cs +++ b/Galactic Colors Control Server/Program.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using System.Net; using System.Net.Sockets; @@ -45,6 +44,9 @@ namespace Galactic_Colors_Control_Server CloseAllSockets(); } + /// + /// Initialise server and start threads. + /// private static void SetupServer() { config = config.Load(); @@ -58,6 +60,9 @@ namespace Galactic_Colors_Control_Server Logger.Write("Server setup complete", Logger.logType.info); } + /// + /// Wait console commands and execute them. + /// private static void ConsoleLoop() { while (_run) @@ -70,8 +75,7 @@ namespace Galactic_Colors_Control_Server } /// - /// Close all connected client (we do not need to shutdown the server socket as its connections - /// are already closed with the clients). + /// Close all connected client. /// private static void CloseAllSockets() { @@ -81,7 +85,6 @@ namespace Galactic_Colors_Control_Server { socket.Shutdown(SocketShutdown.Both); Logger.Write("Shutdown " + Utilities.GetName(socket),Logger.logType.debug); - //socket.Close(); } serverSocket.Close(); Logger.Write("Server stoped", Logger.logType.info); @@ -89,6 +92,9 @@ namespace Galactic_Colors_Control_Server Logger.Updater.Join(); } + /// + /// Wait a client and check if is correct + /// private static void AcceptCallback(IAsyncResult AR) { Socket socket; @@ -103,7 +109,7 @@ namespace Galactic_Colors_Control_Server } if (_open) { - if (clients.Count < Program.config.size) + if (clients.Count < config.size) { AddClient(socket); } @@ -123,6 +129,9 @@ namespace Galactic_Colors_Control_Server serverSocket.BeginAccept(AcceptCallback, null); } + /// + /// Add client and initialise receive + /// private static void AddClient(Socket socket) { clients.Add(socket, new Data()); @@ -135,6 +144,9 @@ namespace Galactic_Colors_Control_Server } } + /// + /// Wait a client commands and execute them + /// private static void ReceiveCallback(IAsyncResult AR) { Socket current = (Socket)AR.AsyncState; @@ -181,6 +193,12 @@ namespace Galactic_Colors_Control_Server if (clients.ContainsKey(current)) { current.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current); } } + /// + /// Execute send command + /// Command + /// Sender socket + /// Is sender server? + /// private static void ExecuteMessage(string text, Socket soc = null, bool server = false) { if (text.Length > 0) diff --git a/Galactic Colors Control Server/Utilities.cs b/Galactic Colors Control Server/Utilities.cs index c396074..a979004 100644 --- a/Galactic Colors Control Server/Utilities.cs +++ b/Galactic Colors Control Server/Utilities.cs @@ -1,10 +1,7 @@ using System; -using System.Collections.Generic; using System.IO; -//using System.Linq; using System.Net; using System.Net.Sockets; -using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using System.Text; @@ -14,6 +11,10 @@ namespace Galactic_Colors_Control_Server { public enum dataType { message, data }; + /// + /// Check if socket is connect + /// + /// Client socket public static bool IsConnect(Socket soc) { if(soc == null) @@ -34,6 +35,11 @@ namespace Galactic_Colors_Control_Server } } + /// + /// Return username from socket + /// + /// Cleint socket + /// Name public static string GetName(Socket soc) { if (soc != null) @@ -55,6 +61,10 @@ namespace Galactic_Colors_Control_Server } } + /// + /// Write line in console with correct colors + /// + /// Text to write public static void ConsoleWrite(string v) { Console.Write("\b"); @@ -65,14 +75,29 @@ namespace Galactic_Colors_Control_Server Console.Write(">"); } + /// + /// Reset Console Colors + /// For non black background console as Ubuntu + /// public static void ConsoleResetColor() { Console.ResetColor(); Console.BackgroundColor = ConsoleColor.Black; } + /// + /// Send data to specified socket + /// + /// Target socket + /// Data to send + /// Type of data public static void Send(Socket soc, object data, dataType dtype) { + /* + Format: + 0-3: dataType + 4-x: data + */ byte[] type = new byte[4]; type = BitConverter.GetBytes((int)dtype); byte[] bytes = null; @@ -101,6 +126,11 @@ namespace Galactic_Colors_Control_Server soc.Send(final); } + /// + /// Send data to all clients + /// + /// Data to send + /// Type of data public static void Broadcast(object data, dataType dtype) { foreach (Socket soc in Program.clients.Keys) @@ -109,6 +139,12 @@ namespace Galactic_Colors_Control_Server } } + /// + /// Send or display if server + /// + /// Text to send + /// Target socket + /// Is server? public static void Return(string message, Socket soc = null, bool server = false) { if (server)