2024-09-23 01:13:24 +00:00
using System.Collections.Concurrent ;
2024-09-23 09:46:46 +00:00
using System.Text ;
2024-09-22 08:12:30 +00:00
using System.Text.Json ;
using Common ;
2024-09-26 09:49:40 +00:00
using Microsoft.AspNetCore.Hosting.Server ;
2024-09-24 09:46:52 +00:00
using RemoteServer ;
2024-09-22 08:12:30 +00:00
namespace ServerTest
{
2024-09-23 09:46:46 +00:00
public class TestPipe ( bool isAES , string id ) : AbsPipeLine ( isAES )
2024-09-22 08:12:30 +00:00
{
private readonly BlockingCollection < Func < byte [ ] > > EventQueue =
new BlockingCollection < Func < byte [ ] > > ( ) ;
private readonly CancellationTokenSource Cts = new CancellationTokenSource ( ) ;
public TestPipe ? Other ;
2024-09-23 09:46:46 +00:00
public string? ErrResult ;
public string Id = id ;
2024-10-12 06:30:56 +00:00
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
2024-09-26 09:49:40 +00:00
public static RemoteSyncServerFactory syncServerFactory ;
2024-10-12 06:30:56 +00:00
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
2024-09-22 08:12:30 +00:00
public override async IAsyncEnumerable < int > Work (
Func < byte [ ] , bool > receiveCb ,
string addr = ""
)
{
yield return 0 ;
await Listen ( receiveCb ) ;
yield return 1 ;
}
public override async Task UploadFile (
string dst ,
2024-09-24 09:46:52 +00:00
string filePath ,
2024-09-22 08:12:30 +00:00
Func < double , bool > progressCb
)
{
2024-09-24 09:46:52 +00:00
dst = Path . Combine ( RemoteSyncServer . TempRootFile , Path . GetFileName ( filePath ) ) ;
2024-09-22 08:12:30 +00:00
await Task . Run ( ( ) = >
{
2024-09-26 09:49:40 +00:00
System . IO . File . Copy ( filePath , dst , true ) ;
2024-09-22 08:12:30 +00:00
progressCb ( 100 ) ;
2024-09-24 09:46:52 +00:00
//if (!Directory.Exists(dst))
//{
// Directory.CreateDirectory(dst);
//}
2024-09-26 09:49:40 +00:00
Task . Run ( ( ) = >
{
2024-10-12 06:30:56 +00:00
var it = syncServerFactory . GetServerByName ( "Test" ) ? ? throw new NullReferenceException ( "找不到服务名称!" ) ;
2024-09-26 09:49:40 +00:00
var h = new UnPackAndReleaseHelper ( it ) ;
2024-09-27 07:02:55 +00:00
it . SetStateHelpBase ( h ) ;
2024-09-26 09:49:40 +00:00
h . UnPack ( ) ;
} ) ;
2024-09-22 08:12:30 +00:00
} ) ;
}
public override async Task Close ( string? CloseReason )
{
2024-09-23 09:46:46 +00:00
ErrResult = CloseReason ;
var Id = this . Id ;
Cts . Cancel ( ) ;
if ( Other ! = null )
2024-09-22 08:12:30 +00:00
{
2024-09-23 09:46:46 +00:00
if ( Other . ErrResult = = null )
{
await Other . Close ( CloseReason ) ;
}
}
2024-09-22 08:12:30 +00:00
}
public new async Task ReceiveMsg ( SyncMsg msg )
{
await Task . Run ( ( ) = >
{
EventQueue . Add ( ( ) = >
{
2024-09-23 09:46:46 +00:00
return JsonSerializer . SerializeToUtf8Bytes ( msg ) ;
2024-09-22 08:12:30 +00:00
} ) ;
} ) ;
}
public override async Task SendMsg ( SyncMsg msg )
{
if ( Other = = null )
{
throw new Exception ( "can't be null" ) ;
}
2024-09-23 09:46:46 +00:00
var r = JsonSerializer . SerializeToUtf8Bytes ( msg ) ;
if ( IsAES )
{
var str = Encoding . UTF8 . GetString ( r ) ;
var t = AESHelper . EncryptStringToBytes_Aes ( str ) ;
var f = AESHelper . DecryptStringFromBytes_Aes ( t ) ;
#pragma warning disable CS8604 // 引用类型参数可能为 null。
await Other . ReceiveMsg ( JsonSerializer . Deserialize < SyncMsg > ( f ) ) ;
#pragma warning restore CS8604 // 引用类型参数可能为 null。
}
else
{
#pragma warning disable CS8604 // 引用类型参数可能为 null。
await Other . ReceiveMsg ( JsonSerializer . Deserialize < SyncMsg > ( r ) ) ;
#pragma warning restore CS8604 // 引用类型参数可能为 null。
}
2024-09-22 08:12:30 +00:00
}
protected override async Task Listen ( Func < byte [ ] , bool > receiveCb )
{
2024-09-23 09:46:46 +00:00
try
2024-09-22 08:12:30 +00:00
{
2024-09-23 09:46:46 +00:00
foreach ( var eventTask in EventQueue . GetConsumingEnumerable ( Cts . Token ) )
2024-09-22 08:12:30 +00:00
{
await Task . Run ( ( ) = >
{
var r = eventTask ( ) ;
receiveCb ( r ) ;
} ) ;
}
}
2024-09-23 09:46:46 +00:00
catch ( OperationCanceledException )
{
//var x = 1;
var id = Id ;
//抛出异常 从 p3 传递到 p2
2024-09-26 09:49:40 +00:00
if ( ErrResult = = "正常退出!" )
{
return ;
}
else
{
throw new Exception ( ErrResult ) ;
}
2024-09-23 09:46:46 +00:00
}
2024-09-22 08:12:30 +00:00
}
}
}