BaseMessageUtils.h

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Distributed under the OpenDDS License.
00005  * See: http://www.opendds.org/license.html
00006  */
00007 
00008 #ifndef RTPS_BASEMESSAGEUTILS_H
00009 #define RTPS_BASEMESSAGEUTILS_H
00010 
00011 #include "RtpsCoreTypeSupportImpl.h"
00012 #include "rtps_export.h"
00013 
00014 #include "dds/DCPS/Serializer.h"
00015 #include "dds/DCPS/TypeSupportImpl.h"
00016 #include "dds/DdsDcpsInfoUtilsC.h"
00017 #include "dds/DdsDcpsInfoUtilsTypeSupportImpl.h"
00018 
00019 #include "md5.h"
00020 
00021 #include "ace/INET_Addr.h"
00022 #include "ace/Message_Block.h"
00023 
00024 #include <cstring>
00025 
00026 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00027 
00028 namespace OpenDDS {
00029 namespace RTPS {
00030   using DCPS::GuidPrefix_t;
00031   using DCPS::GUID_t;
00032   using DCPS::EntityId_t;
00033 
00034 template <typename T>
00035 void marshal_key_hash(const T& msg, KeyHash_t& hash) {
00036   using DCPS::Serializer;
00037 
00038   DCPS::KeyOnly<const T> ko(msg);
00039 
00040   static const size_t HASH_LIMIT = 16;
00041   std::memset(hash.value, 0, HASH_LIMIT);
00042 
00043   // Key Hash must use big endian ordering.
00044   // Native==Little endian means we need to swap
00045 #if defined ACE_LITTLE_ENDIAN
00046   static const bool swap_bytes = true;
00047 #else
00048   static const bool swap_bytes = false;
00049 #endif
00050 
00051   if (DCPS::MarshalTraits<T>::gen_is_bounded_key_size() &&
00052       gen_max_marshaled_size(ko, true /*align*/) <= HASH_LIMIT) {
00053     // If it is bounded and can always fit in 16 bytes, we will use the
00054     // marshaled key
00055     ACE_Message_Block mb(HASH_LIMIT);
00056     Serializer out_serializer(&mb, swap_bytes, Serializer::ALIGN_INITIALIZE);
00057     out_serializer << ko;
00058     std::memcpy(hash.value, mb.rd_ptr(), mb.length());
00059 
00060   } else {
00061     // We will use the hash of the marshaled key
00062     size_t size = 0, padding = 0;
00063     gen_find_size(ko, size, padding);
00064     ACE_Message_Block mb(size + padding);
00065     Serializer out_serializer(&mb, swap_bytes, Serializer::ALIGN_INITIALIZE);
00066     out_serializer << ko;
00067 
00068     MD5_CTX ctx;
00069     MD5_Init(&ctx);
00070     MD5_Update(&ctx, mb.rd_ptr(), static_cast<unsigned long>(mb.length()));
00071     MD5_Final(hash.value, &ctx);
00072   }
00073 }
00074 
00075 inline void assign(GuidPrefix_t& dest, const GuidPrefix_t& src)
00076 {
00077   std::memcpy(&dest[0], &src[0], sizeof(GuidPrefix_t));
00078 }
00079 
00080 inline void assign(DCPS::OctetArray16& dest,
00081                    const DCPS::OctetArray16& src)
00082 {
00083   std::memcpy(&dest[0], &src[0], sizeof(DCPS::OctetArray16));
00084 }
00085 
00086 inline void assign(DCPS::OctetArray16& dest,
00087                    const ACE_CDR::ULong& ipv4addr_be)
00088 {
00089   std::memset(&dest[0], 0, 12);
00090   dest[12] = ipv4addr_be >> 24;
00091   dest[13] = ipv4addr_be >> 16;
00092   dest[14] = ipv4addr_be >> 8;
00093   dest[15] = ipv4addr_be;
00094 }
00095 
00096 inline void assign(DCPS::EntityKey_t& lhs, unsigned int rhs)
00097 {
00098   lhs[0] = static_cast<CORBA::Octet>(rhs);
00099   lhs[1] = static_cast<CORBA::Octet>(rhs >> 8);
00100   lhs[2] = static_cast<CORBA::Octet>(rhs >> 16);
00101 }
00102 
00103 
00104 inline void
00105 address_to_bytes(DCPS::OctetArray16& dest, const ACE_INET_Addr& addr)
00106 {
00107   const void* raw = addr.get_addr();
00108 #ifdef ACE_HAS_IPV6
00109   if (addr.get_type() == AF_INET6) {
00110     const sockaddr_in6* in = static_cast<const sockaddr_in6*>(raw);
00111     std::memcpy(&dest[0], &in->sin6_addr, 16);
00112   } else {
00113 #else
00114   {
00115 #endif
00116     const sockaddr_in* in = static_cast<const sockaddr_in*>(raw);
00117     std::memset(&dest[0], 0, 12);
00118     std::memcpy(&dest[12], &in->sin_addr, 4);
00119   }
00120 }
00121 
00122 inline int
00123 address_to_kind(const ACE_INET_Addr& addr)
00124 {
00125 #ifdef ACE_HAS_IPV6
00126   return addr.get_type() == AF_INET6 ? LOCATOR_KIND_UDPv6 : LOCATOR_KIND_UDPv4;
00127 #else
00128   ACE_UNUSED_ARG(addr);
00129   return LOCATOR_KIND_UDPv4;
00130 #endif
00131 }
00132 
00133 OpenDDS_Rtps_Export
00134 int locator_to_address(ACE_INET_Addr& dest,
00135                        const DCPS::Locator_t& locator,
00136                        bool map /*map IPV4 to IPV6 addr*/);
00137 
00138 OpenDDS_Rtps_Export
00139 DDS::ReturnCode_t blob_to_locators(const DCPS::TransportBLOB& blob,
00140                                    DCPS::LocatorSeq& locators,
00141                                    bool* requires_inline_qos = 0,
00142                                    unsigned int* pBytesRead = 0);
00143 
00144 OpenDDS_Rtps_Export
00145 void locators_to_blob(const DCPS::LocatorSeq& locators,
00146                       DCPS::TransportBLOB& blob);
00147 
00148 template <typename T>
00149 void message_block_to_sequence(const ACE_Message_Block& mb_locator, T& out)
00150 {
00151   out.length (CORBA::ULong(mb_locator.length()));
00152   std::memcpy (out.get_buffer(), mb_locator.rd_ptr(), mb_locator.length());
00153 }
00154 
00155 
00156 }
00157 }
00158 
00159 OPENDDS_END_VERSIONED_NAMESPACE_DECL
00160 
00161 #endif /* RTPS_BASEMESSAGETYPES_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1