summaryrefslogtreecommitdiff
path: root/src/uqm/supermelee/netplay/packetq.c
blob: ee8ec012b032f8eb47b2cb7e4dfece2c4db17d6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 *  Copyright 2006  Serge van den Boom <svdb@stack.nl>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define NETCONNECTION_INTERNAL
#include "netplay.h"
#include "netconnection.h"
#include "packetq.h"
#include "netsend.h"
#include "packetsenders.h"
#ifdef NETPLAY_DEBUG
#	include "libs/log.h"
#endif

#include <errno.h>
#include <stdlib.h>
#include <string.h>

static inline PacketQueueLink *
PacketQueueLink_alloc(void) {
	// XXX: perhaps keep a pool of links?
	return malloc(sizeof (PacketQueueLink));
}

static inline void
PacketQueueLink_delete(PacketQueueLink *link) {
	free(link);
}

// 'maxSize' should at least be 1
void
PacketQueue_init(PacketQueue *queue) {
	queue->size = 0;
	queue->first = NULL;
	queue->end = &queue->first;
}

static void
PacketQueue_deleteLinks(PacketQueueLink *link) {
	while (link != NULL) {
		PacketQueueLink *next = link->next;
		Packet_delete(link->packet);
		PacketQueueLink_delete(link);
		link = next;
	}
}

void
PacketQueue_uninit(PacketQueue *queue) {
	PacketQueue_deleteLinks(queue->first);
}

void
queuePacket(NetConnection *conn, Packet *packet) {
	PacketQueue *queue;
	PacketQueueLink *link;

	assert(NetConnection_isConnected(conn));
	
	queue = &conn->queue;

	link = PacketQueueLink_alloc();
	link->packet = packet;
	link->next = NULL;
	*queue->end = link;
	queue->end = &link->next;

	queue->size++;
	// XXX: perhaps check that this queue isn't getting too large?

#ifdef NETPLAY_DEBUG
	if (packetType(packet) != PACKET_BATTLEINPUT &&
			packetType(packet) != PACKET_CHECKSUM) {
		// Reporting BattleInput or Checksum would get so spammy that it
		// would slow down the battle.
		log_add(log_Debug, "NETPLAY: [%d] ==> Queueing packet of type %s.\n",
				NetConnection_getPlayerNr(conn),
				packetTypeData[packetType(packet)].name);
	}
#ifdef NETPLAY_DEBUG_FILE
	if (conn->debugFile != NULL) {
		uio_fprintf(conn->debugFile,
				"NETPLAY: [%d] ==> Queueing packet of type %s.\n",
				NetConnection_getPlayerNr(conn),
				packetTypeData[packetType(packet)].name);
	}
#endif  /* NETPLAY_DEBUG_FILE */
#endif  /* NETPLAY_DEBUG */
}

// If an error occurs during sending, we leave the unsent packets in
// the queue, and let the caller decide what to do with them.
// This function may return -1 with errno EAGAIN or EWOULDBLOCK
// if we're waiting for the other party to act first.
static int
flushPacketQueueLinks(NetConnection *conn, PacketQueueLink **first) {
	PacketQueueLink *link;
	PacketQueueLink *next;
	PacketQueue *queue = &conn->queue;
	
	for (link = *first; link != NULL; link = next) {
		if (sendPacket(conn, link->packet) == -1) {
			// Errno is set.
			*first = link;
			return -1;
		}
		
		next = link->next;
		Packet_delete(link->packet);
		PacketQueueLink_delete(link);
		queue->size--;
	}

	*first = link;
	return 0;
}

int
flushPacketQueue(NetConnection *conn) {
	int flushResult;
	PacketQueue *queue = &conn->queue;
	
	assert(NetConnection_isConnected(conn));

	flushResult = flushPacketQueueLinks(conn, &queue->first);
	if (queue->first == NULL)
		queue->end = &queue->first;
	if (flushResult == -1) {
		// errno is set
		return -1;
	}
	
	return 0;
}