udp_input.cpp 6.29 KB
Newer Older
jcaceres's avatar
jcaceres committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "udp_input.h"
#include "networkInfo.h"
#include "stream.h"
#include "unistd.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
extern QString *IPv4Addr (char *namebuf);
extern int set_fifo_priority (bool half);

UDPInput::UDPInput (NetworkInfoT netInfo, AudioInfoT audInfo):
InputPlugin ("UDP Input"), netInfo (netInfo), audInfo (audInfo)
{
	bpp = netInfo->getDataBytesPerPacket ();
	cout << "bpp = " << bpp << endl;
	char localhostbuf[100];
	has_peer = false;
	_rcvr = NULL;
19
20
	int dummy; dummy = 1;
	sock = new Q3SocketDevice (Q3SocketDevice::Datagram, Q3SocketDevice::IPv6, dummy);	// for an unreliable UDP socket
jcaceres's avatar
jcaceres committed
21
	sock->setAddressReusable(true);
22
23
24

	//**********IPv4*******************************************
	/*
jcaceres's avatar
jcaceres committed
25
26
27
28
29
30
31
32
33
	if (gethostname (localhostbuf, 99))
	{
		perror ("gethostname");
		exit ();
	}
	cout << "Rx buff = " << sock->receiveBufferSize () << endl;
	QHostAddress *ha = new QHostAddress ();
	QString *s = IPv4Addr (localhostbuf);	// dotted integer from name
	ha->setAddress (*s);
34
35
36
37
38
39
40
41
	*/
	//**********************************************************
	
	QHostAddress *ha = new QHostAddress ();
	//#############################################################
	ha->setAddress (netInfo->getLocalIP());//*****IPv6*************
	//#############################################################

jcaceres's avatar
jcaceres committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
	if (!(sock->bind (*ha, netInfo->getInPort ())))
	{
		perror ("bind\n");
		exit ();
	}
	if (!sock->isValid ())
	{
		cout << "socket creation error " << endl;
	}

	packetIndex = 0;
	wholeSize = sizeof (nsHeader) + (netInfo->getChunksPerPacket () * bpp) + 1;
	packetData = new char[wholeSize];
	memset (packetData, 0, wholeSize);
	numRedundantBuffers = netInfo->getChunksPerPacket() - 1;
	maxPacketIndex = netInfo->getMaxSeq();
58
59
	//**********IPv4*******************************************
	/*
jcaceres's avatar
jcaceres committed
60
61
	cout << endl << "UDPInput binding to " << localhostbuf
		<< " port " << netInfo->getInPort () << endl;
62
63
64
	*/
	//**********************************************************
	//#############################################################
jcaceres's avatar
jcaceres committed
65
66
67
	//cout << "IPv6 Local Address: " << ha->toString().latin1() << endl;//*****IPv6*************
	cout << endl << "UDPInput binding to " << ha->toString().latin1()
	     << " port " << netInfo->getInPort () << endl;
68
69
	//#############################################################

jcaceres's avatar
jcaceres committed
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
}

/** cout that the UDPInput thread has started */

void
UDPInput::Initial ()
{
	cout << "Started UDPInput thread" << endl;
}

/**
 * Receive a buffer from the UDPSocket.
 * @param buf is the location at which to store the buffer.
 * @param le is the size of the buffer to be received.
 */

int
UDPInput::rcv (char *buf)
{
		int	rv = sock->readBlock (packetData, wholeSize);

		char *datapart;
		packetIndex = ((nsHeader *) packetData)->i_seq;
		datapart = packetData + sizeof (nsHeader) + 
		  ((packetIndex % ((nsHeader *) packetData)->i_copies) * bpp);
		memcpy (buf, datapart, bpp);
/*
		((nsHeader *) packetData)->i_type = 0;
		((nsHeader *) packetData)->i_nframes = 1;
		((nsHeader *) packetData)->i_nchans = 2;
		((nsHeader *) packetData)->i_copies = n;
		((nsHeader *) packetData)->i_cksum = 4;
		((nsHeader *) packetData)->i_seq = packetIndex;
		((nsHeader *) packetData)->i_rtnseq = 6;
		((nsHeader *) packetData)->i_rtt = 7;
*/
			if (rv < 0)
			{
				cerr << "bad read..." << endl;
			}
	return packetIndex;
}

int
UDPInput::rcvz1 (char *bufz1, int z)
{
		char *datapart;
		packetIndex = ((nsHeader *) packetData)->i_seq-z;
	if (packetIndex < 0) 
	{
		packetIndex += maxPacketIndex;
//		cout << "backed below 0 (a good thing)" << endl;
	}
		datapart = packetData + sizeof (nsHeader) + 
		  ((packetIndex % ((nsHeader *) packetData)->i_copies) * bpp);
		memcpy (bufz1, datapart, bpp);
	return packetIndex;
}

/**
 * The main run loop.  Calls waitForConnection().  Once connected,
 * while running is true, it checks if the socket has a new buffer.
 * If it does, it writes the buffer to the stream.
 */

void
UDPInput::stop ()
{
	_running = false;
}

void
UDPInput::run ()
{
	_running = true;
	int seq;
	char *buf = (char *) new char[bpp];
	char *bufz1 = (char *) new char[bpp];

	cout << "UDP Input waiting for peer." << endl;
	while (has_peer == false)
	{
		if (sock->bytesAvailable () >= wholeSize)	// not an error
		{
			cout <<"wholeSize = " << wholeSize << " " <<sock->bytesAvailable ()<< endl;			
			//sock->readBlock (buf, wholeSize);
			this->rcv (buf);
			has_peer = true;	// really rcvd something
			cout << "UDP Input Connected!" << endl;			
		} else
		msleep (100);
	}
		msleep (10);
	cout << "Started UDP Input Run" << endl;
		set_fifo_priority (false);
	bool timeout;
	int ret;
	unsigned long now = 0;
	unsigned long lastTickTime = usecTime ();
	int ctr = 0;
	double max = 0.0;	
	int gap;
	double gapAvg = 0.0;
	while (_running)
	{
// If timeout is non-null and no error occurred 
// (i.e. it does not return -1): this function sets *timeout to TRUE, 
// if the reason for returning was that the timeout was reached; 
// otherwise it sets *timeout to FALSE. This is useful to find out 
// if the peer closed the connection.
		ret = (sock->waitForMore (30, &timeout));
		if (ret == -1)
			cerr << "udp in sock problems..." << endl;
		else if (timeout)
			cerr << "udp in waited too long..." << endl;
		else 
		{
			seq = this->rcv (buf);
			if (stream == NULL)
			{
				cerr << "ERROR: UDPInput has no sream to write to!" << endl;
			}
			
			int z = numRedundantBuffers;
			while (z)
			{
			int zseq = this->rcvz1 (bufz1, z);
			stream->writeRedundant (bufz1, key, z, zseq);
				z--;
			}
			gap = stream->writeRedundant (buf, key, 0, seq);
//		cout << "writePosition " << gap <<"\t\t";
			/*
		now = usecTime ();
		ctr++;
		if (ctr == 40)
		{
			ctr = 0;
			gapAvg = gapAvg / 40.0;
//		plotVal (gapAvg);
//		plotVal (max);
			gapAvg = 0.0;
			max = 0.0;
		} else {
		double xxx = (((double)now - (double)lastTickTime)/1000000.0);
//			if (xxx>max) max = xxx;
			max += xxx;
			gapAvg += ((double)gap);
		}
		lastTickTime = now;
			*/
//			stream->write (buf, key);
		}
	}
	cout << "UDP Input stop" << endl;
}

void UDPInput::plotVal (double v)
{
	if(_rcvr!=NULL)
	{
ThreadCommEvent *e = new ThreadCommEvent (-1.0,
								  v,
								  0.0);
			QApplication::postEvent (_rcvr, e);	// to app event loop
	}
}
	
bool UDPInput::hasPeer ()
{
	return has_peer;
}


/** Returns the address of the peer to which the socket is connected. */

QHostAddress UDPInput::peer ()
{
	return sock->peerAddress ();
}