File: XnSocketConnectionFactory.cpp

package info (click to toggle)
openni2 2.2.0.33%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,216 kB
  • sloc: cpp: 111,197; ansic: 35,511; sh: 10,542; python: 1,313; java: 952; makefile: 575; xml: 12
file content (460 lines) | stat: -rw-r--r-- 13,136 bytes parent folder | download | duplicates (4)
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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "XnSocketConnectionFactory.h"
#include "XnSyncSocketConnection.h"
#include "XnSocketInConnection.h"
#include "XnServerSocketInConnection.h"
#include "XnClientSocketInConnection.h"
#include <XnMacros.h>
#include <XnStatus.h>
#include <XnLog.h>

#define XN_MASK_SOCKETS "xnSockets"

namespace xn
{

#define PRIME_CLIENT_INSTALL_PATH_ENV "PRIME_CLIENT_INSTALL_PATH"
#define PRIME_CLIENT_CONFIG_FILE "PrimeClient.ini"



const XnUInt16 SocketConnectionFactory::CONTROL_MAX_PACKET_SIZE = 65535;
const XnUInt16 SocketConnectionFactory::NUM_SERVER_TO_CLIENT_DATA_CONNECTIONS = 1;
const XnUInt16 SocketConnectionFactory::DATA_OUT_MAX_PACKET_SIZE = 65535;
const XnUInt16 SocketConnectionFactory::DATA_IN_MAX_PACKET_SIZE = 65535;
xnl::Array<SocketConnectionFactory::ConnectionStringStruct> SocketConnectionFactory::s_enumerationTargets;

xnl::Array<SyncSocketConnection> SocketConnectionFactory::s_controlConnections;

SocketConnectionFactory::SocketConnectionFactory(Type type)
{
	xnOSMemSet(m_strIP, 0, sizeof(m_strIP));
	m_nControlPort = 0;
	m_nDataOutPort = 0;
	m_nDataInBasePort = 0;
	m_bInitialized = FALSE;
	m_type = type;
}

SocketConnectionFactory::~SocketConnectionFactory()
{
	Shutdown();
}

XnStatus SocketConnectionFactory::Init(const XnChar* strConnString)
{
	XnStatus nRetVal = XN_STATUS_OK;
	
	nRetVal = xnOSInitNetwork();
	XN_IS_STATUS_OK_LOG_ERROR("Init network", nRetVal);
	nRetVal = ParseConnectionString(strConnString, m_strIP, sizeof(m_strIP), m_nControlPort);
	XN_IS_STATUS_OK_LOG_ERROR("Parse connection string", nRetVal);

	/* The ports are always arranged in the following order:
	  - Control Port
	  - Client to server data port
	  - Server to client data port number 1
	  - Server to client data port number 2
	     .
	     .
		 .
	  - Server to client data port number NUM_SERVER_TO_CLIENT_DATA_CONNECTIONS
	*/
	if (m_type == TYPE_SERVER)
	{
		m_nDataInBasePort = (m_nControlPort + 1);
		m_nDataOutPort = (m_nDataInBasePort + 1);

		// Initialize listener
		nRetVal = m_serverListener.Init(m_strIP, 
										m_nControlPort, 
										m_nControlPort + 1,
										m_nControlPort + 2,
										NUM_SERVER_TO_CLIENT_DATA_CONNECTIONS,
										CONTROL_MAX_PACKET_SIZE,
										DATA_OUT_MAX_PACKET_SIZE,
										DATA_IN_MAX_PACKET_SIZE);

		if (nRetVal != XN_STATUS_OK)
		{
			return nRetVal;
		}
	}
	else
	{
		m_nDataOutPort = (m_nControlPort + 1);

		//0x81 is first input port in USB - we want first input port in sockets to be 20002
		m_nDataInBasePort = m_nDataOutPort + 1;
	}

	m_bInitialized = TRUE;
	return XN_STATUS_OK;
}

void SocketConnectionFactory::Shutdown()
{
	m_bInitialized = FALSE;

	if (m_type == TYPE_SERVER)
	{
		m_serverListener.Shutdown();
	}
}

XnBool SocketConnectionFactory::IsInitialized() const
{
	return m_bInitialized;
}

XnUInt16 SocketConnectionFactory::GetNumInputDataConnections() const
{
	if (m_type == TYPE_SERVER) 
	{
		return 1;
	}
	else
	{
		return NUM_SERVER_TO_CLIENT_DATA_CONNECTIONS;
	}
}

XnUInt16 SocketConnectionFactory::GetNumOutputDataConnections() const
{
	if (m_type == TYPE_SERVER) 
	{
		return NUM_SERVER_TO_CLIENT_DATA_CONNECTIONS;
	}
	else
	{
		return 1;
	}
}


XnStatus SocketConnectionFactory::GetControlConnection(ISyncIOConnection*& pConn)
{
	SyncSocketConnection* pSyncSocketConn = NULL;
	XnStatus nRetVal = XN_STATUS_OK;

	if (m_type == TYPE_CLIENT)
	{
		nRetVal = GetControlConnectionImpl(m_strIP, m_nControlPort, pSyncSocketConn);
		XN_IS_STATUS_OK_LOG_ERROR("Get client control connection", nRetVal);
	}
	else
	{
		return m_serverListener.GetControlConnection(pConn);
	}
	
	pConn = pSyncSocketConn;
	return XN_STATUS_OK;
}

XnStatus SocketConnectionFactory::CreateOutputDataConnection(XnUInt16 nID, IOutputConnection*& pConn)
{
	if (!m_bInitialized)
	{
		return XN_STATUS_NOT_INIT;
	}
	SyncSocketConnection* pSyncSocketConnection = NULL;
	if (m_type == TYPE_SERVER)
	{
		// We do not create a new connection, we accept a connection from the listener. 
		return m_serverListener.CreateOutputDataConnection(nID, pConn);
	}
	else
	{
		//TODO: Change SyncSocketConnection to ClientSyncSocketConnection
		pSyncSocketConnection = XN_NEW(SyncSocketConnection);
	}

	XN_VALIDATE_ALLOC_PTR(pSyncSocketConnection);
	XnStatus nRetVal = pSyncSocketConnection->Init(m_strIP, m_nDataOutPort, DATA_OUT_MAX_PACKET_SIZE);
	if (nRetVal != XN_STATUS_OK)
	{
		xnLogError(XN_MASK_SOCKETS, "Initialize output data socket for ip '%s', port %u: %s", 
			m_strIP, m_nDataOutPort, xnGetStatusString(nRetVal));
		XN_ASSERT(FALSE);
		XN_DELETE(pSyncSocketConnection);
		return nRetVal;
	}

	pConn = pSyncSocketConnection;
	return XN_STATUS_OK;
}

XnStatus SocketConnectionFactory::CreateInputDataConnection(XnUInt16 nID, IAsyncInputConnection*& pConn)
{
	XnStatus nRetVal = XN_STATUS_OK;

	if (!m_bInitialized)
	{
		return XN_STATUS_NOT_INIT;
	}
	SocketInConnection* pInSocketConn = NULL;
	if (m_type == TYPE_SERVER)
	{
		// We do not create a new connection, we accept a connection from the listener. 
		// Only one input data connection in server, so nID is not used
		return m_serverListener.CreateInputDataConnection(pConn);
	}
	else
	{
		pInSocketConn = XN_NEW(ClientSocketInConnection);
	}
	XN_VALIDATE_ALLOC_PTR(pInSocketConn);

	nRetVal = pInSocketConn->Init(m_strIP, m_nDataInBasePort + nID, DATA_IN_MAX_PACKET_SIZE);
	if (nRetVal != XN_STATUS_OK)
	{
		xnLogError(XN_MASK_SOCKETS, "Initialize input data socket for ip '%s', port %u: %s", 
			m_strIP, m_nDataInBasePort + nID, xnGetStatusString(nRetVal));
		XN_ASSERT(FALSE);
		XN_DELETE(pInSocketConn);
		return nRetVal;
	}

	pConn = pInSocketConn;
	return XN_STATUS_OK;
}

XnStatus SocketConnectionFactory::AddEnumerationTarget(const XnChar* strConnString)
{
	XnStatus nRetVal = XN_STATUS_OK;
	nRetVal = s_enumerationTargets.SetSize(s_enumerationTargets.GetSize()+1);
	XN_IS_STATUS_OK_LOG_ERROR("Add to enumeration targets", nRetVal);
	nRetVal = xnOSStrCopy(s_enumerationTargets[s_enumerationTargets.GetSize()-1].m_strConn, 
		strConnString, sizeof(XnConnectionString));
	XN_IS_STATUS_OK_LOG_ERROR("Copy connection string", nRetVal);
	return XN_STATUS_OK;
}

XnStatus SocketConnectionFactory::TryAndAddEnumerationTarget(xnl::Array<ConnectionStringStruct>& result, const XnChar* strConnString)
{
	XnStatus nRetVal = XN_STATUS_OK;
	SyncSocketConnection* pConnection = NULL;
	XnChar strIP[XN_FILE_MAX_PATH];
	XnUInt16 nPort = 0;
	nRetVal = ParseConnectionString(strConnString, strIP, sizeof(strIP), nPort);
	XN_IS_STATUS_OK_LOG_ERROR("Parse connection string", nRetVal);

	nRetVal = GetControlConnectionImpl(strIP, nPort, pConnection);
	XN_IS_STATUS_OK_LOG_ERROR("Get control connection", nRetVal);

	nRetVal = pConnection->Connect();
	if (nRetVal == XN_STATUS_OK)
	{
		ConnectionStringStruct connString;
		EncodeConnectionString(connString.m_strConn, sizeof(connString.m_strConn), strIP, nPort);
		
		// make sure we have space
		nRetVal = result.AddLast(connString);
		XN_IS_STATUS_OK(nRetVal);
	}
	else
	{
		xnLogInfo(XN_MASK_SOCKETS, "Couldn't connect to %s:%u - '%s'", strIP, nPort, xnGetStatusString(nRetVal));
	}

	return (XN_STATUS_OK);
}

XnStatus SocketConnectionFactory::AddConfigFileTarget(xnl::Array<ConnectionStringStruct>& result, XnUInt16 nProductID)
{
	XnStatus nRetVal = XN_STATUS_OK;
	
	XnChar strConfigFile[XN_FILE_MAX_PATH];
	nRetVal = xnOSGetEnvironmentVariable(PRIME_CLIENT_INSTALL_PATH_ENV, strConfigFile, XN_FILE_MAX_PATH);
	if (nRetVal == XN_STATUS_OK)
	{
		nRetVal = xnOSStrAppend(strConfigFile, "/Config/", XN_FILE_MAX_PATH);
		XN_IS_STATUS_OK(nRetVal);
	}
	else if (nRetVal == XN_STATUS_OS_ENV_VAR_NOT_FOUND)
	{
		nRetVal = xnOSStrCopy(strConfigFile, "./", sizeof(strConfigFile));
		XN_IS_STATUS_OK(nRetVal);
	}
	else
	{
		return (nRetVal);
	}

	nRetVal = xnOSStrAppend(strConfigFile, PRIME_CLIENT_CONFIG_FILE, XN_FILE_MAX_PATH);
	XN_IS_STATUS_OK(nRetVal);

	XnBool bDoesConfigExist = FALSE;
	nRetVal = xnOSDoesFileExist(strConfigFile, &bDoesConfigExist);
	XN_IS_STATUS_OK(nRetVal);

	if (!bDoesConfigExist)
	{
		// no file
		return (XN_STATUS_OK);
	}

	// file exist. read IP address and port from it
	XnChar strProductID[80];
	sprintf(strProductID, "%04X", nProductID);

	XnChar strIP[XN_FILE_MAX_PATH];
	nRetVal = xnOSReadStringFromINI(strConfigFile, strProductID, "IPAddress", strIP, sizeof(strIP));
	if (nRetVal != XN_STATUS_OK)
	{
		// not found. nothing to connect to
		return (XN_STATUS_OK);
	}

	XnInt32 nPort = 0;
	nRetVal = xnOSReadIntFromINI(strConfigFile, strProductID, "Port", &nPort);
	if (nRetVal != XN_STATUS_OK)
	{
		// not found. nothing to connect to
		return (XN_STATUS_OK);
	}

	XnConnectionString strConnString;
	nRetVal = EncodeConnectionString(strConnString, sizeof(strConnString), strIP, XnUInt16(nPort));
	XN_IS_STATUS_OK_LOG_ERROR("Encode connection string", nRetVal);
	nRetVal = TryAndAddEnumerationTarget(result, strConnString);
	XN_IS_STATUS_OK(nRetVal);
	
	return (XN_STATUS_OK);
}

XnStatus SocketConnectionFactory::EnumerateConnStrings(XnUInt16 nProductID, 
													          XnConnectionString*& astrConnStrings, 
													          XnUInt32& nCount)
{
	XnStatus nRetVal = XN_STATUS_OK;

	astrConnStrings = NULL;
	nCount = 0;

	nRetVal = xnOSInitNetwork();
	XN_IS_STATUS_OK_LOG_ERROR("Init network", nRetVal);
	
	// Enumeration targets can be received from 2 sources:
	// 1. via the AddEnumerationTarget() method.
	// 2. from config file.
	xnl::Array<ConnectionStringStruct> result;

	for (XnUInt32 i = 0; i < s_enumerationTargets.GetSize(); ++i)
	{
		nRetVal = TryAndAddEnumerationTarget(result, s_enumerationTargets[i].m_strConn);
		XN_IS_STATUS_OK(nRetVal);
	}

	// add from config file
	nRetVal = AddConfigFileTarget(result, nProductID);
	XN_IS_STATUS_OK(nRetVal);

	astrConnStrings = (XnConnectionString*)xnOSCalloc(result.GetSize(), sizeof(astrConnStrings[0]));
	XN_VALIDATE_ALLOC_PTR(astrConnStrings);

	for (XnUInt32 i = 0; i < result.GetSize(); ++i)
	{
		nRetVal = xnOSStrCopy(astrConnStrings[i], result[i].m_strConn, sizeof(astrConnStrings[i]));
		XN_IS_STATUS_OK(nRetVal);
	}

	nCount = result.GetSize();

	return XN_STATUS_OK;
}

void SocketConnectionFactory::FreeConnStringsList(XnConnectionString* astrConnStrings)
{
	xnOSFree(astrConnStrings);
}

XnStatus SocketConnectionFactory::GetControlConnectionImpl(const XnChar* strIP, 
														   XnUInt16 nPort, 
														   SyncSocketConnection*& pControlConnection)
{
	XnStatus nRetVal = XN_STATUS_OK;
	pControlConnection = NULL;
	
	//Try and find existing connection
	for (XnUInt32 i = 0; i < s_controlConnections.GetSize(); i++)
	{
		if ((xnOSStrCmp(s_controlConnections[i].GetIP(), strIP) == 0) && 
			(s_controlConnections[i].GetPort() == nPort))
		{
			pControlConnection = &s_controlConnections[i];
			break;
		}
	}

	if (pControlConnection == NULL)
	{
		//Control connection is not in array - add it
		nRetVal = s_controlConnections.SetSize(s_controlConnections.GetSize() + 1);
		XN_IS_STATUS_OK_LOG_ERROR("Add to control connections array", nRetVal);
		pControlConnection = &s_controlConnections[s_controlConnections.GetSize() - 1];
	}

	if (!pControlConnection->IsInitialized())
	{
		//Control connection it not initialized - initialize it
		/*TODO: Change XN_CONTROL_PREDEFINED_MAX_PACKET_SIZE to variable once we have service discovery that 
		  tells us the control endpoint packet size in LinkControlEndpoint. */
		nRetVal = pControlConnection->Init(strIP, nPort, CONTROL_MAX_PACKET_SIZE);
		if (nRetVal != XN_STATUS_OK)
		{
			xnLogError(XN_MASK_SOCKETS, "Failed to initialize control socket for ip '%s', port %u: %s", 
				strIP, nPort, xnGetStatusString(nRetVal));
			XN_ASSERT(FALSE);
			pControlConnection = NULL;
			return nRetVal;
		}
	}

	return XN_STATUS_OK;
}

XnStatus SocketConnectionFactory::ParseConnectionString(const XnChar* strConnString, 
														XnChar* strIP, 
														XnUInt32 nIPBufSize, 
														XnUInt16& nPort)
{	
	XnStatus nRetVal = XN_STATUS_OK;
	const XnChar* pColon = NULL;
	const XnChar* strPort = NULL;
	XnUInt32 nTempPort = 0;

	pColon = strchr(strConnString, ':');
	if (pColon == NULL)
	{
		xnLogError(XN_MASK_SOCKETS, "Invalid connection string - missing ':'.");
		XN_ASSERT(FALSE);
		return XN_STATUS_BAD_PARAM;
	}

	//Take IP address from beginning of connection string
	nRetVal = xnOSStrNCopy(strIP, strConnString, XnUInt32(pColon - strConnString), nIPBufSize);
	XN_IS_STATUS_OK_LOG_ERROR("Copy IP address", nRetVal);
	strIP[pColon - strConnString] = '\0';
	//Take port number from chars after colon
	strPort = (pColon + 1);
	nTempPort = atoi(strPort);
	if ((nTempPort == 0) || (nTempPort > XN_MAX_UINT16))
	{
		xnLogError(XN_MASK_SOCKETS, "Invalid connection string - bad port number %u", nTempPort);
		XN_ASSERT(FALSE);
		return XN_STATUS_BAD_PARAM;
	}
	nPort = XnUInt16(nTempPort);
	return XN_STATUS_OK;
}

XnStatus SocketConnectionFactory::EncodeConnectionString(XnChar* strConnString, XnUInt32 nBufferSize, const XnChar* strIP, XnUInt16 nPort)
{
	XnStatus nRetVal = XN_STATUS_OK;
	XnUInt32 nCharsWritten = 0;
	nRetVal = xnOSStrFormat(strConnString, nBufferSize, &nCharsWritten, "%s:%u", strIP, nPort);
	XN_IS_STATUS_OK_LOG_ERROR("Format connection string", nRetVal);
	return XN_STATUS_OK;
}

}