Frozen Breadstick
Frozen Breadstick
CC#
Created by Frozen Breadstick on 5/20/2025 in #help
✅ Windows Development NamedPipeServerStream in dll halting communications with client
Hi All, I have a C# dll that I am making to side load with a game in roder to expotr data from the game to be processed by a python machine learning algorithm. Right now I am trying to use a NamedPipeServerStream but I am having unexpected issues with communications suddenly halting with no explanation as to why.
C#
public static void Write()
{
try
{
while (true)
{
string local = null;
byte[] bytes;
lock (Gary)
{
local = data + "\n";
}
bytes = Encoding.UTF8.GetBytes(local);
pipeServer.Write(bytes, 0, bytes.Length);
pipeServer.Flush(); // Flush the pipe to ensure all data is sent
Log("Wrote", "write_log.txt");
Thread.Sleep(3); // Sleep for a short time to prevent high CPU usage
}
}
catch (Exception ex)
{
Log($"Error in Write: {ex.Message}", "write_log.txt");
}

}

public static void Read()
{
var sb = new StringBuilder();
while (true)
{
Log("Reading", "read_log.txt");
int b = pipeServer.ReadByte(); // ReadByte blocks until a byte is available, returns -1 if the pipe is closed
Log("Read","read_log.txt");
if (b == -1)
{
Log("Pipe Closed","read_log.txt");
break; // pipe closed
}
if (b == '\n')
{
// Got a full message
string data = sb.ToString().TrimEnd('\r');
var parts = data.Split(',');
lock (Nathaniel)
{
for (int i = 0; i < input.Length && i < parts.Length; i++) input[i] = parts[i] == "1";
}
sb.Clear();
Log("Read full string", "read_log.txt");
}
else
{
sb.Append((char)b);
}
}
}
C#
public static void Write()
{
try
{
while (true)
{
string local = null;
byte[] bytes;
lock (Gary)
{
local = data + "\n";
}
bytes = Encoding.UTF8.GetBytes(local);
pipeServer.Write(bytes, 0, bytes.Length);
pipeServer.Flush(); // Flush the pipe to ensure all data is sent
Log("Wrote", "write_log.txt");
Thread.Sleep(3); // Sleep for a short time to prevent high CPU usage
}
}
catch (Exception ex)
{
Log($"Error in Write: {ex.Message}", "write_log.txt");
}

}

public static void Read()
{
var sb = new StringBuilder();
while (true)
{
Log("Reading", "read_log.txt");
int b = pipeServer.ReadByte(); // ReadByte blocks until a byte is available, returns -1 if the pipe is closed
Log("Read","read_log.txt");
if (b == -1)
{
Log("Pipe Closed","read_log.txt");
break; // pipe closed
}
if (b == '\n')
{
// Got a full message
string data = sb.ToString().TrimEnd('\r');
var parts = data.Split(',');
lock (Nathaniel)
{
for (int i = 0; i < input.Length && i < parts.Length; i++) input[i] = parts[i] == "1";
}
sb.Clear();
Log("Read full string", "read_log.txt");
}
else
{
sb.Append((char)b);
}
}
}
Above are my Read and Write functions (I am avoiding using StreamReaders and StreamWriters) that run in seperate threads to preserve performance at the core of the main dll. My python client is in my first reply. When the program starts I get two data transmissions that are picked up by the client and one full read by the C# dll but then reading and writing stops on the C# side and as such the python also stops. I need serious help here as I have been bashing my head against a wall for days trying to figure it out. My assumptions is maybe the threads are being killed by some lurking AV process maybe?
254 replies