<commit_before><commit_msg>missing hdr<commit_after>/* +---------------------------------------------------------------------------+
   |                     Mobile Robot Programming Toolkit (MRPT)               |
   |                          http://www.mrpt.org/                             |
   |                                                                           |
   | Copyright (c) 2005-2016, Individual contributors, see AUTHORS file        |
   | See: http://www.mrpt.org/Authors - All rights reserved.                   |
   | Released under BSD License. See details in http://www.mrpt.org/License    |
   +---------------------------------------------------------------------------+ */
#ifndef  SERIALIZATION_ZMQ_H
#define  SERIALIZATION_ZMQ_H

#include <mrpt/utils/CSerializable.h>
#include <mrpt/utils/CMemoryStream.h>

namespace mrpt
{
	namespace utils
	{
		/** \addtogroup noncstream_serialization_zmq Serialization functions for ZMQ (in #include <mrpt/utils/serialization_zmq.h>)
		  * \ingroup noncstream_serialization
		  * @{ */

		/** Send an MRPT object to a ZMQ socket.
		 * \param[in] obj The object to be serialized and sent to the socket.
		 * \param[in] zmq_socket The zmq socket object.
		 * \param[in] max_packet_len The object will be split into a series of ZMQ "message parts" of this maximum length (in bytes). Default=0, which means do not split in parts.
		 * \return false on any error, true if all is ok.
		 * \note Including `<mrpt/utils/serialization_zmq.h>` requires libzmq to be available in your system and linked
		 *  to your user code. This function can be used even if MRPT was built without ZMQ support, thanks to the use of templates.
		 * \exception std::exception If the object finds any critical error during serialization.
		 */
		template <typename ZMQ_SOCKET_TYPE>
		bool mrpt_send_to_zmq(
			ZMQ_SOCKET_TYPE zmq_socket,
			const mrpt::utils::CSerializable *obj,
			const size_t max_packet_len = 0)
		{
			mrpt::utils::CMemoryStream buf;
			buf.WriteObject(obj);
			const size_t nBytes = buf.getTotalBytesCount();

			zmq_msg_t message;
			zmq_msg_init_size (&message, nBytes);
			memcpy (zmq_msg_data (&message), buf.getRawBufferData(), nBytes);
			int size = zmq_msg_send (&message, zmq_socket, 0);
			zmq_msg_close (&message);
			return size==static_cast<int>(nBytes);
		}

		/** Receives an MRPT object from a ZMQ socket, determining the type of the
		 *  object on-the-fly.
		 * \param[in] zmq_socket The zmq socket object.
		 * \param[in] dont_wait If true, will fail if there is no data ready to
		 *  be read. If false (default) this function will block until data arrives.
		 * \param[out] rx_obj_length_in_bytes If non-NULL, the object length will be stored here.
		 * \return An empty smart pointer if there was any error. The received
		 *  object if all went OK.
		 * \note Including `<mrpt/utils/serialization_zmq.h>` requires libzmq to be
		 *  available in your system and linked to your user code. This function
		 *  can be used even if MRPT was built without ZMQ support, thanks to the
		 *  use of templates.
		 * \exception std::exception If the object finds any critical error during de-serialization.
		 * \sa mrpt_recv_from_zmq_into
		 */
		template <typename ZMQ_SOCKET_TYPE>
		mrpt::utils::CSerializablePtr mrpt_recv_from_zmq(ZMQ_SOCKET_TYPE zmq_socket, bool dont_wait = false, size_t * rx_obj_length_in_bytes = NULL)
		{
			mrpt::utils::CSerializablePtr obj;
			if (rx_obj_length_in_bytes) *rx_obj_length_in_bytes = 0;
			// Init rx msg:
			zmq_msg_t msg;
			if (0!=zmq_msg_init (&msg))
				return obj;
			// Recv:
			const int len = zmq_msg_recv(&msg,zmq_socket, dont_wait ? ZMQ_DONTWAIT : 0);
			if (len <=0 ) return obj;
			if (rx_obj_length_in_bytes) *rx_obj_length_in_bytes = len;
			// De-serialize:
			{
				CMemoryStream	tmp;
				tmp.assignMemoryNotOwn(zmq_msg_data(&msg), len);
				obj = tmp.ReadObject();
			}
			zmq_msg_close (&msg); // Free msg
			return obj;
		}
		/** Like mrpt_recv_from_zmq() but without dynamically allocating the received object,
		  * more efficient to use if the type of the received object is known in advance.
			* \param[in] target_object The received object will be stored here. An exception will be raised upon type mismatch.
			* \return true if all was OK, false on any ZMQ error.
			* \sa mrpt_recv_from_zmq() for details on the rest of parameters.
		  */
		template <typename ZMQ_SOCKET_TYPE>
		bool mrpt_recv_from_zmq_into(ZMQ_SOCKET_TYPE zmq_socket, mrpt::utils::CSerializable &target_object, bool dont_wait = false, size_t * rx_obj_length_in_bytes = NULL)
		{
			if (rx_obj_length_in_bytes) *rx_obj_length_in_bytes = 0;
			// Init rx msg:
			zmq_msg_t msg;
			if (0!=zmq_msg_init (&msg))
				return false;
			// Recv:
			const int len = zmq_msg_recv(&msg,zmq_socket, dont_wait ? ZMQ_DONTWAIT : 0);
			if (len <=0 ) return false;
			if (rx_obj_length_in_bytes) *rx_obj_length_in_bytes = len;
			// De-serialize:
			{
				CMemoryStream	tmp;
				tmp.assignMemoryNotOwn(zmq_msg_data(&msg), len);
				tmp.ReadObject(&target_object);
			}
			zmq_msg_close (&msg); // Free msg
			return true;
		}

		/** @} */
	} // End of namespace
} // End of namespace
#endif

{'fraction_non_alphanumeric': 0.11969930609097919, 'fraction_numerical': 0.0036622976098689285, 'mean_word_length': 2.7492774566473988, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5169', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1654, 'n_words': 510}