Rename enum types to avoid name conflict

This commit is contained in:
Stephen Seo 2019-09-19 10:58:19 +09:00
parent 8661af9529
commit 0e22357d55
4 changed files with 63 additions and 63 deletions

View file

@ -128,54 +128,54 @@ private:
template<typename... Targs> template<typename... Targs>
void log_impl(UDPC_LoggingType type, Targs... args) { // {{{ void log_impl(UDPC_LoggingType type, Targs... args) { // {{{
switch(loggingType.load()) { switch(loggingType.load()) {
case UDPC_LoggingType::SILENT: case UDPC_LoggingType::UDPC_SILENT:
return; return;
case UDPC_LoggingType::ERROR: case UDPC_LoggingType::UDPC_ERROR:
if(type == UDPC_LoggingType::ERROR) { if(type == UDPC_LoggingType::UDPC_ERROR) {
std::cerr << "ERROR: "; std::cerr << "ERROR: ";
} else { } else {
return; return;
} }
break; break;
case UDPC_LoggingType::WARNING: case UDPC_LoggingType::UDPC_WARNING:
switch(type) { switch(type) {
case UDPC_LoggingType::ERROR: case UDPC_LoggingType::UDPC_ERROR:
std::cerr << "ERROR: "; std::cerr << "ERROR: ";
break; break;
case UDPC_LoggingType::WARNING: case UDPC_LoggingType::UDPC_WARNING:
std::cerr << "WARNING: "; std::cerr << "WARNING: ";
break; break;
default: default:
return; return;
} }
break; break;
case UDPC_LoggingType::VERBOSE: case UDPC_LoggingType::UDPC_VERBOSE:
switch(type) { switch(type) {
case UDPC_LoggingType::ERROR: case UDPC_LoggingType::UDPC_ERROR:
std::cerr << "ERROR: "; std::cerr << "ERROR: ";
break; break;
case UDPC_LoggingType::WARNING: case UDPC_LoggingType::UDPC_WARNING:
std::cerr << "WARNING: "; std::cerr << "WARNING: ";
break; break;
case UDPC_LoggingType::VERBOSE: case UDPC_LoggingType::UDPC_VERBOSE:
std::cerr << "VERBOSE: "; std::cerr << "VERBOSE: ";
break; break;
default: default:
return; return;
} }
break; break;
case UDPC_LoggingType::INFO: case UDPC_LoggingType::UDPC_INFO:
switch(type) { switch(type) {
case UDPC_LoggingType::ERROR: case UDPC_LoggingType::UDPC_ERROR:
std::cerr << "ERROR: "; std::cerr << "ERROR: ";
break; break;
case UDPC_LoggingType::WARNING: case UDPC_LoggingType::UDPC_WARNING:
std::cerr << "WARNING: "; std::cerr << "WARNING: ";
break; break;
case UDPC_LoggingType::VERBOSE: case UDPC_LoggingType::UDPC_VERBOSE:
std::cerr << "VERBOSE: "; std::cerr << "VERBOSE: ";
break; break;
case UDPC_LoggingType::INFO: case UDPC_LoggingType::UDPC_INFO:
std::cerr << "INFO: "; std::cerr << "INFO: ";
break; break;
default: default:
@ -196,28 +196,28 @@ private:
template<typename T, typename... Targs> template<typename T, typename... Targs>
void log_impl_next(UDPC_LoggingType type, T value, Targs... args) { // {{{ void log_impl_next(UDPC_LoggingType type, T value, Targs... args) { // {{{
switch(loggingType.load()) { switch(loggingType.load()) {
case UDPC_LoggingType::SILENT: case UDPC_LoggingType::UDPC_SILENT:
return; return;
case UDPC_LoggingType::ERROR: case UDPC_LoggingType::UDPC_ERROR:
if(type == UDPC_LoggingType::ERROR) { if(type == UDPC_LoggingType::UDPC_ERROR) {
std::cerr << value; std::cerr << value;
} }
break; break;
case UDPC_LoggingType::WARNING: case UDPC_LoggingType::UDPC_WARNING:
if(type == UDPC_LoggingType::ERROR || type == UDPC_LoggingType::WARNING) { if(type == UDPC_LoggingType::UDPC_ERROR || type == UDPC_LoggingType::UDPC_WARNING) {
std::cerr << value; std::cerr << value;
} }
break; break;
case UDPC_LoggingType::VERBOSE: case UDPC_LoggingType::UDPC_VERBOSE:
if(type == UDPC_LoggingType::ERROR || type == UDPC_LoggingType::WARNING if(type == UDPC_LoggingType::UDPC_ERROR || type == UDPC_LoggingType::UDPC_WARNING
|| type == UDPC_LoggingType::VERBOSE) { || type == UDPC_LoggingType::UDPC_VERBOSE) {
std::cerr << value; std::cerr << value;
} }
break; break;
case UDPC_LoggingType::INFO: case UDPC_LoggingType::UDPC_INFO:
if(type == UDPC_LoggingType::ERROR || type == UDPC_LoggingType::WARNING if(type == UDPC_LoggingType::UDPC_ERROR || type == UDPC_LoggingType::UDPC_WARNING
|| type == UDPC_LoggingType::VERBOSE || type == UDPC_LoggingType::UDPC_VERBOSE
|| type == UDPC_LoggingType::INFO) { || type == UDPC_LoggingType::UDPC_INFO) {
std::cerr << value; std::cerr << value;
} }
break; break;

View file

@ -136,9 +136,9 @@ flags(),
isAcceptNewConnections(true), isAcceptNewConnections(true),
protocolID(UDPC_DEFAULT_PROTOCOL_ID), protocolID(UDPC_DEFAULT_PROTOCOL_ID),
#ifndef NDEBUG #ifndef NDEBUG
loggingType(INFO), loggingType(UDPC_INFO),
#else #else
loggingType(WARNING), loggingType(UDPC_WARNING),
#endif #endif
atostrBufIndex(0), atostrBufIndex(0),
rng_engine(), rng_engine(),
@ -170,7 +170,7 @@ void UDPC::Context::update_impl() {
if(temp_dt_fs >= UDPC::CONNECTION_TIMEOUT) { if(temp_dt_fs >= UDPC::CONNECTION_TIMEOUT) {
removed.push_back(iter->first); removed.push_back(iter->first);
log( log(
UDPC_LoggingType::VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Timed out connection with ", "Timed out connection with ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr), UDPC_atostr((UDPC_HContext)this, iter->first.addr),
", port = ", ", port = ",
@ -184,7 +184,7 @@ void UDPC::Context::update_impl() {
if(iter->second.flags.test(1) && !iter->second.flags.test(2)) { if(iter->second.flags.test(1) && !iter->second.flags.test(2)) {
// good mode, bad rtt // good mode, bad rtt
log( log(
UDPC_LoggingType::INFO, UDPC_LoggingType::UDPC_INFO,
"Switching to bad mode in connection with ", "Switching to bad mode in connection with ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr), UDPC_atostr((UDPC_HContext)this, iter->first.addr),
", port = ", ", port = ",
@ -210,7 +210,7 @@ void UDPC::Context::update_impl() {
iter->second.toggleTimer = std::chrono::steady_clock::duration::zero(); iter->second.toggleTimer = std::chrono::steady_clock::duration::zero();
iter->second.toggledTimer = std::chrono::steady_clock::duration::zero(); iter->second.toggledTimer = std::chrono::steady_clock::duration::zero();
log( log(
UDPC_LoggingType::INFO, UDPC_LoggingType::UDPC_INFO,
"Switching to good mode in connection with ", "Switching to good mode in connection with ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr), UDPC_atostr((UDPC_HContext)this, iter->first.addr),
", port = ", ", port = ",
@ -300,14 +300,14 @@ void UDPC::Context::update_impl() {
sizeof(struct sockaddr_in6)); sizeof(struct sockaddr_in6));
if(sentBytes != 20) { if(sentBytes != 20) {
log( log(
UDPC_LoggingType::ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to send packet to initiate connection to ", "Failed to send packet to initiate connection to ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr), UDPC_atostr((UDPC_HContext)this, iter->first.addr),
", port = ", ", port = ",
iter->second.port); iter->second.port);
continue; continue;
} else { } else {
log(UDPC_LoggingType::INFO, "Sent initiate connection to ", log(UDPC_LoggingType::UDPC_INFO, "Sent initiate connection to ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr), UDPC_atostr((UDPC_HContext)this, iter->first.addr),
", port = ", ", port = ",
iter->second.port); iter->second.port);
@ -342,7 +342,7 @@ void UDPC::Context::update_impl() {
sizeof(struct sockaddr_in6)); sizeof(struct sockaddr_in6));
if(sentBytes != 20) { if(sentBytes != 20) {
log( log(
UDPC_LoggingType::ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to send packet to initiate connection to ", "Failed to send packet to initiate connection to ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr), UDPC_atostr((UDPC_HContext)this, iter->first.addr),
", port = ", ", port = ",
@ -386,7 +386,7 @@ void UDPC::Context::update_impl() {
sizeof(struct sockaddr_in6)); sizeof(struct sockaddr_in6));
if(sentBytes != 20) { if(sentBytes != 20) {
log( log(
UDPC_LoggingType::ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to send heartbeat packet to ", "Failed to send heartbeat packet to ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr), UDPC_atostr((UDPC_HContext)this, iter->first.addr),
", port = ", ", port = ",
@ -445,7 +445,7 @@ void UDPC::Context::update_impl() {
sizeof(struct sockaddr_in6)); sizeof(struct sockaddr_in6));
if(sentBytes != 20 + pInfo.dataSize) { if(sentBytes != 20 + pInfo.dataSize) {
log( log(
UDPC_LoggingType::ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to send packet to ", "Failed to send packet to ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr), UDPC_atostr((UDPC_HContext)this, iter->first.addr),
", port = ", ", port = ",
@ -508,7 +508,7 @@ void UDPC::Context::update_impl() {
} else if(bytes < 20) { } else if(bytes < 20) {
// packet size is too small, invalid packet // packet size is too small, invalid packet
log( log(
UDPC_LoggingType::INFO, UDPC_LoggingType::UDPC_INFO,
"Received packet is smaller than header, ignoring packet from ", "Received packet is smaller than header, ignoring packet from ",
UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr), UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr),
", port = ", ", port = ",
@ -520,7 +520,7 @@ void UDPC::Context::update_impl() {
if(temp != protocolID) { if(temp != protocolID) {
// Invalid protocol id in packet // Invalid protocol id in packet
log( log(
UDPC_LoggingType::INFO, UDPC_LoggingType::UDPC_INFO,
"Received packet has invalid protocol id, ignoring packet from ", "Received packet has invalid protocol id, ignoring packet from ",
UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr), UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr),
", port = ", ", port = ",
@ -548,7 +548,7 @@ void UDPC::Context::update_impl() {
// is receiving as server, connection did not already exist // is receiving as server, connection did not already exist
UDPC::ConnectionData newConnection(true, this, receivedData.sin6_addr, receivedData.sin6_scope_id, ntohs(receivedData.sin6_port)); UDPC::ConnectionData newConnection(true, this, receivedData.sin6_addr, receivedData.sin6_scope_id, ntohs(receivedData.sin6_port));
log( log(
UDPC_LoggingType::VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Establishing connection with client ", "Establishing connection with client ",
UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr), UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr),
", port = ", ", port = ",
@ -580,7 +580,7 @@ void UDPC::Context::update_impl() {
iter->second.id = conID; iter->second.id = conID;
iter->second.flags.set(4); iter->second.flags.set(4);
log( log(
UDPC_LoggingType::VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Established connection with server ", "Established connection with server ",
UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr), UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr),
", port = ", ", port = ",
@ -601,7 +601,7 @@ void UDPC::Context::update_impl() {
// packet is valid // packet is valid
log( log(
UDPC_LoggingType::INFO, UDPC_LoggingType::UDPC_INFO,
"Received valid packet from ", "Received valid packet from ",
UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr), UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr),
", port = ", ", port = ",
@ -627,7 +627,7 @@ void UDPC::Context::update_impl() {
iter->second.flags.set(2, iter->second.rtt <= UDPC::GOOD_RTT_LIMIT); iter->second.flags.set(2, iter->second.rtt <= UDPC::GOOD_RTT_LIMIT);
log( log(
UDPC_LoggingType::INFO, UDPC_LoggingType::UDPC_INFO,
"RTT: ", "RTT: ",
UDPC::durationToFSec(iter->second.rtt) * 1000.0f, UDPC::durationToFSec(iter->second.rtt) * 1000.0f,
" milliseconds"); " milliseconds");
@ -660,7 +660,7 @@ void UDPC::Context::update_impl() {
if(duration > UDPC::PACKET_TIMEOUT_TIME) { if(duration > UDPC::PACKET_TIMEOUT_TIME) {
if(sentIter->dataSize <= 20) { if(sentIter->dataSize <= 20) {
log( log(
UDPC_LoggingType::INFO, UDPC_LoggingType::UDPC_INFO,
"Timed out packet has no payload (probably " "Timed out packet has no payload (probably "
"heartbeat packet), ignoring it"); "heartbeat packet), ignoring it");
sentIter->flags |= 0x8; sentIter->flags |= 0x8;
@ -695,7 +695,7 @@ void UDPC::Context::update_impl() {
if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) { if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) {
// already received packet // already received packet
log( log(
UDPC_LoggingType::INFO, UDPC_LoggingType::UDPC_INFO,
"Received packet is already marked as received, ignoring it"); "Received packet is already marked as received, ignoring it");
return; return;
} }
@ -709,7 +709,7 @@ void UDPC::Context::update_impl() {
if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) { if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) {
// already received packet // already received packet
log( log(
UDPC_LoggingType::INFO, UDPC_LoggingType::UDPC_INFO,
"Received packet is already marked as received, ignoring it"); "Received packet is already marked as received, ignoring it");
return; return;
} }
@ -724,14 +724,14 @@ void UDPC::Context::update_impl() {
} else { } else {
// already received packet // already received packet
log( log(
UDPC_LoggingType::INFO, UDPC_LoggingType::UDPC_INFO,
"Received packet is already marked as received, ignoring it"); "Received packet is already marked as received, ignoring it");
return; return;
} }
if(isOutOfOrder) { if(isOutOfOrder) {
log( log(
UDPC_LoggingType::VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Received packet is out of order"); "Received packet is out of order");
} }
@ -751,7 +751,7 @@ void UDPC::Context::update_impl() {
if(iter->second.receivedPkts.size() == iter->second.receivedPkts.capacity()) { if(iter->second.receivedPkts.size() == iter->second.receivedPkts.capacity()) {
log( log(
UDPC_LoggingType::WARNING, UDPC_LoggingType::UDPC_WARNING,
"receivedPkts is full, removing oldest entry to make room"); "receivedPkts is full, removing oldest entry to make room");
iter->second.receivedPkts.pop(); iter->second.receivedPkts.pop();
} }
@ -759,7 +759,7 @@ void UDPC::Context::update_impl() {
iter->second.receivedPkts.push(recPktInfo); iter->second.receivedPkts.push(recPktInfo);
} else if(bytes == 20) { } else if(bytes == 20) {
log( log(
UDPC_LoggingType::VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Received packet has no payload (probably heartbeat packet)"); "Received packet has no payload (probably heartbeat packet)");
} }
} }
@ -887,14 +887,14 @@ UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient) {
UDPC::Context *ctx = new UDPC::Context(false); UDPC::Context *ctx = new UDPC::Context(false);
ctx->flags.set(1, isClient != 0); ctx->flags.set(1, isClient != 0);
ctx->log(UDPC_LoggingType::INFO, "Got listen addr ", ctx->log(UDPC_LoggingType::UDPC_INFO, "Got listen addr ",
UDPC_atostr((UDPC_HContext)ctx, listenId.addr)); UDPC_atostr((UDPC_HContext)ctx, listenId.addr));
// create socket // create socket
ctx->socketHandle = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); ctx->socketHandle = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if(ctx->socketHandle <= 0) { if(ctx->socketHandle <= 0) {
// TODO maybe different way of handling init fail // TODO maybe different way of handling init fail
ctx->log(UDPC_LoggingType::ERROR, "Failed to create socket"); ctx->log(UDPC_LoggingType::UDPC_ERROR, "Failed to create socket");
delete ctx; delete ctx;
return nullptr; return nullptr;
} }
@ -914,7 +914,7 @@ UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient) {
if(bind(ctx->socketHandle, (const struct sockaddr *)&ctx->socketInfo, if(bind(ctx->socketHandle, (const struct sockaddr *)&ctx->socketInfo,
sizeof(struct sockaddr_in6)) < 0) { sizeof(struct sockaddr_in6)) < 0) {
// TODO maybe different way of handling init fail // TODO maybe different way of handling init fail
ctx->log(UDPC_LoggingType::ERROR, "Failed to bind socket"); ctx->log(UDPC_LoggingType::UDPC_ERROR, "Failed to bind socket");
CleanupSocket(ctx->socketHandle); CleanupSocket(ctx->socketHandle);
delete ctx; delete ctx;
return nullptr; return nullptr;
@ -940,13 +940,13 @@ UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient) {
{ {
#endif #endif
// TODO maybe different way of handling init fail // TODO maybe different way of handling init fail
ctx->log(UDPC_LoggingType::ERROR, "Failed to set nonblocking on socket"); ctx->log(UDPC_LoggingType::UDPC_ERROR, "Failed to set nonblocking on socket");
CleanupSocket(ctx->socketHandle); CleanupSocket(ctx->socketHandle);
delete ctx; delete ctx;
return nullptr; return nullptr;
} }
ctx->log(UDPC_LoggingType::INFO, "Initialized UDPC"); ctx->log(UDPC_LoggingType::UDPC_INFO, "Initialized UDPC");
return (UDPC_HContext) ctx; return (UDPC_HContext) ctx;
} }
@ -960,7 +960,7 @@ UDPC_HContext UDPC_init_threaded_update(UDPC_ConnectionId listenId,
ctx->flags.set(0); ctx->flags.set(0);
ctx->thread = std::thread(UDPC::threadedUpdate, ctx); ctx->thread = std::thread(UDPC::threadedUpdate, ctx);
ctx->log(UDPC_LoggingType::INFO, "Initialized threaded UDPC"); ctx->log(UDPC_LoggingType::UDPC_INFO, "Initialized threaded UDPC");
return (UDPC_HContext) ctx; return (UDPC_HContext) ctx;
} }
@ -992,7 +992,7 @@ void UDPC_client_initiate_connection(UDPC_HContext ctx, UDPC_ConnectionId connec
return; return;
} }
c->log(UDPC_LoggingType::INFO, "client_initiate_connection: Got peer a = ", c->log(UDPC_LoggingType::UDPC_INFO, "client_initiate_connection: Got peer a = ",
UDPC_atostr((UDPC_HContext)ctx, connectionId.addr), UDPC_atostr((UDPC_HContext)ctx, connectionId.addr),
", p = ", connectionId.port); ", p = ", connectionId.port);
@ -1013,9 +1013,9 @@ void UDPC_client_initiate_connection(UDPC_HContext ctx, UDPC_ConnectionId connec
addrConIter = insertResult.first; addrConIter = insertResult.first;
} }
addrConIter->second.insert(connectionId); addrConIter->second.insert(connectionId);
c->log(UDPC_LoggingType::VERBOSE, "client_initiate_connection: Initiating connection..."); c->log(UDPC_LoggingType::UDPC_VERBOSE, "client_initiate_connection: Initiating connection...");
} else { } else {
c->log(UDPC_LoggingType::ERROR, "client_initiate_connection: Already connected to peer"); c->log(UDPC_LoggingType::UDPC_ERROR, "client_initiate_connection: Already connected to peer");
} }
} }
@ -1051,7 +1051,7 @@ void UDPC_queue_send(UDPC_HContext ctx, UDPC_ConnectionId destinationId,
auto iter = c->conMap.find(destinationId); auto iter = c->conMap.find(destinationId);
if(iter == c->conMap.end()) { if(iter == c->conMap.end()) {
c->log( c->log(
UDPC_LoggingType::ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to add packet to queue, no established connection " "Failed to add packet to queue, no established connection "
"with recipient"); "with recipient");
return; return;
@ -1146,7 +1146,7 @@ uint32_t UDPC_set_protocol_id(UDPC_HContext ctx, uint32_t id) {
UDPC_LoggingType UDPC_set_logging_type(UDPC_HContext ctx, UDPC_LoggingType loggingType) { UDPC_LoggingType UDPC_set_logging_type(UDPC_HContext ctx, UDPC_LoggingType loggingType) {
UDPC::Context *c = UDPC::verifyContext(ctx); UDPC::Context *c = UDPC::verifyContext(ctx);
if(!c) { if(!c) {
return UDPC_LoggingType::SILENT; return UDPC_LoggingType::UDPC_SILENT;
} }
return static_cast<UDPC_LoggingType>(c->loggingType.exchange(loggingType)); return static_cast<UDPC_LoggingType>(c->loggingType.exchange(loggingType));
} }

View file

@ -48,7 +48,7 @@ extern "C" {
struct UDPC_Context; struct UDPC_Context;
typedef struct UDPC_Context *UDPC_HContext; typedef struct UDPC_Context *UDPC_HContext;
typedef enum { SILENT, ERROR, WARNING, VERBOSE, INFO } UDPC_LoggingType; typedef enum { UDPC_SILENT, UDPC_ERROR, UDPC_WARNING, UDPC_VERBOSE, UDPC_INFO } UDPC_LoggingType;
typedef struct { typedef struct {
struct in6_addr addr; struct in6_addr addr;

View file

@ -99,7 +99,7 @@ int main(int argc, char **argv) {
puts("ERROR: context is NULL"); puts("ERROR: context is NULL");
return 1; return 1;
} }
UDPC_set_logging_type(context, UDPC_LoggingType::INFO); UDPC_set_logging_type(context, UDPC_LoggingType::UDPC_INFO);
unsigned int tick = 0; unsigned int tick = 0;
while(true) { while(true) {
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));