1
0
Fork 0

Documentation du Server

This commit is contained in:
sheychen 2016-10-10 13:41:14 +02:00
parent 14f010c3e8
commit 1abacc8148
5 changed files with 103 additions and 8 deletions

View File

@ -10,8 +10,12 @@ namespace Galactic_Colors_Control_Server.Commands
{ {
public static Dictionary<string, ICommand> commands { get; private set; } = new Dictionary<string, ICommand>(); public static Dictionary<string, ICommand> commands { get; private set; } = new Dictionary<string, ICommand>();
/// <summary>
/// Find all ICommand and add them to commands
/// </summary>
public static void Load() public static void Load()
{ {
//C# is magic
IEnumerable<ICommand> coms = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.GetInterfaces().Contains(typeof(ICommand)) && x.GetConstructor(Type.EmptyTypes) != null).Select(x => Activator.CreateInstance(x) as ICommand); IEnumerable<ICommand> 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) foreach (ICommand com in coms)
{ {
@ -20,6 +24,12 @@ namespace Galactic_Colors_Control_Server.Commands
} }
} }
/// <summary>
/// Execute sended command
/// </summary>
/// <param name="args">command with args</param>
/// <param name="soc">Sender socket</param>
/// <param name="server">Is server?</param>
public static void Execute(string[] args, Socket soc = null, bool server = false) public static void Execute(string[] args, Socket soc = null, bool server = false)
{ {
if (commands.ContainsKey(args[0])) if (commands.ContainsKey(args[0]))
@ -54,6 +64,9 @@ namespace Galactic_Colors_Control_Server.Commands
} }
} }
/// <summary>
/// Check is socket can use specified command
/// </summary>
public static bool CanAccess(ICommand command, Socket soc = null, bool server = false) public static bool CanAccess(ICommand command, Socket soc = null, bool server = false)
{ {
if (server) if (server)

View File

@ -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[] 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 }; public ConsoleColor[] logBackColor = new ConsoleColor[5] { ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Red };
/// <summary>
/// Load config from xml file
/// App.config is too easy
/// </summary>
/// <returns>Loaded config</returns>
public Config Load() public Config Load()
{ {
Logger.Write("Loading config", Logger.logType.info); Logger.Write("Loading config", Logger.logType.info);
@ -49,6 +54,9 @@ namespace Galactic_Colors_Control_Server
return config; return config;
} }
/// <summary>
/// Write actual config in xml file
/// </summary>
public void Save() public void Save()
{ {
XmlSerializer xs = new XmlSerializer(typeof(Config)); XmlSerializer xs = new XmlSerializer(typeof(Config));
@ -60,6 +68,9 @@ namespace Galactic_Colors_Control_Server
if (Program._debug) { logLevel = Logger.logType.debug; } if (Program._debug) { logLevel = Logger.logType.debug; }
} }
/// <summary>
/// Check config format using Schema
/// </summary>
public bool CorrectConfig() public bool CorrectConfig()
{ {
bool isCorrect = false; bool isCorrect = false;

View File

@ -29,6 +29,9 @@ namespace Galactic_Colors_Control_Server
} }
} }
/// <summary>
/// Create log file and start logger thread
/// </summary>
public static void Initialise() public static void Initialise()
{ {
if (!Directory.Exists(Program.config.logPath)) { if (!Directory.Exists(Program.config.logPath)) {
@ -37,6 +40,7 @@ namespace Galactic_Colors_Control_Server
} }
else else
{ {
//Sort old logs
string[] files = Directory.GetFiles(Program.config.logPath); string[] files = Directory.GetFiles(Program.config.logPath);
foreach(string file in files) foreach(string file in files)
{ {
@ -72,23 +76,36 @@ namespace Galactic_Colors_Control_Server
Updater.Start(); Updater.Start();
} }
/// <summary>
/// Add log to log pile
/// </summary>
/// <param name="text">Log text</param>
/// <param name="type">Log status</param>
public static void Write(string text, logType type) public static void Write(string text, logType type)
{ {
Write(new Log(text, type)); Write(new Log(text, type));
} }
/// <summary>
/// Add log to log pile
/// </summary>
/// <param name="log">Log struct</param>
public static void Write(Log log) public static void Write(Log log)
{ {
if (log.type != logType.debug || Program.config.logLevel == logType.debug) if (log.type != logType.debug || Program.config.logLevel == logType.debug)
{ {
if(Program._debug) if(Program._debug)
{ {
//Add Source Method
log.text = "[" + new StackTrace().GetFrame(2).GetMethod().Name + "]: " + log.text; log.text = "[" + new StackTrace().GetFrame(2).GetMethod().Name + "]: " + log.text;
} }
toWriteLogs.Add(log); toWriteLogs.Add(log);
} }
} }
/// <summary>
/// Write log pile to logfile and console
/// </summary>
public static void UpdaterLoop() public static void UpdaterLoop()
{ {
while (_run || toWriteLogs.Count > 0) while (_run || toWriteLogs.Count > 0)

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@ -45,6 +44,9 @@ namespace Galactic_Colors_Control_Server
CloseAllSockets(); CloseAllSockets();
} }
/// <summary>
/// Initialise server and start threads.
/// </summary>
private static void SetupServer() private static void SetupServer()
{ {
config = config.Load(); config = config.Load();
@ -58,6 +60,9 @@ namespace Galactic_Colors_Control_Server
Logger.Write("Server setup complete", Logger.logType.info); Logger.Write("Server setup complete", Logger.logType.info);
} }
/// <summary>
/// Wait console commands and execute them.
/// </summary>
private static void ConsoleLoop() private static void ConsoleLoop()
{ {
while (_run) while (_run)
@ -70,8 +75,7 @@ namespace Galactic_Colors_Control_Server
} }
/// <summary> /// <summary>
/// Close all connected client (we do not need to shutdown the server socket as its connections /// Close all connected client.
/// are already closed with the clients).
/// </summary> /// </summary>
private static void CloseAllSockets() private static void CloseAllSockets()
{ {
@ -81,7 +85,6 @@ namespace Galactic_Colors_Control_Server
{ {
socket.Shutdown(SocketShutdown.Both); socket.Shutdown(SocketShutdown.Both);
Logger.Write("Shutdown " + Utilities.GetName(socket),Logger.logType.debug); Logger.Write("Shutdown " + Utilities.GetName(socket),Logger.logType.debug);
//socket.Close();
} }
serverSocket.Close(); serverSocket.Close();
Logger.Write("Server stoped", Logger.logType.info); Logger.Write("Server stoped", Logger.logType.info);
@ -89,6 +92,9 @@ namespace Galactic_Colors_Control_Server
Logger.Updater.Join(); Logger.Updater.Join();
} }
/// <summary>
/// Wait a client and check if is correct
/// </summary>
private static void AcceptCallback(IAsyncResult AR) private static void AcceptCallback(IAsyncResult AR)
{ {
Socket socket; Socket socket;
@ -103,7 +109,7 @@ namespace Galactic_Colors_Control_Server
} }
if (_open) if (_open)
{ {
if (clients.Count < Program.config.size) if (clients.Count < config.size)
{ {
AddClient(socket); AddClient(socket);
} }
@ -123,6 +129,9 @@ namespace Galactic_Colors_Control_Server
serverSocket.BeginAccept(AcceptCallback, null); serverSocket.BeginAccept(AcceptCallback, null);
} }
/// <summary>
/// Add client and initialise receive
/// </summary>
private static void AddClient(Socket socket) private static void AddClient(Socket socket)
{ {
clients.Add(socket, new Data()); clients.Add(socket, new Data());
@ -135,6 +144,9 @@ namespace Galactic_Colors_Control_Server
} }
} }
/// <summary>
/// Wait a client commands and execute them
/// </summary>
private static void ReceiveCallback(IAsyncResult AR) private static void ReceiveCallback(IAsyncResult AR)
{ {
Socket current = (Socket)AR.AsyncState; 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); } if (clients.ContainsKey(current)) { current.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current); }
} }
/// <summary>
/// Execute send command
/// <param name="text">Command</param>
/// <param name="soc">Sender socket</param>
/// <param name="server">Is sender server?</param>
/// </summary>
private static void ExecuteMessage(string text, Socket soc = null, bool server = false) private static void ExecuteMessage(string text, Socket soc = null, bool server = false)
{ {
if (text.Length > 0) if (text.Length > 0)

View File

@ -1,10 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
//using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using System.Text; using System.Text;
@ -14,6 +11,10 @@ namespace Galactic_Colors_Control_Server
{ {
public enum dataType { message, data }; public enum dataType { message, data };
/// <summary>
/// Check if socket is connect
/// </summary>
/// <param name="soc">Client socket</param>
public static bool IsConnect(Socket soc) public static bool IsConnect(Socket soc)
{ {
if(soc == null) if(soc == null)
@ -34,6 +35,11 @@ namespace Galactic_Colors_Control_Server
} }
} }
/// <summary>
/// Return username from socket
/// </summary>
/// <param name="soc">Cleint socket</param>
/// <returns>Name</returns>
public static string GetName(Socket soc) public static string GetName(Socket soc)
{ {
if (soc != null) if (soc != null)
@ -55,6 +61,10 @@ namespace Galactic_Colors_Control_Server
} }
} }
/// <summary>
/// Write line in console with correct colors
/// </summary>
/// <param name="v">Text to write</param>
public static void ConsoleWrite(string v) public static void ConsoleWrite(string v)
{ {
Console.Write("\b"); Console.Write("\b");
@ -65,14 +75,29 @@ namespace Galactic_Colors_Control_Server
Console.Write(">"); Console.Write(">");
} }
/// <summary>
/// Reset Console Colors
/// For non black background console as Ubuntu
/// </summary>
public static void ConsoleResetColor() public static void ConsoleResetColor()
{ {
Console.ResetColor(); Console.ResetColor();
Console.BackgroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Black;
} }
/// <summary>
/// Send data to specified socket
/// </summary>
/// <param name="soc">Target socket</param>
/// <param name="data">Data to send</param>
/// <param name="dtype">Type of data</param>
public static void Send(Socket soc, object data, dataType dtype) public static void Send(Socket soc, object data, dataType dtype)
{ {
/*
Format:
0-3: dataType
4-x: data
*/
byte[] type = new byte[4]; byte[] type = new byte[4];
type = BitConverter.GetBytes((int)dtype); type = BitConverter.GetBytes((int)dtype);
byte[] bytes = null; byte[] bytes = null;
@ -101,6 +126,11 @@ namespace Galactic_Colors_Control_Server
soc.Send(final); soc.Send(final);
} }
/// <summary>
/// Send data to all clients
/// </summary>
/// <param name="data">Data to send</param>
/// <param name="dtype">Type of data</param>
public static void Broadcast(object data, dataType dtype) public static void Broadcast(object data, dataType dtype)
{ {
foreach (Socket soc in Program.clients.Keys) foreach (Socket soc in Program.clients.Keys)
@ -109,6 +139,12 @@ namespace Galactic_Colors_Control_Server
} }
} }
/// <summary>
/// Send or display if server
/// </summary>
/// <param name="message">Text to send</param>
/// <param name="soc">Target socket</param>
/// <param name="server">Is server?</param>
public static void Return(string message, Socket soc = null, bool server = false) public static void Return(string message, Socket soc = null, bool server = false)
{ {
if (server) if (server)