2020-08-02 20:32:30 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
2020-08-05 18:50:28 +02:00
|
|
|
|
using NeonTea.Quakeball.TeaNet.Packets;
|
2020-08-02 20:32:30 +02:00
|
|
|
|
|
2020-08-05 18:50:28 +02:00
|
|
|
|
namespace NeonTea.Quakeball.TeaNet.Peers {
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public class Peer : PeerMessageListener {
|
|
|
|
|
public UdpClient UdpClient { get; private set; }
|
|
|
|
|
public byte[] Fingerprint { get; private set; }
|
2020-08-02 20:32:30 +02:00
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public ListenerThread ListenerThread;
|
|
|
|
|
public ConnectionManager ConnectionManager;
|
|
|
|
|
public Dictionary<byte, Protocol> RegisteredProtocols = new Dictionary<byte, Protocol>();
|
2020-08-02 20:32:30 +02:00
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public PeerMessageListener MessageListener;
|
2020-08-02 20:32:30 +02:00
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public Peer(byte[] fingerprint) {
|
|
|
|
|
Fingerprint = fingerprint;
|
|
|
|
|
ConnectionManager = new ConnectionManager(this);
|
|
|
|
|
MessageListener = this;
|
|
|
|
|
}
|
2020-08-02 20:32:30 +02:00
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public void Start(int sending_port) {
|
|
|
|
|
UdpClient = new UdpClient(sending_port);
|
|
|
|
|
MessageListener.Message("UdpClient Started");
|
2020-08-02 20:32:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public void Stop() {
|
|
|
|
|
ConnectionManager.StopThread();
|
|
|
|
|
if (ListenerThread != null) {
|
|
|
|
|
ListenerThread.Stop();
|
2020-08-02 20:32:30 +02:00
|
|
|
|
}
|
2020-08-05 03:21:04 +02:00
|
|
|
|
UdpClient.Dispose();
|
|
|
|
|
UdpClient.Close();
|
2020-08-02 20:32:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public void StartListen(string address, int port) {
|
|
|
|
|
IPEndPoint endpoint = new IPEndPoint(FindAddress(address), port);
|
|
|
|
|
StartListen(endpoint);
|
2020-08-02 20:32:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
private void StartListen(IPEndPoint endpoint) {
|
2020-08-05 18:50:28 +02:00
|
|
|
|
if (ListenerThread != null) {
|
|
|
|
|
return; // Cant listen twice
|
|
|
|
|
}
|
2020-08-05 03:21:04 +02:00
|
|
|
|
ListenerThread = new ListenerThread(this, endpoint);
|
|
|
|
|
ListenerThread.Start();
|
|
|
|
|
MessageListener.Message($"Started listening to {endpoint}");
|
2020-08-02 20:32:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 19:45:34 +02:00
|
|
|
|
public void Connect(string address, int port, byte protocolIdent, bool startListening = true) {
|
|
|
|
|
if (startListening) {
|
|
|
|
|
IPEndPoint listenEndpoint = (IPEndPoint)UdpClient.Client.LocalEndPoint;
|
|
|
|
|
StartListen(listenEndpoint);
|
|
|
|
|
}
|
2020-08-05 03:21:04 +02:00
|
|
|
|
IPEndPoint endpoint = new IPEndPoint(FindAddress(address), port);
|
|
|
|
|
ConnectionManager.StartConnection(endpoint, protocolIdent);
|
|
|
|
|
MessageListener.Message($"Connecting to {endpoint}");
|
2020-08-02 20:32:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 19:39:52 +02:00
|
|
|
|
public void SendReliable(ulong uid, Packet packet) {
|
|
|
|
|
ConnectionManager.AddPacketToQueue(uid, packet);
|
|
|
|
|
ConnectionManager.SendPacketQueue(uid);
|
2020-08-05 18:50:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 20:55:17 +02:00
|
|
|
|
public void SendReliableLater(ulong uid, Packet packet) {
|
|
|
|
|
ConnectionManager.AddPacketToQueue(uid, packet);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 19:39:52 +02:00
|
|
|
|
public void SendUnreliable(ulong uid, Packet packet) {
|
|
|
|
|
ConnectionManager.SendSingleUnreliable(uid, packet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Connection GetConnection(ulong uid) {
|
|
|
|
|
return ConnectionManager.GetConnection(uid);
|
2020-08-05 18:50:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public byte RegisterProtocol(Protocol protocol) {
|
|
|
|
|
byte ident = protocol.Identifier;
|
|
|
|
|
if (RegisteredProtocols.ContainsKey(ident)) {
|
|
|
|
|
return 0;
|
2020-08-02 20:32:30 +02:00
|
|
|
|
}
|
2020-08-05 03:21:04 +02:00
|
|
|
|
RegisteredProtocols.Add(ident, protocol);
|
|
|
|
|
protocol.Peer = this;
|
|
|
|
|
return ident;
|
2020-08-02 20:32:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public Protocol GetProtocol(byte ident) {
|
|
|
|
|
if (RegisteredProtocols.ContainsKey(ident)) {
|
|
|
|
|
return RegisteredProtocols[ident];
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2020-08-02 20:32:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 21:54:53 +02:00
|
|
|
|
public void Message(string msg) { }
|
|
|
|
|
public void Err(string msg) { }
|
2020-08-02 20:32:30 +02:00
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public IPAddress FindAddress(string host) {
|
|
|
|
|
IPAddress addr;
|
|
|
|
|
try {
|
|
|
|
|
addr = Dns.GetHostAddresses(host)[0];
|
|
|
|
|
} catch (ArgumentException) {
|
|
|
|
|
addr = IPAddress.Parse(host);
|
|
|
|
|
}
|
|
|
|
|
return addr;
|
2020-08-02 20:32:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
|
public interface PeerMessageListener {
|
|
|
|
|
void Message(string msg);
|
2020-08-05 21:54:53 +02:00
|
|
|
|
void Err(string msg);
|
2020-08-02 20:32:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|