diff --git a/RageCoop.Client/Networking/Networking.cs b/RageCoop.Client/Networking/Networking.cs index a9a11a0..cd420f9 100644 --- a/RageCoop.Client/Networking/Networking.cs +++ b/RageCoop.Client/Networking/Networking.cs @@ -16,7 +16,7 @@ namespace RageCoop.Client public static Security Security; private static readonly Dictionary> PendingResponses = new Dictionary>(); internal static readonly Dictionary> RequestHandlers = new Dictionary>(); - + public static bool IsConnecting { get; private set; } static Networking() { Security=new Security(Main.Logger); @@ -46,8 +46,14 @@ namespace RageCoop.Client { Client.Disconnect("Bye!"); } + else if (IsConnecting) { + _publicKeyReceived.Set(); + IsConnecting = false; + GTA.UI.Notification.Show("Connection has been canceled"); + } else { + IsConnecting = true; password = password ?? Main.Settings.Password; username=username ?? Main.Settings.Username; // 623c92c287cc392406e7aaaac1c0f3b0 = RAGECOOP @@ -111,7 +117,7 @@ namespace RageCoop.Client Main.Logger.Error("Cannot connect to server: ", ex); Main.QueueAction(() => GTA.UI.Notification.Show("Cannot connect to server: "+ex.Message)); } - + IsConnecting=false; }); } } @@ -148,11 +154,12 @@ namespace RageCoop.Client private static bool GetServerPublicKey(string address, int timeout = 10000) { + Security.ServerRSA=null; var msg = Client.CreateMessage(); new Packets.PublicKeyRequest().Pack(msg); var adds = address.Split(':'); Client.SendUnconnectedMessage(msg, adds[0], int.Parse(adds[1])); - return _publicKeyReceived.WaitOne(timeout); + return _publicKeyReceived.WaitOne(timeout) && Security.ServerRSA!=null; } public static void GetResponse(Packet request, Action callback, ConnectionChannel channel = ConnectionChannel.RequestResponse) where T : Packet, new() diff --git a/RageCoop.Client/Scripting/API.cs b/RageCoop.Client/Scripting/API.cs index d0f5ce3..3e4a67c 100644 --- a/RageCoop.Client/Scripting/API.cs +++ b/RageCoop.Client/Scripting/API.cs @@ -165,6 +165,16 @@ namespace RageCoop.Client.Scripting get { return Main.LocalPlayerID; } } + /// + /// Check if player is connected to a server + /// + public static bool IsOnServer { get { return Networking.IsOnServer; } } + + /// + /// Get an that the player is currently connected to, or null if not connected to the server + /// + public static System.Net.IPEndPoint ServerEndPoint { get { return Networking.Client?.ServerConnection.RemoteEndPoint; } } + /// /// Check if a RAGECOOP menu is visible /// @@ -213,6 +223,29 @@ namespace RageCoop.Client.Scripting #region FUNCTIONS /// + /// Connect to a server + /// + /// Address of the server, e.g. 127.0.0.1:4499 + /// When a connection is active or being established + public static void Connect(string address) + { + if (Networking.IsOnServer || Networking.IsConnecting) + { + throw new InvalidOperationException("Cannot connect to server when another connection is active"); + } + Networking.ToggleConnection(address); + } + /// + /// Disconnect from current server or cancel the connection attempt. + /// + public static void Disconnect() + { + if (Networking.IsOnServer || Networking.IsConnecting) + { + Networking.ToggleConnection(null); + } + } + /// /// Send a local chat message to this player /// /// Name of the sender @@ -234,18 +267,11 @@ namespace RageCoop.Client.Scripting /// /// Queue an action to be executed on next tick, allowing you to call scripting API from another thread. /// - /// An action to be executed with a return value indicating whether the action can be removed after execution. + /// An to be executed with a return value indicating whether it can be removed after execution. public static void QueueAction(Func a) { Main.QueueAction(a); } - /// - /// Disconnect from the server - /// - public static void Disconnect() - { - Networking.ToggleConnection(null); - } /// /// Send an event and data to the server.