Cleanup and rewrite some bullshit

This commit is contained in:
Sardelka9515
2023-02-13 20:44:50 +08:00
parent f555fef48d
commit e4f432b593
35 changed files with 255 additions and 219 deletions

View File

@ -14,24 +14,18 @@ namespace RageCoop.Client
{
internal static partial class Networking
{
public static CoopPeer Peer;
public static bool ShowNetworkInfo = false;
public static Security Security;
public static NetConnection ServerConnection;
private static readonly Dictionary<int, Action<PacketType, NetIncomingMessage>> PendingResponses =
new Dictionary<int, Action<PacketType, NetIncomingMessage>>();
internal static readonly Dictionary<PacketType, Func<NetIncomingMessage, Packet>> RequestHandlers =
new Dictionary<PacketType, Func<NetIncomingMessage, Packet>>();
internal static float SimulatedLatency = 0;
public static IPEndPoint _targetServerEP;
static Networking()
{
Security = new Security(Log);
}
public static CoopPeer Peer;
public static bool ShowNetworkInfo = false;
public static Security Security = new();
public static NetConnection ServerConnection;
private static readonly Dictionary<int, Action<PacketType, NetIncomingMessage>> PendingResponses = new();
internal static readonly Dictionary<PacketType, Func<NetIncomingMessage, Packet>> RequestHandlers = new();
internal static float SimulatedLatency = 0;
public static float Latency => ServerConnection.AverageRoundtripTime / 2;
public static bool IsConnecting { get; private set; }
@ -41,24 +35,25 @@ namespace RageCoop.Client
PublicKey publicKey = null)
{
CoopMenu.Menu.Visible = false;
Peer?.Shutdown("Bye");
if (IsOnServer)
{
// ?
}
else if (IsConnecting)
if (IsConnecting)
{
_publicKeyReceived.Set();
IsConnecting = false;
Notification.Show("Connection has been canceled");
}
else
{
Peer?.Dispose();
API.QueueAction(() =>
Notification.Show("Connection has been canceled"));
Peer.Shutdown("bye");
}
else if (IsOnServer)
{
Peer.Shutdown("bye");
}
else
{
IsConnecting = true;
password = password ?? Main.Settings.Password;
username = username ?? Main.Settings.Username;
password ??= Settings.Password;
username ??= Settings.Username;
// 623c92c287cc392406e7aaaac1c0f3b0 = RAGECOOP
var config = new NetPeerConfiguration("623c92c287cc392406e7aaaac1c0f3b0")
@ -104,7 +99,7 @@ namespace RageCoop.Client
// Ensure static constructor invocation
DownloadManager.Cleanup();
Peer = new CoopPeer(config);
Peer = new CoopPeer(config,Log);
Peer.OnMessageReceived += (s, m) =>
{
try
@ -136,9 +131,9 @@ namespace RageCoop.Client
var outgoingMessage = Peer.CreateMessage();
var handshake = new Packets.Handshake
{
PedID = Main.LocalPlayerID,
PedID = LocalPlayerID,
Username = username,
ModVersion = Main.Version.ToString(),
ModVersion = Main.ModVersion.ToString(),
PasswordEncrypted = Security.Encrypt(password.GetBytes()),
InternalEndPoint = new IPEndPoint(CoreUtils.GetLocalAddress(ip[0]), Peer.Port)
};
@ -150,7 +145,7 @@ namespace RageCoop.Client
catch (Exception ex)
{
Log.Error("Cannot connect to server: ", ex);
API.QueueAction(() => Notification.Show("Cannot connect to server: " + ex.Message));
API.QueueAction(() => Notification.Show("~r~Cannot connect to server: " + ex.Message));
}
IsConnecting = false;