クライアントとサーバーの NamedPipeStream を使用しています。クライアントからサーバーにデータを送信しています。データは、バイナリ データを含むシリアル化オブジェクトです。
サーバー側がデータを受信すると、クライアントがさらに多くのデータを送信している間、常に最大 1024 サイズになります!! そのため、データをシリアル化しようとすると、次の例外が発生します: "Unterminated string. Expected delimiter: ". パス「データ」、1 行目、位置 1024."
次のように定義されたサーバーのバッファサイズ:
protected const int BUFFER_SIZE = 4096*4;
var stream = new NamedPipeServerStream(PipeName,
PipeDirection.InOut,
1,
PipeTransmissionMode.Message,
PipeOptions.Asynchronous,
BUFFER_SIZE,
BUFFER_SIZE,
pipeSecurity);
stream.ReadMode = PipeTransmissionMode.Message;
私は使用しています:
/// <summary>
/// StreamWriter for writing messages to the pipe.
/// </summary>
protected StreamWriter PipeWriter { get; set; }
読み取り機能:
/// <summary>
/// Reads a message from the pipe.
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
protected static byte[] ReadMessage(PipeStream stream)
{
MemoryStream memoryStream = new MemoryStream();
byte[] buffer = new byte[BUFFER_SIZE];
try
{
do
{
if (stream != null)
{
memoryStream.Write(buffer, 0, stream.Read(buffer, 0, buffer.Length));
}
} while ((m_stopRequested != false) && (stream != null) && (stream.IsMessageComplete == false));
}
catch
{
return null;
}
return memoryStream.ToArray();
}
protected override void ReadFromPipe(object state)
{
//int i = 0;
try
{
while (Pipe != null && m_stopRequested == false)
{
PipeConnectedSignal.Reset();
if (Pipe.IsConnected == false)
{//Pipe.WaitForConnection();
var asyncResult = Pipe.BeginWaitForConnection(PipeConnected, this);
if (asyncResult.AsyncWaitHandle.WaitOne(5000))
{
if (Pipe != null)
{
Pipe.EndWaitForConnection(asyncResult);
// ...
//success;
}
}
else
{
continue;
}
}
if (Pipe != null && Pipe.CanRead)
{
byte[] msg = ReadMessage(Pipe);
if (msg != null)
{
ThrowOnReceivedMessage(msg);
}
}
}
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(" PipeName.ReadFromPipe Ex:" + ex.Message);
}
}
クライアント側でバッファ サイズを定義または変更できる場所がわかりません。
何か案が?!