MCPcopy Create free account
hub / github.com/cryptonomex/graphene / send_message

Method send_message

libraries/net/message_oriented_connection.cpp:226–268  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

224 }
225
226 void message_oriented_connection_impl::send_message(const message& message_to_send)
227 {
228 VERIFY_CORRECT_THREAD();
229#if 0 // this gets too verbose
230#ifndef NDEBUG
231 fc::optional<fc::ip::endpoint> remote_endpoint;
232 if (_sock.get_socket().is_open())
233 remote_endpoint = _sock.get_socket().remote_endpoint();
234 struct scope_logger {
235 const fc::optional<fc::ip::endpoint>& endpoint;
236 scope_logger(const fc::optional<fc::ip::endpoint>& endpoint) : endpoint(endpoint) { dlog("entering message_oriented_connection::send_message() for peer ${endpoint}", ("endpoint", endpoint)); }
237 ~scope_logger() { dlog("leaving message_oriented_connection::send_message() for peer ${endpoint}", ("endpoint", endpoint)); }
238 } send_message_scope_logger(remote_endpoint);
239#endif
240#endif
241 struct verify_no_send_in_progress {
242 bool& var;
243 verify_no_send_in_progress(bool& var) : var(var)
244 {
245 if (var)
246 elog("Error: two tasks are calling message_oriented_connection::send_message() at the same time");
247 assert(!var);
248 var = true;
249 }
250 ~verify_no_send_in_progress() { var = false; }
251 } _verify_no_send_in_progress(_send_message_in_progress);
252
253 try
254 {
255 size_t size_of_message_and_header = sizeof(message_header) + message_to_send.size;
256 if( message_to_send.size > MAX_MESSAGE_SIZE )
257 elog("Trying to send a message larger than MAX_MESSAGE_SIZE. This probably won't work...");
258 //pad the message we send to a multiple of 16 bytes
259 size_t size_with_padding = 16 * ((size_of_message_and_header + 15) / 16);
260 std::unique_ptr<char[]> padded_message(new char[size_with_padding]);
261 memcpy(padded_message.get(), (char*)&message_to_send, sizeof(message_header));
262 memcpy(padded_message.get() + sizeof(message_header), message_to_send.data.data(), message_to_send.size );
263 _sock.write(padded_message.get(), size_with_padding);
264 _sock.flush();
265 _bytes_sent += size_with_padding;
266 _last_message_sent_time = fc::time_point::now();
267 } FC_RETHROW_EXCEPTIONS( warn, "unable to send message" );
268 }
269
270 void message_oriented_connection_impl::close_connection()
271 {

Callers

nothing calls this directly

Calls 4

nowFunction · 0.85
is_openMethod · 0.80
getMethod · 0.45
flushMethod · 0.45

Tested by

no test coverage detected