2020-08-05 18:50:28 +02:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using NeonTea.Quakeball.TeaNet.Peers;
|
|
|
|
|
using System.Collections.Generic;
|
2020-08-02 02:50:01 +02:00
|
|
|
|
|
|
|
|
|
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-02 02:50:01 +02:00
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public Peer Peer;
|
2020-08-05 19:39:52 +02:00
|
|
|
|
public List<ulong> Connections = new List<ulong>();
|
2020-08-05 18:50:28 +02:00
|
|
|
|
|
|
|
|
|
public bool IsServer = false;
|
2020-08-02 02:50:01 +02:00
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public void StartClient(string address, int port, PeerMessageListener listener) {
|
|
|
|
|
if (Peer != null) {
|
2020-08-02 02:50:01 +02:00
|
|
|
|
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-02 02:50:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2020-08-05 18:50:28 +02:00
|
|
|
|
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-02 02:50:01 +02:00
|
|
|
|
}
|
2020-08-05 03:21:04 +02:00
|
|
|
|
|
|
|
|
|
static void Quit() {
|
|
|
|
|
Singleton.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[RuntimeInitializeOnLoadMethod]
|
|
|
|
|
static void RunOnStart() {
|
|
|
|
|
Application.quitting += Quit;
|
|
|
|
|
}
|
2020-08-02 02:50:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|