(tools, client, server) feat: Remove gRPC support, add TCP back and reorganized project

This commit is contained in:
2025-08-30 17:07:03 +08:00
parent 8fd5e24865
commit 362aa799b9
28 changed files with 378 additions and 490 deletions

View File

@ -1,38 +0,0 @@
using Grpc.Net.Client;
using Grpc.Net.Client.Web;
using Protocol;
using System.Net.Http;
using System.Threading.Tasks;
using UnityEngine;
using Utils;
namespace Network
{
public class GrpcClient : Singleton<GrpcClient>
{
// The address must adopt HTTP.
private const string ServerAddress = "http://127.0.0.1:12345";
private readonly GrpcChannel _channel;
private readonly GameService.GameServiceClient _game;
public GrpcClient()
{
var channelOptions = new GrpcChannelOptions
{
HttpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler())
};
_channel = GrpcChannel.ForAddress(ServerAddress, channelOptions);
_game = new GameService.GameServiceClient(_channel);
Application.quitting += () => _channel.ShutdownAsync().Wait();
}
public async Task<LoginResponse> Login(string username, string password)
{
return await _game.LoginAsync(new LoginRequest { Username = username, Password = password });
}
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: bd54dd152e0e4bbda802e9aa04078197
timeCreated: 1752480707

View File

@ -0,0 +1,70 @@
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using UnityEngine;
using Utils;
namespace Network
{
public class UnityTcpClient : Singleton<UnityTcpClient>, IDisposable
{
private TcpClient _client;
private bool _disposed;
public UnityTcpClient()
{
try
{
_client = new TcpClient();
_client.Connect("127.0.0.1", 12345);
Application.quitting += Dispose;
}
catch (Exception ex)
{
Debug.LogException(ex);
return;
}
}
public async Task<byte[]> SendAndReceiveData(byte[] data)
{
try
{
await using var stream = _client.GetStream();
await stream.WriteAsync(data, 0, data.Length);
var buffer = new byte[1024];
await stream.ReadAsync(buffer);
return 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;
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 39f71cfa622a1194b812b7b3cc5b86c1

View File

@ -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;
}
}
}