/// <summary> /// Sends NGRIDMessage object to the socket. /// </summary> /// <param name="message">Message to be sent</param> private void SendMessageToSocket(NGRIDMessage message) { Logger.Debug("Message is being sent to communicator " + ComminicatorId + ": " + message.GetType().Name); //Create MemoryStream to write message to a byte array var memoryStream = new MemoryStream(); //Write message _wireProtocol.WriteMessage(new NGRIDDefaultSerializer(memoryStream), message); //Check the length of message data if (memoryStream.Length > CommunicationConsts.MaxMessageSize) { throw new Exception("Message is too big to send."); } //SendMessage message (contents of created memory stream) var sendBuffer = memoryStream.ToArray(); var length = sendBuffer.Length; var totalSent = 0; while (totalSent < length) { var sent = _socket.Send(sendBuffer, totalSent, length - totalSent, SocketFlags.None); if (sent <= 0) { throw new Exception("Message can not be sent via TCP socket. Only " + totalSent + " bytes of " + length + " bytes are sent."); } totalSent += sent; } Logger.Debug("Message is sent to communicator " + ComminicatorId + ": " + message.GetType().Name); }