2017-05-08 00:59:50 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
public class Server : MonoBehaviour {
|
2017-05-08 21:11:05 +02:00
|
|
|
|
|
|
|
|
|
private static Server Singleton;
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
|
|
|
|
// Use this for initialization
|
|
|
|
|
void Start () {
|
2017-05-08 21:11:05 +02:00
|
|
|
|
Singleton = this;
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update () {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 21:11:05 +02:00
|
|
|
|
private bool LaunchServer(int port) {
|
|
|
|
|
if (NetworkServer.active) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
2017-05-08 02:59:42 +02:00
|
|
|
|
ConnectionConfig Config = new ConnectionConfig();
|
|
|
|
|
Config.AddChannel(QosType.ReliableSequenced);
|
|
|
|
|
Config.AddChannel(QosType.UnreliableSequenced);
|
|
|
|
|
NetworkServer.Configure(Config, 10);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
|
|
|
|
NetworkServer.Listen(port);
|
|
|
|
|
|
2017-05-08 03:42:18 +02:00
|
|
|
|
NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
|
|
|
|
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
|
|
|
|
|
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
|
|
|
|
NetworkServer.RegisterHandler(MsgType.Error, OnError);
|
|
|
|
|
|
|
|
|
|
Debug.Log("Server started on port " + port);
|
2017-05-08 03:31:32 +02:00
|
|
|
|
Term.Println("Server started on port " + port);
|
2017-05-08 03:42:18 +02:00
|
|
|
|
|
|
|
|
|
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]));
|
|
|
|
|
});
|
2017-05-08 21:11:05 +02:00
|
|
|
|
|
|
|
|
|
return true;
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 21:11:05 +02:00
|
|
|
|
// Custon packet handler
|
|
|
|
|
|
|
|
|
|
private void HandlePacket(NetworkMessage msg) {
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
|
|
|
|
switch(msg.msgType) {
|
2017-05-08 03:42:18 +02:00
|
|
|
|
case PktType.TextMessage:
|
2017-05-08 02:59:42 +02:00
|
|
|
|
TextMessage TextMsg = new TextMessage();
|
|
|
|
|
TextMsg.Deserialize(msg.reader);
|
2017-05-08 03:42:18 +02:00
|
|
|
|
Term.Println(TextMsg.Message);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
2017-05-08 03:31:32 +02:00
|
|
|
|
Term.Println("Received an unknown packet, id: " + msg.msgType);
|
2017-05-08 00:59:50 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
2017-05-08 03:42:18 +02:00
|
|
|
|
}
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
2017-05-08 21:11:05 +02:00
|
|
|
|
// Internal built-in event handler
|
2017-05-08 00:59:50 +02:00
|
|
|
|
|
2017-05-08 21:11:05 +02:00
|
|
|
|
private void OnConnected(NetworkMessage msg) {
|
2017-05-08 00:59:50 +02:00
|
|
|
|
Debug.Log("Someone connected!");
|
2017-05-08 03:31:32 +02:00
|
|
|
|
Term.Println("Someone connected!");
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 21:11:05 +02:00
|
|
|
|
private void OnDisconnected(NetworkMessage msg) {
|
2017-05-08 03:31:32 +02:00
|
|
|
|
Debug.Log("Someone disconnected.");
|
|
|
|
|
Term.Println("Someone disconnected.");
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 21:11:05 +02:00
|
|
|
|
private void OnError(NetworkMessage msg) {
|
2017-05-08 00:59:50 +02:00
|
|
|
|
Debug.LogError("Encountered a network error on server");
|
2017-05-08 03:31:32 +02:00
|
|
|
|
Term.Println("Encountered a network error on server");
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|
2017-05-08 21:11:05 +02:00
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
2017-05-08 00:59:50 +02:00
|
|
|
|
}
|