public static async Task BroadcastAsync(TcpClient[] clients, IMessageProtocol messageProtocol)
        {
            var bytes = MessageCoder.Encode(messageProtocol);

            foreach (var client in clients)
            {
                await WriteAsync(client, bytes);
            }
        }
        public void EncodeWithInvalidName()
        {
            MessageCoder coder = new MessageCoder();

            coder.Name = string.Empty;

            string result = coder.Encode();

            Assert.Equal(invalidEncodedResult, result);
        }
        public void EncodeWithValidName()
        {
            MessageCoder coder = new MessageCoder();

            coder.Name = nameToTest;

            string result = coder.Encode();

            Assert.Equal(validEncodedResult, result);
        }
Exemple #4
0
        static void Main(string[] args)
        {
            const string quitInput = "quit";                // The value to use to indicate that you want to quit the program.
            string       queueName = Constants.ChannelName; // The queue name.

            PrintHeader(quitInput);                         // Prints the header and the instuctions.

            // Setup the communications to the MQ service.
            LocalConnectionFactory factory    = new LocalConnectionFactory();
            IConnection            connection = factory.CreateConnection();
            IModel model = connection.CreateModel();

            model.QueueDeclare(queueName, false, false, false, null);


            // Start the input loop.
            string message;

            byte[]       body;
            MessageCoder messageCoder = new MessageCoder();
            string       nameInput    = GetConsoleInputForName();

            while (nameInput.ToLower() != quitInput)
            {
                // Build the message using our very sofisticated protocol.
                messageCoder.Name = nameInput;
                message           = messageCoder.Encode();

                // The body for the communication must be a byte array.
                body = Encoding.UTF8.GetBytes(message);

                // Publish the message to the queue.
                model.BasicPublish("", queueName, null, body);

                // Indicate that the message was send.
                IndicateSend(message);

                nameInput = GetConsoleInputForName();
            }

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Shutting down Publisher Side...");

            // Close down the communications.
            model.Close();
            connection.Close();
        }
        public static async Task <int> WriteAsync(TcpClient client, IMessageProtocol messageProtocol)
        {
            if (client.Connected)
            {
                var ns = client.GetStream();

                if (ns.CanWrite)
                {
                    var bytes = MessageCoder.Encode(messageProtocol);
                    await ns.WriteAsync(bytes, 0, bytes.Length);

                    await ns.FlushAsync();

                    return(bytes.Length);
                }
            }

            return(0);
        }