Add some client API back

This commit is contained in:
Sardelka
2022-07-31 20:47:04 +08:00
parent ad5ffc22ac
commit e2fce3cf9b
2 changed files with 44 additions and 11 deletions

View File

@ -16,7 +16,7 @@ namespace RageCoop.Client
public static Security Security;
private static readonly Dictionary<int, Action<PacketType, byte[]>> PendingResponses = new Dictionary<int, Action<PacketType, byte[]>>();
internal static readonly Dictionary<PacketType, Func<byte[], Packet>> RequestHandlers = new Dictionary<PacketType, Func<byte[], Packet>>();
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<T>(Packet request, Action<T> callback, ConnectionChannel channel = ConnectionChannel.RequestResponse) where T : Packet, new()

View File

@ -165,6 +165,16 @@ namespace RageCoop.Client.Scripting
get { return Main.LocalPlayerID; }
}
/// <summary>
/// Check if player is connected to a server
/// </summary>
public static bool IsOnServer { get { return Networking.IsOnServer; } }
/// <summary>
/// Get an <see cref="System.Net.IPEndPoint"/> that the player is currently connected to, or null if not connected to the server
/// </summary>
public static System.Net.IPEndPoint ServerEndPoint { get { return Networking.Client?.ServerConnection.RemoteEndPoint; } }
/// <summary>
/// Check if a RAGECOOP menu is visible
/// </summary>
@ -213,6 +223,29 @@ namespace RageCoop.Client.Scripting
#region FUNCTIONS
/// <summary>
/// Connect to a server
/// </summary>
/// <param name="address">Address of the server, e.g. 127.0.0.1:4499</param>
/// <exception cref="InvalidOperationException">When a connection is active or being established</exception>
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);
}
/// <summary>
/// Disconnect from current server or cancel the connection attempt.
/// </summary>
public static void Disconnect()
{
if (Networking.IsOnServer || Networking.IsConnecting)
{
Networking.ToggleConnection(null);
}
}
/// <summary>
/// Send a local chat message to this player
/// </summary>
/// <param name="from">Name of the sender</param>
@ -234,18 +267,11 @@ namespace RageCoop.Client.Scripting
/// <summary>
/// Queue an action to be executed on next tick, allowing you to call scripting API from another thread.
/// </summary>
/// <param name="a"> An action to be executed with a return value indicating whether the action can be removed after execution.</param>
/// <param name="a"> An <see cref="Func{T, TResult}"/> to be executed with a return value indicating whether it can be removed after execution.</param>
public static void QueueAction(Func<bool> a)
{
Main.QueueAction(a);
}
/// <summary>
/// Disconnect from the server
/// </summary>
public static void Disconnect()
{
Networking.ToggleConnection(null);
}
/// <summary>
/// Send an event and data to the server.