VANetNetworkProtocol.cpp 119 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
 #include "VANetNetworkProtocol.h"

#include "VANetServerImpl.h"
#include "VANetClientImpl.h"
#include "VANetMessage.h"
#include "VANetVistaCompatibility.h"

#include <VACoreEvent.h>
#include <VACoreVersion.h>
#include <VAException.h>
#include <VAStruct.h>

#include <VistaBase/VistaExceptionBase.h>
#include <VistaInterProcComm/Connections/VistaByteBufferSerializer.h>
#include <VistaInterProcComm/Connections/VistaByteBufferDeSerializer.h>
#include <VistaInterProcComm/Connections/VistaConnectionIP.h>

#include <assert.h>
#include <sstream>


//////////////////////////////////////////////////
//////////       Main Implementation     /////////
//////////////////////////////////////////////////

CVANetNetworkProtocol::CVANetNetworkProtocol()
: m_pRealCore( NULL )
, m_pServer( NULL )
, m_pClient( NULL )
, m_pLastAnswerConnection( NULL )
, m_pCommandChannel( NULL )
, m_pHeadChannel( NULL )
, m_oIncomingEvent()
, m_vecEventBuffer( 128 )
, m_iReceiveTimeout( 0 )
, m_iExceptionHandlingMode( IVANetClient::EXC_CLIENT_THROW )
, m_bBufferingActive( false )
, m_bBufferSynchronisedModification( false )
, m_pMessage( NULL )
, m_pBufferedMessage( NULL )
, m_nCurrentMessageMode( MESSAGE_WITH_ANSWER )
, m_pEventMessage( NULL )
{
}
CVANetNetworkProtocol::~CVANetNetworkProtocol()
{
	if( m_pLastAnswerConnection )
		m_pLastAnswerConnection->WaitForSendFinish();

	delete m_pMessage;
	delete m_pBufferedMessage;
}

bool CVANetNetworkProtocol::InitializeAsClient( CVANetClientImpl* pClient,
												VistaConnectionIP* pCommandChannel,
												VistaConnectionIP* pHeadChannel,
												int iExceptionhandlingmode,
												bool bBufferSynchronisedModification )
{
	m_iExceptionHandlingMode = iExceptionhandlingmode;
	m_bBufferSynchronisedModification = bBufferSynchronisedModification;
	m_pClient = pClient;
	m_pCommandChannel = pCommandChannel;
	m_pHeadChannel = pHeadChannel;
	
	m_pMessage = new CVANetMessage( m_pCommandChannel->GetByteorderSwapFlag() );
	m_pBufferedMessage = new CVANetMessage( m_pCommandChannel->GetByteorderSwapFlag() );
	m_pEventMessage = new CVANetMessage( m_pCommandChannel->GetByteorderSwapFlag() );
	
	SetExceptionHandlingMode( m_iExceptionHandlingMode );
	return true;
}

bool CVANetNetworkProtocol::InitializeAsServer( CVANetServerImpl* pServer )
{
	m_pServer = pServer;

	m_pMessage = new CVANetMessage( VistaSerializingToolset::DOES_NOT_SWAP_MULTIBYTE_VALUES );
	m_pEventMessage = new CVANetMessage( VistaSerializingToolset::DOES_NOT_SWAP_MULTIBYTE_VALUES );
	return true;
}

void CVANetNetworkProtocol::SetExceptionHandlingMode( int nMode )
{
	m_iExceptionHandlingMode = nMode;
}	
int CVANetNetworkProtocol::GetExceptionHandlingMode() const
{
	return m_iExceptionHandlingMode;
}


void CVANetNetworkProtocol::SetRealVACore( IVACore* pCore )
{
	m_pRealCore = pCore;
}

#define SERVER_FUNCTION_MAPPING(ID,FUNCTION) case (ID): { FUNCTION; break; }
//#define SERVER_FUNCTION_MAPPING(ID,FUNCTION) case (ID): { std::cout << "Processing " << #ID << std::endl; FUNCTION; break; }

void CVANetNetworkProtocol::ServerCallFunctionByMessageType( int nMessageType,
												VistaConnectionIP* pConnection )
{
	switch( nMessageType )
	{
		SERVER_FUNCTION_MAPPING( VA_NP_GET_VERSION_INFO, ServerGetVersionInfo() );
		
		// Base functions		
		SERVER_FUNCTION_MAPPING( VA_NP_GET_STATE, ServerGetState() );
		SERVER_FUNCTION_MAPPING( VA_NP_INITIALIZE, ServerInitialize() );
		SERVER_FUNCTION_MAPPING( VA_NP_FINALIZE, ServerFinalize() );
		SERVER_FUNCTION_MAPPING( VA_NP_RESET, ServerReset() );

		// Module interface
		SERVER_FUNCTION_MAPPING( VA_NP_ENUMERATE_MODULES, ServerEnumerateModules() );
		SERVER_FUNCTION_MAPPING( VA_NP_CALL_MODULE, ServerCallModule() );

		// Event handling
		SERVER_FUNCTION_MAPPING( VA_NP_ATTACH_COREEVENT_HANDLER, ServerAttachCoreEventHandler(pConnection) );
		SERVER_FUNCTION_MAPPING( VA_NP_DETACH_COREEVENT_HANDLER, ServerDetachCoreEventHandler(pConnection) );

		// Directivities
		SERVER_FUNCTION_MAPPING( VA_NP_LOAD_DIRECTIVITY, ServerLoadDirectivity() );
		SERVER_FUNCTION_MAPPING( VA_NP_FREE_DIRECTIVITY, ServerFreeDirectivity() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_DIRECTIVITY_INFO, ServerGetDirectivityInfo() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_DIRECTIVITY_INFOS, ServerGetDirectivityInfos() );

		// Head-related impulse responses (HRIRs)
		SERVER_FUNCTION_MAPPING( VA_NP_LOAD_HRIR_DATASET, ServerLoadHRIRDataset() );
		SERVER_FUNCTION_MAPPING( VA_NP_FREE_HRIR_DATASET, ServerFreeHRIRDataset() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_HRIR_INFO, ServerGetHRIRInfo() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_HRIR_INFOS, ServerGetHRIRInfos() );

		// Sounds
		SERVER_FUNCTION_MAPPING( VA_NP_LOAD_SOUND, ServerLoadSound() );
		SERVER_FUNCTION_MAPPING( VA_NP_FREE_SOUND, ServerFreeSound() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUND_INFO, ServerGetSoundInfo() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUND_INFOS, ServerGetSoundInfos() );
		SERVER_FUNCTION_MAPPING( VA_NP_PLAY_SOUND, ServerPlaySound() );

		// Signal sources
		SERVER_FUNCTION_MAPPING( VA_NP_CREATE_AUDIOFILE_SIGNAL_SOURCE, ServerCreateAudiofileSignalSource() );
		SERVER_FUNCTION_MAPPING( VA_NP_CREATE_SEQUENCER_SIGNALSOURCE, ServerCreateSequencerSignalSource() );
		SERVER_FUNCTION_MAPPING( VA_NP_CREATE_NETWORK_STREAM_SIGNALSOURCE, ServerCreateNetworkStreamSignalSource() );
		SERVER_FUNCTION_MAPPING( VA_NP_CREATE_ENGINE_SIGNALSOURCE, ServerCreateEngineSignalSource() );
		SERVER_FUNCTION_MAPPING( VA_NP_CREATE_MACHINE_SIGNALSOURCE, ServerCreateMachineSignalSource() );
		SERVER_FUNCTION_MAPPING( VA_NP_DELETE_SIGNALSOURCE, ServerDeleteSignalSource() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SIGNALSOURCE_INFO, ServerGetSignalSourceInfo() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SIGNALSOURCE_INFOS, ServerGetSignalSourceInfos() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_AUDIOFILE_SIGNALSOURCE_PLAYSTATE, ServerGetAudiofileSignalSourcePlaybackState() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_AUDIOFILE_SIGNALSOURCE_PLAYACTION, ServerSetAudiofileSignalSourcePlaybackAction() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_AUDIOFILE_SIGNALSOURCE_PLAYPOS, ServerSetAudiofileSignalSourcePlaybackPosition() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_AUDIOFILE_SIGNALSOURCE_LOOPING, ServerSetAudiofileSignalSourceIsLooping() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_AUDIOFILE_SIGNALSOURCE_LOOPING, ServerGetAudiofileSignalSourceIsLooping() );
		SERVER_FUNCTION_MAPPING( VA_NP_SEQUENCER_ADD_SOUND_PLAYBACK, ServerAddSoundPlayback() );
		SERVER_FUNCTION_MAPPING( VA_NP_SEQUENCER_REMOVE_SOUND_PLAYBACK, ServerRemoveSoundPlayback() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SIGNALSOURCE_PARAMETERS, ServerGetSignalSourceParameters() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SIGNALSOURCE_PARAMETERS, ServerSetSignalSourceParameters() );

		// Synchronization functions
		SERVER_FUNCTION_MAPPING( VA_NP_LOCK_SCENE, ServerLockScene() );
		SERVER_FUNCTION_MAPPING( VA_NP_UNLOCK_SCENE, ServerUnlockScene() );
		SERVER_FUNCTION_MAPPING( VA_NP_IS_SCENE_LOCKED, ServerIsSceneLocked() );
		
		// Sound sources
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUND_SOURCE_IDS, ServerGetSoundSourceIDs() );
		SERVER_FUNCTION_MAPPING( VA_NP_CREATE_SOUNDSOURCE, ServerCreateSoundSource() );
		SERVER_FUNCTION_MAPPING( VA_NP_CREATE_SOUNDSOURCE_EXPLICIT_RENDERER, ServerCreateSoundSourceExplicitRenderer() );
		SERVER_FUNCTION_MAPPING( VA_NP_DELETE_SOUNDSOURCE, ServerDeleteSoundSource() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_ENABLED, ServerSetSoundSourceEnabled() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_ENABLED, ServerGetSoundSourceEnabled() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_NAME, ServerSetSoundSourceName() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_NAME, ServerGetSoundSourceName() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_SIGNALSOURCE, ServerSetSoundSourceSignalSource() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_SIGNALSOURCE, ServerGetSoundSourceSignalSource() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_AURAMODE, ServerSetSoundSourceAuralizationMode() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_AURAMODE, ServerGetSoundSourceAuralizationMode() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_PARAMETERS, ServerSetSoundSourceParameters() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_PARAMETERS, ServerGetSoundSourceParameters() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_DIRECTIVITY, ServerSetSoundSourceDirectivity() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_DIRECTIVITY, ServerGetSoundSourceDirectivity() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_VOLUME, ServerSetSoundSourceVolume() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_VOLUME, ServerGetSoundSourceVolume() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_MUTED, ServerSetSoundSourceMuted() );
		SERVER_FUNCTION_MAPPING( VA_NP_IS_SOUNDSOURCE_MUTED, ServerIsSoundSourceMuted() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_POSITION, ServerSetSoundSourcePosition() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_POSITION, ServerGetSoundSourcePosition() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_ORIENTATION_YPR, ServerSetSoundSourceOrientationYPR() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_ORIENTATION_YPR, ServerGetSoundSourceOrientationYPR() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_ORIENTATION_VU, ServerSetSoundSourceOrientationVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_ORIENTATION_VU, ServerGetSoundSourceOrientationVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_POSITION_ORIENT_YPR, ServerSetSoundSourcePositionOrientationYPR() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_POSITION_ORIENT_YPR, ServerGetSoundSourcePositionOrientationYPR() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_POSITION_ORIENT_VU, ServerSetSoundSourcePositionOrientationVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_POSITION_ORIENT_VU, ServerGetSoundSourcePositionOrientationVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_POSITION_ORIENT_VELOCITY_VU, ServerSetSoundSourcePositionOrientationVelocityVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_POSITION_ORIENT_VELOCITY_VU, ServerGetSoundSourcePositionOrientationVelocityVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_SOUNDSOURCE_POSITION_ORIENT_VELOCITY_YPR, ServerSetSoundSourcePositionOrientationVelocityYPR() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SOUNDSOURCE_POSITION_ORIENT_VELOCITY_YPR, ServerGetSoundSourcePositionOrientationVelocityYPR() );

		// Listeners
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_IDS, ServerGetListenerIDs() );
		SERVER_FUNCTION_MAPPING( VA_NP_CREATE_LISTENER, ServerCreateListener() );
		SERVER_FUNCTION_MAPPING( VA_NP_CREATE_LISTENER_EXPLICIT_RENDERER, ServerCreateListenerExplicitRenderer() );
		SERVER_FUNCTION_MAPPING( VA_NP_DELETE_LISTENER, ServerDeleteListener() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_ENABLED, ServerSetListenerEnabled() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_ENABLED, ServerGetListenerEnabled() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_NAME, ServerGetListenerName() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_NAME, ServerSetListenerName() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_AURALIZATION_MODE, ServerGetListenerAuralizationMode() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_AURALIZATION_MODE, ServerSetListenerAuralizationMode() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_PARAMETERS, ServerSetListenerParameters() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_PARAMETERS, ServerGetListenerParameters() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_HRIR_DATASET, ServerGetListenerHRIRDataset() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_HRIR_DATASET, ServerSetListenerHRIRDataset() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_POSITION, ServerGetListenerPosition() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_POSITION, ServerSetListenerPosition() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_ORIENTATION_YPR, ServerGetListenerOrientationYPR() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_ORIENTATION_YPR, ServerSetListenerOrientationYPR() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_ORIENTATION_VU, ServerGetListenerOrientationVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_ORIENTATION_VU, ServerSetListenerOrientationVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_POSITION_ORIENT_YPR, ServerGetListenerPositionOrientationYPR() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_POSITION_ORIENT_YPR, ServerSetListenerPositionOrientationYPR() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_POSITION_ORIENT_VU, ServerGetListenerPositionOrientationVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_POSITION_ORIENT_VU, ServerSetListenerPositionOrientationVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_POSITION_ORIENT_VELOCITY_VU, ServerGetListenerPositionOrientationVelocityVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_POSITION_ORIENT_VELOCITY_VU, ServerSetListenerPositionOrientationVelocityVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_POSITION_ORIENT_VELOCITY_YPR, ServerGetListenerPositionOrientationVelocityYPR() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_POSITION_ORIENT_VELOCITY_YPR, ServerSetListenerPositionOrientationVelocityYPR() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_LISTENER_REAL_WORLD_POSITION_ORIENT_VU, ServerGetListenerRealWorldHeadPositionOrientationVU() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_LISTENER_REAL_WORLD_POSITION_ORIENT_VU, ServerSetListenerRealWorldHeadPositionOrientationVU() );

		// Scene
		SERVER_FUNCTION_MAPPING( VA_NP_LOAD_SCENE, ServerLoadScene() );
		SERVER_FUNCTION_MAPPING( VA_NP_IS_SCENE_LOADED, ServerIsSceneLoaded() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_SCENE_INFO, ServerGetSceneInfo() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_PORTAL_IDS, ServerGetPortalIDs() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_PORTAL_NAME, ServerGetPortalName() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_PORTAL_NAME, ServerSetPortalName() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_PORTAL_STATE, ServerGetPortalState() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_PORTAL_STATE, ServerSetPortalState() );

		// Global functions
		SERVER_FUNCTION_MAPPING( VA_NP_GET_INPUT_GAIN, ServerGetInputGain() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_INPUT_GAIN, ServerSetInputGain() );
		SERVER_FUNCTION_MAPPING( VA_NP_IS_INPUT_MUTED, ServerIsInputMuted() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_INPUT_MUTED, ServerSetInputMuted() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_OUTPUT_GAIN, ServerGetOutputGain() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_OUTPUT_GAIN, ServerSetOutputGain() );
		SERVER_FUNCTION_MAPPING( VA_NP_IS_OUTPUT_MUTED, ServerIsOutputMuted() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_OUTPUT_MUTED, ServerSetOutputMuted() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_GLOBAL_AURALIZATION_MODE, ServerGetGlobalAuralizationMode() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_GLOBAL_AURALIZATION_MODE, ServerSetGlobalAuralizationMode() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_ACTIVE_LISTENER, ServerGetActiveListener() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_ACTIVE_LISTENER, ServerSetActiveListener() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_ACTIVE_LISTENER_EXPLICIT_RENDERER, ServerGetActiveListenerExplicitRenderer() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_ACTIVE_LISTENER_EXPLICIT_RENDERER, ServerSetActiveListenerExplicitRenderer() );
		SERVER_FUNCTION_MAPPING( VA_NP_GET_CORE_CLOCK, ServerGetCoreClock() );
		SERVER_FUNCTION_MAPPING( VA_NP_SET_CORE_CLOCK, ServerSetCoreClock() );
		SERVER_FUNCTION_MAPPING( VA_NP_SUBSTITUTE_MACROS, ServerSubstituteMacros() );

		SERVER_FUNCTION_MAPPING( VA_NP_RENDERER_GET_INFOS, ServerGetRenderingModuleInfos() );
		SERVER_FUNCTION_MAPPING( VA_NP_RENDERER_GET_GAIN, ServerGetRenderingModuleGain() );
		SERVER_FUNCTION_MAPPING( VA_NP_RENDERER_SET_GAIN, ServerSetRenderingModuleGain() );
		SERVER_FUNCTION_MAPPING( VA_NP_RENDERER_IS_MUTED, ServerIsRenderingModuleMuted() );
		SERVER_FUNCTION_MAPPING( VA_NP_RENDERER_SET_MUTED, ServerSetRenderingModuleMuted() );

		SERVER_FUNCTION_MAPPING( VA_NP_REPRODUCTION_GET_INFOS, ServerGetReproductionModuleInfos() );
		SERVER_FUNCTION_MAPPING( VA_NP_REPRODUCTION_GET_GAIN, ServerGetReproductionModuleGain() );
		SERVER_FUNCTION_MAPPING( VA_NP_REPRODUCTION_SET_GAIN, ServerSetReproductionModuleGain() );
		SERVER_FUNCTION_MAPPING( VA_NP_REPRODUCTION_IS_MUTED, ServerIsReproductionModuleMuted() );
		SERVER_FUNCTION_MAPPING( VA_NP_REPRODUCTION_SET_MUTED, ServerSetReproductionModuleMuted() );

		case VA_NP_INVALID:
		default:
		{
			/** @todo: disconnect */
			std::ostringstream oStream;
			oStream << "VANetServer: Unkown Message Type " << nMessageType;
			VA_EXCEPT2( PROTOCOL_ERROR, oStream.str() );
		}
	}
}
 
bool CVANetNetworkProtocol::ProcessMessageFromClient( VistaConnectionIP* pConnection )
{
	assert( m_pRealCore != NULL );

	// just to avoid false connection notifies - these can happen
	if( pConnection->PendingDataSize() == 0 )
		return true;

	// Falls wir zuletzt eine Antwort gesendet haben, mssen wir nun warten,
	// bis das senden abgeschlossen ist, bevor wir die message leeren
	if( m_pLastAnswerConnection != NULL )
		m_pLastAnswerConnection->WaitForSendFinish();

	try
	{
		m_pMessage->ResetMessage();
		m_pMessage->SetConnection( pConnection );
		m_pMessage->ReadMessage();
	}	
	catch( VistaExceptionBase& oException )
	{
		std::cerr << "VANetNetworkProtocol: caught connection exception - " 
			<< oException.GetExceptionText() << std::endl;
		return false;
	}

	int nMessageType = m_pMessage->GetMessageType();

	// we assume an answer - if an exception occurs, or no answer is
	// sent, the value will be rewritten
	m_pMessage->SetAnswerType( VA_NP_SPECIAL_ANSWER );

	// Es kann eine gebufferete Nachricht kommen.
	// In dem fall mssen wir mehrfach Befehle parsen
	// dies tun wir, bis der MessageBuffer leer ist
	try
	{
		if( nMessageType == VA_NP_SPECIAL_BUFFERED_MESSAGE )
		{
			while( m_pMessage->GetIncomingMessageSize() > 0 )
			{
				nMessageType = m_pMessage->ReadInt();
				ServerCallFunctionByMessageType( nMessageType, pConnection );
			}
		}
		else if( nMessageType == VA_NP_SPECIAL_CLIENT_EVENT )
		{
			int nEventID = m_pMessage->ReadInt();
			m_pServer->HandleClientEvent( nEventID, pConnection );
			return true;
		}
		else
		{
			ServerCallFunctionByMessageType( nMessageType, pConnection );
		}
	}
	catch( CVAException& oException )
	{
		if( oException.GetErrorCode() == CVAException::PROTOCOL_ERROR )
			throw;

		switch( m_pMessage->GetExceptionMode() )
		{
			case IVANetClient::EXC_CLIENT_THROW:
			{
				m_pMessage->SetAnswerType( VA_NP_SPECIAL_EXCEPTION );
				m_pMessage->WriteException( oException );
				m_pMessage->WriteAnswer();
				return true;
			}
			case IVANetClient::EXC_SERVER_PRINT:
			{
				std::cerr << "[IVANetServer]: Caught and swallowed exception :\n"
						<< oException.ToString() << std::endl;
				return true;
			}
		}
	}

	if( m_pMessage->GetOutgoingMessageHasData() )
	{
		m_pMessage->WriteAnswer();
	}
	else if( m_pMessage->GetExceptionMode() == IVANetClient::EXC_CLIENT_THROW )
	{
		m_pMessage->SetAnswerType( VA_NP_SPECIAL_NO_EXCEPTION );
		m_pMessage->WriteAnswer();
	}

	return true;
}

bool CVANetNetworkProtocol::GetBufferSynchronisedModification() const
{
	return m_bBufferSynchronisedModification;
}

void CVANetNetworkProtocol::SetBufferSynchronisedModifications( const bool bSet )
{
	m_bBufferSynchronisedModification = bSet;
}

void CVANetNetworkProtocol::ClientPrepareMessageBuffering()
{
	if( m_bBufferSynchronisedModification == false )
		return; 

	// @todo: how to handle this? ignore, still buffer command, abort?
	if( m_bBufferingActive == true )
		return; // we are already buffering

	m_pBufferedMessage->ResetMessage();
	m_pBufferedMessage->SetConnection( m_pCommandChannel );
	m_pBufferedMessage->SetExceptionMode( IVANetClient::EXC_CLIENT_THROW );
	m_pBufferedMessage->SetMessageType( VA_NP_SPECIAL_BUFFERED_MESSAGE );

	m_bBufferingActive = true;
}

int CVANetNetworkProtocol::ClientEndMessageBuffering()
{
	assert( m_bBufferingActive );

	m_bBufferingActive = false;
	
	int iRet = -1;
	try
	{
		m_pBufferedMessage->SetExceptionMode( m_iExceptionHandlingMode );
		m_pBufferedMessage->WriteMessage();

		m_pBufferedMessage->ReadAnswer();
		iRet = m_pBufferedMessage->ReadInt();
	}
	catch( const VistaExceptionBase& oException )
	{
		std::cerr << "VANetNetworkProtocol: caught exception during ClientEndMessageBuffering - " 
			<< oException.GetExceptionText() << std::endl;
	}

	return iRet;
}

int CVANetNetworkProtocol::ClientProcessEventMessageFromServer( VistaConnectionIP* pConnection,
										                        std::list<IVACoreEventHandler*> liCoreEventHandler )
{
	VANetCompat::sint32 iSize = 0;
	VANetCompat::sint32 iType = VA_EVENT_FAIL;
	m_pEventMessage->SetConnection( pConnection );
	
	try
	{
		m_pEventMessage->ReadMessage();

		switch( m_pEventMessage->GetMessageType() )
		{
			case VA_EVENT_CORE_EVENT:
			{
				CVACoreEvent oEvent = m_pEventMessage->ReadCoreEvent();
				for( std::list<IVACoreEventHandler*>::iterator itHandler = liCoreEventHandler.begin();
						itHandler != liCoreEventHandler.end(); ++itHandler )
				{
					(*itHandler)->HandleVACoreEvent( &oEvent );
				}
				break;
			}
			case VA_EVENT_NET_EVENT:
			{
				m_pClient->ProcessNetEvent( m_pEventMessage->ReadInt() );
				break;
			}
			default:
			{
				VA_EXCEPT2( PROTOCOL_ERROR, std::string( "Unknown event message type" ) );
			}
		}
	}
	catch( VistaExceptionBase& oException )
	{
		VA_EXCEPT2( PROTOCOL_ERROR,
					std::string( "VANetNetworkProtocol: caught connection exception - " + 
								oException.GetExceptionText() ) );
	}	

	return m_pEventMessage->GetMessageType();
}


void CVANetNetworkProtocol::ServerPrepareEventMessage( const CVACoreEvent* pEvent, 
													CVANetMessage* pMessage )
{
	pMessage->ResetMessage();
	pMessage->SetMessageType( VA_EVENT_CORE_EVENT );
	pMessage->WriteCoreEvent( *pEvent );
}

void CVANetNetworkProtocol::HandleConnectionClose( VistaConnectionIP* pConnection )
{
	if( m_pLastAnswerConnection = pConnection )
		m_pLastAnswerConnection = NULL;
	if( m_pMessage->GetConnection() == pConnection )
		m_pMessage->ClearConnection();
}

CVANetMessage* CVANetNetworkProtocol::ServerGetMessage()
{
	return m_pMessage;
}
void CVANetNetworkProtocol::ServerSendAnswer( CVANetMessage* pMessage )
{
	// nothing to do
}

CVANetMessage* CVANetNetworkProtocol::ClientInitMessage( int nCommandId,															
											MESSAGE_MODE eMessageMode )
{
	m_nCurrentMessageMode = eMessageMode;
	if( eMessageMode == MESSAGE_ALLOWS_BUFFERING && m_bBufferingActive )
	{
		m_pBufferedMessage->WriteInt( nCommandId );
		return m_pBufferedMessage;
	}
	else
	{
		m_pMessage->ResetMessage();
		m_pMessage->SetConnection( m_pCommandChannel );
		m_pMessage->SetMessageType( nCommandId );		
		return m_pMessage;
	}
}
void CVANetNetworkProtocol::ClientSendCommand( CVANetMessage* pMessage )
{
	if( pMessage != m_pBufferedMessage )
	{
		int nExceptionMode;
		switch( m_nCurrentMessageMode )
		{
			case MESSAGE_WITH_ANSWER:
			case MESSAGE_ENFORCED_EXCEPTION:
			{
				nExceptionMode = IVANetClient::EXC_CLIENT_THROW;
				break;
			}
			case MESSAGE_CONDITIONAL_EXCEPTION:
			case MESSAGE_ALLOWS_BUFFERING:
			{
				nExceptionMode = m_iExceptionHandlingMode;
				break;
			}
		}
		
		try
		{
			pMessage->SetExceptionMode( nExceptionMode );
			pMessage->WriteMessage();

			if( m_nCurrentMessageMode == MESSAGE_WITH_ANSWER )
			{
				pMessage->ReadAnswer();
				if( pMessage->GetAnswerType() == VA_NP_SPECIAL_EXCEPTION )
				{
					throw( pMessage->ReadException() );
				}
				else
				{
					assert( pMessage->GetAnswerType() == VA_NP_SPECIAL_ANSWER );
				}
			}
			else if( nExceptionMode == IVANetClient::EXC_CLIENT_THROW )
			{
				pMessage->ReadAnswer();
				if( pMessage->GetAnswerType() == VA_NP_SPECIAL_EXCEPTION )
				{
					throw( pMessage->ReadException() );
				}
				else
				{
					assert( pMessage->GetAnswerType() == VA_NP_SPECIAL_NO_EXCEPTION );
				}				
			}
		}
		catch( const VistaExceptionBase& )
		{
			//@todo
		}
	}
}


/* +----------------------------------------------------------+ *
 * |                                                          | *
 * |   Base functions                                         | *
 * |                                                          | *
 * +----------------------------------------------------------+ */

void CVANetNetworkProtocol::ClientGetVersionInfo(CVACoreVersionInfo* pVersionInfo)
{	
	if (!pVersionInfo) return;
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_GET_VERSION_INFO,
											 MESSAGE_WITH_ANSWER );
	ClientSendCommand( pMsg );

	*pVersionInfo = pMsg->ReadCoreVersionInfo();
}

void CVANetNetworkProtocol::ServerGetVersionInfo()
{
	CVANetMessage* pMsg = ServerGetMessage();

	CVACoreVersionInfo oVersionInfo;
	m_pRealCore->GetVersionInfo(&oVersionInfo);

	pMsg->WriteCoreVersionInfo( oVersionInfo );
}

int CVANetNetworkProtocol::ClientGetState()
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_GET_STATE,
											 MESSAGE_WITH_ANSWER );
	ClientSendCommand( pMsg );

	return pMsg->ReadInt();
}

void CVANetNetworkProtocol::ServerGetState() 
{
	CVANetMessage* pMsg = ServerGetMessage();
	int iState = m_pRealCore->GetState();
	pMsg->WriteInt( iState );
}

void CVANetNetworkProtocol::ClientInitialize()
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_INITIALIZE,
											 MESSAGE_ENFORCED_EXCEPTION );
	ClientSendCommand( pMsg );
}

void CVANetNetworkProtocol::ServerInitialize()
{
	m_pRealCore->Initialize();
}

void CVANetNetworkProtocol::ClientFinalize()
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_FINALIZE,
											 MESSAGE_ENFORCED_EXCEPTION );
	ClientSendCommand( pMsg );
}

void CVANetNetworkProtocol::ServerFinalize()
{
	m_pRealCore->Finalize();
}

void CVANetNetworkProtocol::ClientReset()
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_RESET,
											 MESSAGE_ENFORCED_EXCEPTION );
	ClientSendCommand( pMsg );

}

void CVANetNetworkProtocol::ServerReset()
{
	m_pRealCore->Reset();
}


/* +----------------------------------------------------------+ *
 * |                                                          | *
 * |   Event handling                                         | *
 * |                                                          | *
 * +----------------------------------------------------------+ */

void CVANetNetworkProtocol::ClientAttachCoreEventHandler()
{	
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_ATTACH_COREEVENT_HANDLER,
										     MESSAGE_ENFORCED_EXCEPTION );
	ClientSendCommand( pMsg );
}

void CVANetNetworkProtocol::ServerAttachCoreEventHandler( VistaConnectionIP* pConn )
{
	m_pServer->AttachCoreEventHandler( pConn );
}

void CVANetNetworkProtocol::ClientDetachCoreEventHandler()
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_DETACH_COREEVENT_HANDLER,
											 MESSAGE_ENFORCED_EXCEPTION );
	ClientSendCommand( pMsg );
}

void CVANetNetworkProtocol::ServerDetachCoreEventHandler( VistaConnectionIP* pConn )
{
	m_pServer->DetachCoreEventHandler( pConn );
}


/* +----------------------------------------------------------+ *
 * |                                                          | *
 * |   Module interface                                       | *
 * |                                                          | *
 * +----------------------------------------------------------+ */

void CVANetNetworkProtocol::ClientEnumerateModules(std::vector<CVAModuleInfo>& m_viModuleInfos) {
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_ENUMERATE_MODULES, 
											 MESSAGE_WITH_ANSWER );
	ClientSendCommand( pMsg );

	int nModules = pMsg->ReadInt();

	m_viModuleInfos.clear();
	m_viModuleInfos.resize(nModules);

	for (int i=0; i<nModules; i++)
		pMsg->ReadModuleInfo( m_viModuleInfos[i] );
}

void CVANetNetworkProtocol::ServerEnumerateModules() {
	CVANetMessage* pMsg = ServerGetMessage();

	std::vector<CVAModuleInfo> viModules;
	m_pRealCore->EnumerateModules(viModules);
	
	pMsg->WriteInt( (int) viModules.size() );
	for (int i=0; i<(int) viModules.size(); i++)
		pMsg->WriteModuleInfo( viModules[i] );
}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
716
717
718
719
720
void CVANetNetworkProtocol::ClientCallModule( const std::string& sModuleName, const CVAStruct& oArgs, CVAStruct& oReturn )
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_CALL_MODULE, MESSAGE_WITH_ANSWER );
	pMsg->WriteString( sModuleName );
	pMsg->WriteVAStruct( oArgs );
Jonas Stienen's avatar
Jonas Stienen committed
721
722
723
	
	ClientSendCommand( pMsg );

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
724
	pMsg->ReadVAStruct( oReturn );
Jonas Stienen's avatar
Jonas Stienen committed
725
726
727
728
729
730
731
732
733
734
735
736
}

void CVANetNetworkProtocol::ServerCallModule()
{
	CVANetMessage* pMsg = ServerGetMessage();

	std::string sModuleName = pMsg->ReadString();
	CVAStruct oArgs, oReturn;
	pMsg->ReadVAStruct( oArgs );
	
	m_pRealCore->CallModule( sModuleName, oArgs, oReturn );

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
737
	pMsg->WriteVAStruct( oReturn );
Jonas Stienen's avatar
Jonas Stienen committed
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
}

/* +----------------------------------------------------------+ *
 * |                                                          | *
 * |   Directivities                                          | *
 * |                                                          | *
 * +----------------------------------------------------------+ */

int CVANetNetworkProtocol::ClientLoadDirectivity( const std::string& sFilename, const std::string& sName )
{	
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_LOAD_DIRECTIVITY, 
											 MESSAGE_WITH_ANSWER );
	pMsg->WriteString( sFilename );
	pMsg->WriteString( sName );

	ClientSendCommand( pMsg );

	return pMsg->ReadInt();
}

void CVANetNetworkProtocol::ServerLoadDirectivity()
{
	CVANetMessage* pMsg = ServerGetMessage();

	std::string sFilename = pMsg->ReadString();
	std::string sName = pMsg->ReadString();

	int iDirID = m_pRealCore->LoadDirectivity(sFilename, sName);

	pMsg->WriteInt( iDirID );
}

bool CVANetNetworkProtocol::ClientFreeDirectivity(int iDirID)
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_FREE_DIRECTIVITY,
											 MESSAGE_WITH_ANSWER );
	pMsg->WriteInt( iDirID );

	ClientSendCommand( pMsg );

	return pMsg->ReadBool();
}

void CVANetNetworkProtocol::ServerFreeDirectivity()
{
	CVANetMessage* pMsg = ServerGetMessage();

	int iDirID = pMsg->ReadInt();
	
	bool bRes = m_pRealCore->FreeDirectivity( iDirID );
	pMsg->WriteBool( bRes );
}

CVADirectivityInfo CVANetNetworkProtocol::ClientGetDirectivityInfo(int iDirID)
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_GET_DIRECTIVITY_INFO,
											 MESSAGE_WITH_ANSWER );
	pMsg->WriteInt( iDirID );

	ClientSendCommand( pMsg );

	CVADirectivityInfo oDirInfo = pMsg->ReadDirectivityInfo();
	return oDirInfo;
}

void CVANetNetworkProtocol::ServerGetDirectivityInfo()
{
	CVANetMessage* pMsg = ServerGetMessage();

	int iDirID = pMsg->ReadInt();
	
	CVADirectivityInfo oDirInfo = m_pRealCore->GetDirectivityInfo(iDirID);

	pMsg->WriteDirectivityInfo( oDirInfo );
}

void CVANetNetworkProtocol::ClientGetDirectivityInfos(std::vector<CVADirectivityInfo>& vdiDest)
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_GET_DIRECTIVITY_INFOS,
											 MESSAGE_WITH_ANSWER );

	ClientSendCommand( pMsg );

	vdiDest.clear();
	int iNumDirectivities = pMsg->ReadInt();
	for (int i=0; i<iNumDirectivities; i++)
		vdiDest.push_back( pMsg->ReadDirectivityInfo() );
}

void CVANetNetworkProtocol::ServerGetDirectivityInfos()
{
	CVANetMessage* pMsg = ServerGetMessage();

	std::vector<CVADirectivityInfo> vInfos;
	m_pRealCore->GetDirectivityInfos(vInfos);
	
	pMsg->WriteInt( (int) vInfos.size() );
	for (size_t i=0; i<vInfos.size(); i++)
		pMsg->WriteDirectivityInfo(vInfos[i]);
}


/* +----------------------------------------------------------+ *
 * |                                                          | *
 * |   Head-related impulse responses (HRIRs)                 | *
 * |                                                          | *
 * +----------------------------------------------------------+ */

int CVANetNetworkProtocol::ClientLoadHRIRDataset(const std::string& sFilename,
												 const std::string& sName) {
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_LOAD_HRIR_DATASET, 
											 MESSAGE_WITH_ANSWER );

	pMsg->WriteString(sFilename);
	pMsg->WriteString(sName);

	ClientSendCommand( pMsg );

	return pMsg->ReadInt();
}

void CVANetNetworkProtocol::ServerLoadHRIRDataset() {
	CVANetMessage* pMsg = ServerGetMessage();

	std::string sFilename = pMsg->ReadString();
	std::string sName = pMsg->ReadString();

	int iHRIRID = m_pRealCore->LoadHRIRDataset(sFilename, sName);

	pMsg->WriteInt( iHRIRID );
}

bool CVANetNetworkProtocol::ClientFreeHRIRDataset(int iHRIRID)
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_FREE_HRIR_DATASET,
											 MESSAGE_WITH_ANSWER );

	pMsg->WriteInt( iHRIRID );

	ClientSendCommand( pMsg );

	return pMsg->ReadBool();
}

void CVANetNetworkProtocol::ServerFreeHRIRDataset()
{
	CVANetMessage* pMsg = ServerGetMessage();

	int iHRIRID = pMsg->ReadInt();

	bool bResult = m_pRealCore->FreeHRIRDataset(iHRIRID);

	pMsg->WriteBool( bResult );
}

CVAHRIRInfo CVANetNetworkProtocol::ClientGetHRIRInfo(int iHRIRID)
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_GET_HRIR_INFO,
											 MESSAGE_WITH_ANSWER );
	pMsg->WriteInt( iHRIRID );

	ClientSendCommand( pMsg );

	CVAHRIRInfo oHRIRInfo = pMsg->ReadHRIRInfo();
	return oHRIRInfo;
}

void CVANetNetworkProtocol::ServerGetHRIRInfo()
{
	CVANetMessage* pMsg = ServerGetMessage();

	int iHRIRID = pMsg->ReadInt();
	
	CVAHRIRInfo oHRIRInfo = m_pRealCore->GetHRIRInfo(iHRIRID);

	pMsg->WriteHRIRInfo( oHRIRInfo );
}

void CVANetNetworkProtocol::ClientGetHRIRInfos(std::vector<CVAHRIRInfo>& vhiDest)
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_GET_HRIR_INFOS,
											 MESSAGE_WITH_ANSWER );

	ClientSendCommand( pMsg );

	vhiDest.clear();
	int iNumHRIRs = pMsg->ReadInt();
	for (int i=0; i<iNumHRIRs; i++)
		vhiDest.push_back( pMsg->ReadHRIRInfo() );
}

void CVANetNetworkProtocol::ServerGetHRIRInfos()
{
	CVANetMessage* pMsg = ServerGetMessage();

	std::vector<CVAHRIRInfo> vInfos;
	m_pRealCore->GetHRIRInfos(vInfos);
	
	pMsg->WriteInt( (int) vInfos.size() );
	for (size_t i=0; i<vInfos.size(); i++)
		pMsg->WriteHRIRInfo(vInfos[i]);
}


/* +--------------------+
   |                    |
   |      Sounds        |
   |                    |
   +--------------------+ */

int CVANetNetworkProtocol::ClientLoadSound(const std::string& sFilename, const std::string& sName)
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_LOAD_SOUND,
											 MESSAGE_WITH_ANSWER );
	pMsg->WriteString( sFilename );
	pMsg->WriteString( sName );

	ClientSendCommand( pMsg );

	return pMsg->ReadInt();
}

void CVANetNetworkProtocol::ServerLoadSound()
{
	CVANetMessage* pMsg = ServerGetMessage();

	std::string sFilename = pMsg->ReadString();
	std::string sName = pMsg->ReadString();

	int iSoundID = m_pRealCore->LoadSound( sFilename, sName );

	pMsg->WriteInt( iSoundID );
}

bool CVANetNetworkProtocol::ClientFreeSound(int iSoundID)
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_FREE_SOUND,
											 MESSAGE_WITH_ANSWER );
	pMsg->WriteInt( iSoundID );

	ClientSendCommand( pMsg );

	return pMsg->ReadBool();
}

void CVANetNetworkProtocol::ServerFreeSound()
{
	CVANetMessage* pMsg = ServerGetMessage();

	int iID = pMsg->ReadInt();

	bool bResult = m_pRealCore->FreeSound( iID );

	pMsg->WriteBool( bResult );
}

CVASoundInfo CVANetNetworkProtocol::ClientGetSoundInfo(int iSoundID)
{
	CVANetMessage* pMsg = ClientInitMessage( VA_NP_GET_SOUND_INFO,
											 MESSAGE_WITH_ANSWER );
	pMsg->WriteInt( iSoundID );

	ClientSendCommand( pMsg );