RAGECOOP-V/Server/Client.cs

232 lines
8.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2021-08-26 17:01:32 +02:00
using Lidgren.Network;
namespace CoopServer
{
public class Client
{
public long NetHandle = 0;
2022-01-01 15:58:14 +01:00
private float CurrentLatency = 0f;
public float Latency
{
get => CurrentLatency;
internal set
{
CurrentLatency = value;
if ((value * 1000f) > Server.MainSettings.MaxLatency)
{
Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle)?.Disconnect($"Too high latency [{value * 1000f}/{(float)Server.MainSettings.MaxLatency}]");
}
}
}
2021-08-26 17:01:32 +02:00
public PlayerData Player;
private readonly Dictionary<string, object> CustomData = new();
private long CallbacksCount = 0;
2021-12-10 13:38:03 +01:00
internal readonly Dictionary<long, Action<object>> Callbacks = new();
2022-04-03 02:27:30 +02:00
internal bool FilesReceived = false;
internal bool FilesSent = false;
#region CUSTOMDATA FUNCTIONS
public void SetData<T>(string name, T data)
{
if (HasData(name))
{
CustomData[name] = data;
}
else
{
CustomData.Add(name, data);
}
}
public bool HasData(string name)
{
return CustomData.ContainsKey(name);
}
public T GetData<T>(string name)
{
return HasData(name) ? (T)CustomData[name] : default;
}
public void RemoveData(string name)
{
if (HasData(name))
{
CustomData.Remove(name);
}
}
#endregion
2021-08-26 17:01:32 +02:00
#region FUNCTIONS
2021-08-26 17:01:32 +02:00
public void Kick(string[] reason)
{
Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle)?.Disconnect(string.Join(" ", reason));
2021-08-26 17:01:32 +02:00
}
2021-12-11 14:00:22 +01:00
public void Kick(string reason)
{
Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle)?.Disconnect(reason);
2021-12-11 14:00:22 +01:00
}
2021-08-26 17:01:32 +02:00
public void SendChatMessage(string message, string from = "Server")
{
try
2021-08-26 17:01:32 +02:00
{
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle);
if (userConnection == null)
{
return;
}
Server.SendChatMessage(from, message, userConnection);
}
catch (Exception e)
{
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
}
2021-08-26 17:01:32 +02:00
}
public void SendNativeCall(ulong hash, params object[] args)
2021-08-26 17:01:32 +02:00
{
try
2021-08-26 17:01:32 +02:00
{
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle);
if (userConnection == null)
{
Logging.Error($"[Client->SendNativeCall(ulong hash, params object[] args)]: Connection \"{NetHandle}\" not found!");
return;
}
if (args != null && args.Length == 0)
{
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing arguments!");
return;
}
2022-01-01 03:07:18 +01:00
Packets.NativeCall packet = new()
{
Hash = hash,
Args = new List<object>(args) ?? new List<object>(),
};
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
packet.PacketToNetOutGoingMessage(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Native);
2021-08-26 17:01:32 +02:00
}
catch (Exception e)
2021-08-26 17:01:32 +02:00
{
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
}
2021-08-26 17:01:32 +02:00
}
2021-11-23 23:17:47 +01:00
public void SendNativeResponse(Action<object> callback, ulong hash, Type returnType, params object[] args)
2021-12-10 13:38:03 +01:00
{
try
{
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle);
2021-12-10 13:38:03 +01:00
if (userConnection == null)
{
Logging.Error($"[Client->SendNativeResponse(Action<object> callback, ulong hash, Type type, params object[] args)]: Connection \"{NetHandle}\" not found!");
2021-12-10 13:38:03 +01:00
return;
}
if (args != null && args.Length == 0)
2021-12-10 13:38:03 +01:00
{
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing arguments!");
return;
2021-12-10 13:38:03 +01:00
}
long id = ++CallbacksCount;
Callbacks.Add(id, callback);
byte returnTypeValue = 0x00;
if (returnType == typeof(int))
2021-12-10 13:38:03 +01:00
{
// NOTHING BECAUSE VALUE IS 0x00
2021-12-10 13:38:03 +01:00
}
else if (returnType == typeof(bool))
2021-12-10 13:38:03 +01:00
{
returnTypeValue = 0x01;
2021-12-10 13:38:03 +01:00
}
else if (returnType == typeof(float))
2021-12-10 13:38:03 +01:00
{
returnTypeValue = 0x02;
2021-12-10 13:38:03 +01:00
}
else if (returnType == typeof(string))
2021-12-10 13:38:03 +01:00
{
returnTypeValue = 0x03;
2021-12-10 13:38:03 +01:00
}
else if (returnType == typeof(LVector3))
2021-12-10 13:38:03 +01:00
{
returnTypeValue = 0x04;
2021-12-10 13:38:03 +01:00
}
else
2021-12-10 13:38:03 +01:00
{
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing return type!");
return;
2021-12-10 13:38:03 +01:00
}
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
2022-01-01 03:07:18 +01:00
new Packets.NativeResponse()
2021-12-10 13:38:03 +01:00
{
Hash = hash,
Args = new List<object>(args) ?? new List<object>(),
ResultType = returnTypeValue,
ID = id
}.PacketToNetOutGoingMessage(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Native);
2021-12-10 13:38:03 +01:00
}
catch (Exception e)
{
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
}
}
public void SendCleanUpWorld()
{
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle);
if (userConnection == null)
{
Logging.Error($"[Client->SendCleanUpWorld()]: Connection \"{NetHandle}\" not found!");
return;
}
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
2022-01-01 03:07:18 +01:00
outgoingMessage.Write((byte)PacketTypes.CleanUpWorld);
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Default);
}
2022-01-01 03:07:18 +01:00
public void SendModPacket(string modName, byte customID, byte[] bytes)
2021-11-23 23:17:47 +01:00
{
try
2021-11-23 23:17:47 +01:00
{
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetHandle);
if (userConnection == null)
{
return;
}
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
2022-01-01 03:07:18 +01:00
new Packets.Mod()
{
NetHandle = 0,
Target = 0,
2022-01-01 03:07:18 +01:00
Name = modName,
CustomPacketID = customID,
Bytes = bytes
}.PacketToNetOutGoingMessage(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, userConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Mod);
Server.MainNetServer.FlushSendQueue();
}
catch (Exception e)
{
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
}
2021-11-23 23:17:47 +01:00
}
#endregion
2021-08-26 17:01:32 +02:00
}
}