46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
|
|
namespace NeonTea.Quakeball.Net.Endpoint {
|
|
public class Client : AbstractEndpoint {
|
|
public override UdpClient UdpClient {
|
|
get { return InnerClient; }
|
|
}
|
|
|
|
private Connection Connection;
|
|
private UdpClient InnerClient;
|
|
|
|
public override void Start(string host, int port) {
|
|
InnerClient = new UdpClient(0);
|
|
|
|
try {
|
|
Connection = new Connection(new IPEndPoint(FindAddress(host), port), 0);
|
|
} catch (Exception e) {
|
|
Debug.Log($"Failed to create initial connection: {e.ToString()}");
|
|
Network.Singleton.Stop();
|
|
}
|
|
StartListen(Connection);
|
|
SendBytes(Encoding.UTF8.GetBytes("Hello! This is testing."));
|
|
|
|
Debug.Log($"Client started at {host}:{port}!");
|
|
}
|
|
|
|
public void OnStop() {
|
|
Connection.ShouldExist = false;
|
|
}
|
|
|
|
public override void ConnectionClosed(Connection conn, ClosingReason reason) {
|
|
if (Connection == conn) { // Make sure the closed connection is this one
|
|
Debug.Log($"Closing client. Connection was closed: {reason.Description}");
|
|
Network.Singleton.Stop();
|
|
}
|
|
}
|
|
|
|
private void SendBytes(Byte[] bytes) {
|
|
UdpClient.Send(bytes, bytes.Length, Connection.Endpoint);
|
|
}
|
|
}
|
|
} |