クライアントとサーバー アプリケーションがあり、シリアル化された小さなオブジェクトをクライアントからサーバーに名前付きパイプ経由で送信したいと考えています。最初の転送を除けば、非常にうまく機能します。アプリケーションを起動してから毎回最大2秒かかります。次の転送はほぼ瞬時に行われます。
ここに私のサーバーコードがあります:
class PipeServer
{
public static string PipeName = @"OrderPipe";
public static async Task<Order> Listen()
{
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
{
await Task.Factory.FromAsync(pipeServer.BeginWaitForConnection, pipeServer.EndWaitForConnection, null);
using (StreamReader reader = new StreamReader(pipeServer))
{
string text = await reader.ReadToEndAsync();
var order = JsonConvert.DeserializeObject<Order>(text);
Console.WriteLine(DateTime.Now + ": Order recieved from Client: " + order.Zertifikat.Wkn + " with price " + order.PriceItem.Price + " with time " + order.PriceItem.Time);
return order;
}
}
}
}
そして、ここにクライアントがあります:
class PipeClient
{
public static string PipeName = @"OrderPipe";
private static async Task SendAwait(Order order, int timeOut = 10)
{
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous))
{
pipeStream.Connect(timeOut);
Console.WriteLine(DateTime.Now + ": Pipe connection to Trader established.");
using (StreamWriter sw = new StreamWriter(pipeStream))
{
string orderSerialized = JsonConvert.SerializeObject(order);
await sw.WriteAsync(orderSerialized);
Console.WriteLine(DateTime.Now + ": Order sent.");
// flush
await pipeStream.FlushAsync();
}
}
}
public static async void SendOrder(Order order)
{
try
{
await SendAwait(order);
}
catch (TimeoutException)
{
Console.WriteLine("Order was not sent because Server could not be reached.");
}
catch (IOException e)
{
Console.WriteLine("Order was not sent because an Exception occured: " + e.Message);
}
}
}
転送されるデータは一種の証券取引所の注文であり、これも非常に時間に敏感です。そのため、私にとっては、最初の注文も遅延なく機能することが重要です。
名前付きパイプの最初の使用を他のものと同じくらい速くする他の方法はありますか? ある種のプリコンパイル?
追加のスレッドが接続を確立できるかどうかをチェックし、遅延を引き起こすそれぞれのオブジェクトを「ウォームアップ」するためにx秒ごとにダミーデータを送信することを本当に避けたいです。
(ところで:コードは私からのものではありません。ここから入手しました!)