我使用Async Duplex命名管道在三个进程之间发送消息。
发件人,缓冲区和接收者。缓冲区有两个示例代码实例,涉及从接收器进入的一个管道和一个到接收器的管道。
只要我想要一个实例就可以完美运行。
缓冲区中有两个实例,它可以像我预期的那样正常工作,直到以每秒2000个数据包运行大约10分钟后,其中一个服务器管道停止工作,没有例外,没有中断,没有任何信息除了服务器管道似乎没有警告就关闭。
有没有人遇到过这样的问题?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Pipes;
using System.Threading;
using System.Security.AccessControl;
using System.Security.Principal;
namespace Test_Processor
{
public class MessageHandler
{
//Transmit Pipe
NamedPipeClientStream TXPipe;
//Receive Pipe
NamedPipeServerStream RXPipe;
//Store Pipe Names
string _TXName;
string _RXName;
//Read Buffer to store Read Data
byte[] ReadBuffer = new byte[1040];
//Write Buffer to store write data
byte[] WriteBuffer = new byte[1040];
//Store for the latest message received
public byte[] ReceivedMessage;
//Latest error message received
public string ErrorMessage = "";
//Time of the last messages
public DateTime LastMessageReceived = new DateTime(1970, 01, 01);
public DateTime LastMessageSent = new DateTime(1970, 01, 01);
//Boolean connected variable
public Boolean IsConnected = false;
//Custom Events
public event EventHandler OnReceivedData;
public event EventHandler OnError;
public event EventHandler OnDisconnect;
//Create instance
public MessageHandler(string TXName, string RXName)
{
try
{
//Create Received Pipe
RXPipe = new NamedPipeServerStream(RXName, PipeDirection.In, 10, PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.WriteThrough);
//Create Transmit Pipe
TXPipe = new NamedPipeClientStream(".", TXName, PipeDirection.Out, PipeOptions.Asynchronous | PipeOptions.WriteThrough);
//Store the names
_TXName = TXName;
_RXName = RXName;
//Put the listener in a thread to keep everything else running
Thread WorkerThread = new Thread(new ThreadStart(StartListener));
WorkerThread.IsBackground = true;
WorkerThread.Start();
}
catch (Exception e)
{
ErrorMessage = e.ToString();
ErrorHandler(EventArgs.Empty);
}
}
//Start Listener waits for the next connection
public void StartListener()
{
try
{
//Begin the async call
RXPipe.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), RXPipe);
}
catch (Exception e)
{
ErrorMessage = e.ToString();
ErrorHandler(EventArgs.Empty);
}
}
//Callback for the async callback
private void WaitForConnectionCallBack(IAsyncResult iar)
{
try
{
// Get the pipe
NamedPipeServerStream pipeServer = (NamedPipeServerStream)iar.AsyncState;
// End waiting for the connection
pipeServer.EndWaitForConnection(iar);
//wait for data to be read
BeginRead(pipeServer);
}
catch
{
return;
}
}
//Begin read event to begin receiving data
private void BeginRead(NamedPipeServerStream Pipe)
{
// Asynchronously read a request from the client
bool isConnected = Pipe.IsConnected;
if (isConnected)
{
try
{
//Pipe.BeginRead(pd.data, 0, pd.data.Length, OnAsyncMessage, pd);
Pipe.BeginRead(ReadBuffer, 0, 1040, OnAsyncMessage, Pipe);
}
catch (Exception)
{
isConnected = false;
}
}
if (!isConnected)
{
//not connected so kill the pipe
Pipe.Close();
DisconnectHandler(EventArgs.Empty);
}
}
//Async handler for the end of the message
private void OnAsyncMessage(IAsyncResult result)
{
// Async read from client completed
NamedPipeServerStream pd = (NamedPipeServerStream)result.AsyncState;
//Read all the data
Int32 bytesRead = pd.EndRead(result);
if (bytesRead != 0)
{
LastMessageReceived = DateTime.Now;
ReceivedMessage = ReadBuffer;
//Call the on received event
ReceivedData(EventArgs.Empty);
}
BeginRead(pd);
}
//Send the Async Data to Pipe
public bool SendAsyncData(byte[] SendData)
{
try
{
//If its not connected connect
if (!TXPipe.IsConnected)
{
TXPipe.Connect(500);
}
//put data in the buffer
WriteBuffer = SendData;
//Begin writing to the pipe
TXPipe.BeginWrite(WriteBuffer, 0, 1040, OnAsyncWriteComplete, TXPipe);
return true;
}
catch (Exception ex)
{
Console.Write(ex.ToString());
return false;
}
}
//On Write Complete
private void OnAsyncWriteComplete(IAsyncResult result)
{
//Finished
PipeStream pipe = (PipeStream)result.AsyncState;
pipe.EndWrite(result);
WriteBuffer = new byte[1040];
}
//Custom Event Handlers
protected virtual void ReceivedData(EventArgs e)
{
EventHandler handler = OnReceivedData;
if (handler != null)
{
handler(this, e);
}
}
protected virtual void ErrorHandler(EventArgs e)
{
EventHandler handler = OnError;
if (handler != null)
{
handler(this, e);
}
}
protected virtual void DisconnectHandler(EventArgs e)
{
EventHandler handler = OnDisconnect;
if (handler != null)
{
handler(this, e);
}
}
}
}