Example #1
0
        private static void SendResponse(Socket client, ICommand receivedCommand, ExecuteCommandVisitor visitor)
        {
            var response = new ResponseCommand(receivedCommand, visitor.Message);

            client.SendCompletelyWithEof(response.ToBytes());
            Console.WriteLine($"Responded with {response}.");
        }
Example #2
0
        private static void HandleClient(Socket client, ImmutableArray <ConcurrentQueue <ICommand> > queues)
        {
            try
            {
                var visitor = new ExecuteCommandVisitor(() => _root, Factory, Initialize, Timestamp);
                var buffer  = new byte[32000];

                while (!visitor.Exit)
                {
                    var data            = client.ReceiveUntilEof(buffer);
                    var receivedCommand = data.To <ICommand>();

                    Console.WriteLine(receivedCommand);
                    receivedCommand.Accept(visitor);
                    var treeClone       = _root.Clone();
                    var timestampClone  = Timestamp.Clone();
                    var statefulCommand = new StatefulCommand(treeClone, receivedCommand, timestampClone);

                    Responses.Clear();

                    foreach (var queue in queues)
                    {
                        queue.Enqueue(statefulCommand);
                    }

                    if (visitor.AwaitResponse)
                    {
                        ReceiveAndPickResponse(client, receivedCommand);
                    }
                    else
                    {
                        SendResponse(client, receivedCommand, visitor);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }