diff --git a/Assets/Scripts/Networking/Client.cs b/Assets/Scripts/Networking/Client.cs index 74559e9..8201b4b 100644 --- a/Assets/Scripts/Networking/Client.cs +++ b/Assets/Scripts/Networking/Client.cs @@ -9,25 +9,66 @@ using UnityEngine.Networking; /// public class Client : MonoBehaviour { - NetworkClient NetClient; + private NetworkClient NetClient; private bool Running = false; private static Client Singleton; + /// + /// Creates the client and sets it as the signleton. + /// public Client() { Singleton = this; } - // Use this for initialization - void Start () { + // Static interface for usage outside of Client + /// + /// Will launch the client and attempt to connect to the server. + /// Returns false if client is already running, otherwise true. + /// + /// Generally instead of this you should use + /// + /// + /// + /// Weather the launch was successful or not. + public static bool Launch(string ip, int port) { + return Singleton.LaunchClient(ip, port); } - - // Update is called once per frame - void Update () { - } - // Launches the client at given IP and port. + /// + /// Send messages to the server if the connection is active. + /// If client is not active, this will return false, otherwise true. + /// + /// + /// + /// Weather the send was successful or not. + public static bool Send(short msgType, MessageBase message) { + if (Singleton.Running) { + Singleton.NetClient.Send(msgType, message); + return true; + } + else { + return false; + } + } + + /// + /// Returns if the client is running or not. + /// This is independant weather the client is connected or not. + /// + /// Weather the client is running or not + public static bool IsRunning() { + return Singleton.Running; + } + + /// + /// Returns if the client is connected or not. + /// + /// Weather the client is connected or not. + public static bool IsConnected() { + return Singleton.NetClient.isConnected; + } private bool LaunchClient(string ip, int port) { if (Running) { @@ -58,8 +99,6 @@ public class Client : MonoBehaviour { return true; } - // Handles custom packets - private void HandlePacket(NetworkMessage msg) { switch (msg.msgType) { case (PktType.TextMessage): @@ -98,51 +137,5 @@ public class Client : MonoBehaviour { NetClient.Disconnect(); Running = false; } - - // Static interface for usage outside of Client - - /// - /// Will launch the client and attempt to connect to the server. - /// Returns false if client is already running, otherwise true. - /// - /// - /// - /// - public static bool Launch(string ip, int port) { - return Singleton.LaunchClient(ip, port); - } - - /// - /// Send messages to the server if the connection is active. - /// If client is not active, this will return false, otherwise true. - /// - /// - /// - /// - public static bool Send(short msgType, MessageBase message) { - if (Singleton.Running) { - Singleton.NetClient.Send(msgType, message); - return true; - } else { - return false; - } - } - - /// - /// Returns if the client is running or not. - /// This is independant weather the client is connected or not. - /// - /// - public static bool isRunning() { - return Singleton.Running; - } - - /// - /// Returns if the client is connected or not. - /// - /// - public static bool isConnected() { - return Singleton.NetClient.isConnected; - } } diff --git a/Assets/Scripts/Networking/Messages/TextMessage.cs b/Assets/Scripts/Networking/Messages/TextMessage.cs index de12e74..33be2f1 100644 --- a/Assets/Scripts/Networking/Messages/TextMessage.cs +++ b/Assets/Scripts/Networking/Messages/TextMessage.cs @@ -14,7 +14,7 @@ public class TextMessage : MessageBase { /// /// Create a TextMessage containing the message to be sent. /// - /// + /// Message to be sent. public TextMessage(string message) { this.Message = message; } diff --git a/Assets/Scripts/Networking/NetworkEstablisher.cs b/Assets/Scripts/Networking/NetworkEstablisher.cs index 54bbce2..2b4dddc 100644 --- a/Assets/Scripts/Networking/NetworkEstablisher.cs +++ b/Assets/Scripts/Networking/NetworkEstablisher.cs @@ -10,14 +10,26 @@ using UnityEngine.UI; * */ public class NetworkEstablisher : MonoBehaviour { + /// + /// Input field for the IP to which connect to. + /// [Tooltip("Required field only if StartClient() is used.")] public InputField IPField; + /// + /// Input field for the client port + /// [Tooltip("Required field only if StartClient() is used.")] public InputField ClientPortField; + /// + /// Input field for the server port + /// [Tooltip("Required field only if StartServer() is used.")] public InputField ServerPortField; + /// + /// World Root node, a GameObject. + /// public GameObject WorldRoot; // Use this for initialization @@ -45,12 +57,10 @@ public class NetworkEstablisher : MonoBehaviour { StartServer(port); }); } - - // Update is called once per frame - void Update () { - - } + /// + /// Starts the client using given input fields. Otherwise functions like . + /// public void StartClient() { string IP = IPField.text; if (IP.Length == 0) { @@ -64,11 +74,20 @@ public class NetworkEstablisher : MonoBehaviour { StartClient(IP, Port); } + /// + /// Starts the client with the given ip and port. + /// This initializes the Client component and launches it properly. + /// + /// IP used to connect. + /// port of the host. public void StartClient(string ip, int port) { WorldRoot.AddComponent(); Client.Launch(ip, port); } + /// + /// Starts the server using given input fields. Otherwise functions like . + /// public void StartServer() { string PortText = ServerPortField.text; int Port = 3935; @@ -78,6 +97,11 @@ public class NetworkEstablisher : MonoBehaviour { StartServer(Port); } + /// + /// Starts the server using given port. + /// This initializes the port and launches the server properly. + /// + /// port used for the server. public void StartServer(int port) { WorldRoot.AddComponent(); Server.Launch(port); diff --git a/Assets/Scripts/Networking/Server.cs b/Assets/Scripts/Networking/Server.cs index 15622ed..6e2de6b 100644 --- a/Assets/Scripts/Networking/Server.cs +++ b/Assets/Scripts/Networking/Server.cs @@ -6,16 +6,70 @@ using UnityEngine.Networking; public class Server : MonoBehaviour { private static Server Singleton; - - // Use this for initialization - void Start () { + + /// + /// Creates the server-component, and sets the singleton as itself. + /// + public Server() { Singleton = this; - } - - // Update is called once per frame - void Update () { - - } + } + + // Static methods for public usage + + /// + /// Launches the server if not already launched. + /// Returns false if the server was already launched, true otherwise. + /// + /// Generally instead of this you should use + /// + /// Port used to host the server. + /// Weather the launch was successful. + public static bool Launch(int port) { + return Singleton.LaunchServer(port); + } + + /// + /// Attempts to send a message to all clients who are listening. + /// Returns false if server wasn't active, true otherwise. + /// + /// Type of the message being sent. + /// The message being sent. + /// Weather sending was successful. + public static bool SendToAll(short msgType, MessageBase message) { + if (NetworkServer.active) { + NetworkServer.SendToAll(msgType, message); + return true; + } + else { + return false; + } + } + + /// + /// Attempts to send a message to a specific client. + /// Returns false if server wasn't active, true otherwise. + /// + /// ID of the client which to send this message. + /// Type of message being sent. + /// The message being sent. + /// Weather sending was successful. + public static bool Send(int clientID, short msgType, MessageBase message) { + if (NetworkServer.active) { + NetworkServer.SendToClient(clientID, msgType, message); + return true; + } + else { + return false; + } + } + + /// + /// Is the server currently active. + /// + /// Weather the server is running or not + public static bool IsRunning() { + return NetworkServer.active; + } private bool LaunchServer(int port) { if (NetworkServer.active) { @@ -46,8 +100,6 @@ public class Server : MonoBehaviour { return true; } - // Custon packet handler - private void HandlePacket(NetworkMessage msg) { switch(msg.msgType) { @@ -79,57 +131,4 @@ public class Server : MonoBehaviour { Debug.LogError("Encountered a network error on server"); Term.Println("Encountered a network error on server"); } - - // Static methods for public usage - - /// - /// Launches the server if not already launched. - /// Returns false if the server was already launched, true otherwise. - /// - /// - /// - public static bool Launch(int port) { - return Singleton.LaunchServer(port); - } - - /// - /// Attempts to send a message to all clients who are listening. - /// Returns false if server wasn't active, true otherwise. - /// - /// - /// - /// - public static bool SendToAll(short msgType, MessageBase message) { - if (NetworkServer.active) { - NetworkServer.SendToAll(msgType, message); - return true; - } else { - return false; - } - } - - /// - /// Attempts to send a message to a specific client. - /// Returns false if server wasn't active, true otherwise. - /// - /// - /// - /// - /// - public static bool Send(int clientID, short msgType, MessageBase message) { - if (NetworkServer.active) { - NetworkServer.SendToClient(clientID, msgType, message); - return true; - } else { - return false; - } - } - - /// - /// Is the server currently active. - /// - /// - public static bool isRunning() { - return NetworkServer.active; - } }