Working on NetManager

This commit is contained in:
in0finite 2019-04-23 15:49:18 +02:00
parent 8e242cd190
commit 0979cabd51
4 changed files with 166 additions and 4 deletions

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7887d71e3778f758daa141a82b7324e9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,147 @@
using UnityEngine;
using Mirror;
namespace SanAndreasUnity.Net
{
public class NetManager
{
public static int defaultListenPortNumber { get { return 7777; } }
public static int listenPortNumber { get { return NetworkServer.listenPort; } }
public static string onlineScene {
get {
return NetworkManager.singleton.onlineScene;
}
set {
NetworkManager.singleton.onlineScene = value;
}
}
public static void StartServer( int portNumber ) {
CheckIfNetworkIsStarted ();
CheckIfPortIsValid (portNumber);
CheckIfOnlineSceneIsAssigned ();
SetupNetworkManger( "", portNumber );
NetworkManager.singleton.StartServer ();
}
public static void StartHost( int portNumber ) {
CheckIfNetworkIsStarted ();
CheckIfPortIsValid (portNumber);
CheckIfOnlineSceneIsAssigned ();
SetupNetworkManger( "", portNumber );
NetworkManager.singleton.StartHost ();
}
public static void StopServer() {
NetworkManager.singleton.StopServer ();
}
public static void StopHost() {
NetworkManager.singleton.StopHost ();
}
public static void StartClient( string ip, int serverPortNumber ) {
CheckIfNetworkIsStarted ();
CheckIfIPAddressIsValid (ip);
CheckIfPortIsValid (serverPortNumber);
SetupNetworkManger( ip, serverPortNumber );
NetworkManager.singleton.StartClient ();
}
public static void StopClient() {
NetworkManager.singleton.StopClient ();
}
/// <summary>
/// Stops both server and client.
/// </summary>
public static void StopNetwork() {
// NetworkManager.singleton.StopHost ();
NetworkManager.singleton.StopServer ();
NetworkManager.singleton.StopClient ();
}
public static void CheckIfServerIsStarted() {
if (NetworkStatus.IsServerStarted ())
throw new System.Exception ("Server already started");
}
public static void CheckIfClientIsStarted() {
if (!NetworkStatus.IsClientDisconnected ())
throw new System.Exception ("Client already started");
}
public static void CheckIfNetworkIsStarted() {
CheckIfServerIsStarted ();
CheckIfClientIsStarted ();
}
public static void CheckIfPortIsValid( int portNumber ) {
if (portNumber < 1 || portNumber > 65535)
throw new System.ArgumentOutOfRangeException ( "portNumber", "Invalid port number");
}
private static void CheckIfIPAddressIsValid( string ip ) {
if (string.IsNullOrEmpty (ip))
throw new System.ArgumentException ("IP address empty");
// System.Net.IPAddress.Parse ();
}
private static void CheckIfOnlineSceneIsAssigned() {
if (string.IsNullOrEmpty (NetManager.onlineScene))
throw new System.Exception ("Online scene is not assigned");
}
private static void SetupNetworkManger( string ip, int port ) {
NetworkManager.singleton.networkAddress = ip;
NetworkManager.singleton.networkPort = port;
}
public static NetworkClient GetClient() {
return NetworkManager.singleton.client;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a24545a2cc7f754199eaf4a9102739c5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -8,12 +8,8 @@
- killing local ped
- update Unity to 2018.3
- scene changing: When network (server/client) is stopped, offline scene should be loaded. But, when switching back to online scene, Loader should not load everything again. Instead, only Cell loading should be done, if the new scene is main scene. But, are old meshes/textures destroyed ? Do we leave memory behind, every time when network is stopped ? Or... just exit the game when network is stopped (display message box first ?).
- add Mirror as git submodule - submodule will not point to original repo, but to a separate repo which contains content from unitypackage (with websockets removed ?)
# Potential problems