quakeball/Assets/Scripts/Net/Net.cs

59 lines
1.8 KiB
C#
Raw Normal View History

using UnityEngine;
using NeonTea.Quakeball.TeaNet.Peers;
using System.Collections.Generic;
namespace NeonTea.Quakeball.Net {
2020-08-02 20:32:30 +02:00
public class Net {
public static Net Singleton = new Net();
2020-08-05 03:21:04 +02:00
private static byte[] FP = new byte[] { 0xFF, 0xF7 };
2020-08-05 03:21:04 +02:00
public Peer Peer;
public List<ulong> Connections = new List<ulong>();
public bool IsServer = false;
2020-08-05 03:21:04 +02:00
public void StartClient(string address, int port, PeerMessageListener listener) {
if (Peer != null) {
Debug.Log("Can not start multiple endpoints at once! Use Server if multiple connections are required.");
return;
}
2020-08-05 03:21:04 +02:00
Peer = new Peer(FP);
Peer.MessageListener = listener;
Peer.Start(0);
byte ident = Peer.RegisterProtocol(new TestProtocol());
Peer.Connect(address, port, ident);
}
2020-08-05 03:21:04 +02:00
public void StartServer(string address, int port, PeerMessageListener listener) {
if (Peer != null) {
Debug.Log("Can not start multiple endpoints at once! Use Server if multiple connections are required.");
return;
}
IsServer = true;
2020-08-05 03:21:04 +02:00
Peer = new Peer(FP);
Peer.MessageListener = listener;
Peer.Start(port);
Peer.RegisterProtocol(new TestProtocol());
Peer.StartListen(address, port);
}
public void Stop() {
if (Peer != null) {
Peer.Stop();
Peer.MessageListener.Message("Stopping");
Peer = null;
2020-08-02 20:32:30 +02:00
}
}
2020-08-05 03:21:04 +02:00
static void Quit() {
Singleton.Stop();
}
[RuntimeInitializeOnLoadMethod]
static void RunOnStart() {
Application.quitting += Quit;
}
}
}