Namespace Networking and seperate Client-/Serverside
This commit is contained in:
parent
ac6e5dfe2a
commit
08f5cdf3cd
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
9
Assets/Scripts/Networking/Clientside.meta
Normal file
9
Assets/Scripts/Networking/Clientside.meta
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 2e91e843b32fb3e41bbe27bc2446e12c
|
||||||
|
folderAsset: yes
|
||||||
|
timeCreated: 1494276932
|
||||||
|
licenseType: Free
|
||||||
|
DefaultImporter:
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
142
Assets/Scripts/Networking/Clientside/Client.cs
Normal file
142
Assets/Scripts/Networking/Clientside/Client.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,44 +1,46 @@
|
|||||||
using UnityEngine.Networking;
|
using UnityEngine.Networking;
|
||||||
|
|
||||||
/// <summary>
|
namespace Cyber.Networking.Messages {
|
||||||
/// Generic Text Message for chat etc.
|
|
||||||
/// To be removed later when no longer necessary.
|
|
||||||
/// </summary>
|
|
||||||
public class TextMessage : MessageBase {
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Message inside the Text Message. Does not container sender information.
|
/// Generic Text Message for chat etc.
|
||||||
|
/// To be removed later when no longer necessary.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Message;
|
public class TextMessage : MessageBase {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a TextMessage containing the message to be sent.
|
/// Message inside the Text Message. Does not container sender information.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">Message to be sent.</param>
|
public string Message;
|
||||||
public TextMessage(string message) {
|
|
||||||
this.Message = message;
|
/// <summary>
|
||||||
|
/// Create a TextMessage containing the message to be sent.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">Message to be sent.</param>
|
||||||
|
public TextMessage(string message) {
|
||||||
|
this.Message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter-less constructor using when deserializing the message.
|
||||||
|
/// </summary>
|
||||||
|
public TextMessage() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used to deserialize a message received via networking.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="reader"></param>
|
||||||
|
public override void Deserialize(NetworkReader reader) {
|
||||||
|
Message = reader.ReadString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used to serialize the message before it is sent.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="writer"></param>
|
||||||
|
public override void Serialize(NetworkWriter writer) {
|
||||||
|
writer.Write(Message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/// <summary>
|
|
||||||
/// Parameter-less constructor using when deserializing the message.
|
|
||||||
/// </summary>
|
|
||||||
public TextMessage() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Used to deserialize a message received via networking.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="reader"></param>
|
|
||||||
public override void Deserialize(NetworkReader reader) {
|
|
||||||
Message = reader.ReadString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Used to serialize the message before it is sent.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="writer"></param>
|
|
||||||
public override void Serialize(NetworkWriter writer) {
|
|
||||||
writer.Write(Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,109 +1,112 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
|
using Cyber.Networking.Clientside;
|
||||||
|
using Cyber.Networking.Serverside;
|
||||||
|
|
||||||
/// <summary>
|
namespace Cyber.Networking {
|
||||||
/// 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>
|
/// <summary>
|
||||||
/// Input field for the IP to which connect to.
|
/// 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>
|
/// </summary>
|
||||||
[Tooltip("Required field only if StartClient() is used.")]
|
public class NetworkEstablisher : MonoBehaviour {
|
||||||
public InputField IPField;
|
|
||||||
/// <summary>
|
|
||||||
/// Input field for the client port
|
|
||||||
/// </summary>
|
|
||||||
[Tooltip("Required field only if StartClient() is used.")]
|
|
||||||
public InputField ClientPortField;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Input field for the server port
|
/// Input field for the IP to which connect to.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Tooltip("Required field only if StartServer() is used.")]
|
[Tooltip("Required field only if StartClient() is used.")]
|
||||||
public InputField ServerPortField;
|
public InputField IPField;
|
||||||
|
/// <summary>
|
||||||
|
/// Input field for the client port
|
||||||
|
/// </summary>
|
||||||
|
[Tooltip("Required field only if StartClient() is used.")]
|
||||||
|
public InputField ClientPortField;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// World Root node, a GameObject.
|
/// Input field for the server port
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public GameObject WorldRoot;
|
[Tooltip("Required field only if StartServer() is used.")]
|
||||||
|
public InputField ServerPortField;
|
||||||
|
|
||||||
// Use this for initialization
|
/// <summary>
|
||||||
void Start () {
|
/// World Root node, a GameObject.
|
||||||
Term.AddCommand("join", "joins a server at localhost:3935", (args) => {
|
/// </summary>
|
||||||
StartClient("localhost", 3935);
|
public GameObject WorldRoot;
|
||||||
});
|
|
||||||
Term.AddCommand("join (ip)", "joins a server at given ip and port 3935", (args) => {
|
|
||||||
string ip = args[0];
|
|
||||||
StartClient(ip, 3935);
|
|
||||||
});
|
|
||||||
Term.AddCommand("join (ip) (port)", "joins a server at given ip and port", (args) => {
|
|
||||||
string ip = args[0];
|
|
||||||
int port = 3935;
|
|
||||||
int.TryParse(args[1], out port);
|
|
||||||
StartClient(ip, port);
|
|
||||||
});
|
|
||||||
|
|
||||||
Term.AddCommand("host", "host a server at port 3935", (args) => {
|
// Use this for initialization
|
||||||
StartServer(3935);
|
void Start () {
|
||||||
});
|
Term.AddCommand("join", "joins a server at localhost:3935", (args) => {
|
||||||
Term.AddCommand("host (port)", "host a server at given port", (args) => {
|
StartClient("localhost", 3935);
|
||||||
int port = 3935;
|
});
|
||||||
int.TryParse(args[0], out port);
|
Term.AddCommand("join (ip)", "joins a server at given ip and port 3935", (args) => {
|
||||||
StartServer(port);
|
string ip = args[0];
|
||||||
});
|
StartClient(ip, 3935);
|
||||||
}
|
});
|
||||||
|
Term.AddCommand("join (ip) (port)", "joins a server at given ip and port", (args) => {
|
||||||
|
string ip = args[0];
|
||||||
|
int port = 3935;
|
||||||
|
int.TryParse(args[1], out port);
|
||||||
|
StartClient(ip, port);
|
||||||
|
});
|
||||||
|
|
||||||
/// <summary>
|
Term.AddCommand("host", "host a server at port 3935", (args) => {
|
||||||
/// Starts the client using given input fields. Otherwise functions like <see cref="StartClient(string, int)"/>.
|
StartServer(3935);
|
||||||
/// </summary>
|
});
|
||||||
public void StartClient() {
|
Term.AddCommand("host (port)", "host a server at given port", (args) => {
|
||||||
string IP = IPField.text;
|
int port = 3935;
|
||||||
if (IP.Length == 0) {
|
int.TryParse(args[0], out port);
|
||||||
IP = "localhost";
|
StartServer(port);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
string PortText = ClientPortField.text;
|
|
||||||
int Port = 3935;
|
/// <summary>
|
||||||
if (PortText.Length > 0) {
|
/// Starts the client using given input fields. Otherwise functions like <see cref="StartClient(string, int)"/>.
|
||||||
Port = Int32.Parse(PortText);
|
/// </summary>
|
||||||
|
public void StartClient() {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
StartClient(IP, Port);
|
||||||
}
|
}
|
||||||
StartClient(IP, Port);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Starts the client with the given ip and port.
|
/// Starts the client with the given ip and port.
|
||||||
/// This initializes the Client component and launches it properly.
|
/// This initializes the Client component and launches it properly.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="ip">IP used to connect.</param>
|
/// <param name="ip">IP used to connect.</param>
|
||||||
/// <param name="port">port of the host.</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() {
|
|
||||||
string PortText = ServerPortField.text;
|
|
||||||
int Port = 3935;
|
|
||||||
if (PortText.Length > 0) {
|
|
||||||
Port = Int32.Parse(PortText);
|
|
||||||
}
|
}
|
||||||
StartServer(Port);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Starts the server using given port.
|
/// Starts the server using given input fields. Otherwise functions like <see cref="StartServer(int)"/>.
|
||||||
/// This initializes the port and launches the server properly.
|
/// </summary>
|
||||||
/// </summary>
|
public void StartServer() {
|
||||||
/// <param name="port">port used for the server.</param>
|
string PortText = ServerPortField.text;
|
||||||
public void StartServer(int port) {
|
int Port = 3935;
|
||||||
WorldRoot.AddComponent<Server>();
|
if (PortText.Length > 0) {
|
||||||
Server.Launch(port);
|
Port = Int32.Parse(PortText);
|
||||||
|
}
|
||||||
|
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) {
|
||||||
|
WorldRoot.AddComponent<Server>();
|
||||||
|
Server.Launch(port);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
namespace Cyber.Networking {
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Types of custom created packets.
|
/// Types of custom created packets.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class PktType {
|
public class PktType {
|
||||||
|
|
||||||
public const short TextMessage = 200;
|
public const short TextMessage = 200;
|
||||||
|
}
|
||||||
}
|
}
|
@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
9
Assets/Scripts/Networking/Serverside.meta
Normal file
9
Assets/Scripts/Networking/Serverside.meta
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 07f9f599ce8d74247ae4ab534cc671af
|
||||||
|
folderAsset: yes
|
||||||
|
timeCreated: 1494276932
|
||||||
|
licenseType: Free
|
||||||
|
DefaultImporter:
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
139
Assets/Scripts/Networking/Serverside/Server.cs
Normal file
139
Assets/Scripts/Networking/Serverside/Server.cs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user