140 lines
4.6 KiB
C#
Raw Normal View History

2022-05-22 15:55:26 +08:00
using System;
using Lidgren.Network;
using RageCoop.Core;
using System.Threading.Tasks;
using System.Threading;
namespace RageCoop.Client
{
internal static partial class Networking
2022-05-22 15:55:26 +08:00
{
public static NetClient Client;
public static float Latency = 0;
public static bool ShowNetworkInfo = false;
2022-06-24 10:33:36 +08:00
public static Security Security;
static Networking()
{
2022-06-24 10:33:36 +08:00
Security=new Security(Main.Logger);
Task.Run(() =>
{
while (true)
{
if (Client!=null)
{
ProcessMessage(Client.WaitMessage(200));
Client.FlushSendQueue();
}
else
{
Thread.Sleep(20);
}
}
});
}
public static void ToggleConnection(string address,string username=null,string password=null)
2022-05-22 15:55:26 +08:00
{
if (IsOnServer)
2022-05-22 15:55:26 +08:00
{
Client.Disconnect("Bye!");
}
else
{
2022-06-24 10:33:36 +08:00
password = password ?? Main.Settings.Password;
username=username ?? Main.Settings.Username;
2022-05-22 15:55:26 +08:00
// 623c92c287cc392406e7aaaac1c0f3b0 = RAGECOOP
NetPeerConfiguration config = new NetPeerConfiguration("623c92c287cc392406e7aaaac1c0f3b0")
{
AutoFlushSendQueue = true
2022-05-22 15:55:26 +08:00
};
config.EnableMessageType(NetIncomingMessageType.ConnectionLatencyUpdated);
2022-06-24 10:33:36 +08:00
config.EnableMessageType(NetIncomingMessageType.UnconnectedData);
2022-05-22 15:55:26 +08:00
string[] ip = new string[2];
int idx = address.LastIndexOf(':');
if (idx != -1)
{
ip[0] = address.Substring(0, idx);
ip[1] = address.Substring(idx + 1);
}
if (ip.Length != 2)
{
throw new Exception("Malformed URL");
}
2022-05-22 15:55:26 +08:00
EntityPool.AddPlayer();
2022-06-24 10:33:36 +08:00
Task.Run(() =>
2022-05-22 15:55:26 +08:00
{
2022-06-27 13:02:31 +08:00
Client = new NetClient(config);
Client.Start();
2022-07-01 12:22:31 +08:00
Main.QueueAction(() => { GTA.UI.Notification.Show($"~y~Trying to connect..."); });
DownloadManager.Cleanup();
2022-06-27 13:02:31 +08:00
Security.Regen();
GetServerPublicKey(address);
2022-06-24 10:33:36 +08:00
// Send HandshakePacket
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
var handshake=new Packets.Handshake()
{
PedID = Main.LocalPlayerID,
Username =username,
2022-06-24 10:33:36 +08:00
ModVersion = Main.CurrentVersion,
PassHashEncrypted=Security.Encrypt(password.GetHash())
2022-07-01 12:22:31 +08:00
};
2022-06-24 10:33:36 +08:00
Security.GetSymmetricKeysCrypted(out handshake.AesKeyCrypted,out handshake.AesIVCrypted);
handshake.Pack(outgoingMessage);
Client.Connect(ip[0], short.Parse(ip[1]), outgoingMessage);
2022-07-01 12:22:31 +08:00
2022-06-24 10:33:36 +08:00
});
2022-05-22 15:55:26 +08:00
}
}
public static bool IsOnServer
2022-05-22 15:55:26 +08:00
{
get { return Client?.ConnectionStatus == NetConnectionStatus.Connected; }
2022-05-22 15:55:26 +08:00
}
#region -- GET --
#region -- PLAYER --
private static void PlayerConnect(Packets.PlayerConnect packet)
2022-05-22 15:55:26 +08:00
{
var p = new PlayerData
{
PedID = packet.PedID,
Username= packet.Username,
};
GTA.UI.Notification.Show($"{p.Username} connected.");
PlayerList.SetPlayer(packet.PedID, packet.Username);
2022-05-22 15:55:26 +08:00
Main.Logger.Debug($"player connected:{p.Username}");
}
private static void PlayerDisconnect(Packets.PlayerDisconnect packet)
2022-05-22 15:55:26 +08:00
{
var name=PlayerList.GetPlayer(packet.PedID).Username;
2022-05-22 15:55:26 +08:00
GTA.UI.Notification.Show($"{name} left.");
PlayerList.RemovePlayer(packet.PedID);
2022-05-22 15:55:26 +08:00
EntityPool.RemoveAllFromPlayer(packet.PedID);
}
#endregion // -- PLAYER --
2022-06-24 10:33:36 +08:00
private static void GetServerPublicKey(string address,int timeout=10000)
{
var msg=Client.CreateMessage();
new Packets.PublicKeyRequest().Pack(msg);
var adds =address.Split(':');
Client.SendUnconnectedMessage(msg,adds[0],int.Parse(adds[1]));
PublicKeyReceived.WaitOne(timeout);
}
2022-05-22 15:55:26 +08:00
#endregion
}
}