1
0
Fork 0
This repository has been archived on 2019-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Galactic_Colors_Control/Galactic Colors Control Server/Utilities.cs

195 lines
6.4 KiB
C#
Raw Normal View History

2016-11-13 00:26:03 +00:00
using Galactic_Colors_Control_Common;
using Galactic_Colors_Control_Common.Protocol;
2016-10-30 14:15:27 +00:00
using System;
2016-10-09 12:27:08 +00:00
using System.Net;
using System.Net.Sockets;
2016-11-30 09:49:53 +00:00
using Console = Galactic_Colors_Control_Common.Console;
2016-10-09 12:27:08 +00:00
namespace Galactic_Colors_Control_Server
{
internal class Utilities
2016-10-09 12:27:08 +00:00
{
2016-10-10 11:41:14 +00:00
/// <summary>
/// Check if socket is connect
/// </summary>
/// <param name="soc">Client socket</param>
2016-10-09 12:27:08 +00:00
public static bool IsConnect(Socket soc)
{
if (soc == null)
2016-10-09 12:27:08 +00:00
return true;
2016-11-20 20:02:51 +00:00
if (Server.clients.ContainsKey(soc))
return Server.clients[soc].status != -1;
2016-11-20 20:02:51 +00:00
Server.logger.Write("IsConnect : Unknown client", Logger.logType.error);
return false;
2016-10-09 12:27:08 +00:00
}
2016-10-10 11:41:14 +00:00
/// <summary>
/// Return username from socket
/// </summary>
/// <param name="soc">Cleint socket</param>
/// <returns>Name</returns>
2016-10-09 12:27:08 +00:00
public static string GetName(Socket soc)
{
if (soc == null)
2016-12-10 15:52:28 +00:00
return Server.multilang.GetWord("Server", Server.config.lang);
2016-11-20 20:02:51 +00:00
if (!Server.clients.ContainsKey(soc))
return "?";
2016-11-20 20:02:51 +00:00
string res = Server.clients[soc].pseudo;
if (res == "") { res = ((IPEndPoint)soc.LocalEndPoint).Address.ToString(); }
return res;
}
public static int GetParty(Socket soc)
{
if (soc == null)
2016-11-20 20:02:51 +00:00
return Server.selectedParty;
2016-11-20 20:02:51 +00:00
return Server.clients[soc].partyID;
2016-10-09 12:27:08 +00:00
}
2016-10-10 11:41:14 +00:00
/// <summary>
/// Send data to specified socket
/// </summary>
/// <param name="soc">Target socket</param>
/// <param name="data">Data to send</param>
public static void Send(Socket soc, Data packet)
2016-10-09 12:27:08 +00:00
{
if (soc.Connected)
2016-10-09 12:27:08 +00:00
{
try
{
soc.Send(packet.ToBytes());
2016-11-20 20:02:51 +00:00
if (Server.config.logLevel == Logger.logType.dev)
2016-10-09 12:27:08 +00:00
{
2016-11-20 20:02:51 +00:00
Server.logger.Write("Send to " + GetName(soc) + " : " + packet.ToLongString(), Logger.logType.dev);
2016-10-09 12:27:08 +00:00
}
}
catch (Exception e)
{
2016-11-20 20:02:51 +00:00
Server.logger.Write("Send exception to " + GetName(soc) + " : " + e.Message, Logger.logType.error);
}
2016-10-09 12:27:08 +00:00
}
}
2016-10-10 11:41:14 +00:00
/// <summary>
/// Send data to all clients
/// </summary>
/// <param name="data">Data to send</param>
/// <param name="dtype">Type of data</param>
/// <param name="message">Message to display for server</param>
public static void Broadcast(Data packet)
2016-10-09 12:27:08 +00:00
{
2016-11-20 20:02:51 +00:00
foreach (Socket soc in Server.clients.Keys)
2016-10-09 12:27:08 +00:00
{
Send(soc, packet);
2016-10-09 12:27:08 +00:00
}
2016-11-16 21:27:28 +00:00
switch (packet.GetType().Name)
{
case "EventData":
2016-12-10 15:52:28 +00:00
Console.Write(new ColorStrings(Parser.GetEventText((EventData)packet, Server.config.lang, Server.multilang)));
2016-11-16 21:27:28 +00:00
break;
default:
2016-11-30 09:49:53 +00:00
Console.Write(new ColorStrings(packet.ToSmallString()));
2016-11-16 21:27:28 +00:00
break;
}
2016-11-06 09:59:05 +00:00
}
/// <summary>
/// Send data to all client of the party
/// </summary>
/// <param name="data">Data to send</param>
/// <param name="dtype">Type of data</param>
/// <param name="party">Id of the party</param>
/// <param name="message">Message to display for server</param>
public static void BroadcastParty(Data data, int party)
2016-11-06 09:59:05 +00:00
{
2016-11-20 20:02:51 +00:00
foreach (Socket soc in Server.clients.Keys)
2016-11-06 09:59:05 +00:00
{
2016-11-20 20:02:51 +00:00
if (Server.clients[soc].partyID == party)
2016-11-06 09:59:05 +00:00
{
Send(soc, data);
2016-11-06 09:59:05 +00:00
}
}
2016-11-20 20:02:51 +00:00
if (Server.selectedParty == party)
{
2016-11-16 21:27:28 +00:00
switch (data.GetType().Name)
{
case "EventData":
2016-12-10 15:52:28 +00:00
Console.Write(new ColorStrings(Parser.GetEventText((EventData)data, Server.config.lang, Server.multilang)));
2016-11-16 21:27:28 +00:00
break;
default:
2016-11-30 09:49:53 +00:00
Console.Write(new ColorStrings(data.ToSmallString()));
2016-11-16 21:27:28 +00:00
break;
}
}
2016-10-09 12:27:08 +00:00
}
2016-10-10 11:41:14 +00:00
/// <summary>
/// Send or display if server
/// </summary>
/// <param name="message">Text to display if server</param>
/// <param name="data">Data to send if client</param>
2016-10-10 11:41:14 +00:00
/// <param name="soc">Target socket</param>
/// <param name="server">Is server?</param>
public static void Return(Data data, Socket soc = null, bool server = false)
2016-10-09 12:27:08 +00:00
{
if (server)
{
2016-11-30 09:49:53 +00:00
Console.Write(new ColorStrings(data.ToSmallString()));
2016-10-09 12:27:08 +00:00
}
else
{
Send(soc, data);
2016-10-09 12:27:08 +00:00
}
}
2016-11-06 09:59:05 +00:00
/// <summary>
/// Try get party of the socket
/// </summary>
/// <param name="partyId">Result party ID</param>
/// <param name="needOwn">Return true only for owner and server</param>
/// <param name="soc">Target socket</param>
/// <param name="server">Is server?</param>
/// <returns>Can access?</returns>
public static bool AccessParty(ref int partyId, string[] args, bool needOwn, Socket soc = null, bool server = false)
2016-11-06 09:59:05 +00:00
{
if (server)
{
2016-11-20 20:02:51 +00:00
if (Server.selectedParty == -1)
return false;
2016-11-20 20:02:51 +00:00
if (Server.parties.ContainsKey(Server.selectedParty))
2016-11-06 09:59:05 +00:00
{
2016-11-20 20:02:51 +00:00
partyId = Server.selectedParty;
return true;
2016-11-06 09:59:05 +00:00
}
else
{
2016-11-20 20:02:51 +00:00
Server.selectedParty = -1;
2016-11-06 09:59:05 +00:00
return false;
}
}
else
{
2016-11-20 20:02:51 +00:00
if (Server.clients[soc].partyID == -1)
return false;
2016-11-20 20:02:51 +00:00
if (!Server.parties.ContainsKey(Server.clients[soc].partyID))
2016-11-06 09:59:05 +00:00
return false;
2016-11-20 20:02:51 +00:00
if (Server.parties[Server.clients[soc].partyID].IsOwner(GetName(soc)) || !needOwn)
{
2016-11-20 20:02:51 +00:00
partyId = Server.clients[soc].partyID;
return true;
2016-11-06 09:59:05 +00:00
}
else { return false; }
2016-11-06 09:59:05 +00:00
}
}
2016-10-09 12:27:08 +00:00
}
}