Move some stuff to core

This commit is contained in:
Sardelka 2022-08-11 16:25:38 +08:00
parent 95e302a698
commit 033d0a3f2e
5 changed files with 92 additions and 56 deletions

View File

@ -10,7 +10,7 @@ namespace RageCoop.Client
{
internal static partial class Networking
{
public static NetPeer Peer;
public static CoopPeer Peer;
public static float Latency => ServerConnection.AverageRoundtripTime/2;
public static bool ShowNetworkInfo = false;
public static Security Security;
@ -22,35 +22,13 @@ namespace RageCoop.Client
static Networking()
{
Security=new Security(Main.Logger);
Task.Run(() =>
{
while (true)
{
if (Peer!=null)
{
try
{
ProcessMessage(Peer.WaitMessage(200));
}
catch(Exception ex)
{
Main.Logger.Error(ex);
}
}
else
{
Thread.Sleep(20);
}
}
});
}
public static void ToggleConnection(string address, string username = null, string password = null)
{
Peer?.Dispose();
if (IsOnServer)
{
Peer.Shutdown("Bye!");
}
else if (IsConnecting) {
_publicKeyReceived.Set();
@ -97,8 +75,12 @@ namespace RageCoop.Client
try
{
DownloadManager.Cleanup();
Peer = new NetPeer(config);
Peer.Start();
Peer = new CoopPeer(config);
Peer.OnMessageReceived+= (s, m) =>
{
try { ProcessMessage(m); }
catch (Exception ex) { Main.Logger.Error(ex); }
};
Main.QueueAction(() => { GTA.UI.Notification.Show($"~y~Trying to connect..."); });
Menus.CoopMenu._serverConnectItem.Enabled=false;
Security.Regen();
@ -116,7 +98,7 @@ namespace RageCoop.Client
Username =username,
ModVersion = Main.CurrentVersion,
PasswordEncrypted=Security.Encrypt(password.GetBytes()),
InternalEndPoint = new System.Net.IPEndPoint(CoreUtils.GetLocalAddress(ip[0]),Peer.Port)
InternalEndPoint = new System.Net.IPEndPoint(CoreUtils.GetLocalAddress(ip[0]), Peer.Port)
};
Security.GetSymmetricKeysCrypted(out handshake.AesKeyCrypted, out handshake.AesIVCrypted);

View File

@ -13,19 +13,11 @@ namespace RageCoop.Client
public static int SyncInterval = 30;
public static List<NetConnection> Targets = new List<NetConnection>();
public static void Send(Packet p, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
public static void SendSync(Packet p, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
{
NetOutgoingMessage outgoingMessage = Peer.CreateMessage();
p.Pack(outgoingMessage);
Peer.SendMessage(outgoingMessage,Targets, method, (int)channel);
Peer.SendTo(p, Targets, channel, method);
}
public static void SendTo(Packet p,NetConnection connection, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
{
NetOutgoingMessage outgoingMessage = Peer.CreateMessage();
p.Pack(outgoingMessage);
Peer.SendMessage(outgoingMessage, connection, method, (int)channel);
}
public static void SendPed(SyncedPed c, bool full)
{
if (c.LastSentStopWatch.ElapsedMilliseconds<SyncInterval)
@ -93,7 +85,7 @@ namespace RageCoop.Client
}
}
}
Send(packet, ConnectionChannel.PedSync);
SendSync(packet, ConnectionChannel.PedSync);
}
public static void SendVehicle(SyncedVehicle v, bool full)
{
@ -150,7 +142,7 @@ namespace RageCoop.Client
}
v.LastEngineHealth=packet.EngineHealth;
}
Send(packet, ConnectionChannel.VehicleSync);
SendSync(packet, ConnectionChannel.VehicleSync);
}
public static void SendProjectile(SyncedProjectile sp)
{
@ -166,14 +158,14 @@ namespace RageCoop.Client
Exploded=p.IsDead
};
if (p.IsDead) { EntityPool.RemoveProjectile(sp.ID, "Dead"); }
Send(packet, ConnectionChannel.ProjectileSync);
SendSync(packet, ConnectionChannel.ProjectileSync);
}
#region SYNC EVENTS
public static void SendBulletShot(Vector3 start, Vector3 end, uint weapon, int ownerID)
{
Send(new Packets.BulletShot()
SendSync(new Packets.BulletShot()
{
StartPosition = start,
EndPosition = end,
@ -184,14 +176,14 @@ namespace RageCoop.Client
#endregion
public static void SendChatMessage(string message)
{
NetOutgoingMessage outgoingMessage = Peer.CreateMessage();
new Packets.ChatMessage(new Func<string, byte[]>((s) =>
Peer.SendTo(new Packets.ChatMessage(new Func<string, byte[]>((s) =>
{
return Security.Encrypt(s.GetBytes());
})) { Username = Main.Settings.Username, Message = message }.Pack(outgoingMessage);
Peer.SendMessage(outgoingMessage,ServerConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Chat);
}))
{ Username = Main.Settings.Username, Message = message },ServerConnection, ConnectionChannel.Chat, NetDeliveryMethod.ReliableOrdered);
Peer.FlushSendQueue();
}
}

View File

@ -280,12 +280,12 @@ namespace RageCoop.Client.Scripting
/// <param name="args">The objects conataing your data, see <see cref="CustomEventReceivedArgs"/> for a list of supported types</param>
public static void SendCustomEvent(int eventHash, params object[] args)
{
var p = new Packets.CustomEvent()
Networking.Peer.SendTo(new Packets.CustomEvent()
{
Args=args,
Hash=eventHash
};
Networking.SendTo(p,Networking.ServerConnection, ConnectionChannel.Event, Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
},Networking.ServerConnection, ConnectionChannel.Event, Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
}
/// <summary>

View File

@ -12,13 +12,13 @@ namespace RageCoop.Client
#region TRIGGER
public static void TriggerPedKilled(SyncedPed victim)
{
Networking.Send(new Packets.PedKilled() { VictimID=victim.ID }, ConnectionChannel.SyncEvents);
Networking.SendSync(new Packets.PedKilled() { VictimID=victim.ID }, ConnectionChannel.SyncEvents);
}
public static void TriggerEnteringVehicle(SyncedPed c, SyncedVehicle veh, VehicleSeat seat)
{
Networking.
Send(new Packets.EnteringVehicle()
SendSync(new Packets.EnteringVehicle()
{
PedID=c.ID,
VehicleID= veh.ID,
@ -34,7 +34,7 @@ namespace RageCoop.Client
veh.LastSynced=Main.Ticked;
TriggerChangeOwner(veh, c.ID);
}
Networking.Send(new Packets.EnteredVehicle()
Networking.SendSync(new Packets.EnteredVehicle()
{
VehicleSeat=(short)seat,
PedID=c.ID,
@ -45,7 +45,7 @@ namespace RageCoop.Client
public static void TriggerChangeOwner(SyncedVehicle c, int newOwnerID)
{
Networking.Send(new Packets.OwnerChanged()
Networking.SendSync(new Packets.OwnerChanged()
{
ID= c.ID,
NewOwnerID= newOwnerID,
@ -70,7 +70,7 @@ namespace RageCoop.Client
public static void TriggerLeaveVehicle(int id)
{
Networking.
Send(new Packets.LeaveVehicle()
SendSync(new Packets.LeaveVehicle()
{
ID=id
}, ConnectionChannel.SyncEvents);
@ -94,7 +94,7 @@ namespace RageCoop.Client
}
public static void TriggerNozzleTransform(int vehID, bool hover)
{
Networking.Send(new Packets.NozzleTransform() { VehicleID=vehID, Hover=hover }, ConnectionChannel.SyncEvents);
Networking.SendSync(new Packets.NozzleTransform() { VehicleID=vehID, Hover=hover }, ConnectionChannel.SyncEvents);
}
#endregion

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Text;
using Lidgren.Network;
using System.Threading;
namespace RageCoop.Core
{
internal class CoopPeer : NetPeer, IDisposable
{
public EventHandler<NetIncomingMessage> OnMessageReceived;
private readonly Thread ListenerThread;
private bool _stopping=false;
public CoopPeer(NetPeerConfiguration config):base(config)
{
Start();
NetIncomingMessage msg;
ListenerThread=new Thread(() =>
{
while (!_stopping)
{
msg=WaitMessage(200);
if (msg!=null)
{
OnMessageReceived?.Invoke(this,msg);
}
}
});
ListenerThread.Start();
}
/// <summary>
/// Terminate all connections and background thread
/// </summary>
public void Dispose()
{
_stopping=true;
Shutdown("Bye!");
ListenerThread.Join();
}
public void SendTo(Packet p, NetConnection connection, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
{
NetOutgoingMessage outgoingMessage = CreateMessage();
p.Pack(outgoingMessage);
SendMessage(outgoingMessage, connection, method, (int)channel);
}
public void SendTo(Packet p, IList<NetConnection> connections, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
{
NetOutgoingMessage outgoingMessage = CreateMessage();
p.Pack(outgoingMessage);
SendMessage(outgoingMessage, connections, method, (int)channel);
}
public void Send(Packet p,IList<NetConnection> cons, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
{
NetOutgoingMessage outgoingMessage = CreateMessage();
p.Pack(outgoingMessage);
SendMessage(outgoingMessage, cons, method, (int)channel);
}
}
}