<commit_before>/*************************************************************************
 * Copyright (c) 2014 eProsima. All rights reserved.
 *
 * This copy of eProsima RTPS is licensed to you under the terms described in the
 * fastrtps_LIBRARY_LICENSE file included in this distribution.
 *
 *************************************************************************/

/*
 * ResourceSendImpl.cpp
 *
 */

#include "fastrtps/rtps/resources/ResourceSendImpl.h"
#include "fastrtps/rtps/common/CDRMessage_t.h"
#include "fastrtps/utils/RTPSLog.h"

#include "fastrtps/utils/IPFinder.h"

using boost::asio::ip::udp;

namespace eprosima {
namespace fastrtps{
namespace rtps {

static const char* const CLASS_NAME = "SendResource";

static const int MAX_BIND_TRIES = 100;

ResourceSendImpl::ResourceSendImpl() :
										m_useIP4(true),
										m_useIP6(true),
										//m_send_socket_v4(m_send_service),
										//m_send_socket_v6(m_send_service),
										m_bytes_sent(0),
										m_send_next(true),
										mp_RTPSParticipant(nullptr),
										mp_mutex(new boost::recursive_mutex())
{

}

bool ResourceSendImpl::initSend(RTPSParticipantImpl* pimpl,const Locator_t& loc,uint32_t sendsockBuffer,bool useIP4,bool useIP6)
{
	const char* const METHOD_NAME = "initSend";
	m_useIP4 = useIP4;
	m_useIP6 = useIP6;

	LocatorList_t list;
	IPFinder::getAllIPAddress(&list);
	boost::asio::socket_base::send_buffer_size option;
	bool not_bind = true;
	int bind_tries = 0;
	for (auto lit = list.begin(); lit != list.end(); ++lit)
	{
		if (lit->kind == LOCATOR_KIND_UDPv4 && m_useIP4)
		{
			mv_sendLocator_v4.push_back(loc);
			auto sendLocv4 = mv_sendLocator_v4.back();
			sendLocv4.port = loc.port;
			//OPEN SOCKETS:
			mv_send_socket_v4.push_back(new boost::asio::ip::udp::socket(m_send_service));
			auto sendSocketv4 = mv_send_socket_v4.back();
			sendSocketv4->open(boost::asio::ip::udp::v4());
			sendSocketv4->set_option(boost::asio::socket_base::send_buffer_size(sendsockBuffer));
			bind_tries = 0;
			udp::endpoint send_endpoint;
			while (not_bind && bind_tries < MAX_BIND_TRIES)
			{
				send_endpoint = udp::endpoint(boost::asio::ip::address_v4(lit->to_IP4_long()), sendLocv4.port);
				try{
					sendSocketv4->bind(send_endpoint);
					not_bind = false;
				}
				catch (boost::system::system_error const& e)
				{
					logInfo(RTPS_MSG_OUT, "UDPv4 Error binding endpoint: (" << send_endpoint << ") with socket: " << sendSocketv4->local_endpoint()
							<< " with boost msg: "<<e.what() , C_YELLOW);
					sendLocv4.port++;
				}
				++bind_tries;
			}
			if(!not_bind)
			{
				sendSocketv4->get_option(option);
				logInfo(RTPS_MSG_OUT, "UDPv4: " << sendSocketv4->local_endpoint() << "|| State: " << sendSocketv4->is_open() <<
						" || buffer size: " << option.value(), C_YELLOW);
			}
			else
			{
				logWarning(RTPS_MSG_OUT,"UDPv4: Maxmimum Number of tries while binding in this interface: "<<send_endpoint,C_YELLOW)
				mv_sendLocator_v4.erase(mv_sendLocator_v4.end()-1);
				delete(*(mv_send_socket_v4.end()-1));
				mv_send_socket_v4.erase(mv_send_socket_v4.end()-1);
			}
			not_bind = true;

		}
		else if (lit->kind == LOCATOR_KIND_UDPv6 && m_useIP6)
		{
			mv_sendLocator_v6.push_back(loc);
			auto sendLocv6 = mv_sendLocator_v6.back();
			sendLocv6.port = loc.port;
			//OPEN SOCKETS:
			mv_send_socket_v6.push_back(new boost::asio::ip::udp::socket(m_send_service));
			auto sendSocketv6 = mv_send_socket_v6.back();
			sendSocketv6->open(boost::asio::ip::udp::v6());
			sendSocketv6->set_option(boost::asio::socket_base::send_buffer_size(sendsockBuffer));
			bind_tries = 0;
			udp::endpoint send_endpoint;
			while (not_bind && bind_tries < MAX_BIND_TRIES)
			{
				boost::asio::ip::address_v6::bytes_type bt;
				for (uint8_t i = 0; i < 16;++i)
					bt[i] = lit->address[i];
				send_endpoint = udp::endpoint(boost::asio::ip::address_v6(bt), sendLocv6.port);
				try{
					sendSocketv6->bind(send_endpoint);
					not_bind = false;
				}
				catch (boost::system::system_error const& e)
				{
					logInfo(RTPS_MSG_OUT, "UDPv6 Error binding endpoint: (" << send_endpoint << ") with socket: " << sendSocketv6->local_endpoint()
							<< " with boost msg: "<<e.what() , C_YELLOW);
					sendLocv6.port++;
				}
				++bind_tries;
			}
			if(!not_bind)
			{
				sendSocketv6->get_option(option);
				logInfo(RTPS_MSG_OUT, "UDPv6: " << sendSocketv6->local_endpoint() << "|| State: " << sendSocketv6->is_open() <<
						" || buffer size: " << option.value(), C_YELLOW);
			}
			else
			{
				logWarning(RTPS_MSG_OUT,"UDPv6: Maxmimum Number of tries while binding in this endpoint: "<<send_endpoint,C_YELLOW);<commit_msg>Added pragma warning to disable unused variable<commit_after>/*************************************************************************
 * Copyright (c) 2014 eProsima. All rights reserved.
 *
 * This copy of eProsima RTPS is licensed to you under the terms described in the
 * fastrtps_LIBRARY_LICENSE file included in this distribution.
 *
 *************************************************************************/

/*
 * ResourceSendImpl.cpp
 *
 */

#include "fastrtps/rtps/resources/ResourceSendImpl.h"
#include "fastrtps/rtps/common/CDRMessage_t.h"
#include "fastrtps/utils/RTPSLog.h"

#include "fastrtps/utils/IPFinder.h"

using boost::asio::ip::udp;

namespace eprosima {
namespace fastrtps{
namespace rtps {

static const char* const CLASS_NAME = "SendResource";

static const int MAX_BIND_TRIES = 100;

ResourceSendImpl::ResourceSendImpl() :
										m_useIP4(true),
										m_useIP6(true),
										//m_send_socket_v4(m_send_service),
										//m_send_socket_v6(m_send_service),
										m_bytes_sent(0),
										m_send_next(true),
										mp_RTPSParticipant(nullptr),
										mp_mutex(new boost::recursive_mutex())
{

}

bool ResourceSendImpl::initSend(RTPSParticipantImpl* pimpl,const Locator_t& loc,uint32_t sendsockBuffer,bool useIP4,bool useIP6)
{
	const char* const METHOD_NAME = "initSend";
	m_useIP4 = useIP4;
	m_useIP6 = useIP6;

	LocatorList_t list;
	IPFinder::getAllIPAddress(&list);
	boost::asio::socket_base::send_buffer_size option;
	bool not_bind = true;
	int bind_tries = 0;
	for (auto lit = list.begin(); lit != list.end(); ++lit)
	{
		if (lit->kind == LOCATOR_KIND_UDPv4 && m_useIP4)
		{
			mv_sendLocator_v4.push_back(loc);
			auto sendLocv4 = mv_sendLocator_v4.back();
			sendLocv4.port = loc.port;
			//OPEN SOCKETS:
			mv_send_socket_v4.push_back(new boost::asio::ip::udp::socket(m_send_service));
			auto sendSocketv4 = mv_send_socket_v4.back();
			sendSocketv4->open(boost::asio::ip::udp::v4());
			sendSocketv4->set_option(boost::asio::socket_base::send_buffer_size(sendsockBuffer));
			bind_tries = 0;
			udp::endpoint send_endpoint;
			while (not_bind && bind_tries < MAX_BIND_TRIES)
			{
				send_endpoint = udp::endpoint(boost::asio::ip::address_v4(lit->to_IP4_long()), sendLocv4.port);
				try{
					sendSocketv4->bind(send_endpoint);
					not_bind = false;
				}
				#pragma warning(disable: 4101)
				catch (boost::system::system_error const& e)
				{
					logInfo(RTPS_MSG_OUT, "UDPv4 Error binding endpoint: (" << send_endpoint << ") with socket: " << sendSocketv4->local_endpoint()
							<< " with boost msg: "<<e.what() , C_YELLOW);
					sendLocv4.port++;
				}
				++bind_tries;
			}
			if(!not_bind)
			{
				sendSocketv4->get_option(option);
				logInfo(RTPS_MSG_OUT, "UDPv4: " << sendSocketv4->local_endpoint() << "|| State: " << sendSocketv4->is_open() <<
						" || buffer size: " << option.value(), C_YELLOW);
			}
			else
			{
				logWarning(RTPS_MSG_OUT,"UDPv4: Maxmimum Number of tries while binding in this interface: "<<send_endpoint,C_YELLOW)
				mv_sendLocator_v4.erase(mv_sendLocator_v4.end()-1);
				delete(*(mv_send_socket_v4.end()-1));
				mv_send_socket_v4.erase(mv_send_socket_v4.end()-1);
			}
			not_bind = true;

		}
		else if (lit->kind == LOCATOR_KIND_UDPv6 && m_useIP6)
		{
			mv_sendLocator_v6.push_back(loc);
			auto sendLocv6 = mv_sendLocator_v6.back();
			sendLocv6.port = loc.port;
			//OPEN SOCKETS:
			mv_send_socket_v6.push_back(new boost::asio::ip::udp::socket(m_send_service));
			auto sendSocketv6 = mv_send_socket_v6.back();
			sendSocketv6->open(boost::asio::ip::udp::v6());
			sendSocketv6->set_option(boost::asio::socket_base::send_buffer_size(sendsockBuffer));
			bind_tries = 0;
			udp::endpoint send_endpoint;
			while (not_bind && bind_tries < MAX_BIND_TRIES)
			{
				boost::asio::ip::address_v6::bytes_type bt;
				for (uint8_t i = 0; i < 16;++i)
					bt[i] = lit->address[i];
				send_endpoint = udp::endpoint(boost::asio::ip::address_v6(bt), sendLocv6.port);
				try{
					sendSocketv6->bind(send_endpoint);
					not_bind = false;
				}
				#pragma warning(disable: 4101)
				catch (boost::system::system_error const& e)
				{
					logInfo(RTPS_MSG_OUT, "UDPv6 Error binding endpoint: (" << send_endpoint << ") with socket: " << sendSocketv6->local_endpoint()
							<< " with boost msg: "<<e.what() , C_YELLOW);
					sendLocv6.port++;
				}
				++bind_tries;
			}
			if(!not_bind)
			{
				sendSocketv6->get_option(option);
				logInfo(RTPS_MSG_OUT, "UDPv6: " << sendSocketv6->local_endpoint() << "|| State: " << sendSocketv6->is_open() <<
						" || buffer size: " << option.value(), C_YELLOW);
			}
			else
			{
				logWarning(RTPS_MSG_OUT,"UDPv6: Maxmimum Number of tries while binding in this endpoint: "<<send_endpoint,C_YELLOW);
{'fraction_non_alphanumeric': 0.17114236999147486, 'fraction_numerical': 0.01854219948849105, 'mean_word_length': 4.359794403198173, 'pattern_counts': {'":': 0, '<': 97, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6976561', 'n_tokens_mistral': 4166, 'n_tokens_neox': 3459, 'n_words': 555}