quakeball/Assets/Scripts/Net/Endpoint/Server.cs

43 lines
1.3 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
namespace NeonTea.Quakeball.Net.Endpoint {
public class Server : AbstractEndpoint {
public override UdpClient UdpClient {
get { return InnerClient; }
}
private static uint ConnectionUUIDCounter;
private Dictionary<uint, Connection> Connections = new Dictionary<uint, Connection>();
private UdpClient InnerClient;
public override void Start(string host, int port) {
InnerClient = new UdpClient(port);
Connection conn;
try {
conn = AddConnection(new IPEndPoint(FindAddress(host), port), uint.MaxValue);
} catch (Exception e) {
Debug.Log($"Failed to create initial connection: {e.ToString()}");
Network.Singleton.Stop();
return;
}
StartListen(conn);
Debug.Log($"Server started at {host}:{port}!");
}
private Connection AddConnection(IPEndPoint endpoint, uint uuid = 0) {
if (uuid == 0) {
uuid = ConnectionUUIDCounter++;
}
Connection conn = new Connection(endpoint, uuid);
Connections.Add(uuid, conn);
return conn;
}
}
}