Ejemplo n.º 1
0
        public static async Task RunAsync(string listenToAppServerPipeName, string sendToAppServerPipeName)
        {
            ConsoleOutput.ConsoleWriteLine("DataServer");

            NamedPipeServerAsync listenToAppServer = new NamedPipeServerAsync(listenToAppServerPipeName);

            TaskCompletionSource <object> taskCompletionSource = new TaskCompletionSource <object>();

            listenToAppServer.TokenReceivedEventAsync += async(t) =>
            {
                UpdateStatus(t, "R");

                if (!t.End)
                {
                    await Task.Delay(500).ConfigureAwait(false);
                }

                UpdateStatus(t, "D");

                await NamedPipeClientAsync.SendAsync(sendToAppServerPipeName, t).ConfigureAwait(false);

                UpdateStatus(t, "F");
            };

            await listenToAppServer.StartAsync().ConfigureAwait(false);
        }
Ejemplo n.º 2
0
        public static async Task RunAsync(int clientNumber, string clientListenPipeName, string clientSendPipeName, string dataserverListenPipeName, string dataserverSendPipeName)
        {
            Console.WriteLine($"AppServer Async Start {clientNumber}");

            using (NamedPipeServerAsync listenToClient = new NamedPipeServerAsync(clientListenPipeName))
            {
                using (NamedPipeServerAsync listenToDataServer = new NamedPipeServerAsync(dataserverListenPipeName))
                {
                    object lockId = new object();

                    listenToClient.TokenReceivedEventAsync += async(t) =>
                    {
                        var token = t;

                        // Start the time when the first value comes in from the first client
                        if (!Program.Start.HasValue)
                        {
                            Program.Start = ConsoleOutput.StartTime = DateTime.Now;
                        }

                        lock (lockId)
                        {
                            t.AppServerID = ID;
                            ID++;
                        }

                        ConsoleOutput.UpdateStatus("AppServer Async: ", token, "R");

                        // Key: Using Async allows the thread to go off and do other work
                        // until the DataServer responds
                        await NamedPipeClientAsync.SendAsync(dataserverSendPipeName, token).ConfigureAwait(false);

                        ConsoleOutput.UpdateStatus("AppServer Async: ", token, "D");
                    };

                    listenToDataServer.TokenReceivedEventAsync += async(t) =>
                    {
                        ConsoleOutput.UpdateStatus("AppServer Async: ", t, "C");
                        await NamedPipeClientAsync.SendAsync(clientSendPipeName, t).ConfigureAwait(false);

                        ConsoleOutput.UpdateStatus("AppServer Async: ", t, "F");
                    };

                    Task[] listenTasks = new Task[2];

                    listenTasks[0] = listenToClient.StartAsync();
                    listenTasks[1] = listenToDataServer.StartAsync();

                    await Task.WhenAll(listenTasks).ConfigureAwait(false);
                }
            }

            //Console.WriteLine($"AppServer Async End {clientNumber}");
        }
Ejemplo n.º 3
0
        public static async Task RunAsync(int numToSend, string sendToAppServerPipeName, string listenToAppServerPipeName)
        {
            object receivedLock = new object();
            int    received     = 0;

            Console.WriteLine($"Client Start {sendToAppServerPipeName}");

            status = new string[numToSend];

            for (var i = 0; i < status.Length; i++)
            {
                status[i] = "_";
            }

            List <Task> sendTasks = new List <Task>();

            using (var listen = new NamedPipeServerAsync(listenToAppServerPipeName))
            {
                List <int> tokens = new List <int>();
                TaskCompletionSource <object> taskCompletionSource = new TaskCompletionSource <object>();

                listen.TokenReceivedEventAsync += (t) =>
                {
                    UpdateStatus(t, "R");
                    if (!t.End)
                    {
                        lock (receivedLock)
                        {
                            received++;
                            tokens.Remove(t.ID);
                            if (tokens.Count == 0)
                            {
                                taskCompletionSource.SetResult(null);
                            }
                        }
                    }

                    return(Task.CompletedTask);
                };


                var listenTask = listen.StartAsync();

                for (var i = 0; i < numToSend; i++)
                {
                    var token = new Token(i);
                    tokens.Add(token.ID);

                    UpdateStatus(token, "S");

                    sendTasks.Add(NamedPipeClientAsync.SendAsync(sendToAppServerPipeName, token));
                }


                await Task.WhenAll(sendTasks.ToArray());

                await taskCompletionSource.Task;

                Console.WriteLine($"Client Send End Token {sendToAppServerPipeName}");

                var endToken = new Token(true);
                await NamedPipeClientAsync.SendAsync(sendToAppServerPipeName, endToken).ConfigureAwait(false);

                await listenTask; // Wait for the End Token to be received and exit the pipe infinit loop
            }


            // Send Token.End = true token to shut down the AppServer (but not the DataServer in case there are multiple clients)

            if (received != numToSend)
            {
                throw new Exception($"Failure: Number sent {numToSend} Number received {received}");
            }

            Console.WriteLine($"Client End {sendToAppServerPipeName}");
        }
Ejemplo n.º 4
0
 public void TestInitialize()
 {
     pipeName        = Guid.NewGuid().ToString();
     namedPipeServer = new NamedPipeServerAsync(pipeName);
 }