diff --git a/Assets/Scripts/Networking/Client.cs b/Assets/Scripts/Networking/Client.cs
index 8201f36..74559e9 100644
--- a/Assets/Scripts/Networking/Client.cs
+++ b/Assets/Scripts/Networking/Client.cs
@@ -3,24 +3,35 @@ using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
+///
+/// Client-class used to connecting to a server and communicating to it.
+/// Also handles all events incoming from the server and forwards them where they should be handled.
+///
public class Client : MonoBehaviour {
NetworkClient NetClient;
private bool Running = false;
+ private static Client Singleton;
+
+ public Client() {
+ Singleton = this;
+ }
+
// Use this for initialization
void Start () {
+
}
// Update is called once per frame
void Update () {
- if (NetClient.isConnected) {
- }
}
- public void LaunchClient(string ip, int port) {
+ // Launches the client at given IP and port.
+
+ private bool LaunchClient(string ip, int port) {
if (Running) {
- return;
+ return false;
}
ConnectionConfig Config = new ConnectionConfig();
@@ -43,9 +54,13 @@ public class Client : MonoBehaviour {
Debug.Log("Client launched!");
Term.Println("Client launched!");
+
+ return true;
}
- public void HandlePacket(NetworkMessage msg) {
+ // Handles custom packets
+
+ private void HandlePacket(NetworkMessage msg) {
switch (msg.msgType) {
case (PktType.TextMessage):
TextMessage TextMsg = new TextMessage();
@@ -59,29 +74,75 @@ public class Client : MonoBehaviour {
}
}
- public void Send(short type, MessageBase message) {
- NetClient.Send(type, message);
- }
-
- public void OnConnected(NetworkMessage msg) {
+ // Built-in handles for some events
+
+ private void OnConnected(NetworkMessage msg) {
Debug.Log("Connected!");
Term.Println("Connected!");
Term.AddCommand("send (message)", "Send a message across the vastness of space and time!", (args) => {
Term.Println("You: " + args[0]);
- Send(PktType.TextMessage, new TextMessage("A Client: " + args[0]));
+ NetClient.Send(PktType.TextMessage, new TextMessage("A Client: " + args[0]));
});
}
- public void OnDisconnected(NetworkMessage msg) {
+ private void OnDisconnected(NetworkMessage msg) {
Debug.Log("Disconnected!");
Term.Println("Disconnected!");
+ Running = false;
}
- public void OnError(NetworkMessage msg) {
+ private void OnError(NetworkMessage msg) {
Debug.LogError("Encountered a network error. Shutting down.");
Term.Println("Encountered a network error. Shutting down.");
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 238f60e..de12e74 100644
--- a/Assets/Scripts/Networking/Messages/TextMessage.cs
+++ b/Assets/Scripts/Networking/Messages/TextMessage.cs
@@ -1,20 +1,42 @@
using UnityEngine.Networking;
+///
+/// Generic Text Message for chat etc.
+/// To be removed later when no longer necessary.
+///
public class TextMessage : MessageBase {
+ ///
+ /// Message inside the Text Message. Does not container sender information.
+ ///
public string Message;
+ ///
+ /// Create a TextMessage containing the message to be sent.
+ ///
+ ///
public TextMessage(string message) {
this.Message = message;
}
+ ///
+ /// Parameter-less constructor using when deserializing the message.
+ ///
public TextMessage() {
}
+ ///
+ /// Used to deserialize a message received via networking.
+ ///
+ ///
public override void Deserialize(NetworkReader reader) {
Message = reader.ReadString();
}
+ ///
+ /// Used to serialize the message before it is sent.
+ ///
+ ///
public override void Serialize(NetworkWriter writer) {
writer.Write(Message);
}
diff --git a/Assets/Scripts/Networking/NetworkEstablisher.cs b/Assets/Scripts/Networking/NetworkEstablisher.cs
index de5ab59..54bbce2 100644
--- a/Assets/Scripts/Networking/NetworkEstablisher.cs
+++ b/Assets/Scripts/Networking/NetworkEstablisher.cs
@@ -65,8 +65,8 @@ public class NetworkEstablisher : MonoBehaviour {
}
public void StartClient(string ip, int port) {
- Client Client = WorldRoot.AddComponent();
- Client.LaunchClient(ip, port);
+ WorldRoot.AddComponent();
+ Client.Launch(ip, port);
}
public void StartServer() {
@@ -79,7 +79,7 @@ public class NetworkEstablisher : MonoBehaviour {
}
public void StartServer(int port) {
- Server Server = WorldRoot.AddComponent();
- Server.LaunchServer(port);
+ WorldRoot.AddComponent();
+ Server.Launch(port);
}
}
diff --git a/Assets/Scripts/Networking/PktType.cs b/Assets/Scripts/Networking/PktType.cs
index c993494..92f010d 100644
--- a/Assets/Scripts/Networking/PktType.cs
+++ b/Assets/Scripts/Networking/PktType.cs
@@ -2,6 +2,9 @@
using System.Collections.Generic;
using UnityEngine;
+///
+/// Types of custom created packets.
+///
public class PktType {
public const short TextMessage = 200;
diff --git a/Assets/Scripts/Networking/Server.cs b/Assets/Scripts/Networking/Server.cs
index 50bb914..15622ed 100644
--- a/Assets/Scripts/Networking/Server.cs
+++ b/Assets/Scripts/Networking/Server.cs
@@ -4,9 +4,12 @@ using UnityEngine;
using UnityEngine.Networking;
public class Server : MonoBehaviour {
+
+ private static Server Singleton;
// Use this for initialization
void Start () {
+ Singleton = this;
}
// Update is called once per frame
@@ -14,7 +17,10 @@ public class Server : MonoBehaviour {
}
- public void LaunchServer(int port) {
+ private bool LaunchServer(int port) {
+ if (NetworkServer.active) {
+ return false;
+ }
ConnectionConfig Config = new ConnectionConfig();
Config.AddChannel(QosType.ReliableSequenced);
@@ -36,9 +42,13 @@ public class Server : MonoBehaviour {
Term.Println("You: " + args[0]);
SendToAll(PktType.TextMessage, new TextMessage("Server: " + args[0]));
});
+
+ return true;
}
- public void HandlePacket(NetworkMessage msg) {
+ // Custon packet handler
+
+ private void HandlePacket(NetworkMessage msg) {
switch(msg.msgType) {
case PktType.TextMessage:
@@ -53,22 +63,73 @@ public class Server : MonoBehaviour {
}
}
- public void SendToAll(short type, MessageBase message) {
- NetworkServer.SendToAll(type, message);
- }
+ // Internal built-in event handler
- public void OnConnected(NetworkMessage msg) {
+ private void OnConnected(NetworkMessage msg) {
Debug.Log("Someone connected!");
Term.Println("Someone connected!");
}
- public void OnDisconnected(NetworkMessage msg) {
+ private void OnDisconnected(NetworkMessage msg) {
Debug.Log("Someone disconnected.");
Term.Println("Someone disconnected.");
}
- public void OnError(NetworkMessage msg) {
+ private void OnError(NetworkMessage msg) {
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;
+ }
}