SanAndreasUnity/Assets/Scripts/Networking/NetStatus.cs

110 lines
2.2 KiB
C#
Raw Normal View History

2019-04-23 14:54:18 +00:00
using Mirror;
namespace SanAndreasUnity.Net
{
public enum NetworkClientStatus
{
Disconnected = 0,
Connecting,
Connected
}
public enum NetworkServerStatus
{
Started = 1,
Starting = 2,
Stopped = 3
}
public class NetStatus
{
private static bool IsNetworkClientConnecting {
get {
return NetworkClient.active && !NetworkClient.isConnected;
}
}
public static NetworkClientStatus clientStatus {
get {
if (NetworkClient.isConnected)
return NetworkClientStatus.Connected;
if (IsNetworkClientConnecting)
return NetworkClientStatus.Connecting;
return NetworkClientStatus.Disconnected;
}
}
public static NetworkServerStatus serverStatus {
get {
if (!NetworkServer.active)
return NetworkServerStatus.Stopped;
return NetworkServerStatus.Started;
}
}
2019-04-24 14:58:13 +00:00
public static bool IsServerStarted => NetStatus.serverStatus == NetworkServerStatus.Started;
2019-04-23 14:54:18 +00:00
/// <summary>
/// Is server active ?
/// </summary>
2019-04-24 14:58:13 +00:00
public static bool IsServer => NetStatus.IsServerStarted;
2019-04-23 14:54:18 +00:00
/// <summary>
/// Is host active ?
/// </summary>
public static bool IsHost() {
2019-04-24 14:58:13 +00:00
if (!NetStatus.IsServer)
2019-04-23 14:54:18 +00:00
return false;
return NetworkServer.localClientActive;
}
public static bool IsClientConnected() {
return clientStatus == NetworkClientStatus.Connected;
}
public static bool IsClientConnecting() {
return clientStatus == NetworkClientStatus.Connecting;
}
public static bool IsClientDisconnected() {
return clientStatus == NetworkClientStatus.Disconnected;
}
/// <summary>
/// Is client connected ?
/// TODO: This method should be corrected to return: is client active.
/// </summary>
public static bool IsClient() {
return NetStatus.IsClientConnected();
}
public static bool IsClientActive() {
return ! NetStatus.IsClientDisconnected ();
}
2019-04-24 14:58:13 +00:00
/// <summary>
/// Throws exception if server is not active.
/// </summary>
public static void ThrowIfNotOnServer()
{
if (!NetStatus.IsServer)
throw new System.Exception("Not on a server");
}
2019-04-23 14:54:18 +00:00
}
}