Files
BotExtBridge/BotExtHost.cs
T
2023-09-06 23:37:35 +08:00

45 lines
1.0 KiB
C#

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