Update Networking docs and put publics on top

This commit is contained in:
Sofia 2017-05-08 23:25:53 +03:00
parent ec0d54bb2f
commit 56ff2536b9
4 changed files with 142 additions and 126 deletions

View File

@ -9,25 +9,66 @@ using UnityEngine.Networking;
/// </summary> /// </summary>
public class Client : MonoBehaviour { public class Client : MonoBehaviour {
NetworkClient NetClient; private NetworkClient NetClient;
private bool Running = false; private bool Running = false;
private static Client Singleton; private static Client Singleton;
/// <summary>
/// Creates the client and sets it as the signleton.
/// </summary>
public Client() { public Client() {
Singleton = this; Singleton = this;
} }
// Use this for initialization // Static interface for usage outside of Client
void Start () {
/// <summary>
/// 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 <see cref="NetworkEstablisher.StartClient(string, int)"/>
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns>Weather the launch was successful or not.</returns>
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. /// <summary>
/// Send messages to the server if the connection is active.
/// If client is not active, this will return false, otherwise true.
/// </summary>
/// <param name="msgType"></param>
/// <param name="message"></param>
/// <returns>Weather the send was successful or not.</returns>
public static bool Send(short msgType, MessageBase message) {
if (Singleton.Running) {
Singleton.NetClient.Send(msgType, message);
return true;
}
else {
return false;
}
}
/// <summary>
/// Returns if the client is running or not.
/// This is independant weather the client is connected or not.
/// </summary>
/// <returns>Weather the client is running or not</returns>
public static bool IsRunning() {
return Singleton.Running;
}
/// <summary>
/// Returns if the client is connected or not.
/// </summary>
/// <returns>Weather the client is connected or not.</returns>
public static bool IsConnected() {
return Singleton.NetClient.isConnected;
}
private bool LaunchClient(string ip, int port) { private bool LaunchClient(string ip, int port) {
if (Running) { if (Running) {
@ -58,8 +99,6 @@ public class Client : MonoBehaviour {
return true; return true;
} }
// Handles custom packets
private void HandlePacket(NetworkMessage msg) { private void HandlePacket(NetworkMessage msg) {
switch (msg.msgType) { switch (msg.msgType) {
case (PktType.TextMessage): case (PktType.TextMessage):
@ -98,51 +137,5 @@ public class Client : MonoBehaviour {
NetClient.Disconnect(); NetClient.Disconnect();
Running = false; Running = false;
} }
// Static interface for usage outside of Client
/// <summary>
/// Will launch the client and attempt to connect to the server.
/// Returns false if client is already running, otherwise true.
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
public static bool Launch(string ip, int port) {
return Singleton.LaunchClient(ip, port);
}
/// <summary>
/// Send messages to the server if the connection is active.
/// If client is not active, this will return false, otherwise true.
/// </summary>
/// <param name="msgType"></param>
/// <param name="message"></param>
/// <returns></returns>
public static bool Send(short msgType, MessageBase message) {
if (Singleton.Running) {
Singleton.NetClient.Send(msgType, message);
return true;
} else {
return false;
}
}
/// <summary>
/// Returns if the client is running or not.
/// This is independant weather the client is connected or not.
/// </summary>
/// <returns></returns>
public static bool isRunning() {
return Singleton.Running;
}
/// <summary>
/// Returns if the client is connected or not.
/// </summary>
/// <returns></returns>
public static bool isConnected() {
return Singleton.NetClient.isConnected;
}
} }

View File

@ -14,7 +14,7 @@ public class TextMessage : MessageBase {
/// <summary> /// <summary>
/// Create a TextMessage containing the message to be sent. /// Create a TextMessage containing the message to be sent.
/// </summary> /// </summary>
/// <param name="message"></param> /// <param name="message">Message to be sent.</param>
public TextMessage(string message) { public TextMessage(string message) {
this.Message = message; this.Message = message;
} }

View File

@ -10,14 +10,26 @@ using UnityEngine.UI;
* */ * */
public class NetworkEstablisher : MonoBehaviour { public class NetworkEstablisher : MonoBehaviour {
/// <summary>
/// Input field for the IP to which connect to.
/// </summary>
[Tooltip("Required field only if StartClient() is used.")] [Tooltip("Required field only if StartClient() is used.")]
public InputField IPField; public InputField IPField;
/// <summary>
/// Input field for the client port
/// </summary>
[Tooltip("Required field only if StartClient() is used.")] [Tooltip("Required field only if StartClient() is used.")]
public InputField ClientPortField; public InputField ClientPortField;
/// <summary>
/// Input field for the server port
/// </summary>
[Tooltip("Required field only if StartServer() is used.")] [Tooltip("Required field only if StartServer() is used.")]
public InputField ServerPortField; public InputField ServerPortField;
/// <summary>
/// World Root node, a GameObject.
/// </summary>
public GameObject WorldRoot; public GameObject WorldRoot;
// Use this for initialization // Use this for initialization
@ -45,12 +57,10 @@ public class NetworkEstablisher : MonoBehaviour {
StartServer(port); StartServer(port);
}); });
} }
// Update is called once per frame
void Update () {
}
/// <summary>
/// Starts the client using given input fields. Otherwise functions like <see cref="StartClient(string, int)"/>.
/// </summary>
public void StartClient() { public void StartClient() {
string IP = IPField.text; string IP = IPField.text;
if (IP.Length == 0) { if (IP.Length == 0) {
@ -64,11 +74,20 @@ public class NetworkEstablisher : MonoBehaviour {
StartClient(IP, Port); StartClient(IP, Port);
} }
/// <summary>
/// Starts the client with the given ip and port.
/// This initializes the Client component and launches it properly.
/// </summary>
/// <param name="ip">IP used to connect.</param>
/// <param name="port">port of the host.</param>
public void StartClient(string ip, int port) { public void StartClient(string ip, int port) {
WorldRoot.AddComponent<Client>(); WorldRoot.AddComponent<Client>();
Client.Launch(ip, port); Client.Launch(ip, port);
} }
/// <summary>
/// Starts the server using given input fields. Otherwise functions like <see cref="StartServer(int)"/>.
/// </summary>
public void StartServer() { public void StartServer() {
string PortText = ServerPortField.text; string PortText = ServerPortField.text;
int Port = 3935; int Port = 3935;
@ -78,6 +97,11 @@ public class NetworkEstablisher : MonoBehaviour {
StartServer(Port); StartServer(Port);
} }
/// <summary>
/// Starts the server using given port.
/// This initializes the port and launches the server properly.
/// </summary>
/// <param name="port">port used for the server.</param>
public void StartServer(int port) { public void StartServer(int port) {
WorldRoot.AddComponent<Server>(); WorldRoot.AddComponent<Server>();
Server.Launch(port); Server.Launch(port);

View File

@ -6,16 +6,70 @@ using UnityEngine.Networking;
public class Server : MonoBehaviour { public class Server : MonoBehaviour {
private static Server Singleton; private static Server Singleton;
// Use this for initialization /// <summary>
void Start () { /// Creates the server-component, and sets the singleton as itself.
/// </summary>
public Server() {
Singleton = this; Singleton = this;
} }
// Update is called once per frame // Static methods for public usage
void Update () {
/// <summary>
} /// Launches the server if not already launched.
/// Returns false if the server was already launched, true otherwise.
///
/// Generally instead of this you should use <see cref="NetworkEstablisher.StartServer(int)"/>
/// </summary>
/// <param name="port">Port used to host the server.</param>
/// <returns>Weather the launch was successful.</returns>
public static bool Launch(int port) {
return Singleton.LaunchServer(port);
}
/// <summary>
/// Attempts to send a message to all clients who are listening.
/// Returns false if server wasn't active, true otherwise.
/// </summary>
/// <param name="msgType">Type of the message being sent.</param>
/// <param name="message">The message being sent.</param>
/// <returns>Weather sending was successful.</returns>
public static bool SendToAll(short msgType, MessageBase message) {
if (NetworkServer.active) {
NetworkServer.SendToAll(msgType, message);
return true;
}
else {
return false;
}
}
/// <summary>
/// Attempts to send a message to a specific client.
/// Returns false if server wasn't active, true otherwise.
/// </summary>
/// <param name="clientID">ID of the client which to send this message.</param>
/// <param name="msgType">Type of message being sent.</param>
/// <param name="message">The message being sent.</param>
/// <returns>Weather sending was successful.</returns>
public static bool Send(int clientID, short msgType, MessageBase message) {
if (NetworkServer.active) {
NetworkServer.SendToClient(clientID, msgType, message);
return true;
}
else {
return false;
}
}
/// <summary>
/// Is the server currently active.
/// </summary>
/// <returns>Weather the server is running or not</returns>
public static bool IsRunning() {
return NetworkServer.active;
}
private bool LaunchServer(int port) { private bool LaunchServer(int port) {
if (NetworkServer.active) { if (NetworkServer.active) {
@ -46,8 +100,6 @@ public class Server : MonoBehaviour {
return true; return true;
} }
// Custon packet handler
private void HandlePacket(NetworkMessage msg) { private void HandlePacket(NetworkMessage msg) {
switch(msg.msgType) { switch(msg.msgType) {
@ -79,57 +131,4 @@ public class Server : MonoBehaviour {
Debug.LogError("Encountered a network error on server"); Debug.LogError("Encountered a network error on server");
Term.Println("Encountered a network error on server"); Term.Println("Encountered a network error on server");
} }
// Static methods for public usage
/// <summary>
/// Launches the server if not already launched.
/// Returns false if the server was already launched, true otherwise.
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
public static bool Launch(int port) {
return Singleton.LaunchServer(port);
}
/// <summary>
/// Attempts to send a message to all clients who are listening.
/// Returns false if server wasn't active, true otherwise.
/// </summary>
/// <param name="msgType"></param>
/// <param name="message"></param>
/// <returns></returns>
public static bool SendToAll(short msgType, MessageBase message) {
if (NetworkServer.active) {
NetworkServer.SendToAll(msgType, message);
return true;
} else {
return false;
}
}
/// <summary>
/// Attempts to send a message to a specific client.
/// Returns false if server wasn't active, true otherwise.
/// </summary>
/// <param name="clientID"></param>
/// <param name="msgType"></param>
/// <param name="message"></param>
/// <returns></returns>
public static bool Send(int clientID, short msgType, MessageBase message) {
if (NetworkServer.active) {
NetworkServer.SendToClient(clientID, msgType, message);
return true;
} else {
return false;
}
}
/// <summary>
/// Is the server currently active.
/// </summary>
/// <returns></returns>
public static bool isRunning() {
return NetworkServer.active;
}
} }