/// <summary> /// sends response(s) via open connection /// </summary> /// <param name="responses"></param> public void SendResponse(Message[] responses) { if (currentSocket == null) { throw new Exception("Something went wrong with connection (socket)"); } byte[] responsesBytes; int count = converter.MessagesToBytes(out responsesBytes, responses); currentSocket.Send(responsesBytes, count); currentSocket.Close(); currentSocket = null; }
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> /// /// </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)); } }