-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy path__sample_provider.c
772 lines (602 loc) · 21.2 KB
/
__sample_provider.c
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
/*
libember_slim sample
Copyright (C) 2012-2016 Lawo GmbH (http://www.lawo.com).
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <time.h>
//$$ MSVCRT specific
#include <WinSock2.h>
#include "emberplus.h"
#include "emberinternal.h"
//$$ MSVCRT specific
// Need to link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
#ifndef SECURE_CRT
#define stringCopy(dest, size, source) \
do { strncpy(dest, source, size); dest[(size) - 1] = 0; } while(0)
#define printf_s printf
#define _itoa_s(value, dest, size, radix) \
itoa(value, dest, radix)
#else
#define stringCopy(dest, size, source) \
do { strncpy_s(dest, size, source, (size) - 1); dest[(size) - 1] = 0; } while(0)
#endif
#define stringDup(pStr) \
(pStr != NULL ? _strdup(pStr) : NULL)
// ====================================================================
//
// utils
//
// ====================================================================
static void onThrowError(int error, pcstr pMessage)
{
printf_s("ber error %d: '%s'\n", error, pMessage);
}
static void onFailAssertion(pcstr pFileName, int lineNumber)
{
printf_s("Debug assertion failed @ '%s' line %d", pFileName, lineNumber);
}
// ====================================================================
//
// streams
//
// ====================================================================
#define STREAMS_COUNT (3)
#define STREAMS_MAX_STRING_LENGTH (16)
static GlowStreamEntry _streams[STREAMS_COUNT];
static void initializePpmStreams()
{
bzero_item(_streams);
_streams[0].streamIdentifier = 0;
_streams[0].streamValue.flag = GlowParameterType_Integer;
_streams[1].streamIdentifier = 1;
_streams[1].streamValue.flag = GlowParameterType_Octets;
_streams[1].streamValue.choice.octets.pOctets = newarr(byte, 8);
_streams[1].streamValue.choice.octets.length = 8;
_streams[2].streamIdentifier = 2;
_streams[2].streamValue.flag = GlowParameterType_String;
_streams[2].streamValue.choice.pString = newarr(char, STREAMS_MAX_STRING_LENGTH);
}
static void freePpmStreams()
{
freeMemory(_streams[1].streamValue.choice.octets.pOctets);
freeMemory(_streams[2].streamValue.choice.pString);
}
static void collectPpmData()
{
// ber integer
_streams[0].streamValue.choice.integer = ((rand() % 80) - 64) * 32; // from -64 to +15 in db/32
// octets containing big endian int16
*(short *)&_streams[1].streamValue.choice.octets.pOctets[0] = htons(((rand() % 256) - 128) * 32); // from -128 to +127 in db/32
// octets containing little endian int32
*(berint *)&_streams[1].streamValue.choice.octets.pOctets[4] = ((rand() % 256) - 255) * 32; // from -255 to 0 in db/32
stringCopy(_streams[2].streamValue.choice.pString, STREAMS_MAX_STRING_LENGTH, "abc");
_itoa_s(rand() % 100, &_streams[2].streamValue.choice.pString[3], STREAMS_MAX_STRING_LENGTH - 3, 10);
}
// ====================================================================
//
// Sample tree
//
// ====================================================================
#define SampleNode_MaxChildren (16)
typedef struct __SampleNode
{
bool isParameter;
GlowFieldFlags fields;
union
{
GlowNode node;
GlowParameter param;
};
int childrenCount;
struct __SampleNode *children[SampleNode_MaxChildren];
} SampleNode;
static void sampleNode_init(SampleNode *pThis, SampleNode *pParent)
{
bzero_item(*pThis);
if(pParent != NULL)
{
if(pParent->childrenCount < SampleNode_MaxChildren)
{
pParent->children[pParent->childrenCount] = pThis;
pParent->childrenCount++;
}
}
}
static void sampleNode_free(SampleNode *pThis)
{
int index;
SampleNode *pChild;
for(index = 0; index < pThis->childrenCount; index++)
{
pChild = pThis->children[index];
if(pChild != NULL)
{
sampleNode_free(pChild);
freeMemory(pChild);
}
}
if(pThis->isParameter)
glowParameter_free(&pThis->param);
else
glowNode_free(&pThis->node);
bzero_item(*pThis);
}
static SampleNode *createNode(SampleNode *pParent, pcstr pIdentifier, pcstr pDescription)
{
SampleNode *pNode = newobj(SampleNode);
dword fields = GlowFieldFlag_Identifier;
sampleNode_init(pNode, pParent);
pNode->node.pIdentifier = stringDup(pIdentifier);
if(pDescription != NULL)
{
pNode->node.pDescription = stringDup(pDescription);
fields |= GlowFieldFlag_Description;
}
pNode->fields = (GlowFieldFlags)fields;
return pNode;
}
static SampleNode *createParameter(SampleNode *pParent, pcstr pIdentifier, pcstr pDescription)
{
SampleNode *pNode = newobj(SampleNode);
dword fields = GlowFieldFlag_Identifier;
sampleNode_init(pNode, pParent);
pNode->isParameter = true;
pNode->param.pIdentifier = stringDup(pIdentifier);
if(pDescription != NULL)
{
pNode->param.pDescription = stringDup(pDescription);
fields |= GlowFieldFlag_Description;
}
pNode->fields = (GlowFieldFlags)fields;
return pNode;
}
static void createGain(SampleNode *pParent)
{
SampleNode *pParam = createParameter(pParent, "gain", "power in db");
dword fields = pParam->fields;
pParam->param.access = GlowAccess_ReadWrite;
fields |= GlowFieldFlag_Access;
pParam->param.value.choice.integer = 0;
pParam->param.value.flag = GlowParameterType_Integer;
fields |= GlowFieldFlag_Value;
pParam->param.minimum.choice.integer = 0;
pParam->param.minimum.flag = GlowParameterType_Integer;
fields |= GlowFieldFlag_Minimum;
pParam->param.maximum.choice.integer = 65535;
pParam->param.maximum.flag = GlowParameterType_Integer;
fields |= GlowFieldFlag_Maximum;
pParam->param.factor = 64;
fields |= GlowFieldFlag_Factor;
pParam->param.pFormat = "%Lf db";
fields |= GlowFieldFlag_Format;
pParam->param.pSchemaIdentifiers = stringDup("de.l-s-b.emberplus.samples.gain");
fields |= GlowFieldFlag_SchemaIdentifier;
pParam->fields = (GlowFieldFlags)fields;
}
static void createVolume(SampleNode *pParent)
{
SampleNode *pParam = createParameter(pParent, "volume", "loud?");
dword fields = pParam->fields;
pParam->param.access = GlowAccess_ReadWrite;
fields |= GlowFieldFlag_Access;
pParam->param.value.choice.real = 0.0;
pParam->param.value.flag = GlowParameterType_Real;
fields |= GlowFieldFlag_Value;
pParam->param.minimum.choice.real = -1000.0;
pParam->param.minimum.flag = GlowParameterType_Real;
fields |= GlowFieldFlag_Minimum;
pParam->param.maximum.choice.real = 1000.0;
pParam->param.maximum.flag = GlowParameterType_Real;
fields |= GlowFieldFlag_Maximum;
pParam->param.pFormat = "%Lf db";
fields |= GlowFieldFlag_Format;
pParam->fields = (GlowFieldFlags)fields;
}
static void createFormat(SampleNode *pParent)
{
SampleNode *pParam = createParameter(pParent, "format", "simple enum");
dword fields = pParam->fields;
pParam->param.access = GlowAccess_ReadWrite;
fields |= GlowFieldFlag_Access;
pParam->param.value.choice.integer = 0;
pParam->param.value.flag = GlowParameterType_Integer;
fields |= GlowFieldFlag_Value;
pParam->param.pEnumeration = "4:3\n16:9\nHD\nFull HD\nCinema";
fields |= GlowFieldFlag_Enumeration;
pParam->fields = (GlowFieldFlags)fields;
}
static void createStream1(SampleNode *pParent)
{
SampleNode *pParam = createParameter(pParent, "stream1", "1/32 db");
dword fields = pParam->fields;
pParam->param.access = GlowAccess_Read;
fields |= GlowFieldFlag_Access;
pParam->param.value.flag = GlowParameterType_Integer;
pParam->param.value.choice.integer = 0;
fields |= GlowFieldFlag_Value;
pParam->param.minimum.choice.integer = -64 * 32;
pParam->param.minimum.flag = GlowParameterType_Integer;
fields |= GlowFieldFlag_Minimum;
pParam->param.maximum.choice.integer = 15 * 32;
pParam->param.maximum.flag = GlowParameterType_Integer;
fields |= GlowFieldFlag_Maximum;
pParam->param.streamIdentifier = 0;
fields |= GlowFieldFlag_StreamIdentifier;
pParam->param.pFormula = "($/32 + log(2) - 1/5 + e^(1/4))\n($*32)";
fields |= GlowFieldFlag_Formula;
pParam->fields = (GlowFieldFlags)fields;
}
static void createStream2(SampleNode *pParent)
{
SampleNode *pParam = createParameter(pParent, "stream2", "1/32 db");
dword fields = pParam->fields;
pParam->param.access = GlowAccess_None;
fields |= GlowFieldFlag_Access;
pParam->param.type = GlowParameterType_Integer;
fields |= GlowFieldFlag_Type;
pParam->param.factor = 32;
fields |= GlowFieldFlag_Factor;
pParam->param.minimum.choice.integer = -128 * 32;
pParam->param.minimum.flag = GlowParameterType_Integer;
fields |= GlowFieldFlag_Minimum;
pParam->param.maximum.choice.integer = 127 * 32;
pParam->param.maximum.flag = GlowParameterType_Integer;
fields |= GlowFieldFlag_Maximum;
pParam->param.streamIdentifier = 1;
fields |= GlowFieldFlag_StreamIdentifier;
pParam->param.streamDescriptor.format = GlowStreamFormat_SignedInt16BigEndian;
pParam->param.streamDescriptor.offset = 0;
fields |= GlowFieldFlag_StreamDescriptor;
pParam->fields = (GlowFieldFlags)fields;
}
static void createStream3(SampleNode *pParent)
{
SampleNode *pParam = createParameter(pParent, "stream3", "1/32 db");
dword fields = pParam->fields;
pParam->param.access = GlowAccess_None;
fields |= GlowFieldFlag_Access;
pParam->param.type = GlowParameterType_Integer;
fields |= GlowFieldFlag_Type;
pParam->param.factor = 32;
fields |= GlowFieldFlag_Factor;
pParam->param.minimum.choice.integer = -255 * 32;
pParam->param.minimum.flag = GlowParameterType_Integer;
fields |= GlowFieldFlag_Minimum;
pParam->param.maximum.choice.integer = 0 * 32;
pParam->param.maximum.flag = GlowParameterType_Integer;
fields |= GlowFieldFlag_Maximum;
pParam->param.streamIdentifier = 1;
fields |= GlowFieldFlag_StreamIdentifier;
pParam->param.streamDescriptor.format = GlowStreamFormat_SignedInt32LittleEndian;
pParam->param.streamDescriptor.offset = 4;
fields |= GlowFieldFlag_StreamDescriptor;
pParam->fields = (GlowFieldFlags)fields;
}
static void createStream4(SampleNode *pParent)
{
SampleNode *pParam = createParameter(pParent, "stream4", "strings");
dword fields = pParam->fields;
pParam->param.access = GlowAccess_None;
fields |= GlowFieldFlag_Access;
pParam->param.type = GlowParameterType_String;
fields |= GlowFieldFlag_Type;
pParam->param.maximum.choice.integer = STREAMS_MAX_STRING_LENGTH - 1;
pParam->param.maximum.flag = GlowParameterType_Integer;
fields |= GlowFieldFlag_Maximum;
pParam->param.streamIdentifier = 2;
fields |= GlowFieldFlag_StreamIdentifier;
pParam->fields = (GlowFieldFlags)fields;
}
static void buildTree(SampleNode *pRoot)
{
SampleNode *pVisualRoot, *pFood, *pNonFood, *pStreams, *pAudio;
pVisualRoot = createNode(pRoot, "root", "root");
pFood = createNode(pVisualRoot, "food", "Some food");
createNode(pFood, "parsley", "tasty herb");
createNode(pFood, "salad", "eat a lot of it!");
pNonFood = createNode(pVisualRoot, "non-food", "tech stuff");
pAudio = createNode(pNonFood, "audio", "ask Lawo");
createGain(pAudio);
createVolume(pAudio);
createFormat(pAudio);
createNode(pAudio, "dangling", NULL);
pStreams = createNode(pNonFood, "streams", "see 'em move");
pStreams->node.pSchemaIdentifiers = stringDup("de.l-s-b.emberplus.samples.streams");
pStreams->fields |= GlowFieldFlag_SchemaIdentifier;
createStream1(pStreams);
createStream2(pStreams);
createStream3(pStreams);
createStream4(pStreams);
}
static SampleNode *findNode(SampleNode *pRoot, const berint *pPath, int *pPathLength)
{
int pathIndex;
int nodeIndex;
SampleNode *pCursor = pRoot;
for(pathIndex = 0; pathIndex < *pPathLength; pathIndex++)
{
nodeIndex = pPath[pathIndex];
if(nodeIndex < pCursor->childrenCount)
pCursor = pCursor->children[nodeIndex];
else
break;
}
*pPathLength = pathIndex;
return pCursor;
}
// ====================================================================
//
// glow provider (tcp/ip)
//
// ====================================================================
typedef struct
{
SOCKET sock;
unsigned int streamSubscriptions[STREAMS_COUNT];
pstr pEnumeration;
} ClientInfo;
// ====================================================================
//
// glow communication
//
// ====================================================================
SampleNode _root;
static void onCommand(const GlowCommand *pCommand, const berint *pPath, int pathLength, voidptr state)
{
int bufferSize = 512;
byte *pBuffer;
GlowOutput output;
ClientInfo *pClientInfo = (ClientInfo *)state;
berint *pOutPath;
int nodeIndex;
SampleNode *pCurrent;
SampleNode *pCursor = findNode(&_root, pPath, &pathLength);
SOCKET sock = pClientInfo->sock;
int fields;
if(pCommand->number == GlowCommandType_GetDirectory)
{
pOutPath = newarr(berint, GLOW_MAX_TREE_DEPTH);
memcpy(pOutPath, pPath, pathLength * sizeof(berint));
pBuffer = newarr(byte, bufferSize);
glowOutput_init(&output, pBuffer, bufferSize, 0);
if(pCursor->isParameter)
{
// parameter - send single complete parameter
fields = pCursor->fields & pCommand->options.dirFieldMask;
glowOutput_beginPackage(&output, true);
glow_writeQualifiedParameter(&output, &pCursor->param, (GlowFieldFlags)fields, pOutPath, pathLength);
send(sock, (char *)pBuffer, glowOutput_finishPackage(&output), 0);
}
else if(pCursor->childrenCount == 0)
{
// leaf node - send single empty node
fields = 0;
glowOutput_beginPackage(&output, true);
glow_writeQualifiedNode(&output, &pCursor->node, (GlowFieldFlags)fields, pOutPath, pathLength);
send(sock, (char *)pBuffer, glowOutput_finishPackage(&output), 0);
}
else
{
// node - send children
for(nodeIndex = 0; nodeIndex < pCursor->childrenCount; nodeIndex++)
{
pCurrent = pCursor->children[nodeIndex];
if(pCurrent != NULL)
{
glowOutput_beginPackage(&output, nodeIndex == pCursor->childrenCount - 1);
pOutPath[pathLength] = nodeIndex;
fields = pCurrent->fields & pCommand->options.dirFieldMask;
if(pCurrent->isParameter)
glow_writeQualifiedParameter(&output, &pCurrent->param, (GlowFieldFlags)fields, pOutPath, pathLength + 1);
else
glow_writeQualifiedNode(&output, &pCurrent->node, (GlowFieldFlags)fields, pOutPath, pathLength + 1);
send(sock, (char *)pBuffer, glowOutput_finishPackage(&output), 0);
}
}
}
freeMemory(pBuffer);
freeMemory(pOutPath);
}
else if(pCommand->number == GlowCommandType_Subscribe)
{
if(pCursor->isParameter
&& pCursor->param.streamIdentifier >= 0 && pCursor->param.streamIdentifier < STREAMS_COUNT)
pClientInfo->streamSubscriptions[pCursor->param.streamIdentifier]++;
}
else if(pCommand->number == GlowCommandType_Unsubscribe)
{
if(pCursor->isParameter
&& pCursor->param.streamIdentifier >= 0 && pCursor->param.streamIdentifier < STREAMS_COUNT)
pClientInfo->streamSubscriptions[pCursor->param.streamIdentifier]--;
}
}
static void onParameter(const GlowParameter *pParameter, GlowFieldFlags fields, const berint *pPath, int pathLength, voidptr state)
{
GlowOutput output;
const int bufferSize = 512;
byte *pBuffer;
ClientInfo *pClientInfo = (ClientInfo *)state;
SampleNode *pCursor = findNode(&_root, pPath, &pathLength);
SOCKET sock = pClientInfo->sock;
if(pCursor->isParameter
&& (fields & GlowFieldFlag_Value) == GlowFieldFlag_Value)
{
glowValue_free(&pCursor->param.value);
glowValue_copyFrom(&pCursor->param.value, &pParameter->value);
pBuffer = newarr(byte, bufferSize);
glowOutput_init(&output, pBuffer, bufferSize, 0);
glowOutput_beginPackage(&output, true);
glow_writeQualifiedParameter(&output, &pCursor->param, GlowFieldFlag_Value, pPath, pathLength);
send(sock, (char *)pBuffer, glowOutput_finishPackage(&output), 0);
freeMemory(pBuffer);
}
}
static void onOtherPackageReceived(const byte *pPackage, int length, voidptr state)
{
ClientInfo *pClientInfo = (ClientInfo *)state;
byte buffer[16];
unsigned int txLength;
if(length >= 4
&& pPackage[1] == EMBER_MESSAGE_ID
&& pPackage[2] == EMBER_COMMAND_KEEPALIVE_REQUEST)
{
txLength = emberFraming_writeKeepAliveResponse(buffer, sizeof(buffer), pPackage[0]);
send(pClientInfo->sock, (char *)buffer, txLength, 0);
}
}
static void sendPpmStreams(SOCKET sock, byte *pBuffer, int bufferSize, const ClientInfo *pClientInfo)
{
GlowOutput output;
int index;
int entriesWritten = 0;
for(index = 0; index < STREAMS_COUNT; index++)
{
if(pClientInfo->streamSubscriptions[index] > 0)
{
if(entriesWritten == 0)
{
glowOutput_init(&output, pBuffer, bufferSize, 0);
glowOutput_beginStreamPackage(&output, true);
}
glow_writeStreamEntry(&output, &_streams[index]);
entriesWritten++;
}
}
if(entriesWritten > 0)
send(sock, (char *)pBuffer, glowOutput_finishPackage(&output), 0);
}
static void handleClient(SOCKET sock)
{
byte buffer[64];
int read;
const int rxBufferSize = 1024; // max size of unfamed package
const struct timeval timeout = {0, 50 * 1000}; // 50 milliseconds timeout for select()
const int noDelay = 1;
const int streamBufferSize = 128;
fd_set fdset;
int fdsReady;
ClientInfo client;
// allocate large objects on the heap
GlowReader *pReader = newobj(GlowReader);
byte *pRxBuffer = newarr(byte, rxBufferSize);
byte *pStreamBuffer = newarr(byte, streamBufferSize);
srand((unsigned int)time(NULL));
// turn off nagle algorithm for ppm data
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&noDelay, sizeof(noDelay));
bzero_item(client);
client.sock = sock;
// only handle parameters and commands
glowReader_init(pReader, NULL, onParameter, onCommand, NULL, (voidptr)&client, pRxBuffer, rxBufferSize);
pReader->onOtherPackageReceived = onOtherPackageReceived;
while(true)
{
FD_ZERO(&fdset);
FD_SET(sock, &fdset);
// wait for input, if none received -> send ppm data
fdsReady = select(1, &fdset, NULL, NULL, &timeout);
if(fdsReady == 1) // socket is ready to read
{
if(FD_ISSET(sock, &fdset))
{
read = recv(sock, (char *)buffer, sizeof(buffer), 0);
printf_s("received %d bytes\n", read);
if(read > 0)
glowReader_readBytes(pReader, buffer, read);
else
break;
}
}
else if(fdsReady == 0) // timeout
{
// generate some random ppm data for this sample. usually, ppm data is delivered by some ppm driver
collectPpmData();
sendPpmStreams(sock, pStreamBuffer, streamBufferSize, &client);
}
else
{
break;
}
}
closesocket(sock);
glowReader_free(pReader);
freeMemory(pStreamBuffer);
freeMemory(pRxBuffer);
freeMemory(pReader);
}
static void initSockets()
{
//$$ MSVCRT specific
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
}
static void shutdownSockets()
{
//$$ MSVCRT specific
WSACleanup();
}
static void acceptClient(int port)
{
SOCKET listenSock, clientSock;
struct sockaddr_in localAddr;
initSockets();
listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = INADDR_ANY;
localAddr.sin_port = htons((unsigned short)port);
bind(listenSock, (struct sockaddr *)&localAddr, sizeof(localAddr));
listen(listenSock, 1);
clientSock = accept(listenSock, NULL, NULL);
if(clientSock > 0)
{
printf_s("client accepted\n");
handleClient(clientSock);
}
closesocket(listenSock);
shutdownSockets();
}
// ====================================================================
//
// provider sample entry point
//
// ====================================================================
static int allocCount = 0;
static void *allocMemoryImpl(size_t size)
{
void *pMemory = malloc(size);
//if(sizeof(void *) == 8)
// printf("allocate %lu bytes: %llX\n", size, (unsigned long long)pMemory);
//else
// printf("allocate %lu bytes: %lX\n", size, (unsigned long)pMemory);
allocCount++;
return pMemory;
}
static void freeMemoryImpl(void *pMemory)
{
//if(sizeof(void *) == 8)
// printf("free: %llX\n", (unsigned long long)pMemory);
//else
// printf("free: %lX\n", (unsigned long)pMemory);
allocCount--;
free(pMemory);
}
void runProvider(int argc, char **argv)
{
int port;
ember_init(onThrowError, onFailAssertion, malloc, free);
initializePpmStreams();
buildTree(&_root);
if(argc >= 2)
{
port = atoi(argv[1]);
printf_s("accepting client on port %d\n", port);
acceptClient(port);
}
sampleNode_free(&_root);
freePpmStreams();
}