Named Pipe: First data transfer after starting the app is very slow

573 Views Asked by At

I have a client and a server application and want to send serialized small objects from the client to the server via a Named Pipe. It works very well, apart from the very first transfer: It takes up to two seconds every time after i started the applications. Following transfers are almost instant.

Here is my Server Code:

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;
            }
        }
    }
}

And here is the client:

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);
        }
    }
}

The data being transfered is kind of a stock exchange order which is also highly time sensitive, so for me it is crucial that also the very first order works without delay.

Is there any other way to make also the first usage of the named pipe as fast as the other ones? Some sort of precompiling?

I really want to avoid additional threads checking if the connection can be established and sending dummy data every x seconds just to "warm up" the respective objects which cause the delay.

(BTW: Code is not from me, I got it from here!)

0

There are 0 best solutions below