Move some stuff to core
This commit is contained in:
@ -10,7 +10,7 @@ namespace RageCoop.Client
|
|||||||
{
|
{
|
||||||
internal static partial class Networking
|
internal static partial class Networking
|
||||||
{
|
{
|
||||||
public static NetPeer Peer;
|
public static CoopPeer Peer;
|
||||||
public static float Latency => ServerConnection.AverageRoundtripTime/2;
|
public static float Latency => ServerConnection.AverageRoundtripTime/2;
|
||||||
public static bool ShowNetworkInfo = false;
|
public static bool ShowNetworkInfo = false;
|
||||||
public static Security Security;
|
public static Security Security;
|
||||||
@ -22,35 +22,13 @@ namespace RageCoop.Client
|
|||||||
static Networking()
|
static Networking()
|
||||||
{
|
{
|
||||||
Security=new Security(Main.Logger);
|
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)
|
public static void ToggleConnection(string address, string username = null, string password = null)
|
||||||
{
|
{
|
||||||
|
Peer?.Dispose();
|
||||||
if (IsOnServer)
|
if (IsOnServer)
|
||||||
{
|
{
|
||||||
Peer.Shutdown("Bye!");
|
|
||||||
}
|
}
|
||||||
else if (IsConnecting) {
|
else if (IsConnecting) {
|
||||||
_publicKeyReceived.Set();
|
_publicKeyReceived.Set();
|
||||||
@ -97,8 +75,12 @@ namespace RageCoop.Client
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
DownloadManager.Cleanup();
|
DownloadManager.Cleanup();
|
||||||
Peer = new NetPeer(config);
|
Peer = new CoopPeer(config);
|
||||||
Peer.Start();
|
Peer.OnMessageReceived+= (s, m) =>
|
||||||
|
{
|
||||||
|
try { ProcessMessage(m); }
|
||||||
|
catch (Exception ex) { Main.Logger.Error(ex); }
|
||||||
|
};
|
||||||
Main.QueueAction(() => { GTA.UI.Notification.Show($"~y~Trying to connect..."); });
|
Main.QueueAction(() => { GTA.UI.Notification.Show($"~y~Trying to connect..."); });
|
||||||
Menus.CoopMenu._serverConnectItem.Enabled=false;
|
Menus.CoopMenu._serverConnectItem.Enabled=false;
|
||||||
Security.Regen();
|
Security.Regen();
|
||||||
@ -116,7 +98,7 @@ namespace RageCoop.Client
|
|||||||
Username =username,
|
Username =username,
|
||||||
ModVersion = Main.CurrentVersion,
|
ModVersion = Main.CurrentVersion,
|
||||||
PasswordEncrypted=Security.Encrypt(password.GetBytes()),
|
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);
|
Security.GetSymmetricKeysCrypted(out handshake.AesKeyCrypted, out handshake.AesIVCrypted);
|
||||||
|
@ -13,19 +13,11 @@ namespace RageCoop.Client
|
|||||||
|
|
||||||
public static int SyncInterval = 30;
|
public static int SyncInterval = 30;
|
||||||
public static List<NetConnection> Targets = new List<NetConnection>();
|
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();
|
Peer.SendTo(p, Targets, channel, method);
|
||||||
p.Pack(outgoingMessage);
|
|
||||||
Peer.SendMessage(outgoingMessage,Targets, method, (int)channel);
|
|
||||||
}
|
}
|
||||||
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)
|
public static void SendPed(SyncedPed c, bool full)
|
||||||
{
|
{
|
||||||
if (c.LastSentStopWatch.ElapsedMilliseconds<SyncInterval)
|
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)
|
public static void SendVehicle(SyncedVehicle v, bool full)
|
||||||
{
|
{
|
||||||
@ -150,7 +142,7 @@ namespace RageCoop.Client
|
|||||||
}
|
}
|
||||||
v.LastEngineHealth=packet.EngineHealth;
|
v.LastEngineHealth=packet.EngineHealth;
|
||||||
}
|
}
|
||||||
Send(packet, ConnectionChannel.VehicleSync);
|
SendSync(packet, ConnectionChannel.VehicleSync);
|
||||||
}
|
}
|
||||||
public static void SendProjectile(SyncedProjectile sp)
|
public static void SendProjectile(SyncedProjectile sp)
|
||||||
{
|
{
|
||||||
@ -166,14 +158,14 @@ namespace RageCoop.Client
|
|||||||
Exploded=p.IsDead
|
Exploded=p.IsDead
|
||||||
};
|
};
|
||||||
if (p.IsDead) { EntityPool.RemoveProjectile(sp.ID, "Dead"); }
|
if (p.IsDead) { EntityPool.RemoveProjectile(sp.ID, "Dead"); }
|
||||||
Send(packet, ConnectionChannel.ProjectileSync);
|
SendSync(packet, ConnectionChannel.ProjectileSync);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#region SYNC EVENTS
|
#region SYNC EVENTS
|
||||||
public static void SendBulletShot(Vector3 start, Vector3 end, uint weapon, int ownerID)
|
public static void SendBulletShot(Vector3 start, Vector3 end, uint weapon, int ownerID)
|
||||||
{
|
{
|
||||||
Send(new Packets.BulletShot()
|
SendSync(new Packets.BulletShot()
|
||||||
{
|
{
|
||||||
StartPosition = start,
|
StartPosition = start,
|
||||||
EndPosition = end,
|
EndPosition = end,
|
||||||
@ -184,14 +176,14 @@ namespace RageCoop.Client
|
|||||||
#endregion
|
#endregion
|
||||||
public static void SendChatMessage(string message)
|
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());
|
return Security.Encrypt(s.GetBytes());
|
||||||
})) { Username = Main.Settings.Username, Message = message }.Pack(outgoingMessage);
|
}))
|
||||||
|
{ Username = Main.Settings.Username, Message = message },ServerConnection, ConnectionChannel.Chat, NetDeliveryMethod.ReliableOrdered);
|
||||||
Peer.SendMessage(outgoingMessage,ServerConnection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Chat);
|
|
||||||
Peer.FlushSendQueue();
|
Peer.FlushSendQueue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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>
|
/// <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)
|
public static void SendCustomEvent(int eventHash, params object[] args)
|
||||||
{
|
{
|
||||||
var p = new Packets.CustomEvent()
|
|
||||||
|
Networking.Peer.SendTo(new Packets.CustomEvent()
|
||||||
{
|
{
|
||||||
Args=args,
|
Args=args,
|
||||||
Hash=eventHash
|
Hash=eventHash
|
||||||
};
|
},Networking.ServerConnection, ConnectionChannel.Event, Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
|
||||||
Networking.SendTo(p,Networking.ServerConnection, ConnectionChannel.Event, Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -12,13 +12,13 @@ namespace RageCoop.Client
|
|||||||
#region TRIGGER
|
#region TRIGGER
|
||||||
public static void TriggerPedKilled(SyncedPed victim)
|
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)
|
public static void TriggerEnteringVehicle(SyncedPed c, SyncedVehicle veh, VehicleSeat seat)
|
||||||
{
|
{
|
||||||
Networking.
|
Networking.
|
||||||
Send(new Packets.EnteringVehicle()
|
SendSync(new Packets.EnteringVehicle()
|
||||||
{
|
{
|
||||||
PedID=c.ID,
|
PedID=c.ID,
|
||||||
VehicleID= veh.ID,
|
VehicleID= veh.ID,
|
||||||
@ -34,7 +34,7 @@ namespace RageCoop.Client
|
|||||||
veh.LastSynced=Main.Ticked;
|
veh.LastSynced=Main.Ticked;
|
||||||
TriggerChangeOwner(veh, c.ID);
|
TriggerChangeOwner(veh, c.ID);
|
||||||
}
|
}
|
||||||
Networking.Send(new Packets.EnteredVehicle()
|
Networking.SendSync(new Packets.EnteredVehicle()
|
||||||
{
|
{
|
||||||
VehicleSeat=(short)seat,
|
VehicleSeat=(short)seat,
|
||||||
PedID=c.ID,
|
PedID=c.ID,
|
||||||
@ -45,7 +45,7 @@ namespace RageCoop.Client
|
|||||||
public static void TriggerChangeOwner(SyncedVehicle c, int newOwnerID)
|
public static void TriggerChangeOwner(SyncedVehicle c, int newOwnerID)
|
||||||
{
|
{
|
||||||
|
|
||||||
Networking.Send(new Packets.OwnerChanged()
|
Networking.SendSync(new Packets.OwnerChanged()
|
||||||
{
|
{
|
||||||
ID= c.ID,
|
ID= c.ID,
|
||||||
NewOwnerID= newOwnerID,
|
NewOwnerID= newOwnerID,
|
||||||
@ -70,7 +70,7 @@ namespace RageCoop.Client
|
|||||||
public static void TriggerLeaveVehicle(int id)
|
public static void TriggerLeaveVehicle(int id)
|
||||||
{
|
{
|
||||||
Networking.
|
Networking.
|
||||||
Send(new Packets.LeaveVehicle()
|
SendSync(new Packets.LeaveVehicle()
|
||||||
{
|
{
|
||||||
ID=id
|
ID=id
|
||||||
}, ConnectionChannel.SyncEvents);
|
}, ConnectionChannel.SyncEvents);
|
||||||
@ -94,7 +94,7 @@ namespace RageCoop.Client
|
|||||||
}
|
}
|
||||||
public static void TriggerNozzleTransform(int vehID, bool hover)
|
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
|
#endregion
|
||||||
|
62
RageCoop.Core/Networking/CoopPeer.cs
Normal file
62
RageCoop.Core/Networking/CoopPeer.cs
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user