]> git.seodisparate.com - UDPConnection/commitdiff
Refactor addr logprint via template specialization
authorStephen Seo <seo.disparate@gmail.com>
Mon, 26 Jun 2023 05:01:42 +0000 (14:01 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 26 Jun 2023 05:01:42 +0000 (14:01 +0900)
src/UDPC_Defines.hpp
src/UDPConnection.cpp

index 2aad9363af3364271c8ff3c25f4eb2f17c7a3337..86dcc899183f949a8278746388aa8a88181c2784 100644 (file)
@@ -212,12 +212,15 @@ private:
         log_impl_next(type, args...);
     } // }}}
 
+    template<typename... Targs>
+    void log_impl_next(UDPC_LoggingType type, UDPC_IPV6_ADDR_TYPE addr, Targs... args) { // {{{
+        std::cerr << UDPC_atostr((UDPC_HContext)this, addr);
+        log_impl_next(type, args...);
+    } // }}}
+
 public:
     void update_impl();
 
-    const char *conditional_atostr(UDPC_LoggingType logType,
-                                   UDPC_IPV6_ADDR_TYPE addr);
-
     uint_fast32_t _contextIdentifier;
 
     char recvBuf[UDPC_PACKET_MAX_SIZE];
index e7d1036ff5703602ae827bc678092dad38402b96..7336949272b516d410cd9465e361d74207f518d8 100644 (file)
@@ -336,8 +336,7 @@ void UDPC::Context::update_impl() {
                         UDPC_LoggingType::UDPC_ERROR,
                         "Failed to init ConnectionData instance (libsodium "
                         "init fail) while client establishing connection with ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                           event.conId.addr),
+                        event.conId.addr,
                         " port ",
                         event.conId.port);
                     continue;
@@ -363,8 +362,7 @@ void UDPC::Context::update_impl() {
                     UDPC_CHECK_LOG(this,
                         UDPC_LoggingType::UDPC_INFO,
                         "Client initiating connection to ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_INFO,
-                                           event.conId.addr),
+                        event.conId.addr,
                         " port ",
                         event.conId.port,
                         " ...");
@@ -372,8 +370,7 @@ void UDPC::Context::update_impl() {
                     UDPC_CHECK_LOG(this,
                         UDPC_LoggingType::UDPC_WARNING,
                         "Client initiate connection, already connected to peer ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_WARNING,
-                                           event.conId.addr),
+                        event.conId.addr,
                         " port ",
                         event.conId.port);
                 }
@@ -422,8 +419,7 @@ void UDPC::Context::update_impl() {
                 UDPC_CHECK_LOG(this,
                     UDPC_LoggingType::UDPC_VERBOSE,
                     "Timed out connection with ",
-                    conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE,
-                                       iter->first.addr),
+                    iter->first.addr,
                     ", port = ",
                     iter->second.port);
                 continue;
@@ -437,8 +433,7 @@ void UDPC::Context::update_impl() {
                 UDPC_CHECK_LOG(this,
                     UDPC_LoggingType::UDPC_VERBOSE,
                     "Switching to bad mode in connection with ",
-                    conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE,
-                                       iter->first.addr),
+                    iter->first.addr,
                     ", port = ",
                     iter->second.port);
                 iter->second.flags.reset(1);
@@ -469,8 +464,7 @@ void UDPC::Context::update_impl() {
                     UDPC_CHECK_LOG(this,
                         UDPC_LoggingType::UDPC_VERBOSE,
                         "Switching to good mode in connection with ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE,
-                                           iter->first.addr),
+                        iter->first.addr,
                         ", port = ",
                         iter->second.port);
                     iter->second.flags.set(1);
@@ -550,8 +544,7 @@ void UDPC::Context::update_impl() {
                             UDPC_CHECK_LOG(this,
                                 UDPC_LoggingType::UDPC_DEBUG,
                                 "Not queueing packet to ",
-                                conditional_atostr(UDPC_LoggingType::UDPC_DEBUG,
-                                                   next->receiver.addr),
+                                next->receiver.addr,
                                 ", port = ",
                                 next->receiver.port,
                                 ", connection's queue reached max size");
@@ -573,8 +566,7 @@ void UDPC::Context::update_impl() {
                         UDPC_CHECK_LOG(this,
                             UDPC_LoggingType::UDPC_WARNING,
                             "Dropped queued packets to ",
-                            conditional_atostr(UDPC_LoggingType::UDPC_WARNING,
-                                               next->receiver.addr),
+                            next->receiver.addr,
                             ", port = ",
                             next->receiver.port,
                             " due to connection not existing");
@@ -633,8 +625,7 @@ void UDPC::Context::update_impl() {
                         iter->second.sk) != 0) {
                         UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
                             "Failed to sign packet for peer ",
-                            conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                               iter->first.addr),
+                            iter->first.addr,
                             ", port ",
                             iter->second.port);
                         continue;
@@ -668,8 +659,7 @@ void UDPC::Context::update_impl() {
                     UDPC_CHECK_LOG(this,
                         UDPC_LoggingType::UDPC_ERROR,
                         "Failed to send disconnect packet to ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                           iter->first.addr),
+                        iter->first.addr,
                         ", port = ",
                         iter->second.port);
                     continue;
@@ -774,8 +764,7 @@ void UDPC::Context::update_impl() {
                         UDPC_CHECK_LOG(this,
                             UDPC_LoggingType::UDPC_ERROR,
                             "Failed to send packet to initiate connection to ",
-                            conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                               iter->first.addr),
+                            iter->first.addr,
                             ", port = ",
                             iter->second.port);
                         continue;
@@ -784,8 +773,7 @@ void UDPC::Context::update_impl() {
                             this,
                             UDPC_LoggingType::UDPC_INFO,
                             "Sent initiate connection to ",
-                            conditional_atostr(UDPC_LoggingType::UDPC_INFO,
-                                               iter->first.addr),
+                            iter->first.addr,
                             ", port = ",
                             iter->second.port,
                             flags.test(2) && iter->second.flags.test(6) ?
@@ -856,16 +844,14 @@ void UDPC::Context::update_impl() {
                         UDPC_CHECK_LOG(this,
                             UDPC_LoggingType::UDPC_ERROR,
                             "Failed to send packet to initiate connection to ",
-                            conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                               iter->first.addr),
+                            iter->first.addr,
                             ", port = ",
                             iter->second.port);
                         continue;
                     }
                     UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_DEBUG,
                         "Sent init pkt to client ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_DEBUG,
-                                           destinationInfo.sin6_addr),
+                        destinationInfo.sin6_addr,
                         ", port ", iter->second.port);
                 }
                 continue;
@@ -908,8 +894,7 @@ void UDPC::Context::update_impl() {
                         iter->second.sk) != 0) {
                         UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
                             "Failed to sign packet for peer ",
-                            conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                               iter->first.addr),
+                            iter->first.addr,
                             ", port ",
                             iter->second.port);
                         continue;
@@ -943,8 +928,7 @@ void UDPC::Context::update_impl() {
                     UDPC_CHECK_LOG(this,
                         UDPC_LoggingType::UDPC_ERROR,
                         "Failed to send heartbeat packet to ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                           iter->first.addr),
+                        iter->first.addr,
                         ", port = ",
                         iter->second.port);
                     continue;
@@ -1014,8 +998,7 @@ void UDPC::Context::update_impl() {
                         iter->second.sk) != 0) {
                         UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
                             "Failed to sign packet for peer ",
-                            conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                               iter->first.addr),
+                            iter->first.addr,
                             ", port ",
                             iter->second.port);
                         std::free(pInfo.data);
@@ -1054,8 +1037,7 @@ void UDPC::Context::update_impl() {
                     UDPC_CHECK_LOG(this,
                         UDPC_LoggingType::UDPC_ERROR,
                         "Failed to send packet to ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                           iter->first.addr),
+                        iter->first.addr,
                         ", port = ",
                         iter->second.port);
                     std::free(pInfo.data);
@@ -1169,8 +1151,7 @@ void UDPC::Context::update_impl() {
             UDPC_CHECK_LOG(this,
                 UDPC_LoggingType::UDPC_VERBOSE,
                 "Received packet is smaller than header, ignoring packet from ",
-                conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE,
-                                   receivedData.sin6_addr),
+                receivedData.sin6_addr,
                 ", port = ",
                 ntohs(receivedData.sin6_port));
             continue;
@@ -1185,8 +1166,7 @@ void UDPC::Context::update_impl() {
                 UDPC_LoggingType::UDPC_VERBOSE,
                 "Received packet has invalid protocol id, ignoring packet "
                 "from ",
-                conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE,
-                                   receivedData.sin6_addr),
+                receivedData.sin6_addr,
                 ", port = ",
                 ntohs(receivedData.sin6_port));
             continue;
@@ -1216,8 +1196,7 @@ void UDPC::Context::update_impl() {
             UDPC_CHECK_LOG(this,
                 UDPC_LoggingType::UDPC_VERBOSE,
                 "Got connect packet of invalid size from ",
-                conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE,
-                                   receivedData.sin6_addr),
+                receivedData.sin6_addr,
                 ", port = ",
                 ntohs(receivedData.sin6_port),
                 ", ignoring");
@@ -1228,8 +1207,7 @@ void UDPC::Context::update_impl() {
             UDPC_CHECK_LOG(this,
                 UDPC_LoggingType::UDPC_VERBOSE,
                 "Got non-connect packet of invalid size from ",
-                conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE,
-                                   receivedData.sin6_addr),
+                receivedData.sin6_addr,
                 ", port = ",
                 ntohs(receivedData.sin6_port),
                 ", ignoring");
@@ -1250,8 +1228,7 @@ void UDPC::Context::update_impl() {
             default:
                 UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_WARNING,
                     "Got invalid connect pktType from ",
-                    conditional_atostr(UDPC_LoggingType::UDPC_WARNING,
-                                       receivedData.sin6_addr),
+                    receivedData.sin6_addr,
                     ", port ", ntohs(receivedData.sin6_port));
                 continue;
             }
@@ -1265,8 +1242,7 @@ void UDPC::Context::update_impl() {
             } else {
                 UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_WARNING,
                     "Got invalid pktType from ",
-                    conditional_atostr(UDPC_LoggingType::UDPC_WARNING,
-                                       receivedData.sin6_addr),
+                    receivedData.sin6_addr,
                     ", port ", ntohs(receivedData.sin6_port));
                 continue;
             }
@@ -1285,8 +1261,7 @@ void UDPC::Context::update_impl() {
                             == UDPC_AuthPolicy::UDPC_AUTH_POLICY_STRICT) {
                     UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
                         "Client peer ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                           receivedData.sin6_addr),
+                        receivedData.sin6_addr,
                         " port ",
                         ntohs(receivedData.sin6_port),
                         " attempted connection with packet authentication "
@@ -1298,8 +1273,7 @@ void UDPC::Context::update_impl() {
                             == UDPC_AuthPolicy::UDPC_AUTH_POLICY_STRICT) {
                     UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
                         "Client peer ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                           receivedData.sin6_addr),
+                        receivedData.sin6_addr,
                         " port ",
                         ntohs(receivedData.sin6_port),
                         " attempted connection with packet authentication "
@@ -1332,8 +1306,7 @@ void UDPC::Context::update_impl() {
                         UDPC_LoggingType::UDPC_ERROR,
                         "Failed to init ConnectionData instance (libsodium init"
                         " fail) while server establishing connection with ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                           receivedData.sin6_addr),
+                        receivedData.sin6_addr,
                         ", port = ",
                         ntohs(receivedData.sin6_port));
                     continue;
@@ -1399,8 +1372,7 @@ void UDPC::Context::update_impl() {
                 UDPC_CHECK_LOG(this,
                     UDPC_LoggingType::UDPC_INFO,
                     "Establishing connection with client ",
-                    conditional_atostr(UDPC_LoggingType::UDPC_INFO,
-                                       receivedData.sin6_addr),
+                    receivedData.sin6_addr,
                     ", port = ",
                     ntohs(receivedData.sin6_port),
                     ", giving client id = ", newConnection.id,
@@ -1436,8 +1408,7 @@ void UDPC::Context::update_impl() {
                 if(iter == conMap.end() || !iter->second.flags.test(3)) {
                     UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_DEBUG,
                         "client dropped pkt from ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_DEBUG,
-                                           receivedData.sin6_addr),
+                        receivedData.sin6_addr,
                         ", port ", ntohs(receivedData.sin6_port));
                     continue;
                 }
@@ -1452,8 +1423,7 @@ void UDPC::Context::update_impl() {
                     // without auth (if fallback).
                     UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
                         "Server peer ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                           receivedData.sin6_addr),
+                        receivedData.sin6_addr,
                         " port ",
                         ntohs(receivedData.sin6_port),
                         " attempted connection with packet authentication "
@@ -1465,8 +1435,7 @@ void UDPC::Context::update_impl() {
                             == UDPC_AuthPolicy::UDPC_AUTH_POLICY_STRICT) {
                     UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
                         "Server peer ",
-                        conditional_atostr(UDPC_LoggingType::UDPC_ERROR,
-                                           receivedData.sin6_addr),
+                        receivedData.sin6_addr,
                         " port ",
                         ntohs(receivedData.sin6_port),
                         " attempted connection with packet authentication "
@@ -1502,8 +1471,7 @@ void UDPC::Context::update_impl() {
                         iter->second.peer_pk) != 0) {
                         UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_WARNING,
                             "Failed to verify peer (server) ",
-                            conditional_atostr(UDPC_LoggingType::UDPC_WARNING,
-                                               receivedData.sin6_addr),
+                            receivedData.sin6_addr,
                             ", port = ",
                             ntohs(receivedData.sin6_port));
                         continue;
@@ -1529,8 +1497,7 @@ void UDPC::Context::update_impl() {
                 iter->second.flags.set(4);
                 UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_INFO,
                     "Established connection with server ",
-                    conditional_atostr(UDPC_LoggingType::UDPC_INFO,
-                                       receivedData.sin6_addr),
+                    receivedData.sin6_addr,
                     ", port = ",
                     ntohs(receivedData.sin6_port),
                     ", got id = ", conID,
@@ -1575,8 +1542,7 @@ void UDPC::Context::update_impl() {
                     this,
                     UDPC_LoggingType::UDPC_INFO,
                     "Failed to verify received packet from",
-                    conditional_atostr(UDPC_LoggingType::UDPC_INFO,
-                                       receivedData.sin6_addr),
+                    receivedData.sin6_addr,
                     ", port = ",
                     ntohs(receivedData.sin6_port),
                     ", ignoring");
@@ -1594,8 +1560,7 @@ void UDPC::Context::update_impl() {
         UDPC_CHECK_LOG(this,
             UDPC_LoggingType::UDPC_VERBOSE,
             "Received valid packet from ",
-            conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE,
-                               receivedData.sin6_addr),
+            receivedData.sin6_addr,
             ", port = ",
             ntohs(receivedData.sin6_port),
             ", packet id = ", seqID,
@@ -1842,15 +1807,6 @@ void UDPC::Context::update_impl() {
     } while (true);
 }
 
-const char *UDPC::Context::conditional_atostr(UDPC_LoggingType logType,
-                                              UDPC_IPV6_ADDR_TYPE addr) {
-    if (willLog(logType)) {
-        return UDPC_atostr((UDPC_HContext)this, addr);
-    } else {
-        return "";
-    }
-}
-
 UDPC::Context *UDPC::verifyContext(UDPC_HContext ctx) {
     if(ctx == nullptr) {
         return nullptr;
@@ -2076,7 +2032,7 @@ UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient, int isUsingLib
     ctx->authPolicy.exchange(UDPC_AuthPolicy::UDPC_AUTH_POLICY_FALLBACK);
 
     UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Got listen addr ",
-        ctx->conditional_atostr(UDPC_LoggingType::UDPC_INFO, listenId.addr));
+        listenId.addr);
 
     if(isUsingLibsodium) {
 #ifdef UDPC_LIBSODIUM_ENABLED