From 7038b4625126879230784d4c6376c67b512f503f Mon Sep 17 00:00:00 2001 From: excitedneon Date: Mon, 8 May 2017 03:59:42 +0300 Subject: [PATCH] Conventionalized networking scripts. --- Assets/Scripts/Networking/Client.cs | 36 ++++++++--------- .../Networking/Messages/TextMessage.cs | 8 ++-- .../Scripts/Networking/NetworkEstablisher.cs | 40 +++++++++---------- .../Networking/{MsgType.cs => PktType.cs} | 0 .../{MsgType.cs.meta => PktType.cs.meta} | 4 +- Assets/Scripts/Networking/Server.cs | 14 +++---- 6 files changed, 51 insertions(+), 51 deletions(-) rename Assets/Scripts/Networking/{MsgType.cs => PktType.cs} (100%) rename Assets/Scripts/Networking/{MsgType.cs.meta => PktType.cs.meta} (76%) diff --git a/Assets/Scripts/Networking/Client.cs b/Assets/Scripts/Networking/Client.cs index 1ba881c..ddb2575 100644 --- a/Assets/Scripts/Networking/Client.cs +++ b/Assets/Scripts/Networking/Client.cs @@ -5,8 +5,8 @@ using UnityEngine.Networking; public class Client : MonoBehaviour { - NetworkClient client; - private bool running = false; + NetworkClient NetClient; + private bool Running = false; // Use this for initialization void Start () { @@ -14,37 +14,37 @@ public class Client : MonoBehaviour { // Update is called once per frame void Update () { - if (client.isConnected) { + if (NetClient.isConnected) { } } public void LaunchClient(string ip, int port) { - if (running) { + if (Running) { return; } - ConnectionConfig config = new ConnectionConfig(); - config.AddChannel(QosType.ReliableSequenced); - config.AddChannel(QosType.UnreliableSequenced); - NetworkServer.Configure(config, 10); + ConnectionConfig Config = new ConnectionConfig(); + Config.AddChannel(QosType.ReliableSequenced); + Config.AddChannel(QosType.UnreliableSequenced); + NetworkServer.Configure(Config, 10); - client = new NetworkClient(); - client.Configure(config, 10); + NetClient = new NetworkClient(); + NetClient.Configure(Config, 10); - running = true; + Running = true; - client.RegisterHandler(MsgType.Connect, OnConnected); - client.RegisterHandler(MsgType.Disconnect, OnDisconnected); - client.RegisterHandler(MsgType.Error, OnError); + NetClient.RegisterHandler(MsgType.Connect, OnConnected); + NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected); + NetClient.RegisterHandler(MsgType.Error, OnError); - client.Connect(ip, port); + NetClient.Connect(ip, port); Debug.Log("Client launched!"); } public void OnConnected(NetworkMessage msg) { Debug.Log("Connected!"); - client.Send(PktType.TestMessage, new TextMessage("Hai, I connected!")); + NetClient.Send(PktType.TestMessage, new TextMessage("Hai, I connected!")); } public void OnDisconnected(NetworkMessage msg) { @@ -53,7 +53,7 @@ public class Client : MonoBehaviour { public void OnError(NetworkMessage msg) { Debug.LogError("Encountered a network error. Shutting down."); - client.Disconnect(); - running = false; + NetClient.Disconnect(); + Running = false; } } diff --git a/Assets/Scripts/Networking/Messages/TextMessage.cs b/Assets/Scripts/Networking/Messages/TextMessage.cs index 4c19f3f..238f60e 100644 --- a/Assets/Scripts/Networking/Messages/TextMessage.cs +++ b/Assets/Scripts/Networking/Messages/TextMessage.cs @@ -2,21 +2,21 @@ public class TextMessage : MessageBase { - public string message; + public string Message; public TextMessage(string message) { - this.message = message; + this.Message = message; } public TextMessage() { } public override void Deserialize(NetworkReader reader) { - message = reader.ReadString(); + Message = reader.ReadString(); } public override void Serialize(NetworkWriter writer) { - writer.Write(message); + writer.Write(Message); } } diff --git a/Assets/Scripts/Networking/NetworkEstablisher.cs b/Assets/Scripts/Networking/NetworkEstablisher.cs index 713a517..126f9af 100644 --- a/Assets/Scripts/Networking/NetworkEstablisher.cs +++ b/Assets/Scripts/Networking/NetworkEstablisher.cs @@ -13,12 +13,12 @@ public class NetworkEstablisher : MonoBehaviour { [Tooltip("Required field only if StartClient() is used.")] public InputField IPField; [Tooltip("Required field only if StartClient() is used.")] - public InputField clientPortField; + public InputField ClientPortField; [Tooltip("Required field only if StartServer() is used.")] - public InputField serverPortField; + public InputField ServerPortField; - public GameObject worldRoot; + public GameObject WorldRoot; // Use this for initialization void Start () { @@ -31,34 +31,34 @@ public class NetworkEstablisher : MonoBehaviour { } public void StartClient() { - string ip = IPField.text; - if (ip.Length == 0) { - ip = "localhost"; + string IP = IPField.text; + if (IP.Length == 0) { + IP = "localhost"; } - string portText = clientPortField.text; - int port = 3935; - if (portText.Length > 0) { - port = Int32.Parse(portText); + string PortText = ClientPortField.text; + int Port = 3935; + if (PortText.Length > 0) { + Port = Int32.Parse(PortText); } - StartClient(ip, port); + StartClient(IP, Port); } public void StartClient(string ip, int port) { - Client client = worldRoot.AddComponent(); - client.LaunchClient(ip, port); + Client Client = WorldRoot.AddComponent(); + Client.LaunchClient(ip, port); } public void StartServer() { - string portText = serverPortField.text; - int port = 3935; - if (portText.Length > 0) { - port = Int32.Parse(portText); + string PortText = ServerPortField.text; + int Port = 3935; + if (PortText.Length > 0) { + Port = Int32.Parse(PortText); } - StartServer(port); + StartServer(Port); } public void StartServer(int port) { - Server server = worldRoot.AddComponent(); - server.LaunchServer(port); + Server Server = WorldRoot.AddComponent(); + Server.LaunchServer(port); } } diff --git a/Assets/Scripts/Networking/MsgType.cs b/Assets/Scripts/Networking/PktType.cs similarity index 100% rename from Assets/Scripts/Networking/MsgType.cs rename to Assets/Scripts/Networking/PktType.cs diff --git a/Assets/Scripts/Networking/MsgType.cs.meta b/Assets/Scripts/Networking/PktType.cs.meta similarity index 76% rename from Assets/Scripts/Networking/MsgType.cs.meta rename to Assets/Scripts/Networking/PktType.cs.meta index 88c5720..5152377 100644 --- a/Assets/Scripts/Networking/MsgType.cs.meta +++ b/Assets/Scripts/Networking/PktType.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 430a933ca3f0fa849bc55f725fe9cffc -timeCreated: 1494190350 +guid: 91755ff90ed584281ae73cf477fb817e +timeCreated: 1494205063 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/Scripts/Networking/Server.cs b/Assets/Scripts/Networking/Server.cs index 2388274..96e7c1e 100644 --- a/Assets/Scripts/Networking/Server.cs +++ b/Assets/Scripts/Networking/Server.cs @@ -16,10 +16,10 @@ public class Server : MonoBehaviour { public void LaunchServer(int port) { - ConnectionConfig config = new ConnectionConfig(); - config.AddChannel(QosType.ReliableSequenced); - config.AddChannel(QosType.UnreliableSequenced); - NetworkServer.Configure(config, 10); + ConnectionConfig Config = new ConnectionConfig(); + Config.AddChannel(QosType.ReliableSequenced); + Config.AddChannel(QosType.UnreliableSequenced); + NetworkServer.Configure(Config, 10); NetworkServer.Listen(port); @@ -36,9 +36,9 @@ public class Server : MonoBehaviour { switch(msg.msgType) { case PktType.TestMessage: - TextMessage textMsg = new TextMessage(); - textMsg.Deserialize(msg.reader); - Debug.Log("Received message: " + textMsg.message); + TextMessage TextMsg = new TextMessage(); + TextMsg.Deserialize(msg.reader); + Debug.Log("Received message: " + TextMsg.Message); break; default: Debug.LogError("Received an unknown packet, id: " + msg.msgType);