readd ACK
This commit is contained in:
1 parent
ee908baa7a
commit
ff32db6e12
3 files changed
+215
-35
No files matched your search
+212
-30
@@ -179,6 +179,12 @@ typedef struct
|
||||
// ack return to send (like sliding window protocol)
|
||||
UINT8 firstacktosend;
|
||||
|
||||
// when no consecutive packets are received we keep in mind what packets
|
||||
// we already received in a queue
|
||||
UINT8 acktosend_head;
|
||||
UINT8 acktosend_tail;
|
||||
UINT8 acktosend[MAXACKTOSEND];
|
||||
|
||||
// automatically send keep alive packet when not enough trafic
|
||||
tic_t lasttimeacktosend_sent;
|
||||
// detect connection lost
|
||||
@@ -199,17 +205,22 @@ static node_t nodes[MAXNETNODES];
|
||||
// 0 if a = n (mod 256)
|
||||
// >0 if a > b (mod 256)
|
||||
// mnemonic: to use it compare to 0: cmpack(a,b)<0 is "a < b" ...
|
||||
FUNCMATH static inline INT32 cmpack(UINT8 a, UINT8 b)
|
||||
FUNCMATH static INT32 cmpack(UINT8 a, UINT8 b)
|
||||
{
|
||||
return (SINT8)(a - b);
|
||||
register INT32 d = a - b;
|
||||
|
||||
if (d >= 127 || d < -128)
|
||||
return -d;
|
||||
return d;
|
||||
}
|
||||
|
||||
/** Sets freeack to a free acknum and copies the netbuffer in the ackpak table
|
||||
*
|
||||
* \param freeack The address to store the free acknum at
|
||||
* \param lowtimer ???
|
||||
* \return True if a free acknum was found
|
||||
*/
|
||||
static boolean GetFreeAcknum(UINT8 *freeack)
|
||||
static boolean GetFreeAcknum(UINT8 *freeack, boolean lowtimer)
|
||||
{
|
||||
node_t *node = &nodes[doomcom->remotenode];
|
||||
INT32 i, numfreeslot = 0;
|
||||
@@ -238,8 +249,17 @@ static boolean GetFreeAcknum(UINT8 *freeack)
|
||||
node->nextacknum++;
|
||||
ackpak[i].destinationnode = (UINT8)(node - nodes);
|
||||
ackpak[i].length = doomcom->datalength;
|
||||
ackpak[i].senttime = I_GetTime();
|
||||
ackpak[i].resentnum = 0;
|
||||
if (lowtimer)
|
||||
{
|
||||
// Lowtime means can't be sent now so try it as soon as possible
|
||||
ackpak[i].senttime = 0;
|
||||
ackpak[i].resentnum = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ackpak[i].senttime = I_GetTime();
|
||||
ackpak[i].resentnum = 0;
|
||||
}
|
||||
M_Memcpy(ackpak[i].pak.raw, netbuffer, ackpak[i].length);
|
||||
|
||||
*freeack = ackpak[i].acknum;
|
||||
@@ -256,6 +276,38 @@ static boolean GetFreeAcknum(UINT8 *freeack)
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Counts how many acks are free
|
||||
*
|
||||
* \param urgent True if the type of the packet meant to
|
||||
* use an ack is lower than PT_CANFAIL
|
||||
* If for some reason you don't want use it
|
||||
* for any packet type in particular,
|
||||
* just set to false
|
||||
* \return The number of free acks
|
||||
*
|
||||
*/
|
||||
INT32 Net_GetFreeAcks(boolean urgent)
|
||||
{
|
||||
INT32 i, numfreeslot = 0;
|
||||
INT32 n = 0; // Number of free acks found
|
||||
|
||||
for (i = 0; i < MAXACKPACKETS; i++)
|
||||
if (!ackpak[i].acknum)
|
||||
{
|
||||
// For low priority packets, make sure to let freeslots so urgent packets can be sent
|
||||
if (!urgent)
|
||||
{
|
||||
numfreeslot++;
|
||||
if (numfreeslot <= URGENTFREESLOTNUM)
|
||||
continue;
|
||||
}
|
||||
|
||||
n++;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
// Get a ack to send in the queue of this node
|
||||
static UINT8 GetAcktosend(INT32 node)
|
||||
{
|
||||
@@ -275,6 +327,7 @@ static void RemoveAck(INT32 i)
|
||||
// We have got a packet, proceed the ack request and ack return
|
||||
static boolean Processackpak(void)
|
||||
{
|
||||
INT32 i;
|
||||
boolean goodpacket = true;
|
||||
node_t *node = &nodes[doomcom->remotenode];
|
||||
|
||||
@@ -283,8 +336,8 @@ static boolean Processackpak(void)
|
||||
{
|
||||
node->remotefirstack = netbuffer->ackreturn;
|
||||
// Search the ackbuffer and free it
|
||||
for (INT32 i = 0; i < MAXACKPACKETS; i++)
|
||||
if (ackpak[i].acknum && ackpak[i].destinationnode == doomcom->remotenode
|
||||
for (i = 0; i < MAXACKPACKETS; i++)
|
||||
if (ackpak[i].acknum && ackpak[i].destinationnode == node - nodes
|
||||
&& cmpack(ackpak[i].acknum, netbuffer->ackreturn) <= 0)
|
||||
{
|
||||
RemoveAck(i);
|
||||
@@ -304,22 +357,79 @@ static boolean Processackpak(void)
|
||||
}
|
||||
else
|
||||
{
|
||||
// Is a good packet so increment the acknowledge number,
|
||||
// Then search for a "hole" in the queue
|
||||
UINT8 nextfirstack = (UINT8)(node->firstacktosend + 1);
|
||||
if (!nextfirstack)
|
||||
nextfirstack = 1;
|
||||
// Check if it is not already in the queue
|
||||
for (i = node->acktosend_tail; i != node->acktosend_head; i = (i+1) % MAXACKTOSEND)
|
||||
if (node->acktosend[i] == ack)
|
||||
{
|
||||
DEBFILE(va("Discard(2) ack %d (duplicated)\n", ack));
|
||||
duppacket++;
|
||||
goodpacket = false; // Discard packet (duplicate)
|
||||
break;
|
||||
}
|
||||
if (goodpacket)
|
||||
{
|
||||
// Is a good packet so increment the acknowledge number,
|
||||
// Then search for a "hole" in the queue
|
||||
UINT8 nextfirstack = (UINT8)(node->firstacktosend + 1);
|
||||
if (!nextfirstack)
|
||||
nextfirstack = 1;
|
||||
|
||||
if (ack == nextfirstack)
|
||||
{
|
||||
node->firstacktosend = nextfirstack;
|
||||
}
|
||||
else // Out of order packet
|
||||
{
|
||||
// Don't increment firsacktosend, put it in asktosend queue
|
||||
// Will be incremented when the nextfirstack comes (code above)
|
||||
DEBFILE(va("out of order packet (%d expected)\n", nextfirstack));
|
||||
goodpacket = false;
|
||||
if (ack == nextfirstack)
|
||||
{
|
||||
UINT8 hm1; // head - 1
|
||||
boolean change = true;
|
||||
|
||||
node->firstacktosend = nextfirstack++;
|
||||
if (!nextfirstack)
|
||||
nextfirstack = 1;
|
||||
hm1 = (UINT8)((node->acktosend_head-1+MAXACKTOSEND) % MAXACKTOSEND);
|
||||
while (change)
|
||||
{
|
||||
change = false;
|
||||
for (i = node->acktosend_tail; i != node->acktosend_head;
|
||||
i = (i+1) % MAXACKTOSEND)
|
||||
{
|
||||
if (cmpack(node->acktosend[i], nextfirstack) <= 0)
|
||||
{
|
||||
if (node->acktosend[i] == nextfirstack)
|
||||
{
|
||||
node->firstacktosend = nextfirstack++;
|
||||
if (!nextfirstack)
|
||||
nextfirstack = 1;
|
||||
change = true;
|
||||
}
|
||||
if (i == node->acktosend_tail)
|
||||
{
|
||||
node->acktosend[node->acktosend_tail] = 0;
|
||||
node->acktosend_tail = (UINT8)((i+1) % MAXACKTOSEND);
|
||||
}
|
||||
else if (i == hm1)
|
||||
{
|
||||
node->acktosend[hm1] = 0;
|
||||
node->acktosend_head = hm1;
|
||||
hm1 = (UINT8)((hm1-1+MAXACKTOSEND) % MAXACKTOSEND);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else // Out of order packet
|
||||
{
|
||||
// Don't increment firsacktosend, put it in asktosend queue
|
||||
// Will be incremented when the nextfirstack comes (code above)
|
||||
UINT8 newhead = (UINT8)((node->acktosend_head+1) % MAXACKTOSEND);
|
||||
DEBFILE(va("out of order packet (%d expected)\n", nextfirstack));
|
||||
if (newhead != node->acktosend_tail)
|
||||
{
|
||||
node->acktosend[node->acktosend_head] = ack;
|
||||
node->acktosend_head = newhead;
|
||||
}
|
||||
else // Buffer full discard packet, sender will resend it
|
||||
{ // We can admit the packet but we will not detect the duplication after :(
|
||||
DEBFILE("no more freeackret\n");
|
||||
goodpacket = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -327,6 +437,43 @@ static boolean Processackpak(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
// send special packet with only ack on it
|
||||
void Net_SendAcks(INT32 node)
|
||||
{
|
||||
#ifdef NONET
|
||||
(void)node;
|
||||
#else
|
||||
netbuffer->packettype = PT_NOTHING;
|
||||
M_Memcpy(netbuffer->u.textcmd, nodes[node].acktosend, MAXACKTOSEND);
|
||||
HSendPacket(node, false, 0, MAXACKTOSEND);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef NONET
|
||||
static void GotAcks(void)
|
||||
{
|
||||
INT32 i, j;
|
||||
|
||||
for (j = 0; j < MAXACKTOSEND; j++)
|
||||
if (netbuffer->u.textcmd[j])
|
||||
for (i = 0; i < MAXACKPACKETS; i++)
|
||||
if (ackpak[i].acknum && ackpak[i].destinationnode == doomcom->remotenode)
|
||||
{
|
||||
if (ackpak[i].acknum == netbuffer->u.textcmd[j])
|
||||
RemoveAck(i);
|
||||
// nextacknum is first equal to acknum, then when receiving bigger ack
|
||||
// there is big chance the packet is lost
|
||||
// When resent, nextacknum = nodes[node].nextacknum
|
||||
// will redo the same but with different value
|
||||
else if (cmpack(ackpak[i].nextacknum, netbuffer->u.textcmd[j]) <= 0
|
||||
&& ackpak[i].senttime > 0)
|
||||
{
|
||||
ackpak[i].senttime--; // hurry up
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void Net_ConnectionTimeout(INT32 node)
|
||||
{
|
||||
// Don't timeout several times
|
||||
@@ -386,6 +533,11 @@ void Net_AckTicker(void)
|
||||
// This is something like node open flag
|
||||
if (nodes[i].firstacktosend)
|
||||
{
|
||||
// We haven't sent a packet for a long time
|
||||
// Acknowledge packet if needed
|
||||
if (nodes[i].lasttimeacktosend_sent + ACKTOSENDTIMEOUT < I_GetTime())
|
||||
Net_SendAcks(i);
|
||||
|
||||
if (!(nodes[i].flags & NF_CLOSE)
|
||||
&& nodes[i].lasttimepacketreceived + connectiontimeout < I_GetTime())
|
||||
{
|
||||
@@ -403,12 +555,37 @@ void Net_UnAcknowledgePacket(INT32 node)
|
||||
#ifdef NONET
|
||||
(void)node;
|
||||
#else
|
||||
INT32 hm1 = (nodes[node].acktosend_head-1+MAXACKTOSEND) % MAXACKTOSEND;
|
||||
DEBFILE(va("UnAcknowledge node %d\n", node));
|
||||
if (!node)
|
||||
return;
|
||||
nodes[node].firstacktosend--;
|
||||
if (!nodes[node].firstacktosend)
|
||||
nodes[node].firstacktosend = UINT8_MAX;
|
||||
if (nodes[node].acktosend[hm1] == netbuffer->ack)
|
||||
{
|
||||
nodes[node].acktosend[hm1] = 0;
|
||||
nodes[node].acktosend_head = (UINT8)hm1;
|
||||
}
|
||||
else if (nodes[node].firstacktosend == netbuffer->ack)
|
||||
{
|
||||
nodes[node].firstacktosend--;
|
||||
if (!nodes[node].firstacktosend)
|
||||
nodes[node].firstacktosend = UINT8_MAX;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (nodes[node].firstacktosend != netbuffer->ack)
|
||||
{
|
||||
nodes[node].acktosend_tail = (UINT8)
|
||||
((nodes[node].acktosend_tail-1+MAXACKTOSEND) % MAXACKTOSEND);
|
||||
nodes[node].acktosend[nodes[node].acktosend_tail] = nodes[node].firstacktosend;
|
||||
|
||||
nodes[node].firstacktosend--;
|
||||
if (!nodes[node].firstacktosend)
|
||||
nodes[node].firstacktosend = UINT8_MAX;
|
||||
}
|
||||
nodes[node].firstacktosend++;
|
||||
if (!nodes[node].firstacktosend)
|
||||
nodes[node].firstacktosend = 1;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -460,6 +637,8 @@ void Net_WaitAllAckReceived(UINT32 timeout)
|
||||
|
||||
static void InitNode(node_t *node)
|
||||
{
|
||||
node->acktosend_head = node->acktosend_tail = 0;
|
||||
memset(node->acktosend, 0, sizeof(node->acktosend));
|
||||
node->firstacktosend = 0;
|
||||
node->nextacknum = 1;
|
||||
node->remotefirstack = 0;
|
||||
@@ -531,11 +710,11 @@ void Net_CloseConnection(INT32 node)
|
||||
|
||||
nodes[node].flags |= NF_CLOSE;
|
||||
|
||||
if (nodes[node].firstacktosend)
|
||||
// try to Send ack back (two army problem)
|
||||
if (GetAcktosend(node))
|
||||
{
|
||||
// send a PT_NOTHING back to acknowledge the packet
|
||||
netbuffer->packettype = PT_NOTHING;
|
||||
HSendPacket(node, false, 0, 0);
|
||||
Net_SendAcks(node);
|
||||
Net_SendAcks(node);
|
||||
}
|
||||
|
||||
// check if we are waiting for an ack from this node
|
||||
@@ -907,7 +1086,7 @@ boolean HSendPacket(INT32 node, boolean reliable, UINT8 acknum, size_t packetlen
|
||||
netbuffer->ackreturn = 0;
|
||||
if (reliable)
|
||||
{
|
||||
if (!GetFreeAcknum(&netbuffer->ack))
|
||||
if (!GetFreeAcknum(&netbuffer->ack, false))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
@@ -1029,7 +1208,10 @@ boolean HGetPacket(void)
|
||||
|
||||
// A packet with just ackreturn
|
||||
if (netbuffer->packettype == PT_NOTHING)
|
||||
{
|
||||
GotAcks();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
#endif // ifndef NONET
|
||||
|
||||
+2
-2
@@ -18,8 +18,6 @@
|
||||
#ifndef __D_NET__
|
||||
#define __D_NET__
|
||||
|
||||
#include "doomtype.h"
|
||||
|
||||
// Max computers in a game
|
||||
// 127 is probably as high as this can go, because
|
||||
// SINT8 is used for nodes sometimes >:(
|
||||
@@ -47,6 +45,7 @@ extern boolean nodeingame[MAXNETNODES]; // Set false as nodes leave game
|
||||
|
||||
extern boolean serverrunning;
|
||||
|
||||
INT32 Net_GetFreeAcks(boolean urgent);
|
||||
void Net_AckTicker(void);
|
||||
|
||||
// If reliable return true if packet sent, 0 else
|
||||
@@ -63,6 +62,7 @@ void Net_UnAcknowledgePacket(INT32 node);
|
||||
void Net_CloseConnection(INT32 node);
|
||||
void Net_ConnectionTimeout(INT32 node);
|
||||
void Net_AbortPacketType(UINT8 packettype);
|
||||
void Net_SendAcks(INT32 node);
|
||||
void Net_WaitAllAckReceived(UINT32 timeout);
|
||||
const char *Net_GetPacketName(UINT8 packettype);
|
||||
|
||||
|
||||
+1
-3
@@ -1052,9 +1052,7 @@ void Got_Filetxpak(void)
|
||||
// Send ack back quickly
|
||||
if (++filetime == 3)
|
||||
{
|
||||
// send a PT_NOTHING back to acknowledge the packet
|
||||
netbuffer->packettype = PT_NOTHING;
|
||||
HSendPacket(servernode, false, 0, 0);
|
||||
Net_SendAcks(servernode);
|
||||
filetime = 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user