Migrate from Grpc.Core to Grpc.Net.Client

pull/3728/head
2dust 2023-04-14 15:51:34 +08:00
parent 34b4b9d099
commit 165454dc57
3 changed files with 25 additions and 26 deletions

View File

@ -10,7 +10,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.22.3" /> <PackageReference Include="Google.Protobuf" Version="3.22.3" />
<PackageReference Include="Grpc.Core" Version="2.46.6" /> <PackageReference Include="Grpc.Net.Client" Version="2.52.0" />
<PackageReference Include="Grpc.Tools" Version="2.53.0"> <PackageReference Include="Grpc.Tools" Version="2.53.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@ -65,10 +65,10 @@ namespace v2rayN
Environment.Exit(0); Environment.Exit(0);
return; return;
} }
if (RuntimeInformation.ProcessArchitecture != Architecture.X86 && RuntimeInformation.ProcessArchitecture != Architecture.X64) //if (RuntimeInformation.ProcessArchitecture != Architecture.X86 && RuntimeInformation.ProcessArchitecture != Architecture.X64)
{ //{
_config.guiItem.enableStatistics = false; // _config.guiItem.enableStatistics = false;
} //}
} }
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{ {

View File

@ -1,4 +1,5 @@
using Grpc.Core; using Grpc.Core;
using Grpc.Net.Client;
using ProtosLib.Statistics; using ProtosLib.Statistics;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@ -7,17 +8,17 @@ using v2rayN.Mode;
namespace v2rayN.Handler namespace v2rayN.Handler
{ {
class StatisticsHandler internal class StatisticsHandler
{ {
private Mode.Config config_; private Mode.Config config_;
private Channel channel_; private GrpcChannel _channel;
private StatsService.StatsServiceClient client_; private StatsService.StatsServiceClient _client;
private bool exitFlag_; private bool _exitFlag;
private ServerStatItem? _serverStatItem; private ServerStatItem? _serverStatItem;
private List<ServerStatItem> _lstServerStat; private List<ServerStatItem> _lstServerStat;
public List<ServerStatItem> ServerStat => _lstServerStat; public List<ServerStatItem> ServerStat => _lstServerStat;
Action<ServerSpeedItem> updateFunc_; private Action<ServerSpeedItem> _updateFunc;
public bool Enable public bool Enable
{ {
@ -28,8 +29,8 @@ namespace v2rayN.Handler
{ {
config_ = config; config_ = config;
Enable = config.guiItem.enableStatistics; Enable = config.guiItem.enableStatistics;
updateFunc_ = update; _updateFunc = update;
exitFlag_ = false; _exitFlag = false;
Init(); Init();
GrpcInit(); GrpcInit();
@ -39,13 +40,12 @@ namespace v2rayN.Handler
private void GrpcInit() private void GrpcInit()
{ {
if (channel_ == null) if (_channel == null)
{ {
Global.statePort = GetFreePort(); Global.statePort = GetFreePort();
channel_ = new Channel($"{Global.Loopback}:{Global.statePort}", ChannelCredentials.Insecure); _channel = GrpcChannel.ForAddress($"{Global.httpProtocol}{Global.Loopback}:{Global.statePort}");
channel_.ConnectAsync(); _client = new StatsService.StatsServiceClient(_channel);
client_ = new StatsService.StatsServiceClient(channel_);
} }
} }
@ -53,8 +53,8 @@ namespace v2rayN.Handler
{ {
try try
{ {
exitFlag_ = true; _exitFlag = true;
channel_.ShutdownAsync(); //channel_.ShutdownAsync();
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -62,18 +62,18 @@ namespace v2rayN.Handler
} }
} }
public void Run() public async void Run()
{ {
while (!exitFlag_) while (!_exitFlag)
{ {
try try
{ {
if (Enable && channel_.State == ChannelState.Ready) if (Enable && _channel.State == ConnectivityState.Ready)
{ {
QueryStatsResponse? res = null; QueryStatsResponse? res = null;
try try
{ {
res = client_.QueryStats(new QueryStatsRequest() { Pattern = "", Reset = true }); res = await _client.QueryStatsAsync(new QueryStatsRequest() { Pattern = "", Reset = true });
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -99,13 +99,13 @@ namespace v2rayN.Handler
server.todayDown = _serverStatItem.todayDown; server.todayDown = _serverStatItem.todayDown;
server.totalUp = _serverStatItem.totalUp; server.totalUp = _serverStatItem.totalUp;
server.totalDown = _serverStatItem.totalDown; server.totalDown = _serverStatItem.totalDown;
updateFunc_(server); _updateFunc(server);
} }
} }
} }
var sleep = config_.guiItem.statisticsFreshRate < 1 ? 1 : config_.guiItem.statisticsFreshRate; var sleep = config_.guiItem.statisticsFreshRate < 1 ? 1 : config_.guiItem.statisticsFreshRate;
Thread.Sleep(1000 * sleep); Thread.Sleep(1000 * sleep);
channel_.ConnectAsync(); await _channel.ConnectAsync();
} }
catch catch
{ {
@ -182,7 +182,6 @@ namespace v2rayN.Handler
server = new(); server = new();
try try
{ {
foreach (Stat stat in source) foreach (Stat stat in source)
{ {
string name = stat.Name; string name = stat.Name;
@ -245,4 +244,4 @@ namespace v2rayN.Handler
} }
} }
} }
} }