<commit_before>#include "core.h"
#include "ini.h"


NetCore::NetCore() {
	clientCount = 0;
	for (int i = 0; i < CLIENT_LIMIT; i++)
		clients[i] = NULL;
	serverCount = 0;
	for (int i = 0; i < SERVER_LIMIT; i++)
		servers[i] = NULL;

	nextWindowID = 1;
}

Client *NetCore::findClientWithSessionKey(uint8_t *key) const {
	for (int i = 0; i < clientCount; i++)
		if (!memcmp(clients[i]->sessionKey, key, SESSION_KEY_SIZE))
			return clients[i];

	return 0;
}

int NetCore::registerServer(Server *server) {
	if (serverCount >= SERVER_LIMIT)
		return -1;

	int id = serverCount++;
	servers[id] = server;
	server->attachedToCore();
	return id;
}
void NetCore::deregisterServer(int id) {
	Server *server = servers[id];
	server->close();
	delete server;

	serverCount--;
	servers[id] = servers[serverCount];
}
int NetCore::findServerID(Server *server) const {
	for (int i = 0; i < SERVER_LIMIT; i++)
		if (servers[i] == server)
			return i;
	return -1;
}

int NetCore::execute() {
	// prepare the listen socket
	int listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (listener == -1) {
		perror("Could not create the listener socket");
		return -1;
	}

	int v = 1;
	if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1) {
		perror("Could not set SO_REUSEADDR");
		return -2;
	}

	sockaddr_in listenAddr;
	listenAddr.sin_family = AF_INET;
	listenAddr.sin_port = htons(5454);
	listenAddr.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(listener, (sockaddr *)&listenAddr, sizeof(listenAddr)) == -1) {
		perror("Could not bind to the listener socket");
		return -3;
	}

	if (!SocketRWCommon::setSocketNonBlocking(listener)) {
		perror("[Listener] Could not set non-blocking");
		return -4;
	}

	if (listen(listener, 10) == -1) {
		perror("Could not listen()");
		return -5;
	}

	printf("Listening!\n");


	// do stuff!
	while (!quitFlag) {
		fd_set readSet, writeSet;
		FD_ZERO(&readSet);
		FD_ZERO(&writeSet);

		int maxFD = listener;
		FD_SET(listener, &readSet);

		time_t now = time(NULL);

		for (int i = 0; i < clientCount; i++) {
#ifdef USE_GNUTLS
			if (clients[i]->state == Client::CS_TLS_HANDSHAKE)
				clients[i]->tryTLSHandshake();
#endif<commit_msg>fix NetCore::quitFlag being uninitialised (thanks, Valgrind/memcheck!)<commit_after>#include "core.h"
#include "ini.h"


NetCore::NetCore() {
	clientCount = 0;
	for (int i = 0; i < CLIENT_LIMIT; i++)
		clients[i] = NULL;
	serverCount = 0;
	for (int i = 0; i < SERVER_LIMIT; i++)
		servers[i] = NULL;

	nextWindowID = 1;
}

Client *NetCore::findClientWithSessionKey(uint8_t *key) const {
	for (int i = 0; i < clientCount; i++)
		if (!memcmp(clients[i]->sessionKey, key, SESSION_KEY_SIZE))
			return clients[i];

	return 0;
}

int NetCore::registerServer(Server *server) {
	if (serverCount >= SERVER_LIMIT)
		return -1;

	int id = serverCount++;
	servers[id] = server;
	server->attachedToCore();
	return id;
}
void NetCore::deregisterServer(int id) {
	Server *server = servers[id];
	server->close();
	delete server;

	serverCount--;
	servers[id] = servers[serverCount];
}
int NetCore::findServerID(Server *server) const {
	for (int i = 0; i < SERVER_LIMIT; i++)
		if (servers[i] == server)
			return i;
	return -1;
}

int NetCore::execute() {
	// prepare the listen socket
	int listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (listener == -1) {
		perror("Could not create the listener socket");
		return -1;
	}

	int v = 1;
	if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1) {
		perror("Could not set SO_REUSEADDR");
		return -2;
	}

	sockaddr_in listenAddr;
	listenAddr.sin_family = AF_INET;
	listenAddr.sin_port = htons(5454);
	listenAddr.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(listener, (sockaddr *)&listenAddr, sizeof(listenAddr)) == -1) {
		perror("Could not bind to the listener socket");
		return -3;
	}

	if (!SocketRWCommon::setSocketNonBlocking(listener)) {
		perror("[Listener] Could not set non-blocking");
		return -4;
	}

	if (listen(listener, 10) == -1) {
		perror("Could not listen()");
		return -5;
	}

	printf("Listening!\n");


	// do stuff!
	quitFlag = false;

	while (!quitFlag) {
		fd_set readSet, writeSet;
		FD_ZERO(&readSet);
		FD_ZERO(&writeSet);

		int maxFD = listener;
		FD_SET(listener, &readSet);

		time_t now = time(NULL);

		for (int i = 0; i < clientCount; i++) {
#ifdef USE_GNUTLS
			if (clients[i]->state == Client::CS_TLS_HANDSHAKE)
				clients[i]->tryTLSHandshake();
#endif
{'fraction_non_alphanumeric': 0.15879828326180256, 'fraction_numerical': 0.012649649875762368, 'mean_word_length': 4.3478260869565215, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2086317', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1690, 'n_words': 416}