quakeball/Assets/Scripts/Net/Net.cs

57 lines
1.7 KiB
C#
Raw Normal View History

2020-08-05 03:21:04 +02:00
using System.Collections.Generic;
using System;
using UnityEngine;
2020-08-02 20:32:30 +02:00
using NeonTea.Quakeball.Net.Peers;
using System.Threading;
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;
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;
}
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;
}
}
}