void message_oriented_connection_impl::send_message(const message& message_to_send) { VERIFY_CORRECT_THREAD(); #if 0 // this gets too verbose #ifndef NDEBUG fc::optional<fc::ip::endpoint> remote_endpoint; if (_sock.get_socket().is_open()) remote_endpoint = _sock.get_socket().remote_endpoint(); struct scope_logger { const fc::optional<fc::ip::endpoint>& endpoint; scope_logger(const fc::optional<fc::ip::endpoint>& endpoint) : endpoint(endpoint) { dlog("entering message_oriented_connection::send_message() for peer ${endpoint}", ("endpoint", endpoint)); } ~scope_logger() { dlog("leaving message_oriented_connection::send_message() for peer ${endpoint}", ("endpoint", endpoint)); } } send_message_scope_logger(remote_endpoint); #endif #endif struct verify_no_send_in_progress { bool& var; verify_no_send_in_progress(bool& var) : var(var) { if (var) elog("Error: two tasks are calling message_oriented_connection::send_message() at the same time"); assert(!var); var = true; } ~verify_no_send_in_progress() { var = false; } } _verify_no_send_in_progress(_send_message_in_progress); try { size_t size_of_message_and_header = sizeof(message_header) + message_to_send.size; if( message_to_send.size > MAX_MESSAGE_SIZE ) elog("Trying to send a message larger than MAX_MESSAGE_SIZE. This probably won't work..."); //pad the message we send to a multiple of 16 bytes size_t size_with_padding = 16 * ((size_of_message_and_header + 15) / 16); std::unique_ptr<char[]> padded_message(new char[size_with_padding]); memcpy(padded_message.get(), (char*)&message_to_send, sizeof(message_header)); memcpy(padded_message.get() + sizeof(message_header), message_to_send.data.data(), message_to_send.size ); char* paddingSpace = padded_message.get() + sizeof(message_header) + message_to_send.size; size_t toClean = size_with_padding - size_of_message_and_header; memset(paddingSpace, 0, toClean); _sock.write(padded_message.get(), size_with_padding); _sock.flush(); _bytes_sent += size_with_padding; _last_message_sent_time = fc::time_point::now(); } FC_RETHROW_EXCEPTIONS( warn, "unable to send message" ); }
void message_oriented_connection_impl::send_message(const message& message_to_send) { try { size_t size_of_message_and_header = sizeof(message_header) + message_to_send.size; //pad the message we send to a multiple of 16 bytes size_t size_with_padding = 16 * ((size_of_message_and_header + 15) / 16); std::unique_ptr<char[]> padded_message(new char[size_with_padding]); memcpy(padded_message.get(), (char*)&message_to_send, sizeof(message_header)); memcpy(padded_message.get() + sizeof(message_header), message_to_send.data.data(), message_to_send.size ); fc::scoped_lock<fc::mutex> lock(_send_mutex); _sock.write(padded_message.get(), size_with_padding); _sock.flush(); _bytes_sent += size_with_padding; _last_message_sent_time = fc::time_point::now(); } FC_RETHROW_EXCEPTIONS( warn, "unable to send message" ); }