(tools, client, server) feat: Remove gRPC support, add TCP back and reorganized project
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
@ -5,24 +6,62 @@ using Utils;
|
||||
|
||||
namespace Network
|
||||
{
|
||||
public class UnityUdpClient : Singleton<UnityUdpClient>
|
||||
public class UnityUdpClient : Singleton<UnityUdpClient>, IDisposable
|
||||
{
|
||||
private readonly UdpClient _client;
|
||||
private UdpClient _client;
|
||||
private bool _disposed;
|
||||
|
||||
public UnityUdpClient()
|
||||
{
|
||||
_client = new UdpClient();
|
||||
_client.Connect("127.0.0.1", 12345);
|
||||
|
||||
Application.quitting += () => _client.Close();
|
||||
try
|
||||
{
|
||||
_client = new UdpClient();
|
||||
_client.Connect("127.0.0.1", 12345);
|
||||
Application.quitting += Dispose;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogException(ex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<byte[]> SendAndReceiveData(byte[] data)
|
||||
{
|
||||
await _client.SendAsync(data, data.Length);
|
||||
try
|
||||
{
|
||||
await _client.SendAsync(data, data.Length);
|
||||
|
||||
var result = await _client.ReceiveAsync();
|
||||
return result.Buffer;
|
||||
var result = await _client.ReceiveAsync();
|
||||
return result.Buffer;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogException(ex);
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
|
||||
try
|
||||
{
|
||||
_client.Close();
|
||||
_client.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogException(ex);
|
||||
return;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_client = null;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user