summaryrefslogtreecommitdiff
path: root/test/serverapp/serverapp.cpp
blob: cb44162d72d2e3cf04ae7ac1b24fe3182b9b222c (plain) (blame)
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
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
1001
1002
1003
1004
// serverapp.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <math.h>

#if !defined(__ORBIS__)
#include <signal.h>
#endif

#if defined(__linux__) || defined(__ORBIS__)
#define TARGET_PLATFORM_NIXLIKE
#include <time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <errno.h>
#else
#include <tchar.h>
#include <conio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#if defined(__linux__) 
#include <termios.h>
#include <ifaddrs.h>
#endif

#if defined(__ORBIS__) 
#include <sys/socket.h>
#include <net.h>
#include <libnetctl.h>
#include <kernel.h>
#endif

#ifdef _XBOX_ONE
#include <collection.h>
#endif

#if defined(__ORBIS__) 
#include <GFSDK_WaveWorks_Orbis.h>
#else
#include <GFSDK_WaveWorks.h>
#endif

#include "message_types.h"
#include "socket_wrapper.h"

#include "ThreadWrap.h"

volatile bool g_DoQuit = false;

#if defined(__ORBIS__) 
unsigned int sceLibcHeapExtendedAlloc = 1;  /* Switch to dynamic allocation */
size_t       sceLibcHeapSize = SCE_LIBC_HEAP_SIZE_EXTENDED_ALLOC_NO_LIMIT; /* no upper limit for heap area */
#endif

/*********************************************************************************
 PRINTF REDIRECTION ON CONSOLES
*********************************************************************************/
#ifdef _XBOX_ONE
#define printf(...) xbone_printf(__VA_ARGS__)

void xbone_printf(const char* fmt, ...)
{
	// Fwd. to debug spew
	va_list arg;
	va_start(arg, fmt);
	const int numChars = _vscprintf(fmt,arg)+1;
	const int bufferSize = (numChars) * sizeof(char);
	va_end(arg);

	char* pBuffer = new char[numChars];
	va_start(arg, fmt);
	_vsprintf_p(pBuffer,bufferSize,fmt,arg);
	va_end(arg);

	OutputDebugStringA(pBuffer);

	delete pBuffer;

	// Also fwd to stdout...
	va_list args;
	va_start (args, fmt);
	vprintf (fmt, args);
	va_end (args);
}

#endif

/*********************************************************************************
 TIMING
*********************************************************************************/

volatile double g_CoordinatedTime = 0.0;
CRITICAL_SECTION g_CoordinatedTimeCriticalSection;

// A somewhat vile WAR to make sure we can compile on older Linuxes, however in our defense
// we do check for _RAW support by relaxing down to no-_RAW if a call to clock_gettime() fails
#ifndef CLOCK_MONOTONIC_RAW
#define CLOCK_MONOTONIC_RAW 4
#endif

static int g_clk_id = CLOCK_MONOTONIC_RAW;

double GetCoordinatedTime()
{
	EnterCriticalSection(&g_CoordinatedTimeCriticalSection);
	double result = g_CoordinatedTime;
	LeaveCriticalSection(&g_CoordinatedTimeCriticalSection);
	return result;
}

#ifdef TARGET_PLATFORM_NIXLIKE
timespec g_BaseTime = {0,0};

double CalcDiff(const timespec& xarg, const timespec& yarg)
{
    timespec x = xarg;
    timespec y = yarg;

    /* Perform the carry for the later subtraction by updating y. */
    if (x.tv_nsec < y.tv_nsec) {
     int numsec = (y.tv_nsec - x.tv_nsec) / 1000000000 + 1;
     y.tv_nsec -= 1000000000 * numsec;
     y.tv_sec += numsec;
    }
    if (x.tv_nsec - y.tv_nsec > 1000000000) {
     int numsec = (x.tv_nsec - y.tv_nsec) / 1000000000;
     y.tv_nsec += 1000000000 * numsec;
     y.tv_sec -= numsec;
    }

    /* Compute the time remaining to wait.
      tv_nsec is certainly positive. */
    timespec diff;
    diff.tv_sec = x.tv_sec - y.tv_sec;
    diff.tv_nsec = x.tv_nsec - y.tv_nsec;

    return (double) diff.tv_sec + 0.000000001 * (double) diff.tv_nsec;
}

void UpdateCoordinatedTime()
{
	EnterCriticalSection(&g_CoordinatedTimeCriticalSection);

    timespec currTime;
    clock_gettime(g_clk_id, &currTime);

	g_CoordinatedTime = CalcDiff(currTime,g_BaseTime);

	LeaveCriticalSection(&g_CoordinatedTimeCriticalSection);
}

void InitCoordinatedTime()
{
	InitializeCriticalSection(&g_CoordinatedTimeCriticalSection);

	EnterCriticalSection(&g_CoordinatedTimeCriticalSection);

	// Record base-time, so that we are zero-based
    if(clock_gettime(g_clk_id, &g_BaseTime))
    {
        // Fall back to no-_RAW if necessary
        g_clk_id = CLOCK_MONOTONIC;
        clock_gettime(g_clk_id, &g_BaseTime);
    }

	// Init coordinated time
	g_CoordinatedTime = 0.0;

	LeaveCriticalSection(&g_CoordinatedTimeCriticalSection);
}


#else // !__linux__ ...
LONGLONG g_BaseTime = 0;
LONGLONG g_QPFTicksPerSec = 0;

void UpdateCoordinatedTime()
{
	EnterCriticalSection(&g_CoordinatedTimeCriticalSection);

    LARGE_INTEGER qwTime;
    QueryPerformanceCounter( &qwTime );

	g_CoordinatedTime = (double) ( qwTime.QuadPart - g_BaseTime ) / (double) g_QPFTicksPerSec;

	LeaveCriticalSection(&g_CoordinatedTimeCriticalSection);
}

void InitCoordinatedTime()
{
	InitializeCriticalSection(&g_CoordinatedTimeCriticalSection);

	EnterCriticalSection(&g_CoordinatedTimeCriticalSection);

	// Record tick frequency
    LARGE_INTEGER qwTicksPerSec;
    QueryPerformanceFrequency( &qwTicksPerSec );
    g_QPFTicksPerSec = qwTicksPerSec.QuadPart;

	// Record base-time, so that we are zero-based
    LARGE_INTEGER qwTime;
    QueryPerformanceCounter( &qwTime );
	g_BaseTime = qwTime.QuadPart;

	// Init coordinated time
	g_CoordinatedTime = 0.0;

	LeaveCriticalSection(&g_CoordinatedTimeCriticalSection);
}

#endif // __linux__

/*********************************************************************************
 SIMULATION
*********************************************************************************/
SimulationConfig g_SimConfig;
int g_SimConfigVersion = 0;
CRITICAL_SECTION g_SimulationCritSec;
GFSDK_WaveWorks_Simulation_Params g_SimParams;
GFSDK_WaveWorks_Simulation_Settings g_SimSettings;
GFSDK_WaveWorks_SimulationHandle g_hSim = NULL;
GFSDK_WaveWorks_Simulation_Stats g_SimStats;
const float kSimulationTickTargetInterval = 0.01f;
const float kPrintStatsTargetInterval = 1.f;
float g_SimulationTickActualInterval = 0.f;
double g_LastSimulationTick = 0.0;
double g_LastPrintStats = 0.0;
bool g_EnablePrintStats = false;

void ReleaseSimulation()
{
	EnterCriticalSection(&g_SimulationCritSec);

	if(g_hSim) {
		GFSDK_WaveWorks_Simulation_Destroy(g_hSim);
		g_hSim = NULL;
	}

	GFSDK_WaveWorks_ReleaseNoGraphics();

	LeaveCriticalSection(&g_SimulationCritSec);
}

int SetSimulationConfig(const SimulationConfig& src)
{
	EnterCriticalSection(&g_SimulationCritSec);

	g_SimConfig = src;
	++g_SimConfigVersion;
	UpdateWaveWorksParams(g_SimParams, g_SimConfig);

	if(gfsdk_waveworks_result_OK != GFSDK_WaveWorks_Simulation_UpdateProperties(g_hSim, g_SimSettings, g_SimParams)) {
		LeaveCriticalSection(&g_SimulationCritSec);
		fprintf(stderr,"ERROR: WaveWorks Simulation_UpdateProperties failed\n");
		ReleaseSimulation();
		return -1;
	}

	LeaveCriticalSection(&g_SimulationCritSec);
	return 0;
}

int GetSimulationConfigVersion()
{
	EnterCriticalSection(&g_SimulationCritSec);
	int result = g_SimConfigVersion;
	LeaveCriticalSection(&g_SimulationCritSec);

	return result;
}

int GetSimulationConfig(SimulationConfig& dst)
{
	EnterCriticalSection(&g_SimulationCritSec);
	int result = g_SimConfigVersion;
	dst = g_SimConfig;
	LeaveCriticalSection(&g_SimulationCritSec);

	return result;
}

void UpdateSimulationParams()
{
	EnterCriticalSection(&g_SimulationCritSec);
	UpdateWaveWorksParams(g_SimParams, g_SimConfig);
	LeaveCriticalSection(&g_SimulationCritSec);
}

int InitSimulation()
{
	InitializeCriticalSection(&g_SimulationCritSec);

	EnterCriticalSection(&g_SimulationCritSec);

#if defined(__ORBIS__)
	sceKernelLoadStartModule("lib_gfsdk_waveworks.ps4.prx", 0, 0, 0, NULL, NULL);
#endif

	g_SimConfig.wind_dir_x = 0.8f;
	g_SimConfig.wind_dir_y = 0.6f;
	g_SimConfig.wind_speed = 2.f;
    g_SimConfig.wind_dependency = 0.98f;
    g_SimConfig.time_scale = 0.5f;
	g_SimConfig.small_wave_fraction = 0.f;

	UpdateSimulationParams();

	g_SimSettings.detail_level = GFSDK_WaveWorks_Simulation_DetailLevel_Normal;
	g_SimSettings.fft_period = 1000.f;
	g_SimSettings.use_Beaufort_scale = true;
	g_SimSettings.readback_displacements = true;
	g_SimSettings.num_readback_FIFO_entries	= 0;
	g_SimSettings.aniso_level = 0;
	g_SimSettings.CPU_simulation_threading_model = GFSDK_WaveWorks_Simulation_CPU_Threading_Model_Automatic;
	g_SimSettings.num_GPUs = 1;
	g_SimSettings.enable_CUDA_timers = true;
	g_SimSettings.enable_gfx_timers	 = false;
	g_SimSettings.enable_CPU_timers	 = true;

    printf("INFO: WaveWorks build = %s\n",GFSDK_WaveWorks_GetBuildString());

	if(gfsdk_waveworks_result_OK != GFSDK_WaveWorks_InitNoGraphics(NULL,GFSDK_WAVEWORKS_API_GUID)) {
		LeaveCriticalSection(&g_SimulationCritSec);
		fprintf(stderr,"ERROR: WaveWorks InitNoGraphics failed\n");
		return -1;
	}

	if(gfsdk_waveworks_result_OK != GFSDK_WaveWorks_Simulation_CreateNoGraphics(g_SimSettings,g_SimParams,&g_hSim)) {
		LeaveCriticalSection(&g_SimulationCritSec);
		fprintf(stderr,"ERROR: WaveWorks Simulation_CreateNoGraphics failed\n");
		ReleaseSimulation();
		return -1;
	}

	// Prime the sim
	do {
		UpdateCoordinatedTime();
		g_LastSimulationTick = GetCoordinatedTime();
		GFSDK_WaveWorks_Simulation_SetTime(g_hSim,g_LastSimulationTick);
		GFSDK_WaveWorks_Simulation_KickNoGraphics(g_hSim,NULL);
	} while(gfsdk_waveworks_result_NONE==GFSDK_WaveWorks_Simulation_GetStagingCursor(g_hSim,NULL));

    // Prime global stats at the same time
    g_LastPrintStats = g_LastSimulationTick;
    GFSDK_WaveWorks_Simulation_GetStats(g_hSim, g_SimStats);

	LeaveCriticalSection(&g_SimulationCritSec);

	return 0;
}

#define FOR_ALL_STATS \
PER_STAT(CPU_main_thread_wait_time) \
PER_STAT(CPU_threads_start_to_finish_time) \
PER_STAT(CPU_threads_total_time) \
PER_STAT(GPU_simulation_time) \
PER_STAT(GPU_FFT_simulation_time) \
PER_STAT(GPU_gfx_time) \
PER_STAT(GPU_update_time)

void FilterStats(const GFSDK_WaveWorks_Simulation_Stats& src, GFSDK_WaveWorks_Simulation_Stats& dst, float deltaTime)
{
    const float lambda = expf(-deltaTime);
    const float invl = 1.f - lambda;

    #define PER_STAT(x) dst.x = lambda * dst.x + invl * src.x;
        FOR_ALL_STATS
    #undef PER_STAT

    g_SimulationTickActualInterval = lambda * g_SimulationTickActualInterval + invl * deltaTime;
}

void PrintStats(const GFSDK_WaveWorks_Simulation_Stats& stats)
{
    printf("INFO: <stats>\n");
    #define PER_STAT(x) printf("INFO:    " #x ": %.2fms\n", stats.x);
        FOR_ALL_STATS
    #undef PER_STAT
    printf("INFO:    TicksPerSec: %.1f\n", 1.f/g_SimulationTickActualInterval);
    printf("INFO: <\\stats>\n");
}

void TickSimulation()
{
	EnterCriticalSection(&g_SimulationCritSec);

	UpdateCoordinatedTime();
	const double t = GetCoordinatedTime();

	float timeSinceLastTick = float(t - g_LastSimulationTick);
	if(timeSinceLastTick > kSimulationTickTargetInterval)
	{
		g_LastSimulationTick = t;
		GFSDK_WaveWorks_Simulation_SetTime(g_hSim,g_LastSimulationTick);
		GFSDK_WaveWorks_Simulation_KickNoGraphics(g_hSim,NULL);

        GFSDK_WaveWorks_Simulation_Stats thisTickStats;
        GFSDK_WaveWorks_Simulation_GetStats(g_hSim, thisTickStats);
        FilterStats(thisTickStats, g_SimStats, timeSinceLastTick);        // Ad-hoc low-pass filter on stats

        float timeSinceLastPrintStats = float(t - g_LastPrintStats);
        if(timeSinceLastPrintStats > kPrintStatsTargetInterval)
        {
            g_LastPrintStats = t;
            if(g_EnablePrintStats)
            {
                PrintStats(g_SimStats);
            }
        }
	}

	LeaveCriticalSection(&g_SimulationCritSec);
}

void SimulationGetPositions(const gfsdk_float2* inSamplePoints, gfsdk_float4* outDisplacements, gfsdk_U32 numSamples)
{
	EnterCriticalSection(&g_SimulationCritSec);

	GFSDK_WaveWorks_Simulation_GetDisplacements(g_hSim,inSamplePoints,outDisplacements,numSamples);

	// Add the sample positions back to the raw displacements
	for(gfsdk_U32 sampleIx = 0; sampleIx != numSamples; ++sampleIx)
	{
		outDisplacements[sampleIx].x += inSamplePoints[sampleIx].x;
		outDisplacements[sampleIx].y += inSamplePoints[sampleIx].y;
	}

	LeaveCriticalSection(&g_SimulationCritSec);
}



/*********************************************************************************
 PER-CLIENT THREAD
*********************************************************************************/
#ifdef TARGET_PLATFORM_NIXLIKE
    typedef FAKE_HANDLE HANDLE;
    typedef void* THREADFUNC_RETTYPE;
    #define THREADFUNC_CALL THREADFUNC_RETTYPE

    void Sleep(FAKE_DWORD dwMilliseconds)
    {
        timespec req, rem;
        req.tv_sec = 0;
        req.tv_nsec = dwMilliseconds * 1000000;
        nanosleep(&req,&rem);
    }

    #define FALSE (0)

	#ifdef __ORBIS__
	const char* inet_ntoa(const in_addr& addr)
	{
		static char buff[256];
		return sceNetInetNtop(SCE_NET_AF_INET, &addr.s_addr, buff, sizeof(buff)/sizeof(buff[0]));
	}
	#endif

#else
    typedef DWORD THREADFUNC_RETTYPE;
    #define THREADFUNC_CALL THREADFUNC_RETTYPE _stdcall
#endif

struct ClientThreadInfo
{
	HANDLE threadUpEvent;
	SOCKET_TYPE clientSocket;
	sockaddr_in clientAddr;
};

volatile int g_RunningThreadCount = 0;
CRITICAL_SECTION g_RunningThreadCountCritSec;

int GetRunningThreadCount()
{
	int result;
	EnterCriticalSection(&g_RunningThreadCountCritSec);
	result = g_RunningThreadCount;
	LeaveCriticalSection(&g_RunningThreadCountCritSec);
	return result;
}

namespace
{
    struct RequestMarkers
    {
	    int num_markers;
		int pad;
	    gfsdk_float2 marker_coords;
    };

    struct ReplyMarkers
    {
	    int num_markers;
		int pad1;
		int pad2;
		int pad3;
	    gfsdk_float4 marker_positions;
    };
}

THREADFUNC_CALL clientThread(void* p)
{
	const ClientThreadInfo clientThreadInfo = *(ClientThreadInfo*)p;

	EnterCriticalSection(&g_RunningThreadCountCritSec);
	++g_RunningThreadCount;
	LeaveCriticalSection(&g_RunningThreadCountCritSec);

	// Info safely copied, thread count safely updated, signal the spawning thread to continue
	SetEvent(clientThreadInfo.threadUpEvent);

	// Log some info
	const in_addr& addr = clientThreadInfo.clientAddr.sin_addr;
	printf("INFO: accepted connection from %s\n",inet_ntoa(addr));

	// Set the socket to non-blocking mode
	if(set_non_blocking(clientThreadInfo.clientSocket)) {
		fprintf(stderr,"ERROR: Failed to set client socket to non-blocking\n");
		return (THREADFUNC_RETTYPE)-1;
	}

	// Set up a send/recv wrapper, and pump it
	// NB: we carefully scope the wrapper so that it goes out of scope before we close the socket
	{
		bool hasSentSimulationConfigToClient = false;
		int lastSimulationConfigVersionSentToClient = 0;
		SocketWrapper sockwrapper(clientThreadInfo.clientSocket);
		while(!g_DoQuit && sockwrapper.is_usable()) {
			if(SocketWrapper::PumpResult_NoTraffic == sockwrapper.pump())
			{
				// Check for client needing a config update
				if(hasSentSimulationConfigToClient && GetSimulationConfigVersion() > lastSimulationConfigVersionSentToClient)
				{
					Message<SimulationConfig> msg;
					msg.messageTypeID = MessageTypeID_ServerSendConfigToClient;
					lastSimulationConfigVersionSentToClient = GetSimulationConfig(msg.payload);
					sockwrapper.enqueue_send_message(&msg, sizeof(msg));
				}
				else
				{
					// Avoid busy waits (because they hog CPU!)
					Sleep(1);
				}
			} else {
				while(sockwrapper.has_recv_message() && sockwrapper.is_usable())
				{
					void* raw_msg = sockwrapper.recv_message();
					int* pMsgType = (int*)raw_msg;
					switch(*pMsgType)
					{
					case MessageTypeID_ClientRequestTimeFromServer:
						{
							Message<double> msg;
							msg.messageTypeID = MessageTypeID_ServerSendTimeToClient;
							msg.payload = GetCoordinatedTime();
							sockwrapper.enqueue_send_message(&msg, sizeof(msg));
						}
						break;
					case MessageTypeID_ClientRequestConfigFromServer:
						{
							Message<SimulationConfig> msg;
							msg.messageTypeID = MessageTypeID_ServerSendConfigToClient;
							lastSimulationConfigVersionSentToClient = GetSimulationConfig(msg.payload);
							sockwrapper.enqueue_send_message(&msg, sizeof(msg));
							hasSentSimulationConfigToClient = true;
						}
						break;
					case MessageTypeID_ClientRequestMarkersFromServer:
						{
							Message<RequestMarkers>* pInMsg = (Message<RequestMarkers>*)raw_msg;

							const unsigned int replySize = sizeof(Message<ReplyMarkers>) + pInMsg->payload.num_markers * sizeof(gfsdk_float4);
							Message<ReplyMarkers>* pOutMsg = (Message<ReplyMarkers>*)malloc(replySize);
							pOutMsg->messageTypeID = MessageTypeID_ServerSendMarkersToClient;
							pOutMsg->payload.num_markers = pInMsg->payload.num_markers;
							SimulationGetPositions(&pInMsg->payload.marker_coords,&pOutMsg->payload.marker_positions,pOutMsg->payload.num_markers);
							sockwrapper.enqueue_send_message(pOutMsg, replySize);
							free(pOutMsg);
						}
						break;
					}
					sockwrapper.consume_recv_message();
				}
			}
		}
	}

	printf("INFO: closing connection from %s\n",inet_ntoa(addr));
	closesocket(clientThreadInfo.clientSocket);

	EnterCriticalSection(&g_RunningThreadCountCritSec);
	--g_RunningThreadCount;
	LeaveCriticalSection(&g_RunningThreadCountCritSec);

	return 0;
}



/*********************************************************************************
 SERVER
*********************************************************************************/
#if defined(__linux__) || defined(__ORBIS__)

    typedef char TCHAR;
    #define _tmain main

    class TerminalInputNixLike
    {
    public:

        bool hasch() const
        {
            struct timeval tv;
            fd_set rdfs;
            
            tv.tv_sec = 0;
            tv.tv_usec = 0;

            FD_ZERO(&rdfs);
            FD_SET(STDIN_FILENO, &rdfs);

            select(STDIN_FILENO+1, &rdfs, NULL, NULL, &tv);
            return 0!=FD_ISSET(STDIN_FILENO, &rdfs);
        }

        TCHAR ch() const
        {
            char buf=0;
            if(read(0,&buf,1)<0)
                fprintf(stderr,"read()");

			// Crude hack to window-dress crappy echo on PS4
			#if defined(__ORBIS__)
			fflush(stdout);
			fprintf(stdout,"\n");
			#endif

            return buf;
        }
    };
#endif

#if defined(__ORBIS__)
	void print_host_interfaces(SOCKET_TYPE, const char* fmt)
	{
		SceNetCtlInfo ci;
		if(sceNetCtlGetInfo(SCE_NET_CTL_INFO_IP_ADDRESS,&ci))
		{
			return;
		}

		printf(fmt, "eth", ci.ip_address, "(IP4)"); 
	}

	typedef TerminalInputNixLike TerminalInput;

#elif defined(__linux__)
	void print_host_interfaces(SOCKET_TYPE, const char* fmt)
	{
		struct ifaddrs * ifAddrStruct=NULL;
		struct ifaddrs * ifa=NULL;
		void * tmpAddrPtr=NULL;

		getifaddrs(&ifAddrStruct);

		for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
			if(ifa ->ifa_addr) {
				if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4
					// is a valid IP4 Address
					tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
					char addressBuffer[INET_ADDRSTRLEN];
					inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
					printf(fmt, ifa->ifa_name, addressBuffer, "(IP4)"); 
				} else if (ifa->ifa_addr->sa_family==AF_INET6) { // check it is IP6
					// is a valid IP6 Address
					tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
					char addressBuffer[INET6_ADDRSTRLEN];
					inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
					printf(fmt, ifa->ifa_name, addressBuffer, "(IP6)"); 
				}
			}
		}
		if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
	}

    class TerminalInput : public TerminalInputNixLike
    {
    public:
        TerminalInput()
        {
            fflush(stdout);
            if(tcgetattr(0, &m_original_term)<0)
                fprintf(stderr,"tcsetattr()");
            struct termios newterm = m_original_term;
            newterm.c_lflag&=~ICANON;
            newterm.c_lflag&=~ECHO;
            newterm.c_cc[VMIN]=0;
            newterm.c_cc[VTIME]=0;
            if(tcsetattr(0, TCSANOW, &newterm)<0)
                fprintf(stderr,"tcsetattr ICANON %s", strerror(errno));

            atexit(term_restore);
        }

    private:

        static void term_restore(void)
        {
            // Restore original
            fflush(stdout);
            if(tcsetattr(0, TCSANOW, &m_original_term)<0) {
                fprintf(stderr,"tcsetattr ICANON %s", strerror(errno));
            } else {
                printf("INFO: terminal mode restored\n");
            }
        }

        static struct termios m_original_term;
    };

    struct termios TerminalInput::m_original_term;

#else // !__linux__ && !__ORBIS__...

	void print_host_interfaces(SOCKET_TYPE sock, const char* fmt)
	{
		INTERFACE_INFO InterfaceList[16];
		unsigned long nBytesReturned;
		if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList, sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR)
		{
			fprintf(stderr,"ERROR: failed to retrieve interface list from listening socket\n");
			return;
		}

		// NB: assumes IPv4, which is fine because we create the socket with AF_INET
		const int numInterfaces = nBytesReturned/sizeof(InterfaceList[0]);
		for(int i = 0; i != numInterfaces; ++i)
		{
			printf(fmt, "", inet_ntoa(InterfaceList[i].iiAddress.AddressIn.sin_addr), "(IP4)" );
		}
	}

#ifdef _XBOX_ONE

	using namespace Windows::Xbox::Input;
	using namespace Windows::Foundation::Collections;

    class TerminalInput
    {
    public:

		TerminalInput()
		{
			m_ch = '\0';
		}

        bool hasch()
		{
			bool result = false;

			auto allGamepads = Gamepad::Gamepads;
			if( allGamepads->Size > 0 )
			{
				IGamepad^ gamepad = allGamepads->GetAt( 0 );
				IGamepadReading^ gamepadReading = gamepad->GetCurrentReading();
				if(m_prevGamepadReading)
				{
					if(gamepadReading->IsBPressed && !m_prevGamepadReading->IsBPressed)
					{
						m_ch = 'q';
						result = true;
					}
					else if(gamepadReading->IsYPressed && !m_prevGamepadReading->IsYPressed)
					{
						m_ch = 's';
						result = true;
					}
					else if(gamepadReading->IsDPadLeftPressed && !m_prevGamepadReading->IsDPadLeftPressed)
					{
						m_ch = '-';
						result = true;
					}
					else if(gamepadReading->IsDPadRightPressed && !m_prevGamepadReading->IsDPadRightPressed)
					{
						m_ch = '+';
						result = true;
					}
				}

				m_prevGamepadReading = gamepadReading;
			}

			return result;
		}
        TCHAR ch() const { return m_ch; }

	private:

		IGamepadReading^ m_prevGamepadReading;
		TCHAR m_ch;
    };

#else // _XBOX_ONE

    class TerminalInput
    {
    public:

        bool hasch() const { return 0!=_kbhit(); }
        TCHAR ch() const { return _gettch(); }
    };

#endif

#endif

const unsigned short kServerListenPort = ServerPort;

void sighandler(int s){
	if(s == SIGINT) {
		g_DoQuit = true;
	}
}

int run_the_server(SOCKET_TYPE listeningSocket, TerminalInput& termin)
{
	int retval = 0;

	// Get listening socket addr
	sockaddr_in listen_addr;
	listen_addr.sin_family = AF_INET;
	listen_addr.sin_addr.s_addr=htonl(INADDR_ANY);
	listen_addr.sin_port=htons(kServerListenPort);

	// Bind the socket
    if (bind(listeningSocket,(sockaddr*)&listen_addr, sizeof(listen_addr))) {
		fprintf(stderr,"ERROR: Failed to bind listening socket\n");
		return -1;
	}

	// Get the socket listening
    if (listen(listeningSocket,SOMAXCONN)) {
		fprintf(stderr,"ERROR: Failed to set listening socket to listen\n");
		return -1;
	}

	// Set the socket to non-blocking mode
	if(set_non_blocking(listeningSocket)) {
		fprintf(stderr,"ERROR: Failed to set listening socket to non-blocking\n");
		return -1;
	}

    // Output the hostname and IP addresses of the system, for info
    char szHostName[256];
    if(0 == gethostname(szHostName, (sizeof(szHostName)/sizeof(szHostName[0]))-1))
    {
        printf("INFO: listening on host %s\n",szHostName);
    }
    else
    {
        printf("WARNING: listening on unknown host (hostname lookup failed)\n");
    }

    printf("INFO: host interfaces...\n");
	print_host_interfaces(listeningSocket, "INFO:    %s -> %s %s\n");
    printf("INFO: ...end of host interfaces\n");

	// Hook Ctrl-C gracefully
	#if !defined(__ORBIS__)
	signal(SIGINT, sighandler);
	#endif

	// Init the sim
    InitCoordinatedTime();
	if(InitSimulation()) {
		return -1;
	}

	// Accept connections
	InitializeCriticalSection(&g_RunningThreadCountCritSec);
	while(!g_DoQuit) {

		TickSimulation();

		sockaddr_in accept_addr;
		socklen_t accept_addr_size = sizeof(accept_addr);
		SOCKET_TYPE acceptedSocket = accept(listeningSocket,(sockaddr*)&accept_addr,&accept_addr_size);
		if(acceptedSocket == INVALID_SOCKET) {
			if(wouldblock()) {
				Sleep(1);
			} else {
				// Once we get here, an element of grace is required to shutdown because we potentially
				// have threads in flight
				fprintf(stderr,"ERROR: accept() failed on listening socket\n");
				retval = -1;
				g_DoQuit = true;
			}
		} else {
			// Start server thread for connected client
			ClientThreadInfo clientThreadInfo;
			clientThreadInfo.clientAddr = accept_addr;
			clientThreadInfo.clientSocket = acceptedSocket;
			clientThreadInfo.threadUpEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
			CreateThread(NULL,0,clientThread,(void*)&clientThreadInfo,0,NULL);
			WaitForSingleObject(clientThreadInfo.threadUpEvent,INFINITE);
			CloseHandle(clientThreadInfo.threadUpEvent);
		}

		if(termin.hasch()) {
			switch(termin.ch())
			{
            case TCHAR('s'):
                g_EnablePrintStats = !g_EnablePrintStats;
                break;
			case TCHAR('q'):
				g_DoQuit = true;
				break;
			case TCHAR('-'):
				{
					SimulationConfig cfg;
					GetSimulationConfig(cfg);
					printf("INFO: old wind_speed=%.2f\n",cfg.wind_speed);
					cfg.wind_speed *= 1.f/1.1f;
					if(SetSimulationConfig(cfg)) {
						// Something failed, so quit
						g_DoQuit = true;
					}
					printf("INFO: new wind_speed=%.2f\n",cfg.wind_speed);
				}
				break;
			case TCHAR('+'):
				{
					SimulationConfig cfg;
					GetSimulationConfig(cfg);
					printf("INFO: old wind_speed=%.2f\n",cfg.wind_speed);
					cfg.wind_speed *= 1.1f;
					if(SetSimulationConfig(cfg)) {
						// Something failed, so quit
						g_DoQuit = true;
					}
					printf("INFO: new wind_speed=%.2f\n",cfg.wind_speed);
				}
				break;
			}
		}
	}

	// Wait for threads to come home
	printf("INFO: server shutting down, waiting for client threads to graceful exit...\n");
	while(GetRunningThreadCount())
	{
		Sleep(1);
	}
	printf("INFO: ...done, all threads are safely home\n");

	ReleaseSimulation();

	return retval;
}


/*********************************************************************************
 APP
*********************************************************************************/

#ifdef _XBOX_ONE
[Platform::MTAThread] int main( Platform::Array< Platform::String^ >^ /*params*/ )
#else
int _tmain(int /*argc*/, TCHAR** /*argv[]*/)
#endif
{
    TerminalInput term;

	// Start networking
	if(init_networking()) {
		fprintf(stderr,"ERROR: init_networking failed\n");
		return -1;
	}

	// Init listening socket
	SOCKET_TYPE listeningSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(INVALID_SOCKET == listeningSocket) {
		fprintf(stderr,"ERROR: Failed to create listening socket\n");
		term_networking();
		return -1;
	}

	// Run the server
	const int result = run_the_server(listeningSocket,term);

	// Cleanup
	closesocket(listeningSocket);
	term_networking();

	return result;
}