示例#1
0
        public void MultipleMessagesToBytesTest()
        {
            MessageToBytesConverter converter = new MessageToBytesConverter();

            Message[] messages = new Message[] {
                new DivideProblem()
                {
                    ProblemType        = "abc",
                    ComputationalNodes = 696
                }
                , new Register()
                {
                    Type = new RegisterType()
                    {
                        Value = ComponentType.CommunicationServer,
                    },
                    Id          = 212,
                    IdSpecified = true
                }, new Status()
                {
                    Id      = 10,
                    Threads = new StatusThread[] { new StatusThread()
                                                   {
                                                       ProblemType = "def"
                                                   } }
                }
            };
            byte[] bytes;
            int    count = converter.MessagesToBytes(out bytes, messages);

            Assert.IsTrue(count > 0);

            Message[] outMessages = converter.BytesToMessages(bytes, count);

            Assert.AreEqual(messages.Length, outMessages.Length);
            Assert.AreEqual(messages[0].GetType(), outMessages[0].GetType());
            Assert.AreEqual(messages[1].GetType(), outMessages[1].GetType());
            Assert.AreEqual(messages[2].GetType(), outMessages[2].GetType());
            Assert.AreEqual((messages[0] as DivideProblem).ComputationalNodes,
                            (outMessages[0] as DivideProblem).ComputationalNodes);
            Assert.AreEqual((messages[0] as DivideProblem).ProblemType,
                            (outMessages[0] as DivideProblem).ProblemType);
            Assert.AreEqual((messages[1] as Register).Type.Value, (outMessages[1] as Register).Type.Value);
            Assert.AreEqual((messages[1] as Register).Id, (outMessages[1] as Register).Id);
            Assert.AreEqual((messages[2] as Status).Threads.Length, (outMessages[2] as Status).Threads.Length);
            Assert.AreEqual((messages[2] as Status).Threads[0].ProblemType,
                            (outMessages[2] as Status).Threads[0].ProblemType);
            Assert.AreEqual((messages[2] as Status).Id, (outMessages[2] as Status).Id);
        }
        /// <summary>
        /// waits for connection, then receives request(s)
        /// </summary>
        /// <returns>messages passed to socket</returns>
        public Message[] WaitForRequest()
        {
            try
            {
                currentSocket = tcpListener.AcceptSocket();
            }
            catch (Exception)
            {
                tcpListener.Start();
                currentSocket = tcpListener.AcceptSocket();
            }
            byte[] requestBytes = new byte[Properties.Settings.Default.MaxBufferSize];
            int    len          = currentSocket.Receive(requestBytes, Properties.Settings.Default.MaxBufferSize);

            return(converter.BytesToMessages(requestBytes, len));
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="requests">Requests to send</param>
        /// <returns>Responses from the server</returns>
        public Message[] SendRequests(Message[] requests)
        {
            byte[] requestsBytes;
            int    count = _converter.MessagesToBytes(out requestsBytes, requests);

            ITcpClient _tcpClient = _tcpFactory.Create();

            _tcpClient.Connect(_address, _port);
            using (INetworkStream networkStream = _tcpClient.GetStream())
            {
                networkStream.Write(requestsBytes, count);
                byte[] responseBytes = new byte[Properties.Settings.Default.MaxBufferSize];
                int    len           = networkStream.Read(responseBytes, Properties.Settings.Default.MaxBufferSize);
                networkStream.Close();
                _tcpClient.Close();
                return(_converter.BytesToMessages(responseBytes, len));
            }
        }