Namespace Networking and seperate Client-/Serverside

This commit is contained in:
Sofia 2017-05-09 00:06:18 +03:00
parent ac6e5dfe2a
commit 08f5cdf3cd
11 changed files with 440 additions and 414 deletions

View File

@ -1,141 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// 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.
/// </summary>
public class Client : MonoBehaviour {
private NetworkClient NetClient;
private bool Running = false;
private static Client Singleton;
/// <summary>
/// Creates the client and sets it as the signleton.
/// </summary>
public Client() {
Singleton = this;
}
// 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.
///
/// 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);
}
/// <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) {
if (Running) {
return false;
}
ConnectionConfig Config = new ConnectionConfig();
Config.AddChannel(QosType.ReliableSequenced);
Config.AddChannel(QosType.UnreliableSequenced);
NetworkServer.Configure(Config, 10);
NetClient = new NetworkClient();
NetClient.Configure(Config, 10);
Running = true;
NetClient.RegisterHandler(PktType.TextMessage, HandlePacket);
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
NetClient.RegisterHandler(MsgType.Error, OnError);
NetClient.Connect(ip, port);
Debug.Log("Client launched!");
Term.Println("Client launched!");
return true;
}
private void HandlePacket(NetworkMessage msg) {
switch (msg.msgType) {
case (PktType.TextMessage):
TextMessage TextMsg = new TextMessage();
TextMsg.Deserialize(msg.reader);
Term.Println(TextMsg.Message);
break;
default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
Term.Println("Received an unknown packet, id: " + msg.msgType);
break;
}
}
// 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]);
NetClient.Send(PktType.TextMessage, new TextMessage("A Client: " + args[0]));
});
}
private void OnDisconnected(NetworkMessage msg) {
Debug.Log("Disconnected!");
Term.Println("Disconnected!");
Running = false;
}
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;
}
}

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 2e91e843b32fb3e41bbe27bc2446e12c
folderAsset: yes
timeCreated: 1494276932
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,142 @@
using Cyber.Networking.Messages;
using UnityEngine;
using UnityEngine.Networking;
namespace Cyber.Networking.Clientside {
/// <summary>
/// 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.
/// </summary>
public class Client : MonoBehaviour {
private NetworkClient NetClient;
private bool Running = false;
private static Client Singleton;
/// <summary>
/// Creates the client and sets it as the signleton.
/// </summary>
public Client() {
Singleton = this;
}
// 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.
///
/// 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);
}
/// <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) {
if (Running) {
return false;
}
ConnectionConfig Config = new ConnectionConfig();
Config.AddChannel(QosType.ReliableSequenced);
Config.AddChannel(QosType.UnreliableSequenced);
NetworkServer.Configure(Config, 10);
NetClient = new NetworkClient();
NetClient.Configure(Config, 10);
Running = true;
NetClient.RegisterHandler(PktType.TextMessage, HandlePacket);
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
NetClient.RegisterHandler(MsgType.Error, OnError);
NetClient.Connect(ip, port);
Debug.Log("Client launched!");
Term.Println("Client launched!");
return true;
}
private void HandlePacket(NetworkMessage msg) {
switch (msg.msgType) {
case (PktType.TextMessage):
TextMessage TextMsg = new TextMessage();
TextMsg.Deserialize(msg.reader);
Term.Println(TextMsg.Message);
break;
default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
Term.Println("Received an unknown packet, id: " + msg.msgType);
break;
}
}
// 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]);
NetClient.Send(PktType.TextMessage, new TextMessage("A Client: " + args[0]));
});
}
private void OnDisconnected(NetworkMessage msg) {
Debug.Log("Disconnected!");
Term.Println("Disconnected!");
Running = false;
}
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;
}
}
}

View File

@ -1,10 +1,12 @@
using UnityEngine.Networking;
/// <summary>
/// Generic Text Message for chat etc.
/// To be removed later when no longer necessary.
/// </summary>
public class TextMessage : MessageBase {
namespace Cyber.Networking.Messages {
/// <summary>
/// Generic Text Message for chat etc.
/// To be removed later when no longer necessary.
/// </summary>
public class TextMessage : MessageBase {
/// <summary>
/// Message inside the Text Message. Does not container sender information.
@ -40,5 +42,5 @@ public class TextMessage : MessageBase {
public override void Serialize(NetworkWriter writer) {
writer.Write(Message);
}
}
}

View File

@ -1,14 +1,16 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Cyber.Networking.Clientside;
using Cyber.Networking.Serverside;
/// <summary>
/// This class is used pretty much anywhere in order to make the "first step" of networking.
/// It adds the proper components to World Root and tells them to start.
/// </summary>
public class NetworkEstablisher : MonoBehaviour {
namespace Cyber.Networking {
/// <summary>
/// This class is used pretty much anywhere in order to make the "first step" of networking.
/// It adds the proper components to World Root and tells them to start.
/// </summary>
public class NetworkEstablisher : MonoBehaviour {
/// <summary>
/// Input field for the IP to which connect to.
@ -106,4 +108,5 @@ public class NetworkEstablisher : MonoBehaviour {
WorldRoot.AddComponent<Server>();
Server.Launch(port);
}
}
}

View File

@ -1,11 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Cyber.Networking {
/// <summary>
/// Types of custom created packets.
/// </summary>
public class PktType {
/// <summary>
/// Types of custom created packets.
/// </summary>
public class PktType {
public const short TextMessage = 200;
}
}

View File

@ -1,137 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// Server-class used to host a server and communicate to clients.
/// </summary>
public class Server : MonoBehaviour {
private static Server Singleton;
/// <summary>
/// Creates the server-component, and sets the singleton as itself.
/// </summary>
public Server() {
Singleton = this;
}
// Static methods for public usage
/// <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) {
if (NetworkServer.active) {
return false;
}
ConnectionConfig Config = new ConnectionConfig();
Config.AddChannel(QosType.ReliableSequenced);
Config.AddChannel(QosType.UnreliableSequenced);
NetworkServer.Configure(Config, 10);
NetworkServer.Listen(port);
NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket);
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
NetworkServer.RegisterHandler(MsgType.Error, OnError);
Debug.Log("Server started on port " + port);
Term.Println("Server started on port " + port);
Term.AddCommand("send (message)", "Howl at the darkness of space. Does it echo though?", (args) => {
Term.Println("You: " + args[0]);
SendToAll(PktType.TextMessage, new TextMessage("Server: " + args[0]));
});
return true;
}
private void HandlePacket(NetworkMessage msg) {
switch(msg.msgType) {
case PktType.TextMessage:
TextMessage TextMsg = new TextMessage();
TextMsg.Deserialize(msg.reader);
Term.Println(TextMsg.Message);
break;
default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
Term.Println("Received an unknown packet, id: " + msg.msgType);
break;
}
}
// Internal built-in event handler
private void OnConnected(NetworkMessage msg) {
Debug.Log("Someone connected!");
Term.Println("Someone connected!");
}
private void OnDisconnected(NetworkMessage msg) {
Debug.Log("Someone disconnected.");
Term.Println("Someone disconnected.");
}
private void OnError(NetworkMessage msg) {
Debug.LogError("Encountered a network error on server");
Term.Println("Encountered a network error on server");
}
}

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 07f9f599ce8d74247ae4ab534cc671af
folderAsset: yes
timeCreated: 1494276932
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,139 @@
using Cyber.Networking.Messages;
using UnityEngine;
using UnityEngine.Networking;
namespace Cyber.Networking.Serverside {
/// <summary>
/// Server-class used to host a server and communicate to clients.
/// </summary>
public class Server : MonoBehaviour {
private static Server Singleton;
/// <summary>
/// Creates the server-component, and sets the singleton as itself.
/// </summary>
public Server() {
Singleton = this;
}
// Static methods for public usage
/// <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) {
if (NetworkServer.active) {
return false;
}
ConnectionConfig Config = new ConnectionConfig();
Config.AddChannel(QosType.ReliableSequenced);
Config.AddChannel(QosType.UnreliableSequenced);
NetworkServer.Configure(Config, 10);
NetworkServer.Listen(port);
NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket);
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
NetworkServer.RegisterHandler(MsgType.Error, OnError);
Debug.Log("Server started on port " + port);
Term.Println("Server started on port " + port);
Term.AddCommand("send (message)", "Howl at the darkness of space. Does it echo though?", (args) => {
Term.Println("You: " + args[0]);
SendToAll(PktType.TextMessage, new TextMessage("Server: " + args[0]));
});
return true;
}
private void HandlePacket(NetworkMessage msg) {
switch (msg.msgType) {
case PktType.TextMessage:
TextMessage TextMsg = new TextMessage();
TextMsg.Deserialize(msg.reader);
Term.Println(TextMsg.Message);
break;
default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
Term.Println("Received an unknown packet, id: " + msg.msgType);
break;
}
}
// Internal built-in event handler
private void OnConnected(NetworkMessage msg) {
Debug.Log("Someone connected!");
Term.Println("Someone connected!");
}
private void OnDisconnected(NetworkMessage msg) {
Debug.Log("Someone disconnected.");
Term.Println("Someone disconnected.");
}
private void OnError(NetworkMessage msg) {
Debug.LogError("Encountered a network error on server");
Term.Println("Encountered a network error on server");
}
}
}