реализация модуля потокового модуля взаимодействия
This commit is contained in:
@@ -5,12 +5,12 @@
|
||||
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
|
||||
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
|
||||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
|
||||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.10.1</NuGetToolVersion>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.0</NuGetToolVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<SourceRoot Include="C:\Users\Mimm\.nuget\packages\" />
|
||||
<SourceRoot Include="C:\Users\Mikhail\.nuget\packages\" />
|
||||
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
|
||||
</ItemGroup>
|
||||
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
@@ -21,6 +21,6 @@
|
||||
<Import Project="$(NuGetPackageRoot)microsoft.net.test.sdk\17.12.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.props" Condition="Exists('$(NuGetPackageRoot)microsoft.net.test.sdk\17.12.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.props')" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<PkgNUnit_Analyzers Condition=" '$(PkgNUnit_Analyzers)' == '' ">C:\Users\Mimm\.nuget\packages\nunit.analyzers\4.6.0</PkgNUnit_Analyzers>
|
||||
<PkgNUnit_Analyzers Condition=" '$(PkgNUnit_Analyzers)' == '' ">C:\Users\Mikhail\.nuget\packages\nunit.analyzers\4.6.0</PkgNUnit_Analyzers>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,54 @@
|
||||
namespace mROA.Implementation;
|
||||
|
||||
public class StreamBasedInteractionModule : IInteractionModule
|
||||
{
|
||||
private Dictionary<int, Stream> _streams = new();
|
||||
private Action<int, byte[]> _handler;
|
||||
|
||||
public void RegisterClient(Stream stream)
|
||||
{
|
||||
var id = Random.Shared.Next();
|
||||
_streams.Add(id, stream);
|
||||
ListenTo((id, stream), _handler);
|
||||
}
|
||||
|
||||
public void SetMessageHandler(Action<int, byte[]> handler)
|
||||
{
|
||||
_handler = handler;
|
||||
}
|
||||
|
||||
public void SendTo(int clientId, byte[] message)
|
||||
{
|
||||
if (!_streams.TryGetValue(clientId, out var stream))
|
||||
{
|
||||
throw new KeyNotFoundException($"Client {clientId} not found");
|
||||
}
|
||||
|
||||
stream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort));
|
||||
stream.Write(message, 0, message.Length);
|
||||
|
||||
}
|
||||
|
||||
private async Task ListenTo((int id, Stream stream) client, Action<int, byte[]> action)
|
||||
{
|
||||
const int bufferSize = ushort.MaxValue;
|
||||
try
|
||||
{
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
while (client.stream.CanRead)
|
||||
{
|
||||
await client.stream.ReadExactlyAsync(buffer, 0, 2);
|
||||
var len = BitConverter.ToUInt16(buffer, 0);
|
||||
await client.stream.ReadExactlyAsync(buffer, 0, len);
|
||||
|
||||
action(client.id, buffer[..len]);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Client handling finished:{client.id}");
|
||||
_streams.Remove(client.id);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ public class ConsoleFrontendInteractionModule : IInteractionModule.IFrontendInte
|
||||
{
|
||||
public byte[] ReceiveMessage()
|
||||
{
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public void PostMessage(byte[] message)
|
||||
|
||||
@@ -5,12 +5,12 @@
|
||||
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
|
||||
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
|
||||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
|
||||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.10.1</NuGetToolVersion>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.0</NuGetToolVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<SourceRoot Include="C:\Users\Mimm\.nuget\packages\" />
|
||||
<SourceRoot Include="C:\Users\Mikhail\.nuget\packages\" />
|
||||
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user