From 082c25b252dbbfd7e4a0f2b120ac908c21312d05 Mon Sep 17 00:00:00 2001 From: woni <320753691@qq.com> Date: Wed, 6 Sep 2023 23:37:35 +0800 Subject: [PATCH] Add project files. --- BotBridge.csproj | 12 ++++++++++++ BotBridge.sln | 25 ++++++++++++++++++++++++ BotExtHost.cs | 45 ++++++++++++++++++++++++++++++++++++++++++ BotExtPipe.cs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ Program.cs | 28 ++++++++++++++++++++++++++ 5 files changed, 161 insertions(+) create mode 100644 BotBridge.csproj create mode 100644 BotBridge.sln create mode 100644 BotExtHost.cs create mode 100644 BotExtPipe.cs create mode 100644 Program.cs diff --git a/BotBridge.csproj b/BotBridge.csproj new file mode 100644 index 0000000..fbb4a9b --- /dev/null +++ b/BotBridge.csproj @@ -0,0 +1,12 @@ + + + + Exe + net8.0 + enable + enable + true + true + + + diff --git a/BotBridge.sln b/BotBridge.sln new file mode 100644 index 0000000..60c36fe --- /dev/null +++ b/BotBridge.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34024.191 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotBridge", "BotBridge.csproj", "{81206CA0-73E6-48B0-B061-97131963EA7D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {81206CA0-73E6-48B0-B061-97131963EA7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {81206CA0-73E6-48B0-B061-97131963EA7D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {81206CA0-73E6-48B0-B061-97131963EA7D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {81206CA0-73E6-48B0-B061-97131963EA7D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FC656DCA-2289-41AA-91ED-49EFF75A916A} + EndGlobalSection +EndGlobal diff --git a/BotExtHost.cs b/BotExtHost.cs new file mode 100644 index 0000000..744868f --- /dev/null +++ b/BotExtHost.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BotBridge; + +internal class BotExtHost +{ + + private readonly Stream _stdIn = Console.OpenStandardInput(); + private readonly Stream _stdOut = Console.OpenStandardOutput(); + + + public void WriteData(byte[] data) + { + byte[] dataLength = BitConverter.GetBytes(data.Length); + if (BitConverter.IsLittleEndian) + { + Array.Reverse(dataLength); + } + _stdOut.Write(dataLength, 0, dataLength.Length); + + _stdOut.Write(data, 0, data.Length); + _stdIn.Flush(); + } + + public byte[] ReadData() + { + byte[] dataLengthBytes = new byte[4]; + _stdIn.Read(dataLengthBytes, 0, 4); + if (BitConverter.IsLittleEndian) + { + Array.Reverse(dataLengthBytes); + } + int dataLength = BitConverter.ToInt32(dataLengthBytes, 0); + + byte[] data = new byte[dataLength]; + _stdIn.Read(data, 0, dataLength); + return data; + } + + +} \ No newline at end of file diff --git a/BotExtPipe.cs b/BotExtPipe.cs new file mode 100644 index 0000000..c2a6113 --- /dev/null +++ b/BotExtPipe.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO.Pipes; + +namespace BotBridge; + +internal class BotExtPipe +{ + private readonly NamedPipeServerStream _pipeServerStream; + + public BotExtPipe(string pipeName) + { + _pipeServerStream = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); + } + + public void WaitForConnection() + { + _pipeServerStream.WaitForConnection(); + } + + public byte[] ReadData() + { + int messageLength = BitConverter.ToInt32(ReadBytes(_pipeServerStream, 4), 0); + return ReadBytes(_pipeServerStream, messageLength); + } + + public void WriteData(byte[] data) + { + byte[] dateLength = BitConverter.GetBytes(data.Length); + _pipeServerStream.Write(dateLength, 0, dateLength.Length); + _pipeServerStream.Write(data, 0, data.Length); + } + + + private static byte[] ReadBytes(NamedPipeServerStream pipeServerStream, int bytesToRead) + { + byte[] buffer = new byte[bytesToRead]; + int bytesRead = 0; + int totalBytesRead = 0; + do + { + bytesRead = pipeServerStream.Read(buffer, totalBytesRead, bytesToRead - totalBytesRead); + totalBytesRead += bytesRead; + } while (bytesRead > 0 && totalBytesRead < bytesToRead); + return buffer; + } + +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..203086b --- /dev/null +++ b/Program.cs @@ -0,0 +1,28 @@ +using System.Text; + +namespace BotBridge; + + +internal class Program +{ + static void Main(string[] args) + { + BotExtHost botExtHost = new BotExtHost(); + + while (true) + { + BotExtPipe botExtPipe = new BotExtPipe("BotExtPipe"); + botExtPipe.WaitForConnection(); + + while (true) + { + + byte[] clientData = botExtPipe.ReadData(); + botExtHost.WriteData(clientData); + byte[] serverData = botExtHost.ReadData(); + botExtPipe.WriteData(serverData); + + } + } + } +} \ No newline at end of file