Blob Blame Raw

#include <cstring>

#include "tunnel.h"
#include "connection.h"
#include "udpsendqueue.h"



UdpSendQueue::UdpSendQueue(Connection &connection):
	connection(connection),
	nextSendTime(connection.tunnel.time + connection.tunnel.udpSendDuration),
	nextPartialSendTime(connection.tunnel.time + connection.tunnel.udpPartialSendDuration),
	finalEntryAdded(),
	nextIndex(),
	freeFirst(entries),
	busyFirst(),
	busyLast(),
	entries()
{
	Entry *e = entries;
	Entry *end = e + sizeof(entries)/sizeof(*entries) - 1;
	while(e < end) { e->next = e + 1; ++e; }
}


UdpSendQueue::~UdpSendQueue()
	{ }


UdpSendQueue::Entry* UdpSendQueue::allocEntry() {
	Entry *e = freeFirst;
	if (!e) return nullptr;
	freeFirst = e->next;
	e->prev = nullptr;
	e->next = busyFirst;
	(e->next ? e->next->prev : busyLast) = e;
	busyLast = e;
	return e;
}


void UdpSendQueue::moveToEnd(Entry *e) {
	if (!e->next) return;
	(e->prev ? e->prev->next : busyFirst) = e->next;
	(e->next ? e->next->prev : busyLast ) = e->prev;
	e->next = nullptr;
	e->prev = busyLast;
	(e->prev ? e->prev->next : busyFirst) = e;
}


void UdpSendQueue::freeEntry(Entry *e) {
	(e->prev ? e->prev->next : busyFirst) = e->next;
	(e->next ? e->next->prev : busyLast ) = e->prev;
	memset(e, 0, sizeof(*e));
	e->next = freeFirst;
	freeFirst = e;
}


void UdpSendQueue::confirm(unsigned int index, const unsigned char *bits, unsigned int bitsCount) {
	while(busyFirst && cycleLess(busyFirst->index, index))
		freeEntry(busyFirst);
	Entry *e = busyFirst;
	while(e) {
		unsigned int bit = e->index - index;
		if (bit > bitsCount)
			break;
		unsigned int byte = bit / 8;
		bit %= 8;
		if (bits[byte] & (1 << bit)) {
			Entry *ee = e;
			e = e->next;
			freeEntry(ee);
		} else {
			e = e->next;
		}
	}
}


bool UdpSendQueue::sendUdpSingle() {
	Time time = connection.tunnel.time;
	if (timeLess(time, nextSendTime))
		return false;
	
	Entry *e = busyFirst;
	if (!timeLequal(e->resendTime, time))
		e = nullptr;
	
	if (!e && !freeFirst)
		return false;
	
	if (!e) {
		int size = connection.tcpRecvQueue.busySize();
		if (size > PACKET_SIZE) size = PACKET_SIZE;
		if (size < 0) size = 0;
		
		if (size == 0) {
			if (connection.tcpSockId >= 0 || finalEntryAdded) {
				nextPartialSendTime = time + connection.tunnel.udpPartialSendDuration;
				return false;
			}
		} else {
			if (size < PACKET_SIZE && timeLess(time, nextPartialSendTime))
				return false;
		}
		
		if (( e = allocEntry() ))
			return false;
		e->index = nextIndex++;
		e->size = size;
		if (e->size) connection.tcpRecvQueue.pop(e->data, e->size); else finalEntryAdded = true;
	}
	
	Packet packet;
	packet.init(connection.id.id, e->index, CMD_DATA, e->size);
	if (e->size) memcpy(packet.payload, e->data, e->size);
	connection.tunnel.sendUdpPacket(connection.id.address, packet);
	
	e->resendTime = time + connection.tunnel.udpResendDuration;
	nextSendTime += connection.tunnel.udpPartialSendDuration;
	nextPartialSendTime = time + connection.tunnel.udpPartialSendDuration;
	moveToEnd(e);
	return true;
}


int UdpSendQueue::sendUdp() {
	int sent = 0;
	while(sendUdpSingle()) {
		++sent;
		if (sent == 100) {
			if (timeLess(nextSendTime, connection.tunnel.time))
				nextSendTime = connection.tunnel.time;
			break;
		}
	}
	return sent;
}


bool UdpSendQueue::canSentToUdp() const
	{ return connection.udpActive && (busyFirst || connection.tcpRecvQueue.busySize()); }


void UdpSendQueue::whenWriteToUdp(Time &t) const {
	int size = connection.tcpRecvQueue.busySize();
	if (size >= PACKET_SIZE) {
		if (timeLess(nextSendTime, t)) t = nextSendTime;
		return;
	}

	Time tt = t;
	if (size > 0 && timeLess(nextPartialSendTime, tt)) tt = nextPartialSendTime;
	if (timeLess(busyFirst->resendTime, tt)) tt = busyFirst->resendTime;
	if (timeLess(tt, nextSendTime)) tt = nextSendTime;
	if (timeLess(t, tt)) t = tt;
}