1
0
Fork 0

Ajout des groupes de commandes

This commit is contained in:
sheychen 2016-11-03 21:01:16 +01:00
parent c24ec3076f
commit c8920ef977
21 changed files with 231 additions and 98 deletions

View File

@ -0,0 +1,16 @@
using System.Net.Sockets;
namespace Galactic_Colors_Control_Server
{
public class Client
{
public int status = -1;
public string pseudo = "";
public int partyID = -1;
public Party party
{
get { if (partyID != -1) { return Program.parties[partyID]; } else { return null; } }
}
}
}

View File

@ -8,6 +8,7 @@ namespace Galactic_Colors_Control_Server.Commands
public string Name { get { return "clear"; } } public string Name { get { return "clear"; } }
public string DescText { get { return "Clears the console screen."; } } public string DescText { get { return "Clears the console screen."; } }
public string HelpText { get { return "Use /clear to execute Console.Clear()."; } } public string HelpText { get { return "Use /clear to execute Console.Clear()."; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.root; } }
public bool IsServer { get { return true; } } public bool IsServer { get { return true; } }
public bool IsClient { get { return true; } } public bool IsClient { get { return true; } }
public bool IsClientSide { get { return true; } } public bool IsClientSide { get { return true; } }

View File

@ -3,13 +3,14 @@ using System.Net.Sockets;
namespace Galactic_Colors_Control_Server.Commands namespace Galactic_Colors_Control_Server.Commands
{ {
public class CountCommand : ICommand public class ClientCountCommand : ICommand
{ {
public string Name { get { return "count"; } } public string Name { get { return "count"; } }
public string DescText { get { return "Counts connected clients."; } } public string DescText { get { return "Counts connected clients."; } }
public string HelpText { get { return "Use /count to show connected clients count and size"; } } public string HelpText { get { return "Use /client count to show connected clients count and size"; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.client; } }
public bool IsServer { get { return true; } } public bool IsServer { get { return true; } }
public bool IsClient { get { return false; } } public bool IsClient { get { return true; } }
public bool IsClientSide { get { return false; } } public bool IsClientSide { get { return false; } }
public bool IsNoConnect { get { return false; } } public bool IsNoConnect { get { return false; } }
public int minArgs { get { return 0; } } public int minArgs { get { return 0; } }
@ -17,7 +18,7 @@ namespace Galactic_Colors_Control_Server.Commands
public void Execute(string[] args, Socket soc, bool server = false) public void Execute(string[] args, Socket soc, bool server = false)
{ {
Utilities.ConsoleWrite(Program.clients.Count + "/" + Program.config.size); Utilities.Return(Program.clients.Count + "/" + Program.config.size, soc, server);
} }
} }
} }

View File

@ -6,11 +6,12 @@ using System.Net.Sockets;
namespace Galactic_Colors_Control_Server.Commands namespace Galactic_Colors_Control_Server.Commands
{ {
public class KickCommand : ICommand public class ClientKickCommand : ICommand
{ {
public string Name { get { return "kick"; } } public string Name { get { return "kick"; } }
public string DescText { get { return "Kicks selected client."; } } public string DescText { get { return "Kicks selected client."; } }
public string HelpText { get { return "Use /kick [username] <reason> to kick client from server."; } } public string HelpText { get { return "Use /client kick [username] <reason> to kick client from server."; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.client; } }
public bool IsServer { get { return true; } } public bool IsServer { get { return true; } }
public bool IsClient { get { return false; } } public bool IsClient { get { return false; } }
public bool IsClientSide { get { return false; } } public bool IsClientSide { get { return false; } }
@ -23,15 +24,15 @@ namespace Galactic_Colors_Control_Server.Commands
Socket target = null; Socket target = null;
foreach(Socket client in Program.clients.Keys) foreach(Socket client in Program.clients.Keys)
{ {
if(Utilities.GetName(client) == args[1]) { target = client; } if(Utilities.GetName(client) == args[2]) { target = client; }
} }
if (target != null) if (target != null)
{ {
Logger.Write(args[1] + " was kick by server.", Logger.logType.info); Logger.Write(args[2] + " was kick by server.", Logger.logType.info);
if (args.Length > 2) if (args.Length > 2)
{ {
Utilities.Send(target, "/kick " + args[2], Common.dataType.message); Utilities.Send(target, "/kick " + args[3], Common.dataType.message);
Logger.Write("because" + args[1], Logger.logType.debug); Logger.Write("because" + args[2], Logger.logType.debug);
} }
else { else {
Utilities.Send(target, "/kick", Common.dataType.message); Utilities.Send(target, "/kick", Common.dataType.message);
@ -39,7 +40,7 @@ namespace Galactic_Colors_Control_Server.Commands
} }
else else
{ {
Utilities.Return("Can't find " + args[1], soc, server); Utilities.Return("Can't find " + args[2], soc, server);
} }
} }
} }

View File

@ -3,13 +3,14 @@ using System.Net.Sockets;
namespace Galactic_Colors_Control_Server.Commands namespace Galactic_Colors_Control_Server.Commands
{ {
public class ListCommand : ICommand public class ClientListCommand : ICommand
{ {
public string Name { get { return "list"; } } public string Name { get { return "list"; } }
public string DescText { get { return "Lists connected clients."; } } public string DescText { get { return "Lists connected clients."; } }
public string HelpText { get { return "Use /list to display all connected client username or IP."; } } public string HelpText { get { return "Use /client list to display all connected client username or IP."; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.client; } }
public bool IsServer { get { return true; } } public bool IsServer { get { return true; } }
public bool IsClient { get { return false; } } public bool IsClient { get { return true; } }
public bool IsClientSide { get { return false; } } public bool IsClientSide { get { return false; } }
public bool IsNoConnect { get { return false; } } public bool IsNoConnect { get { return false; } }
public int minArgs { get { return 0; } } public int minArgs { get { return 0; } }
@ -23,7 +24,7 @@ namespace Galactic_Colors_Control_Server.Commands
text += (Utilities.GetName(socket) + ", "); text += (Utilities.GetName(socket) + ", ");
} }
text = text.Remove(text.Length - 2, 2); text = text.Remove(text.Length - 2, 2);
Utilities.ConsoleWrite(text); Utilities.Return(text, soc, server);
} }
} }
} }

View File

@ -0,0 +1,45 @@
using Galactic_Colors_Control_Common;
using System;
using System.Net;
using System.Net.Sockets;
namespace Galactic_Colors_Control_Server.Commands
{
public class ClientStatusCommand : ICommand
{
public string Name { get { return "status"; } }
public string DescText { get { return "Get client status."; } }
public string HelpText { get { return "Use /client status [username] to show client status."; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.client; } }
public bool IsServer { get { return true; } }
public bool IsClient { get { return false; } }
public bool IsClientSide { get { return false; } }
public bool IsNoConnect { get { return true; } }
public int minArgs { get { return 1; } }
public int maxArgs { get { return 1; } }
public void Execute(string[] args, Socket soc, bool server = false)
{
Socket target = null;
foreach (Socket client in Program.clients.Keys)
{
if (Utilities.GetName(client) == args[2]) { target = client; }
}
if (target != null)
{
string text = "";
text += ("Name : " + Utilities.GetName(target) + Environment.NewLine);
text += ("IP : " + ((IPEndPoint)target.LocalEndPoint).Address.ToString() + Environment.NewLine);
if(Program.clients[target].party != null)
{
text += ("Party : " + Program.clients[target].party + Environment.NewLine);
}
Utilities.ConsoleWrite(text);
}
else
{
Utilities.Return("Can't find " + args[2], soc, server);
}
}
}
}

View File

@ -10,6 +10,7 @@ namespace Galactic_Colors_Control_Server.Commands
public string Name { get { return "connect"; } } public string Name { get { return "connect"; } }
public string DescText { get { return "Gets an username."; } } public string DescText { get { return "Gets an username."; } }
public string HelpText { get { return "Use /connect [username] to start identification"; } } public string HelpText { get { return "Use /connect [username] to start identification"; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.root; } }
public bool IsServer { get { return false; } } public bool IsServer { get { return false; } }
public bool IsClient { get { return true; } } public bool IsClient { get { return true; } }
public bool IsClientSide { get { return false; } } public bool IsClientSide { get { return false; } }
@ -23,7 +24,7 @@ namespace Galactic_Colors_Control_Server.Commands
{ {
Logger.Write("Identifiaction request from " + Utilities.GetName(soc), Logger.logType.debug); Logger.Write("Identifiaction request from " + Utilities.GetName(soc), Logger.logType.debug);
bool allreadyconnected = false; bool allreadyconnected = false;
foreach(Data client in Program.clients.Values) foreach(Client client in Program.clients.Values)
{ {
if(client.pseudo == args[1]) { allreadyconnected = true; break; } if(client.pseudo == args[1]) { allreadyconnected = true; break; }
} }

View File

@ -7,9 +7,10 @@ namespace Galactic_Colors_Control_Server.Commands
public class ExitCommand : ICommand public class ExitCommand : ICommand
{ {
public string Name { get { return "exit"; } } public string Name { get { return "exit"; } }
public string DescText { get { return "Leave the program."; } } public string DescText { get { return "Leave the server."; } }
public string HelpText { get { return "Use /exit to stop actual program."; } } public string HelpText { get { return "Use /exit to stop actual program."; } }
public bool IsServer { get { return true; } } public Manager.CommandGroup Group { get { return Manager.CommandGroup.root; } }
public bool IsServer { get { return false; } }
public bool IsClient { get { return true; } } public bool IsClient { get { return true; } }
public bool IsClientSide { get { return false; } } public bool IsClientSide { get { return false; } }
public bool IsNoConnect { get { return true; } } public bool IsNoConnect { get { return true; } }
@ -18,22 +19,14 @@ namespace Galactic_Colors_Control_Server.Commands
public void Execute(string[] args, Socket soc, bool server = false) public void Execute(string[] args, Socket soc, bool server = false)
{ {
if (server) soc.Shutdown(SocketShutdown.Both);
{ Logger.Write("Client disconnected from " + Utilities.GetName(soc), Logger.logType.info);
Program._run = false; string username = Utilities.GetName(soc);
Utilities.ConsoleWrite("Exit server"); bool connected = Program.clients[soc].status != -1;
} soc.Close();
else Program.clients.Remove(soc);
{ if (connected) { Utilities.Broadcast(username + " leave the server", Common.dataType.message); }
soc.Shutdown(SocketShutdown.Both); Logger.Write("Size: " + Program.clients.Count + "/" + Program.config.size, Logger.logType.debug);
Logger.Write("Client disconnected from " + Utilities.GetName(soc), Logger.logType.info);
string username = Utilities.GetName(soc);
bool connected = Program.clients[soc].status != -1;
soc.Close();
Program.clients.Remove(soc);
if (connected) { Utilities.Broadcast(username + " leave the server", Common.dataType.message); }
Logger.Write("Size: " + Program.clients.Count + "/" + Program.config.size, Logger.logType.debug);
}
} }
} }
} }

View File

@ -10,51 +10,60 @@ namespace Galactic_Colors_Control_Server.Commands
public string Name { get { return "help"; } } public string Name { get { return "help"; } }
public string DescText { get { return "Shows the help."; } } public string DescText { get { return "Shows the help."; } }
public string HelpText { get { return "Use /help [command] to display command help."; } } public string HelpText { get { return "Use /help [command] to display command help."; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.root; } }
public bool IsServer { get { return true; } } public bool IsServer { get { return true; } }
public bool IsClient { get { return true; } } public bool IsClient { get { return true; } }
public bool IsClientSide { get { return false; } } public bool IsClientSide { get { return false; } }
public bool IsNoConnect { get { return false; } } public bool IsNoConnect { get { return false; } }
public int minArgs { get { return 0; } } public int minArgs { get { return 0; } }
public int maxArgs { get { return 1; } } public int maxArgs { get { return 2; } }
public void Execute(string[] args, Socket soc, bool server = false) public void Execute(string[] args, Socket soc, bool server = false)
{ {
if(args.Length == 1) if(args.Length == 1)
{ {
List<string> list = new List<string>();
int maxLen = 0; int maxLen = 0;
foreach (string com in Manager.commands.Keys) List<ICommand> list = new List<ICommand>();
foreach (ICommand com in Manager.commands)
{ {
if(Manager.CanAccess(Manager.commands[com], soc, server)) if(Manager.CanAccess(com, soc, server))
{ {
list.Add(com); list.Add(com);
if(com.Length > maxLen) { maxLen = com.Length; } if(com.Name.Length + (com.Group == 0 ? 0 : 4) > maxLen) { maxLen = com.Name.Length + (com.Group == 0 ? 0 : 4); }
} }
} }
list.Sort(); list.Sort((x,y) => x.Group.CompareTo(y.Group));
string text = "Use /help [command] for more informations." + Environment.NewLine + "Available commands:" + Environment.NewLine; string text = "Use /help [command] for more informations." + Environment.NewLine + "Available commands:" + Environment.NewLine;
foreach (var key in list) Manager.CommandGroup actualGroup = 0;
foreach (ICommand com in list)
{ {
text += (" " + key + new string(' ', maxLen - key.Length) + " : " + Manager.commands[key].DescText + Environment.NewLine); if(com.Group != actualGroup)
{
text += (Environment.NewLine + " " + com.Group.ToString() + Environment.NewLine);
actualGroup = com.Group;
}
text += (" " + (com.Group != 0 ? new string(' ',4) : "") + com.Name + new string(' ', maxLen - com.Name.Length - (com.Group == 0 ? 0 : 4)) + " : " + com.DescText + Environment.NewLine);
} }
Utilities.Return(text, soc, server); Utilities.Return(text, soc, server);
} }
else else
{ {
if (Manager.commands.ContainsKey(args[1])) ICommand command = null;
args = args.Skip(1).ToArray();
if (Manager.TryGetCommand(args, ref command))
{ {
if (Manager.CanAccess(Manager.commands[args[1]], soc, server)) if (Manager.CanAccess(command, soc, server))
{ {
Utilities.Return(Manager.commands[args[1]].HelpText, soc, server); Utilities.Return(command.HelpText, soc, server);
} }
else else
{ {
Utilities.Return("Any help for " + args[1], soc, server); Utilities.Return("Any help for " + Manager.CommandToString(args), soc, server);
} }
} }
else else
{ {
Utilities.Return("Any help for " + args[1], soc, server); Utilities.Return("Any help for " + Manager.CommandToString(args), soc, server);
} }
} }
} }

View File

@ -7,6 +7,7 @@ namespace Galactic_Colors_Control_Server.Commands
string Name { get; } string Name { get; }
string DescText { get; } string DescText { get; }
string HelpText { get; } string HelpText { get; }
Manager.CommandGroup Group { get; }
bool IsServer { get; } bool IsServer { get; }
bool IsClient { get; } bool IsClient { get; }
bool IsClientSide { get; } bool IsClientSide { get; }

View File

@ -8,6 +8,7 @@ namespace Galactic_Colors_Control_Server.Commands
public string Name { get { return "loglevel"; } } public string Name { get { return "loglevel"; } }
public string DescText { get { return "Change console loglevel."; } } public string DescText { get { return "Change console loglevel."; } }
public string HelpText { get { return "Use /loglevel [loglevel] to change Loglevel."; } } public string HelpText { get { return "Use /loglevel [loglevel] to change Loglevel."; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.root; } }
public bool IsServer { get { return true; } } public bool IsServer { get { return true; } }
public bool IsClient { get { return false; } } public bool IsClient { get { return false; } }
public bool IsClientSide { get { return true; } } public bool IsClientSide { get { return true; } }

View File

@ -6,9 +6,10 @@ using System.Reflection;
namespace Galactic_Colors_Control_Server.Commands namespace Galactic_Colors_Control_Server.Commands
{ {
class Manager public class Manager
{ {
public static Dictionary<string, ICommand> commands { get; private set; } = new Dictionary<string, ICommand>(); public static List<ICommand> commands { get; private set; } = new List<ICommand>();
public enum CommandGroup { root, server, party, client}
/// <summary> /// <summary>
/// Find all ICommand and add them to commands /// Find all ICommand and add them to commands
@ -19,8 +20,8 @@ namespace Galactic_Colors_Control_Server.Commands
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)
{ {
commands.Add(com.Name, com); commands.Add(com);
Logger.Write("Added command " + com.GetType().Name, Logger.logType.debug); Logger.Write("Added command " + com.Group.ToString() + " " + com.Name, Logger.logType.debug);
} }
} }
@ -32,12 +33,12 @@ namespace Galactic_Colors_Control_Server.Commands
/// <param name="server">Is server?</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])) ICommand command = null;
if (TryGetCommand(args, ref command))
{ {
ICommand command = commands[args[0]];
if (CanAccess(command, soc, server)) if (CanAccess(command, soc, server))
{ {
if (command.IsClientSide) if (!server && command.IsClientSide)
{ {
Utilities.Return("It's a client side command", soc, server); Utilities.Return("It's a client side command", soc, server);
} }
@ -45,30 +46,85 @@ namespace Galactic_Colors_Control_Server.Commands
{ {
if (args.Length > command.minArgs) if (args.Length > command.minArgs)
{ {
if (args.Length - 1 <= command.maxArgs) if (args.Length - (command.Group == 0 ? 1 : 2) <= command.maxArgs)
{ {
command.Execute(args, soc, server); command.Execute(args, soc, server);
} }
else else
{ {
Utilities.Return("Command " + command.Name + " require at most " + command.minArgs + " argument(s).", soc, server); Utilities.Return("Command " + CommandToString(command) + " require at most " + command.minArgs + " argument(s).", soc, server);
} }
} }
else else
{ {
Utilities.Return("Command " + command.Name + " require at least " + command.minArgs + " argument(s).", soc, server); Utilities.Return("Command " + CommandToString(command) + " require at least " + command.minArgs + " argument(s).", soc, server);
} }
} }
} }
else else
{ {
Utilities.Return("Unknown command : " + args[0], soc, server); Utilities.Return("Unknown command : " + CommandToString(args), soc, server);
} }
} }
else else
{ {
Utilities.Return("Unknown command : " + args[0], soc, server); Utilities.Return("Unknown command : " + CommandToString(args), soc, server);
}
}
public static string CommandToString(ICommand command)
{
string text = "";
if(command.Group != 0) { text += (command.Group.ToString() + " "); }
text += command.Name;
return text;
}
public static string CommandToString(string[] args)
{
if (args.Length > 0)
{
string text = "";
foreach(string arg in args)
{
text += (arg + " ");
}
return text;
}
else
{
return null;
}
}
public static bool TryGetCommand(string[] args, ref ICommand command)
{
if (args.Length > 0)
{
List<string> groups = Enum.GetNames(typeof(CommandGroup)).ToList();
CommandGroup group = 0;
if (groups.Contains(args[0]))
{
if (args.Length > 1)
{
group = (CommandGroup)Enum.Parse(typeof(CommandGroup), args[0]);
}
}
IEnumerable<ICommand> coms = commands.Where(p => (p.Name == args[group == 0 ? 0 : 1] && p.Group == group));
if (coms.Count() == 1)
{
command = coms.First();
return true;
}
else
{
return false;
}
}
else
{
return false;
} }
} }

View File

@ -7,6 +7,7 @@ namespace Galactic_Colors_Control_Server.Commands
public string Name { get { return "ping"; } } public string Name { get { return "ping"; } }
public string DescText { get { return "Clears the console screen."; } } public string DescText { get { return "Clears the console screen."; } }
public string HelpText { get { return "Use /ping to display our ping."; } } public string HelpText { get { return "Use /ping to display our ping."; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.root; } }
public bool IsServer { get { return false; } } public bool IsServer { get { return false; } }
public bool IsClient { get { return true; } } public bool IsClient { get { return true; } }
public bool IsClientSide { get { return true; } } public bool IsClientSide { get { return true; } }

View File

@ -3,17 +3,18 @@ using System.Net.Sockets;
namespace Galactic_Colors_Control_Server.Commands namespace Galactic_Colors_Control_Server.Commands
{ {
public class CloseCommand : ICommand public class ServerCloseCommand : ICommand
{ {
public string Name { get { return "close"; } } public string Name { get { return "close"; } }
public string DescText { get { return "Closes server from connections."; } } public string DescText { get { return "Close server."; } }
public string HelpText { get { return "Use /close to stop connection process"; } } public string HelpText { get { return "Use /server close to close server for connections"; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.server; } }
public bool IsServer { get { return true; } } public bool IsServer { get { return true; } }
public bool IsClient { get { return false; } } public bool IsClient { get { return false; } }
public bool IsClientSide { get { return false; } } public bool IsClientSide { get { return false; } }
public bool IsNoConnect { get { return false; } } public bool IsNoConnect { get { return false; } }
public int minArgs { get { return 0; } } public int minArgs { get { return 0; } }
public int maxArgs { get { return 0; } } public int maxArgs { get { return 1; } }
public void Execute(string[] args, Socket soc, bool server = false) public void Execute(string[] args, Socket soc, bool server = false)
{ {

View File

@ -3,17 +3,18 @@ using System.Net.Sockets;
namespace Galactic_Colors_Control_Server.Commands namespace Galactic_Colors_Control_Server.Commands
{ {
public class OpenCommand : ICommand public class ServerOpenCommand : ICommand
{ {
public string Name { get { return "open"; } } public string Name { get { return "open"; } }
public string DescText { get { return "Opens server for connections."; } } public string DescText { get { return "Open server."; } }
public string HelpText { get { return "Use /open to restart connection process"; } } public string HelpText { get { return "Use /server open to open server for connections"; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.server; } }
public bool IsServer { get { return true; } } public bool IsServer { get { return true; } }
public bool IsClient { get { return false; } } public bool IsClient { get { return false; } }
public bool IsClientSide { get { return false; } } public bool IsClientSide { get { return false; } }
public bool IsNoConnect { get { return false; } } public bool IsNoConnect { get { return false; } }
public int minArgs { get { return 0; } } public int minArgs { get { return 0; } }
public int maxArgs { get { return 0; } } public int maxArgs { get { return 1; } }
public void Execute(string[] args, Socket soc, bool server = false) public void Execute(string[] args, Socket soc, bool server = false)
{ {

View File

@ -3,11 +3,12 @@ using System.Net.Sockets;
namespace Galactic_Colors_Control_Server.Commands namespace Galactic_Colors_Control_Server.Commands
{ {
public class StatusCommand : ICommand public class ServerStatusCommand : ICommand
{ {
public string Name { get { return "status"; } } public string Name { get { return "status"; } }
public string DescText { get { return "Shows server status."; } } public string DescText { get { return "Shows server status."; } }
public string HelpText { get { return "Use /status to display server actual status."; } } public string HelpText { get { return "Use /server status to display server actual status."; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.server; } }
public bool IsServer { get { return true; } } public bool IsServer { get { return true; } }
public bool IsClient { get { return false; } } public bool IsClient { get { return false; } }
public bool IsClientSide { get { return false; } } public bool IsClientSide { get { return false; } }
@ -19,11 +20,14 @@ namespace Galactic_Colors_Control_Server.Commands
{ {
if (Program._open) if (Program._open)
{ {
Utilities.ConsoleWrite("Server open"); Utilities.ConsoleWrite("Server : open", ConsoleColor.Green);
} }
else { else
Utilities.ConsoleWrite("Server close"); {
Utilities.ConsoleWrite("Server : close", ConsoleColor.Red);
} }
Utilities.ConsoleWrite("Clients : " + Program.clients.Count + "/" + Program.config.size);
Utilities.ConsoleWrite("Parties : " + Program.parties.Count);
} }
} }
} }

View File

@ -8,6 +8,7 @@ namespace Galactic_Colors_Control_Server.Commands
public string Name { get { return "time"; } } public string Name { get { return "time"; } }
public string DescText { get { return "Gives server time."; } } public string DescText { get { return "Gives server time."; } }
public string HelpText { get { return "Use /time to display server time. (format is server dependent)"; } } public string HelpText { get { return "Use /time to display server time. (format is server dependent)"; } }
public Manager.CommandGroup Group { get { return Manager.CommandGroup.root; } }
public bool IsServer { get { return true; } } public bool IsServer { get { return true; } }
public bool IsClient { get { return true; } } public bool IsClient { get { return true; } }
public bool IsClientSide { get { return false; } } public bool IsClientSide { get { return false; } }

View File

@ -1,11 +0,0 @@
using System.Net.Sockets;
namespace Galactic_Colors_Control_Server
{
public class Data
{
public int id;
public int status = -1;
public string pseudo = "";
}
}

View File

@ -56,24 +56,27 @@
<Compile Include="..\AssemblyInfoCommon.cs"> <Compile Include="..\AssemblyInfoCommon.cs">
<Link>Properties\AssemblyInfoCommon.cs</Link> <Link>Properties\AssemblyInfoCommon.cs</Link>
</Compile> </Compile>
<Compile Include="Client.cs" />
<Compile Include="Commands\ClearCommand.cs" /> <Compile Include="Commands\ClearCommand.cs" />
<Compile Include="Commands\Client\StatusCommand.cs" />
<Compile Include="Commands\PingCommand.cs" /> <Compile Include="Commands\PingCommand.cs" />
<Compile Include="Commands\CloseCommand.cs" /> <Compile Include="Commands\Server\ServerOpenCommand.cs" />
<Compile Include="Commands\ConnectCommand.cs" /> <Compile Include="Commands\ConnectCommand.cs" />
<Compile Include="Commands\CountCommand.cs" /> <Compile Include="Commands\Client\CountCommand.cs" />
<Compile Include="Commands\ExitCommand.cs" /> <Compile Include="Commands\ExitCommand.cs" />
<Compile Include="Commands\HelpCommand.cs" /> <Compile Include="Commands\HelpCommand.cs" />
<Compile Include="Commands\ICommand.cs" /> <Compile Include="Commands\ICommand.cs" />
<Compile Include="Commands\KickCommand.cs" /> <Compile Include="Commands\Client\KickCommand.cs" />
<Compile Include="Commands\ListCommand.cs" /> <Compile Include="Commands\Client\ListCommand.cs" />
<Compile Include="Commands\LogLevelCommand.cs" /> <Compile Include="Commands\LogLevelCommand.cs" />
<Compile Include="Commands\Manager.cs" /> <Compile Include="Commands\Manager.cs" />
<Compile Include="Commands\OpenCommand.cs" /> <Compile Include="Commands\Server\ServerCloseCommand.cs" />
<Compile Include="Commands\StatusCommand.cs" /> <Compile Include="Commands\Server\ServerStopCommand.cs" />
<Compile Include="Commands\Server\ServerStatusCommand.cs" />
<Compile Include="Commands\TimeCommand.cs" /> <Compile Include="Commands\TimeCommand.cs" />
<Compile Include="Config.cs" /> <Compile Include="Config.cs" />
<Compile Include="Data.cs" />
<Compile Include="Logger.cs" /> <Compile Include="Logger.cs" />
<Compile Include="Party.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utilities.cs" /> <Compile Include="Utilities.cs" />
@ -103,6 +106,9 @@
<Name>Galactic Colors Control Common</Name> <Name>Galactic Colors Control Common</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Commands\Party\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -17,7 +17,10 @@ namespace Galactic_Colors_Control_Server
public static bool _run = true; public static bool _run = true;
public static bool _open = true; public static bool _open = true;
private static readonly byte[] buffer = new byte[BUFFER_SIZE]; private static readonly byte[] buffer = new byte[BUFFER_SIZE];
public static Dictionary<Socket, Data> clients { get; private set; } = new Dictionary<Socket, Data>();
public static Dictionary<Socket, Client> clients { get; private set; } = new Dictionary<Socket, Client>();
public static List<Party> parties { get; private set; } = new List<Party>();
public static Config config = new Config(); public static Config config = new Config();
private static void Main(string[] args) private static void Main(string[] args)
@ -135,7 +138,8 @@ namespace Galactic_Colors_Control_Server
/// </summary> /// </summary>
private static void AddClient(Socket socket) private static void AddClient(Socket socket)
{ {
clients.Add(socket, new Data()); Client client = new Client();
clients.Add(socket, client);
socket.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, socket); socket.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, socket);
Logger.Write("Client connection from " + Utilities.GetName(socket), Logger.logType.info); Logger.Write("Client connection from " + Utilities.GetName(socket), Logger.logType.info);
Logger.Write("Size: " + clients.Count + "/" + config.size, Logger.logType.debug); Logger.Write("Size: " + clients.Count + "/" + config.size, Logger.logType.debug);
@ -159,9 +163,9 @@ namespace Galactic_Colors_Control_Server
} }
catch (SocketException) catch (SocketException)
{ {
Logger.Write("Client forcefully disconnected from " + Utilities.GetName(current), Logger.logType.info); Logger.Write("Client forcefully disconnected from " + Utilities.GetName(current) + " : SocketException", Logger.logType.info);
string username = Utilities.GetName(current); string username = Utilities.GetName(current);
bool connected = Program.clients[current].status != -1; bool connected = clients[current].status != -1;
Logger.Write("Size: " + clients.Count + "/" + config.size, Logger.logType.debug); Logger.Write("Size: " + clients.Count + "/" + config.size, Logger.logType.debug);
current.Close(); // Don't shutdown because the socket may be disposed and its disconnected anyway. current.Close(); // Don't shutdown because the socket may be disposed and its disconnected anyway.
clients.Remove(current); clients.Remove(current);
@ -198,7 +202,7 @@ 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); }
} }
catch (Exception) { catch (Exception) {
Logger.Write("Client forcefully disconnected from " + Utilities.GetName(current), Logger.logType.info); Logger.Write("Client forcefully disconnected from " + Utilities.GetName(current) + " : ReceiveException", Logger.logType.info);
if (clients.ContainsKey(current)) { clients.Remove(current); } if (clients.ContainsKey(current)) { clients.Remove(current); }
} }
} }

View File

@ -64,11 +64,11 @@ namespace Galactic_Colors_Control_Server
/// Write line in console with correct colors /// Write line in console with correct colors
/// </summary> /// </summary>
/// <param name="v">Text to write</param> /// <param name="v">Text to write</param>
public static void ConsoleWrite(string v) public static void ConsoleWrite(string v, ConsoleColor Fore = ConsoleColor.White, ConsoleColor Back = ConsoleColor.Black)
{ {
Console.Write("\b"); Console.Write("\b");
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = Fore;
Console.BackgroundColor = ConsoleColor.Black; Console.BackgroundColor = Back;
Console.WriteLine(v); Console.WriteLine(v);
ConsoleResetColor(); ConsoleResetColor();
Console.Write(">"); Console.Write(">");