quakeball/Assets/Scripts/TeaNet/Peers/Peer.cs

112 lines
3.6 KiB
C#
Raw Normal View History

2020-08-02 20:32:30 +02:00
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
using NeonTea.Quakeball.TeaNet.Packets;
2020-08-02 20:32:30 +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) {
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
}
public void SendReliable(ulong uid, Packet packet) {
ConnectionManager.AddPacketToQueue(uid, packet);
ConnectionManager.SendPacketQueue(uid);
}
public void SendUnreliable(ulong uid, Packet packet) {
ConnectionManager.SendSingleUnreliable(uid, packet);
}
public Connection GetConnection(ulong uid) {
return ConnectionManager.GetConnection(uid);
}
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 03:21:04 +02:00
public void Message(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-02 20:32:30 +02:00
}
}