Пример #1
0
int main(void)
{
	int i;

	harness_boot();
	transport = harness_openTransport();

	/* since we are multi-threaded and subject to off-chip timing
         * differences this will not be sufficient to make this test 
         * deterministic
         */
        srand(0);

	/* create all the ports */
	for (i=0; i<NUM_PORTS; i++) {
		char localName[]  = "port?" LOCAL;
		char remoteName[] = "port?" REMOTE;

		/* update the port names */
		localName[4] = remoteName[4] = '0' + i;

		/* create the receiving port */
		EMBX(CreatePort(transport, localName, &(inPort[i])));
		
		/* now create both transmission ports */
		EMBX(ConnectBlock(transport, remoteName, &(outPort[i])));
		EMBX(ConnectBlock(transport, localName,  &(outPort[i+NUM_PORTS])));
	}

	/* now create the threads that will actually run the test */
	for (i=0; i<NUM_PORTS; i++) {
		harness_createThread(workerThread, (void *) i);
	}

	/* now inject some messages into the test system (we only inject
	 * half the requested number because our partner will inject the
	 * other half)
	 */
	for (i=0; i<(NUM_INJECTED / 2); i++) {
		EMBX_VOID *message;
		message = fabricateMessage();
		assert(message);
		assert(sendMessage(message));
	}

	/* now wait for the test to run out of steam */
	harness_waitForChildren();

	/* clean up after ourselves */
	for (i=0; i<NUM_PORTS; i++) {
		EMBX(ClosePort(inPort[i]));
		EMBX(ClosePort(outPort[i]));
		EMBX(ClosePort(outPort[i+NUM_PORTS]));
	}

	printf("\n"); /* pretty printing */
	harness_shutdown();
	return 0;
}
Пример #2
0
bool Ban::acceptConnection(uint32_t clientip)
{
	std::lock_guard<std::recursive_mutex> lockClass(lock);

	uint64_t currentTime = OTSYS_TIME();

	auto it = ipConnectMap.find(clientip);
	if (it == ipConnectMap.end()) {
		ipConnectMap.emplace(clientip, ConnectBlock(currentTime, 0, 1));
		return true;
	}

	ConnectBlock& connectBlock = it->second;
	if (connectBlock.blockTime > currentTime) {
		connectBlock.blockTime += 250;
		return false;
	}

	int64_t timeDiff = currentTime - connectBlock.lastAttempt;
	connectBlock.lastAttempt = currentTime;
	if (timeDiff <= 5000) {
		if (++connectBlock.count > 5) {
			connectBlock.count = 0;
			if (timeDiff <= 500) {
				connectBlock.blockTime = currentTime + 3000;
				return false;
			}
		}
	} else {
		connectBlock.count = 1;
	}
	return true;
}