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() {
const auto now = std::chrono::steady_clock::now();
std::chrono::steady_clock::duration dt = now - lastUpdated;
temp_dt_fs = now - iter->second.received;
if(temp_dt_fs >= UDPC::CONNECTION_TIMEOUT) {
removed.push_back(iter->first);
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE,
"Timed out connection with ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr),
iter->second.toggledTimer += dt;
if(iter->second.flags.test(1) && !iter->second.flags.test(2)) {
// good mode, bad rtt
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO,
"Switching to bad mode in connection with ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr),
if(iter->second.toggledTimer >= iter->second.toggleT) {
iter->second.toggleTimer = std::chrono::steady_clock::duration::zero();
iter->second.toggledTimer = std::chrono::steady_clock::duration::zero();
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO,
"Switching to good mode in connection with ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr),
(struct sockaddr*) &destinationInfo,
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
if(sentBytes != 20) {
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_ERROR,
"Failed to send packet to initiate connection to ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr),
iter->second.port);
continue;
} 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),
", port = ",
iter->second.port);
(struct sockaddr*) &destinationInfo,
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
if(sentBytes != 20) {
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_ERROR,
"Failed to send packet to initiate connection to ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr),
(struct sockaddr*) &destinationInfo,
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
if(sentBytes != 20) {
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_ERROR,
"Failed to send heartbeat packet to ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr),
(struct sockaddr*) &destinationInfo,
sizeof(UDPC_IPV6_SOCKADDR_TYPE));
if(sentBytes != 20 + pInfo.dataSize) {
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_ERROR,
"Failed to send packet to ",
UDPC_atostr((UDPC_HContext)this, iter->first.addr),
} else if(bytes == SOCKET_ERROR) {
int error = WSAGetLastError();
if(error != WSAEWOULDBLOCK) {
- log(UDPC_LoggingType::UDPC_ERROR,
+ UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
"Error receiving packet, ", error);
}
return;
} else if(bytes < 20) {
// packet size is too small, invalid packet
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO,
"Received packet is smaller than header, ignoring packet from ",
UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr),
return;
} else if(bytes < 20) {
// packet size is too small, invalid packet
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO,
"Received packet is smaller than header, ignoring packet from ",
UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr),
uint32_t temp = ntohl(*((uint32_t*)recvBuf));
if(temp != protocolID) {
// Invalid protocol id in packet
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO,
"Received packet has invalid protocol id, ignoring packet from ",
UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr),
&& conMap.find(identifier) == conMap.end()) {
// 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));
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE,
"Establishing connection with client ",
UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr),
iter->second.flags.reset(3);
iter->second.id = conID;
iter->second.flags.set(4);
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE,
"Established connection with server ",
UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr),
}
// packet is valid
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO,
"Received valid packet from ",
UDPC_atostr((UDPC_HContext)this, receivedData.sin6_addr),
iter->second.flags.set(2, iter->second.rtt <= UDPC::GOOD_RTT_LIMIT);
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO,
"RTT: ",
UDPC::durationToFSec(iter->second.rtt) * 1000.0f,
auto duration = now - sentInfoIter->second->sentTime;
if(duration > UDPC::PACKET_TIMEOUT_TIME) {
if(sentIter->dataSize <= 20) {
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO,
"Timed out packet has no payload (probably "
"heartbeat packet), ignoring it");
diff = 0xFFFFFFFF - seqID + 1 + iter->second.rseq;
if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) {
// already received packet
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO,
"Received packet is already marked as received, ignoring it");
return;
// sequence is older
if((iter->second.ack & (0x80000000 >> (diff - 1))) != 0) {
// already received packet
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO,
"Received packet is already marked as received, ignoring it");
return;
}
} else {
// already received packet
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO,
"Received packet is already marked as received, ignoring it");
return;
}
if(isOutOfOrder) {
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE,
"Received packet is out of order");
}
recPktInfo.receiver.port = ntohs(socketInfo.sin6_port);
if(!receivedPkts.push(recPktInfo)) {
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_WARNING,
"receivedPkts is full, removing oldest entry to make room");
receivedPkts.pop();
receivedPkts.push(recPktInfo);
}
} else if(bytes == 20) {
- log(
+ UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE,
"Received packet has no payload (probably heartbeat packet)");
}
UDPC::Context *ctx = new UDPC::Context(false);
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));
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
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;
return nullptr;
}
if(UDPC_SOCKET_RETURN_ERROR(ctx->socketHandle)) {
// TODO maybe different way of handling init fail
#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());
#else
- ctx->log(UDPC_LoggingType::UDPC_ERROR, "Failed to create socket");
+ UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_ERROR, "Failed to create socket");
#endif
delete ctx;
return nullptr;
if(bind(ctx->socketHandle, (const struct sockaddr *)&ctx->socketInfo,
sizeof(UDPC_IPV6_SOCKADDR_TYPE)) < 0) {
// 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);
delete ctx;
return nullptr;
{
#endif
// 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);
delete ctx;
return nullptr;
}
- ctx->log(UDPC_LoggingType::UDPC_INFO, "Initialized UDPC");
+ UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Initialized UDPC");
return (UDPC_HContext) ctx;
}
ctx->flags.set(0);
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;
}
- 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),
", p = ", connectionId.port);
addrConIter = insertResult.first;
}
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 {
- 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");
}
}
auto iter = c->conMap.find(destinationId);
if(iter == c->conMap.end()) {
- c->log(
+ UDPC_CHECK_LOG(c,
UDPC_LoggingType::UDPC_ERROR,
"Failed to add packet to queue, no established connection "
"with recipient");
unsigned int status = 0;
c->receivedPkts.changeCapacity(newCapacity, &status);
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");
}
return 1;