我使用命名管道进行 C# 和 Delphi 之间的过程间通信。 C# 使用System.IO.Pipes
包,而 Delphi 使用Libby's pipes.pas
。不幸的是,通信几乎是高性能的:分析显示通信占用了整个运行时间的 72%,其余的用于计算。
我能够找到一个可能占用资源的问题:如果我没有在 Delphi 中显式断开发送客户端的连接,C# 根本不接收任何数据.
德尔福(发送)
FClient1.Write(msg[1], Length(msg));
FClient1.FlushPipeBuffers;
FClient1.WaitForReply(20);
FClient1.Disconnect; // disconnect to signalize C# that the writing is finished
FClient1.Connect; // connect again to prevent synchronization problems
C#(接收)
// Wait for a client to connect
stc.pipeServer.WaitForConnection();
while (reconnect_attempts < MAX_RECONNECT_ATTEMPTS) //
{
string tmp = sr.ReadLine();
// if result is empty, try again for <MAX_RECONNECT_ATTEMPTS> times
// so you can eliminate the chance that there's just a single empty request
while (tmp != null)// && result != tmp)
{
tmp = sr.ReadLine();
result += tmp;
}
// sleep, increment reconnect, write debugging...
}
stc.pipeServer.Close();
尽管我猜重新连接的成本很高,但我并不完全确定。一个数据流(大约 1 / 11 kb)总共需要 130 毫秒(11kb 分别为 270 毫秒)(发送和接收)。
我的问题是:
是否有必要强制断开管道以表明客户端已完成写入?就我的观察而言,仅当使用 libby 发送时才需要此操作。是否还有其他可能导致性能不佳的原因?提前致谢。
另外,这里是相反的发送和接收:
C#(发送)
stc.pipeClient.Connect();
StreamWriter sw = new StreamWriter(stc.pipeClient);
//sw.AutoFlush = true;
sw.WriteLine(msg);
sw.Flush();
stc.pipeClient.WaitForPipeDrain(); // waits for the other end to read all bytes
// neither disconnect nor dispose
德尔福(接收)
SetLength(S, Stream.Size); Stream.Read(S[1], Length(S));
FPipeBuffer := FPipeBuffer + S; { TODO 2 : switch case ID }
// if the XML is complete, i.e. ends with the closing checksum
if (IsFullMessage()) then
begin
// end reading, set flag
FIsPipeReady := true;
end