Use macro to conditionally log
This should skip evaluating log parameters if the log level is not met.
This commit is contained in:
parent
5a3c7cd9a0
commit
27528bfbc5
2 changed files with 60 additions and 36 deletions
|
@ -16,6 +16,8 @@
|
||||||
#define UDPC_ATOSTR_BUFSIZE 40
|
#define UDPC_ATOSTR_BUFSIZE 40
|
||||||
#define UDPC_ATOSTR_SIZE (UDPC_ATOSTR_BUFCOUNT * UDPC_ATOSTR_BUFSIZE)
|
#define UDPC_ATOSTR_SIZE (UDPC_ATOSTR_BUFCOUNT * UDPC_ATOSTR_BUFSIZE)
|
||||||
|
|
||||||
|
#define UDPC_CHECK_LOG(ctx, type, ...) if(ctx->willLog(type)){ctx->log(type, __VA_ARGS__);}
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
@ -114,6 +116,8 @@ struct Context {
|
||||||
public:
|
public:
|
||||||
Context(bool isThreaded);
|
Context(bool isThreaded);
|
||||||
|
|
||||||
|
bool willLog(UDPC_LoggingType);
|
||||||
|
|
||||||
void log(UDPC_LoggingType) {}
|
void log(UDPC_LoggingType) {}
|
||||||
|
|
||||||
template<typename... Targs>
|
template<typename... Targs>
|
||||||
|
|
|
@ -176,6 +176,26 @@ mutex()
|
||||||
threadRunning.store(true);
|
threadRunning.store(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool UDPC::Context::willLog(UDPC_LoggingType type) {
|
||||||
|
switch(loggingType.load()) {
|
||||||
|
case UDPC_LoggingType::UDPC_SILENT:
|
||||||
|
return false;
|
||||||
|
case UDPC_LoggingType::UDPC_ERROR:
|
||||||
|
return type == UDPC_LoggingType::UDPC_ERROR;
|
||||||
|
case UDPC_LoggingType::UDPC_WARNING:
|
||||||
|
return type == UDPC_LoggingType::UDPC_ERROR
|
||||||
|
|| type == UDPC_LoggingType::UDPC_WARNING;
|
||||||
|
case UDPC_LoggingType::UDPC_VERBOSE:
|
||||||
|
return type == UDPC_LoggingType::UDPC_ERROR
|
||||||
|
|| type == UDPC_LoggingType::UDPC_WARNING
|
||||||
|
|| type == UDPC_LoggingType::UDPC_VERBOSE;
|
||||||
|
case UDPC_LoggingType::UDPC_INFO:
|
||||||
|
return type != UDPC_LoggingType::UDPC_SILENT;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void UDPC::Context::update_impl() {
|
void UDPC::Context::update_impl() {
|
||||||
const auto now = std::chrono::steady_clock::now();
|
const auto now = std::chrono::steady_clock::now();
|
||||||
std::chrono::steady_clock::duration dt = now - lastUpdated;
|
std::chrono::steady_clock::duration dt = now - lastUpdated;
|
||||||
|
@ -189,7 +209,7 @@ void UDPC::Context::update_impl() {
|
||||||
temp_dt_fs = now - iter->second.received;
|
temp_dt_fs = now - iter->second.received;
|
||||||
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(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -203,7 +223,7 @@ void UDPC::Context::update_impl() {
|
||||||
iter->second.toggledTimer += dt;
|
iter->second.toggledTimer += dt;
|
||||||
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(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -229,7 +249,7 @@ void UDPC::Context::update_impl() {
|
||||||
if(iter->second.toggledTimer >= iter->second.toggleT) {
|
if(iter->second.toggledTimer >= iter->second.toggleT) {
|
||||||
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(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -319,7 +339,7 @@ void UDPC::Context::update_impl() {
|
||||||
(struct sockaddr*) &destinationInfo,
|
(struct sockaddr*) &destinationInfo,
|
||||||
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
|
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
|
||||||
if(sentBytes != 20) {
|
if(sentBytes != 20) {
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -327,7 +347,7 @@ void UDPC::Context::update_impl() {
|
||||||
iter->second.port);
|
iter->second.port);
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
log(UDPC_LoggingType::UDPC_INFO, "Sent initiate connection to ",
|
UDPC_CHECK_LOG(this, 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);
|
||||||
|
@ -361,7 +381,7 @@ void UDPC::Context::update_impl() {
|
||||||
(struct sockaddr*) &destinationInfo,
|
(struct sockaddr*) &destinationInfo,
|
||||||
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
|
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
|
||||||
if(sentBytes != 20) {
|
if(sentBytes != 20) {
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -408,7 +428,7 @@ void UDPC::Context::update_impl() {
|
||||||
(struct sockaddr*) &destinationInfo,
|
(struct sockaddr*) &destinationInfo,
|
||||||
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
|
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
|
||||||
if(sentBytes != 20) {
|
if(sentBytes != 20) {
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -473,7 +493,7 @@ void UDPC::Context::update_impl() {
|
||||||
(struct sockaddr*) &destinationInfo,
|
(struct sockaddr*) &destinationInfo,
|
||||||
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
|
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
|
||||||
if(sentBytes != 20 + pInfo.dataSize) {
|
if(sentBytes != 20 + pInfo.dataSize) {
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -536,13 +556,13 @@ void UDPC::Context::update_impl() {
|
||||||
} else if(bytes == SOCKET_ERROR) {
|
} else if(bytes == SOCKET_ERROR) {
|
||||||
int error = WSAGetLastError();
|
int error = WSAGetLastError();
|
||||||
if(error != WSAEWOULDBLOCK) {
|
if(error != WSAEWOULDBLOCK) {
|
||||||
log(UDPC_LoggingType::UDPC_ERROR,
|
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
|
||||||
"Error receiving packet, ", error);
|
"Error receiving packet, ", error);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else if(bytes < 20) {
|
} else if(bytes < 20) {
|
||||||
// packet size is too small, invalid packet
|
// packet size is too small, invalid packet
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -556,7 +576,7 @@ void UDPC::Context::update_impl() {
|
||||||
return;
|
return;
|
||||||
} else if(bytes < 20) {
|
} else if(bytes < 20) {
|
||||||
// packet size is too small, invalid packet
|
// packet size is too small, invalid packet
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -569,7 +589,7 @@ void UDPC::Context::update_impl() {
|
||||||
uint32_t temp = ntohl(*((uint32_t*)recvBuf));
|
uint32_t temp = ntohl(*((uint32_t*)recvBuf));
|
||||||
if(temp != protocolID) {
|
if(temp != protocolID) {
|
||||||
// Invalid protocol id in packet
|
// Invalid protocol id in packet
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -597,7 +617,7 @@ void UDPC::Context::update_impl() {
|
||||||
&& conMap.find(identifier) == conMap.end()) {
|
&& conMap.find(identifier) == conMap.end()) {
|
||||||
// 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(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -629,7 +649,7 @@ void UDPC::Context::update_impl() {
|
||||||
iter->second.flags.reset(3);
|
iter->second.flags.reset(3);
|
||||||
iter->second.id = conID;
|
iter->second.id = conID;
|
||||||
iter->second.flags.set(4);
|
iter->second.flags.set(4);
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -650,7 +670,7 @@ void UDPC::Context::update_impl() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// packet is valid
|
// packet is valid
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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),
|
||||||
|
@ -676,7 +696,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(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_INFO,
|
UDPC_LoggingType::UDPC_INFO,
|
||||||
"RTT: ",
|
"RTT: ",
|
||||||
UDPC::durationToFSec(iter->second.rtt) * 1000.0f,
|
UDPC::durationToFSec(iter->second.rtt) * 1000.0f,
|
||||||
|
@ -709,7 +729,7 @@ void UDPC::Context::update_impl() {
|
||||||
auto duration = now - sentInfoIter->second->sentTime;
|
auto duration = now - sentInfoIter->second->sentTime;
|
||||||
if(duration > UDPC::PACKET_TIMEOUT_TIME) {
|
if(duration > UDPC::PACKET_TIMEOUT_TIME) {
|
||||||
if(sentIter->dataSize <= 20) {
|
if(sentIter->dataSize <= 20) {
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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");
|
||||||
|
@ -744,7 +764,7 @@ void UDPC::Context::update_impl() {
|
||||||
diff = 0xFFFFFFFF - seqID + 1 + iter->second.rseq;
|
diff = 0xFFFFFFFF - seqID + 1 + iter->second.rseq;
|
||||||
if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) {
|
if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) {
|
||||||
// already received packet
|
// already received packet
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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;
|
||||||
|
@ -758,7 +778,7 @@ void UDPC::Context::update_impl() {
|
||||||
// sequence is older
|
// sequence is older
|
||||||
if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) {
|
if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) {
|
||||||
// already received packet
|
// already received packet
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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;
|
||||||
|
@ -773,14 +793,14 @@ void UDPC::Context::update_impl() {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// already received packet
|
// already received packet
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_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(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_VERBOSE,
|
UDPC_LoggingType::UDPC_VERBOSE,
|
||||||
"Received packet is out of order");
|
"Received packet is out of order");
|
||||||
}
|
}
|
||||||
|
@ -800,14 +820,14 @@ void UDPC::Context::update_impl() {
|
||||||
recPktInfo.receiver.port = ntohs(socketInfo.sin6_port);
|
recPktInfo.receiver.port = ntohs(socketInfo.sin6_port);
|
||||||
|
|
||||||
if(!receivedPkts.push(recPktInfo)) {
|
if(!receivedPkts.push(recPktInfo)) {
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_WARNING,
|
UDPC_LoggingType::UDPC_WARNING,
|
||||||
"receivedPkts is full, removing oldest entry to make room");
|
"receivedPkts is full, removing oldest entry to make room");
|
||||||
receivedPkts.pop();
|
receivedPkts.pop();
|
||||||
receivedPkts.push(recPktInfo);
|
receivedPkts.push(recPktInfo);
|
||||||
}
|
}
|
||||||
} else if(bytes == 20) {
|
} else if(bytes == 20) {
|
||||||
log(
|
UDPC_CHECK_LOG(this,
|
||||||
UDPC_LoggingType::UDPC_VERBOSE,
|
UDPC_LoggingType::UDPC_VERBOSE,
|
||||||
"Received packet has no payload (probably heartbeat packet)");
|
"Received packet has no payload (probably heartbeat packet)");
|
||||||
}
|
}
|
||||||
|
@ -936,7 +956,7 @@ 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::UDPC_INFO, "Got listen addr ",
|
UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Got listen addr ",
|
||||||
UDPC_atostr((UDPC_HContext)ctx, listenId.addr));
|
UDPC_atostr((UDPC_HContext)ctx, listenId.addr));
|
||||||
|
|
||||||
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
|
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
|
||||||
|
@ -944,7 +964,7 @@ UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient) {
|
||||||
WORD wVersionRequested = MAKEWORD(2, 2);
|
WORD wVersionRequested = MAKEWORD(2, 2);
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
if(WSAStartup(wVersionRequested, &wsaData) != 0) {
|
if(WSAStartup(wVersionRequested, &wsaData) != 0) {
|
||||||
ctx->log(UDPC_LoggingType::UDPC_ERROR, "Failed to initialize Winsock");
|
UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_ERROR, "Failed to initialize Winsock");
|
||||||
delete ctx;
|
delete ctx;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -955,10 +975,10 @@ UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient) {
|
||||||
if(UDPC_SOCKET_RETURN_ERROR(ctx->socketHandle)) {
|
if(UDPC_SOCKET_RETURN_ERROR(ctx->socketHandle)) {
|
||||||
// TODO maybe different way of handling init fail
|
// TODO maybe different way of handling init fail
|
||||||
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
|
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
|
||||||
ctx->log(UDPC_LoggingType::UDPC_ERROR, "Failed to create socket, ",
|
UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_ERROR, "Failed to create socket, ",
|
||||||
WSAGetLastError());
|
WSAGetLastError());
|
||||||
#else
|
#else
|
||||||
ctx->log(UDPC_LoggingType::UDPC_ERROR, "Failed to create socket");
|
UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_ERROR, "Failed to create socket");
|
||||||
#endif
|
#endif
|
||||||
delete ctx;
|
delete ctx;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -983,7 +1003,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(UDPC_IPV6_SOCKADDR_TYPE)) < 0) {
|
sizeof(UDPC_IPV6_SOCKADDR_TYPE)) < 0) {
|
||||||
// TODO maybe different way of handling init fail
|
// TODO maybe different way of handling init fail
|
||||||
ctx->log(UDPC_LoggingType::UDPC_ERROR, "Failed to bind socket");
|
UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_ERROR, "Failed to bind socket");
|
||||||
UDPC_CLEANUPSOCKET(ctx->socketHandle);
|
UDPC_CLEANUPSOCKET(ctx->socketHandle);
|
||||||
delete ctx;
|
delete ctx;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -1009,13 +1029,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::UDPC_ERROR, "Failed to set nonblocking on socket");
|
UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_ERROR, "Failed to set nonblocking on socket");
|
||||||
UDPC_CLEANUPSOCKET(ctx->socketHandle);
|
UDPC_CLEANUPSOCKET(ctx->socketHandle);
|
||||||
delete ctx;
|
delete ctx;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->log(UDPC_LoggingType::UDPC_INFO, "Initialized UDPC");
|
UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Initialized UDPC");
|
||||||
|
|
||||||
return (UDPC_HContext) ctx;
|
return (UDPC_HContext) ctx;
|
||||||
}
|
}
|
||||||
|
@ -1029,7 +1049,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::UDPC_INFO, "Initialized threaded UDPC");
|
UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Initialized threaded UDPC");
|
||||||
|
|
||||||
return (UDPC_HContext) ctx;
|
return (UDPC_HContext) ctx;
|
||||||
}
|
}
|
||||||
|
@ -1064,7 +1084,7 @@ void UDPC_client_initiate_connection(UDPC_HContext ctx, UDPC_ConnectionId connec
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
c->log(UDPC_LoggingType::UDPC_INFO, "client_initiate_connection: Got peer a = ",
|
UDPC_CHECK_LOG(c, 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);
|
||||||
|
|
||||||
|
@ -1085,9 +1105,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::UDPC_VERBOSE, "client_initiate_connection: Initiating connection...");
|
UDPC_CHECK_LOG(c, UDPC_LoggingType::UDPC_VERBOSE, "client_initiate_connection: Initiating connection...");
|
||||||
} else {
|
} else {
|
||||||
c->log(UDPC_LoggingType::UDPC_ERROR, "client_initiate_connection: Already connected to peer");
|
UDPC_CHECK_LOG(c, UDPC_LoggingType::UDPC_ERROR, "client_initiate_connection: Already connected to peer");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1122,7 +1142,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(
|
UDPC_CHECK_LOG(c,
|
||||||
UDPC_LoggingType::UDPC_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");
|
||||||
|
@ -1282,7 +1302,7 @@ int UDPC_set_received_capacity(UDPC_HContext ctx, unsigned int newCapacity) {
|
||||||
unsigned int status = 0;
|
unsigned int status = 0;
|
||||||
c->receivedPkts.changeCapacity(newCapacity, &status);
|
c->receivedPkts.changeCapacity(newCapacity, &status);
|
||||||
if(status == 1) {
|
if(status == 1) {
|
||||||
c->log(UDPC_LoggingType::UDPC_WARNING,
|
UDPC_CHECK_LOG(c, UDPC_LoggingType::UDPC_WARNING,
|
||||||
"Received Queue: Previous size was truncated to new capacity");
|
"Received Queue: Previous size was truncated to new capacity");
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Reference in a new issue