not working

This commit is contained in:
Matthias Heil
2026-03-31 18:48:55 +02:00
parent ac7149fb9b
commit 83f8031862
12 changed files with 215 additions and 1 deletions

View File

@@ -0,0 +1,47 @@
using System;
using System.IO;
using System.Text;
namespace NamedPipes
{
// Defines the data protocol for reading and writing strings on our stream.
public class StreamString
{
private readonly Stream ioStream;
private readonly UnicodeEncoding streamEncoding;
public StreamString(Stream ioStream)
{
this.ioStream = ioStream;
streamEncoding = new UnicodeEncoding();
}
public string ReadString()
{
int len;
len = ioStream.ReadByte() * 256;
len += ioStream.ReadByte();
var inBuffer = new byte[len];
ioStream.Read(inBuffer, 0, len);
return streamEncoding.GetString(inBuffer);
}
public int WriteString(string outString)
{
byte[] outBuffer = streamEncoding.GetBytes(outString);
int len = outBuffer.Length;
if (len > UInt16.MaxValue)
{
len = (int)UInt16.MaxValue;
}
ioStream.WriteByte((byte)(len / 256));
ioStream.WriteByte((byte)(len & 255));
ioStream.Write(outBuffer, 0, len);
ioStream.Flush();
return outBuffer.Length + 2;
}
}
}