quakeball/Assets/Scripts/Net/Net.cs
2020-08-05 04:21:04 +03:00

57 lines
1.7 KiB
C#

using System.Collections.Generic;
using System;
using UnityEngine;
using NeonTea.Quakeball.Net.Peers;
using System.Threading;
namespace NeonTea.Quakeball.Net {
public class Net {
public static Net Singleton = new Net();
private static byte[] FP = new byte[] { 0xFF, 0xF7 };
public Peer Peer;
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;
}
Peer = new Peer(FP);
Peer.MessageListener = listener;
Peer.Start(0);
byte ident = Peer.RegisterProtocol(new TestProtocol());
Peer.Connect(address, port, ident);
}
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;
}
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;
}
}
static void Quit() {
Singleton.Stop();
}
[RuntimeInitializeOnLoadMethod]
static void RunOnStart() {
Application.quitting += Quit;
}
}
}