using Galactic_Colors_Control_Common; using Galactic_Colors_Control_Common.Protocol; using System; using System.Net; using System.Net.Sockets; namespace Galactic_Colors_Control_Server { internal class Utilities { /// /// Check if socket is connect /// /// Client socket public static bool IsConnect(Socket soc) { if (soc == null) return true; if (Program.clients.ContainsKey(soc)) return Program.clients[soc].status != -1; Logger.Write("IsConnect : Unknown client", Logger.logType.error); return false; } /// /// Return username from socket /// /// Cleint socket /// Name public static string GetName(Socket soc) { if (soc == null) return "Server"; if (!Program.clients.ContainsKey(soc)) return "?"; string res = Program.clients[soc].pseudo; if (res == "") { res = ((IPEndPoint)soc.LocalEndPoint).Address.ToString(); } return res; } public static int GetParty(Socket soc) { if (soc == null) return Program.selectedParty; return Program.clients[soc].partyID; } /// /// Send data to specified socket /// /// Target socket /// Data to send public static void Send(Socket soc, Data packet) { if (soc.Connected) { try { soc.Send(packet.ToBytes()); if (Program.config.logLevel == Logger.logType.dev) { Logger.Write("Send to " + GetName(soc) + " : " + packet.ToLongString(), Logger.logType.dev); } } catch (Exception e) { Logger.Write("Send exception to " + GetName(soc) + " : " + e.Message, Logger.logType.error); } } } /// /// Send data to all clients /// /// Data to send /// Type of data /// Message to display for server public static void Broadcast(Data packet) { foreach (Socket soc in Program.clients.Keys) { Send(soc, packet); } Common.ConsoleWrite(packet.ToSmallString()); } /// /// Send data to all client of the party /// /// Data to send /// Type of data /// Id of the party /// Message to display for server public static void BroadcastParty(Data data, int party) { foreach (Socket soc in Program.clients.Keys) { if (Program.clients[soc].partyID == party) { Send(soc, data); } } if (Program.selectedParty == party) { Common.ConsoleWrite(data.ToSmallString()); } } /// /// Send or display if server /// /// Text to display if server /// Data to send if client /// Target socket /// Is server? public static void Return(Data data, Socket soc = null, bool server = false) { if (server) { Common.ConsoleWrite(data.ToSmallString()); } else { Send(soc, data); } } /// /// Try get party of the socket /// /// Result party ID /// Return true only for owner and server /// Target socket /// Is server? /// Can access? public static bool AccessParty(ref int partyId, string[] args, bool needOwn, Socket soc = null, bool server = false) { if (server) { if (Program.selectedParty == -1) return false; if (Program.parties.ContainsKey(Program.selectedParty)) { partyId = Program.selectedParty; return true; } else { Program.selectedParty = -1; return false; } } else { if (Program.clients[soc].partyID == -1) return false; if (!Program.parties.ContainsKey(Program.clients[soc].partyID)) return false; if (Program.parties[Program.clients[soc].partyID].IsOwner(GetName(soc)) || !needOwn) { partyId = Program.clients[soc].partyID; return true; } else { return false; } } } } }