forked from u-n-k-n-o-w-n/BonDriverProxy_Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBonDriverProxyEx.cpp
More file actions
1851 lines (1738 loc) · 48.6 KB
/
BonDriverProxyEx.cpp
File metadata and controls
1851 lines (1738 loc) · 48.6 KB
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
#include "BonDriverProxyEx.h"
namespace BonDriverProxyEx {
#ifdef DEBUG
static BOOL g_bStop; // 初期値FALSE
static void Handler(int sig)
{
g_bStop = TRUE;
}
static void CleanUp()
{
for (int i = 0; i < MAX_DRIVERS; i++)
{
if (g_ppDriver[i] == NULL)
break;
std::vector<stDriver> &v = DriversMap[g_ppDriver[i][0]];
for (size_t j = 0; j < v.size(); j++)
{
if (v[j].hModule != NULL)
{
::dlclose(v[j].hModule);
::fprintf(stderr, "[%s] unloaded\n", v[j].strBonDriver);
}
}
delete[] g_ppDriver[i][0];
delete[] g_ppDriver[i];
}
DriversMap.clear();
}
#endif
static BOOL IsTagMatch(const char *line, const char *tag, char **value)
{
const int taglen = ::strlen(tag);
const char *p;
if (::strncmp(line, tag, taglen) != 0)
return FALSE;
p = line + taglen;
while (*p == ' ' || *p == '\t')
p++;
if (value == NULL && *p == '\0')
return TRUE;
if (*p++ != '=')
return FALSE;
while (*p == ' ' || *p == '\t')
p++;
*value = const_cast<char *>(p);
return TRUE;
}
static int Init(int ac, char *av[])
{
if (ac < 3)
return -1;
::strncpy(g_Host, av[1], sizeof(g_Host) - 1);
g_Host[sizeof(g_Host) - 1] = '\0';
::strncpy(g_Port, av[2], sizeof(g_Port) - 1);
g_Port[sizeof(g_Port) - 1] = '\0';
if (ac > 3)
{
g_OpenTunerRetDelay = ::atoi(av[3]);
if (ac > 4)
{
g_PacketFifoSize = ::atoi(av[4]);
if (ac > 5)
g_TsPacketBufSize = ::atoi(av[5]);
}
}
FILE *fp;
char *p, buf[1024];
#ifdef LINUX
ssize_t len;
if ((len = ::readlink("/proc/self/exe", buf, sizeof(buf) - 8)) == -1)
return -2;
buf[len] = '\0';
#else
Dl_info info;
if (::dladdr((void *)Init, &info) == 0)
return -2;
::strncpy(buf, info.dli_fname, sizeof(buf) - 8);
buf[sizeof(buf) - 8] = '\0';
#endif
::strcat(buf, ".conf");
#ifdef DEBUG
::fprintf(stderr, "conf: [%s]\n", buf);
#endif
fp = ::fopen(buf, "r");
if (fp == NULL)
return -3;
int cntD, cntT = 0, num = 0;
char *str, *pos, *pp[MAX_DRIVERS], **ppDriver;
char tag[4];
while (::fgets(buf, sizeof(buf), fp) && (num < MAX_DRIVERS))
{
if (buf[0] == ';')
continue;
p = buf + ::strlen(buf) - 1;
while ((p >= buf) && (*p == '\r' || *p == '\n'))
*p-- = '\0';
if (p < buf)
continue;
// ;BONDRIVER
// 00=PT-T;./BonDriver_LinuxPT-T0.so;./BonDriver_LinuxPT-T1.so
// 01=PT-S;./BonDriver_LinuxPT-S0.so;./BonDriver_LinuxPT-S1.so
tag[0] = (char)('0' + (num / 10));
tag[1] = (char)('0' + (num % 10));
tag[2] = '\0';
num++;
if (IsTagMatch(buf, tag, &p))
{
// format: GroupName;BonDriver1;BonDriver2;BonDriver3...
// e.g. : PT-T;./BonDriver_LinuxPT-T0.so;./BonDriver_LinuxPT-T1.so
str = new char[::strlen(p) + 1];
::strcpy(str, p);
pos = pp[0] = str;
cntD = 1;
for (;;)
{
p = ::strchr(pos, ';');
if (p)
{
*p = '\0';
pos = pp[cntD++] = p + 1;
if (cntD > (MAX_DRIVERS - 1))
break;
}
else
break;
}
if (cntD == 1)
{
delete[] str;
continue;
}
ppDriver = g_ppDriver[cntT++] = new char *[cntD];
::memcpy(ppDriver, pp, sizeof(char *) * cntD);
std::vector<stDriver> vstDriver(cntD - 1);
for (int i = 1; i < cntD; i++)
{
vstDriver[i-1].strBonDriver = ppDriver[i];
vstDriver[i-1].hModule = NULL;
vstDriver[i-1].bUsed = FALSE;
}
DriversMap[ppDriver[0]] = vstDriver;
}
else
{
g_ppDriver[cntT] = NULL;
break;
}
}
::fclose(fp);
#ifdef DEBUG
for (int i = 0; i < MAX_DRIVERS; i++)
{
if (g_ppDriver[i] == NULL)
break;
::fprintf(stderr, "%02d: %s\n", i, g_ppDriver[i][0]);
std::vector<stDriver> &v = DriversMap[g_ppDriver[i][0]];
for (size_t j = 0; j < v.size(); j++)
::fprintf(stderr, " : %s\n", v[j].strBonDriver);
}
#endif
return 0;
}
cProxyServerEx::cProxyServerEx() : m_Error(m_c, m_m), m_fifoSend(m_c, m_m), m_fifoRecv(m_c, m_m)
{
m_s = INVALID_SOCKET;
m_hModule = NULL;
m_pIBon = m_pIBon2 = m_pIBon3 = NULL;
m_bTunerOpen = FALSE;
m_bChannelLock = 0;
m_hTsRead = 0;
m_pTsReaderArg = NULL;
m_dwSpace = m_dwChannel = 0x7fffffff; // INT_MAX
m_pDriversMapKey = NULL;
m_iDriverNo = -1;
m_iDriverUseOrder = 0;
pthread_mutexattr_t attr;
::pthread_mutexattr_init(&attr);
::pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
::pthread_mutex_init(&m_m, &attr);
::pthread_cond_init(&m_c, NULL);
}
cProxyServerEx::~cProxyServerEx()
{
LOCK(g_Lock);
BOOL bRelease = TRUE;
std::list<cProxyServerEx *>::iterator it = g_InstanceList.begin();
while (it != g_InstanceList.end())
{
if (*it == this)
g_InstanceList.erase(it++);
else
{
if ((m_hModule != NULL) && (m_hModule == (*it)->m_hModule))
bRelease = FALSE;
++it;
}
}
if (bRelease)
{
if (m_hTsRead)
{
m_pTsReaderArg->StopTsRead = TRUE;
::pthread_join(m_hTsRead, NULL);
delete m_pTsReaderArg;
}
if (m_pIBon)
m_pIBon->Release();
if (m_hModule)
{
std::vector<stDriver> &vstDriver = DriversMap[m_pDriversMapKey];
vstDriver[m_iDriverNo].bUsed = FALSE;
if (!g_DisableUnloadBonDriver)
{
::dlclose(m_hModule);
vstDriver[m_iDriverNo].hModule = NULL;
#ifdef DEBUG
::fprintf(stderr, "[%s] unloaded\n", vstDriver[m_iDriverNo].strBonDriver);
#endif
}
}
}
else
{
if (m_hTsRead)
StopTsReceive();
}
::pthread_cond_destroy(&m_c);
::pthread_mutex_destroy(&m_m);
if (m_s != INVALID_SOCKET)
::close(m_s);
}
void *cProxyServerEx::Reception(LPVOID pv)
{
cProxyServerEx *pProxy = static_cast<cProxyServerEx *>(pv);
pProxy->Process();
delete pProxy;
return NULL;
}
DWORD cProxyServerEx::Process()
{
pthread_t hThread[2];
if (::pthread_create(&hThread[0], NULL, cProxyServerEx::Sender, this))
return 1;
if (::pthread_create(&hThread[1], NULL, cProxyServerEx::Receiver, this))
{
m_Error.Set();
::pthread_join(hThread[0], NULL);
return 2;
}
cEvent *h[2] = { &m_Error, m_fifoRecv.GetEventHandle() };
for (;;)
{
DWORD dwRet = cEvent::MultipleWait(2, h);
switch (dwRet)
{
case WAIT_OBJECT_0:
goto end;
case WAIT_OBJECT_0 + 1:
{
// コマンド処理の全体をロックするので、BonDriver_Proxyをロードして自分自身に
// 接続させるとデッドロックする
// しかしそうしなければ困る状況と言うのは多分無いと思うので、これは仕様と言う事で
LOCK(g_Lock);
cPacketHolder *pPh = NULL;
m_fifoRecv.Pop(&pPh);
#ifdef DEBUG
{
const char *CommandName[]={
"eSelectBonDriver",
"eCreateBonDriver",
"eOpenTuner",
"eCloseTuner",
"eSetChannel1",
"eGetSignalLevel",
"eWaitTsStream",
"eGetReadyCount",
"eGetTsStream",
"ePurgeTsStream",
"eRelease",
"eGetTunerName",
"eIsTunerOpening",
"eEnumTuningSpace",
"eEnumChannelName",
"eSetChannel2",
"eGetCurSpace",
"eGetCurChannel",
"eGetTotalDeviceNum",
"eGetActiveDeviceNum",
"eSetLnbPower",
"eGetClientInfo",
};
if (pPh->GetCommand() <= eGetClientInfo)
{
::fprintf(stderr, "Recieve Command : [%s]\n", CommandName[pPh->GetCommand()]);
}
else
{
::fprintf(stderr, "Illegal Command : [%d]\n", (int)(pPh->GetCommand()));
}
}
#endif
switch (pPh->GetCommand())
{
case eSelectBonDriver:
{
if (pPh->GetBodyLength() <= sizeof(char))
makePacket(eSelectBonDriver, FALSE);
else
{
char *p;
if ((p = ::strrchr((char *)(pPh->m_pPacket->payload), ':')) != NULL)
{
if (::strcmp(p, ":desc") == 0) // 降順
{
*p = '\0';
m_iDriverUseOrder = 1;
}
else if (::strcmp(p, ":asc") == 0) // 昇順
*p = '\0';
}
BOOL b = SelectBonDriver((LPCSTR)(pPh->m_pPacket->payload), 0);
if (b)
g_InstanceList.push_back(this);
makePacket(eSelectBonDriver, b);
}
break;
}
case eCreateBonDriver:
{
if (m_pIBon == NULL)
{
BOOL bFind = FALSE;
for (std::list<cProxyServerEx *>::iterator it = g_InstanceList.begin(); it != g_InstanceList.end(); ++it)
{
if (*it == this)
continue;
if (m_hModule == (*it)->m_hModule)
{
if ((*it)->m_pIBon != NULL)
{
bFind = TRUE; // ここに来るのはかなりのレアケースのハズ
m_pIBon = (*it)->m_pIBon;
m_pIBon2 = (*it)->m_pIBon2;
m_pIBon3 = (*it)->m_pIBon3;
break;
}
// ここに来るのは上より更にレアケース
// 一応リストの最後まで検索してみて、それでも見つからなかったら
// CreateBonDriver()をやらせてみる
}
}
if (!bFind)
{
if ((CreateBonDriver() != NULL) && (m_pIBon2 != NULL))
makePacket(eCreateBonDriver, TRUE);
else
{
makePacket(eCreateBonDriver, FALSE);
m_Error.Set();
}
}
else
makePacket(eCreateBonDriver, TRUE);
}
else
makePacket(eCreateBonDriver, TRUE);
break;
}
case eOpenTuner:
{
BOOL bFind = FALSE;
{
for (std::list<cProxyServerEx *>::iterator it = g_InstanceList.begin(); it != g_InstanceList.end(); ++it)
{
if (*it == this)
continue;
if ((m_pIBon != NULL) && (m_pIBon == (*it)->m_pIBon))
{
if ((*it)->m_bTunerOpen)
{
bFind = TRUE;
m_bTunerOpen = TRUE;
break;
}
}
}
}
if (!bFind)
m_bTunerOpen = OpenTuner();
makePacket(eOpenTuner, m_bTunerOpen);
break;
}
case eCloseTuner:
{
BOOL bFind = FALSE;
for (std::list<cProxyServerEx *>::iterator it = g_InstanceList.begin(); it != g_InstanceList.end(); ++it)
{
if (*it == this)
continue;
if ((m_pIBon != NULL) && (m_pIBon == (*it)->m_pIBon))
{
if ((*it)->m_bTunerOpen)
{
bFind = TRUE;
break;
}
}
}
if (!bFind)
{
if (m_hTsRead)
{
m_pTsReaderArg->StopTsRead = TRUE;
::pthread_join(m_hTsRead, NULL);
delete m_pTsReaderArg;
}
CloseTuner();
}
else
{
if (m_hTsRead)
StopTsReceive();
}
m_bChannelLock = 0;
m_dwSpace = m_dwChannel = 0x7fffffff; // INT_MAX
m_hTsRead = 0;
m_pTsReaderArg = NULL;
m_bTunerOpen = FALSE;
break;
}
case ePurgeTsStream:
{
if (m_hTsRead)
{
m_pTsReaderArg->TsLock.Enter();
if (m_pTsReaderArg->TsReceiversList.size() <= 1)
{
PurgeTsStream();
m_pTsReaderArg->pos = 0;
}
#ifdef DEBUG
::fprintf(stderr, "ePurgeTsStream : [%d] / size[%zu]\n", (m_pTsReaderArg->TsReceiversList.size() <= 1) ? 1 : 0, m_pTsReaderArg->TsReceiversList.size());
#endif
m_pTsReaderArg->TsLock.Leave();
makePacket(ePurgeTsStream, TRUE);
}
else
makePacket(ePurgeTsStream, FALSE);
break;
}
case eRelease:
m_Error.Set();
break;
case eEnumTuningSpace:
{
if (pPh->GetBodyLength() != sizeof(DWORD))
{
TCHAR c = 0;
makePacket(eEnumTuningSpace, &c);
}
else
{
DWORD *pdw = (DWORD *)(pPh->m_pPacket->payload);
LPCTSTR p = EnumTuningSpace(ntohl(*pdw));
if (p)
makePacket(eEnumTuningSpace, p);
else
{
TCHAR c = 0;
makePacket(eEnumTuningSpace, &c);
}
}
break;
}
case eEnumChannelName:
{
if (pPh->GetBodyLength() != (sizeof(DWORD) * 2))
{
TCHAR c = 0;
makePacket(eEnumChannelName, &c);
}
else
{
DWORD *pdw1 = (DWORD *)(pPh->m_pPacket->payload);
DWORD *pdw2 = (DWORD *)(&(pPh->m_pPacket->payload[sizeof(DWORD)]));
LPCTSTR p = EnumChannelName(ntohl(*pdw1), ntohl(*pdw2));
if (p)
makePacket(eEnumChannelName, p);
else
{
TCHAR c = 0;
makePacket(eEnumChannelName, &c);
}
}
break;
}
case eSetChannel2:
{
if (pPh->GetBodyLength() != ((sizeof(DWORD) * 2) + sizeof(BYTE)))
makePacket(eSetChannel2, (DWORD)0xff);
else
{
BYTE bChannelLock = pPh->m_pPacket->payload[sizeof(DWORD) * 2];
DWORD *pdw1 = (DWORD *)(pPh->m_pPacket->payload);
DWORD *pdw2 = (DWORD *)(&(pPh->m_pPacket->payload[sizeof(DWORD)]));
DWORD dwReqSpace = ntohl(*pdw1);
DWORD dwReqChannel = ntohl(*pdw2);
if ((dwReqSpace == m_dwSpace) && (dwReqChannel == m_dwChannel))
{
// 既にリクエストされたチャンネルを選局済み
#ifdef DEBUG
::fprintf(stderr, "** already tuned! ** : m_dwSpace[%d] / m_dwChannel[%d]\n", dwReqSpace, dwReqChannel);
#endif
// 必ずtrueのハズだけど、一応
if (m_hTsRead)
{
// このインスタンスが要求している優先度が255であった場合に
if (bChannelLock == 0xff)
{
// 現在の配信リストには優先度255のインスタンスが既にいるか?
BOOL bFind = FALSE;
m_pTsReaderArg->TsLock.Enter();
std::list<cProxyServerEx *>::iterator it = m_pTsReaderArg->TsReceiversList.begin();
while (it != m_pTsReaderArg->TsReceiversList.end())
{
if ((*it != this) && ((*it)->m_bChannelLock == 0xff))
{
bFind = TRUE;
break;
}
++it;
}
m_pTsReaderArg->TsLock.Leave();
if (bFind)
{
// いた場合は、このインスタンスの優先度を暫定的に254にする
bChannelLock = 0xfe;
// 排他権取得待ちリストにまだ自身が含まれていなければ追加
bFind = FALSE;
it = m_pTsReaderArg->WaitExclusivePrivList.begin();
while (it != m_pTsReaderArg->WaitExclusivePrivList.end())
{
if (*it == this)
{
bFind = TRUE;
break;
}
++it;
}
if (!bFind)
m_pTsReaderArg->WaitExclusivePrivList.push_back(this);
#ifdef DEBUG
::fprintf(stderr, "** exclusive tuner! ** : wait-exclusivepriv-list size[%zu] / added[%d]\n", m_pTsReaderArg->WaitExclusivePrivList.size(), bFind ? 0 : 1);
#endif
}
}
}
m_bChannelLock = bChannelLock;
makePacket(eSetChannel2, (DWORD)0x00);
}
else
{
BOOL bSuccess;
BOOL bLocked = FALSE;
BOOL bShared = FALSE;
BOOL bSetChannel = FALSE;
cProxyServerEx *pHavePriv = NULL;
for (std::list<cProxyServerEx *>::iterator it = g_InstanceList.begin(); it != g_InstanceList.end(); ++it)
{
if (*it == this)
continue;
// ひとまず現在のインスタンスが共有されているかどうかを確認しておく
if ((m_pIBon != NULL) && (m_pIBon == (*it)->m_pIBon))
bShared = TRUE;
// 対象BonDriver群の中でチューナをオープンしているもの
if (m_pDriversMapKey == (*it)->m_pDriversMapKey && (*it)->m_pIBon != NULL && (*it)->m_bTunerOpen)
{
// かつクライアントからの要求と同一チャンネルを選択しているもの
if ((*it)->m_dwSpace == dwReqSpace && (*it)->m_dwChannel == dwReqChannel)
{
// 今クライアントがオープンしているチューナに関して
if (m_pIBon != NULL)
{
BOOL bModule = FALSE;
BOOL bIBon = FALSE;
BOOL bTuner = FALSE;
for (std::list<cProxyServerEx *>::iterator it2 = g_InstanceList.begin(); it2 != g_InstanceList.end(); ++it2)
{
if (*it2 == this)
continue;
if (m_hModule == (*it2)->m_hModule)
{
bModule = TRUE; // モジュール使用者有り
if (m_pIBon == (*it2)->m_pIBon)
{
bIBon = TRUE; // インスタンス使用者有り
if ((*it2)->m_bTunerOpen)
{
bTuner = TRUE; // チューナ使用者有り
break;
}
}
}
}
// チューナ使用者無しならクローズ
if (!bTuner)
{
if (m_hTsRead)
{
m_pTsReaderArg->StopTsRead = TRUE;
::pthread_join(m_hTsRead, NULL);
//m_hTsRead = 0;
delete m_pTsReaderArg;
//m_pTsReaderArg = NULL;
}
CloseTuner();
//m_bTunerOpen = FALSE;
// かつインスタンス使用者も無しならインスタンスリリース
if (!bIBon)
{
m_pIBon->Release();
// m_pIBon = NULL;
// かつモジュール使用者も無しならモジュールリリース
if (!bModule)
{
std::vector<stDriver> &vstDriver = DriversMap[m_pDriversMapKey];
vstDriver[m_iDriverNo].bUsed = FALSE;
if (!g_DisableUnloadBonDriver)
{
::dlclose(m_hModule);
// m_hModule = NULL;
vstDriver[m_iDriverNo].hModule = NULL;
#ifdef DEBUG
::fprintf(stderr, "[%s] unloaded\n", vstDriver[m_iDriverNo].strBonDriver);
#endif
}
}
}
}
else // 他にチューナ使用者有りの場合
{
// 現在TSストリーム配信中ならその配信対象リストから自身を削除
if (m_hTsRead)
StopTsReceive();
}
}
// このインスタンスが要求している優先度が255であった場合に
if (bChannelLock == 0xff)
{
// 切り替え先チューナに対して優先度255のインスタンスが既にいるか?
for (std::list<cProxyServerEx *>::iterator it2 = g_InstanceList.begin(); it2 != g_InstanceList.end(); ++it2)
{
if (*it2 == this)
continue;
if ((*it2)->m_pIBon == (*it)->m_pIBon)
{
if ((*it2)->m_bChannelLock == 0xff)
{
// いた場合は、このインスタンスの優先度を暫定的に254にする
// (そうしないと、優先度255のインスタンスもチャンネル変更できなくなる為)
bChannelLock = 0xfe;
pHavePriv = *it2;
break;
}
}
}
}
// インスタンス切り替え
m_hModule = (*it)->m_hModule;
m_iDriverNo = (*it)->m_iDriverNo;
m_pIBon = (*it)->m_pIBon;
m_pIBon2 = (*it)->m_pIBon2;
m_pIBon3 = (*it)->m_pIBon3;
m_bTunerOpen = TRUE;
m_hTsRead = (*it)->m_hTsRead; // この時点でもNULLの可能性はゼロではない
m_pTsReaderArg = (*it)->m_pTsReaderArg;
if (m_hTsRead)
{
m_pTsReaderArg->TsLock.Enter();
m_pTsReaderArg->TsReceiversList.push_back(this);
m_pTsReaderArg->TsLock.Leave();
}
#ifdef DEBUG
::fprintf(stderr, "** found! ** : m_hModule[%p] / m_iDriverNo[%d] / m_pIBon[%p]\n", m_hModule, m_iDriverNo, m_pIBon);
::fprintf(stderr, " : m_dwSpace[%d] / m_dwChannel[%d] / m_bChannelLock[%d]\n", dwReqSpace, dwReqChannel, bChannelLock);
#endif
// このインスタンスの優先度が下げられた場合
if (pHavePriv != NULL)
{
if (m_hTsRead)
{
// 排他権取得待ちリストにまだ自身が含まれていなければ追加
BOOL bFind = FALSE;
std::list<cProxyServerEx *>::iterator it2 = m_pTsReaderArg->WaitExclusivePrivList.begin();
while (it2 != m_pTsReaderArg->WaitExclusivePrivList.end())
{
if (*it2 == this)
{
bFind = TRUE;
break;
}
++it2;
}
if (!bFind)
m_pTsReaderArg->WaitExclusivePrivList.push_back(this);
#ifdef DEBUG
::fprintf(stderr, "** exclusive tuner! ** : wait-exclusivepriv-list size[%zu] / added[%d]\n", m_pTsReaderArg->WaitExclusivePrivList.size(), bFind ? 0 : 1);
#endif
}
else
{
// この処理の意図は少し下の同じ処理のコメント参照
pHavePriv->m_bChannelLock = 0;
bChannelLock = 0xff;
}
}
goto ok; // これは酷い
}
}
}
// 同一チャンネルを使用中のチューナは見つからず、現在のチューナは共有されていたら
if (bShared)
{
// 出来れば未使用、無理ならなるべくロックされてないチューナを選択して、
// 一気にチューナオープン状態にまで持って行く
if (SelectBonDriver(m_pDriversMapKey, bChannelLock))
{
if (m_pIBon == NULL)
{
// 未使用チューナがあった
if ((CreateBonDriver() == NULL) || (m_pIBon2 == NULL))
{
makePacket(eSetChannel2, (DWORD)0xff);
m_Error.Set();
break;
}
}
if (!m_bTunerOpen)
{
m_bTunerOpen = OpenTuner();
if (!m_bTunerOpen)
{
makePacket(eSetChannel2, (DWORD)0xff);
m_Error.Set();
break;
}
}
}
else
{
makePacket(eSetChannel2, (DWORD)0xff);
m_Error.Set();
break;
}
// 使用チューナのチャンネルロック状態確認
for (std::list<cProxyServerEx *>::iterator it = g_InstanceList.begin(); it != g_InstanceList.end(); ++it)
{
if (*it == this)
continue;
if (m_pIBon == (*it)->m_pIBon)
{
if ((*it)->m_bChannelLock > bChannelLock)
bLocked = TRUE;
else if ((*it)->m_bChannelLock == 0xff)
{
// 対象チューナに対して優先度255のインスタンスが既にいる状態で、このインスタンスが
// 要求している優先度も255の場合、このインスタンスの優先度を暫定的に254にする
// (そうしないと、優先度255のインスタンスもチャンネル変更できなくなる為)
bChannelLock = 0xfe;
bLocked = TRUE;
pHavePriv = *it;
}
if (bLocked)
break;
}
}
// このインスタンスの優先度が下げられた場合
if (pHavePriv != NULL)
{
if (m_hTsRead)
{
// 排他権取得待ちリストにまだ自身が含まれていなければ追加
BOOL bFind = FALSE;
std::list<cProxyServerEx *>::iterator it = m_pTsReaderArg->WaitExclusivePrivList.begin();
while (it != m_pTsReaderArg->WaitExclusivePrivList.end())
{
if (*it == this)
{
bFind = TRUE;
break;
}
++it;
}
if (!bFind)
m_pTsReaderArg->WaitExclusivePrivList.push_back(this);
#ifdef DEBUG
::fprintf(stderr, "** exclusive tuner! ** : wait-exclusivepriv-list size[%zu] / added[%d]\n", m_pTsReaderArg->WaitExclusivePrivList.size(), bFind ? 0 : 1);
#endif
}
else
{
// このインスタンスの優先度が下げられたが、排他権を持っているインスタンスへの配信が
// 開始されていない場合は、そのインスタンスから排他権を奪う
// こうする事が挙動として望ましいのかどうかは微妙だが、そもそもここに来るのは、
// 当該インスタンスでのSetChannel()の失敗後、何もせずに接続だけ続けている状態であり、
// 可能性としてはゼロではないものの、かなりのレアケースに限られるはず
pHavePriv->m_bChannelLock = 0;
bChannelLock = 0xff;
}
}
}
#ifdef DEBUG
::fprintf(stderr, "eSetChannel2 : bShared[%d] / bLocked[%d]\n", bShared, bLocked);
::fprintf(stderr, " : dwReqSpace[%d] / dwReqChannel[%d] / m_bChannelLock[%d]\n", dwReqSpace, dwReqChannel, bChannelLock);
#endif
if (bLocked)
{
// ロックされてる時は単純にロックされてる事を通知
// この場合クライアントアプリへのSetChannel()の戻り値は成功になる
// (おそらく致命的な問題にはならない)
makePacket(eSetChannel2, (DWORD)0x01);
}
else
{
if (m_hTsRead)
m_pTsReaderArg->TsLock.Enter();
bSuccess = SetChannel(dwReqSpace, dwReqChannel);
if (m_hTsRead)
{
// 一旦ロックを外すとチャンネル変更前のデータが送信されない事を保証できなくなる為、
// チャンネル変更前のデータの破棄とCNRの更新指示はここで行う
if (bSuccess)
{
// 同一チャンネルを使用中のチューナが見つからなかった場合は、このリクエストで
// インスタンスの切り替えが発生していたとしても、この時点ではどうせチャンネルが
// 変更されているので、未送信バッファを破棄しても別に問題にはならないハズ
m_pTsReaderArg->pos = 0;
m_pTsReaderArg->ChannelChanged = TRUE;
}
m_pTsReaderArg->TsLock.Leave();
}
if (bSuccess)
{
bSetChannel = TRUE;
ok:
m_dwSpace = dwReqSpace;
m_dwChannel = dwReqChannel;
m_bChannelLock = bChannelLock;
makePacket(eSetChannel2, (DWORD)0x00);
if (m_hTsRead == 0)
{
m_pTsReaderArg = new stTsReaderArg();
m_pTsReaderArg->TsReceiversList.push_back(this);
m_pTsReaderArg->pIBon = m_pIBon;
if (::pthread_create(&m_hTsRead, NULL, cProxyServerEx::TsReader, m_pTsReaderArg))
{
m_hTsRead = 0;
delete m_pTsReaderArg;
m_pTsReaderArg = NULL;
m_Error.Set();
}
}
if (bSetChannel)
{
// SetChannel()が行われた場合は、同一BonDriverインスタンスを使用しているインスタンスの保持チャンネルを変更
for (std::list<cProxyServerEx *>::iterator it = g_InstanceList.begin(); it != g_InstanceList.end(); ++it)
{
if (*it == this)
continue;
if (m_pIBon == (*it)->m_pIBon)
{
(*it)->m_dwSpace = dwReqSpace;
(*it)->m_dwChannel = dwReqChannel;
// 対象インスタンスがまだ一度もSetChannel()を行っていなかった場合
if ((*it)->m_hTsRead == 0)
{
// 強制的に配信開始
(*it)->m_bTunerOpen = TRUE;
(*it)->m_hTsRead = m_hTsRead;
(*it)->m_pTsReaderArg = m_pTsReaderArg;
if (m_hTsRead)
{
m_pTsReaderArg->TsLock.Enter();
m_pTsReaderArg->TsReceiversList.push_back(*it);
m_pTsReaderArg->TsLock.Leave();
}
}
}
}
}
}
else
makePacket(eSetChannel2, (DWORD)0xff);
}
}
}
break;
}
case eGetTotalDeviceNum:
makePacket(eGetTotalDeviceNum, GetTotalDeviceNum());
break;
case eGetActiveDeviceNum:
makePacket(eGetActiveDeviceNum, GetActiveDeviceNum());
break;
case eSetLnbPower:
{
if (pPh->GetBodyLength() != sizeof(BYTE))
makePacket(eSetLnbPower, FALSE);
else
makePacket(eSetLnbPower, SetLnbPower((BOOL)(pPh->m_pPacket->payload[0])));
break;
}
case eGetClientInfo:
{
union {
sockaddr_storage ss;
sockaddr_in si4;
sockaddr_in6 si6;
};
char addr[INET6_ADDRSTRLEN], buf[2048], info[4096], *p, *exinfo;
int port, len, num = 0;
size_t left, size;
socklen_t slen;
p = info;
p[0] = '\0';
left = size = sizeof(info);
exinfo = NULL;
std::list<cProxyServerEx *>::iterator it = g_InstanceList.begin();
while (it != g_InstanceList.end())
{
slen = sizeof(ss);
if (::getpeername((*it)->m_s, (sockaddr *)&ss, &slen) == 0)
{
if (ss.ss_family == AF_INET)
{
// IPv4
::inet_ntop(AF_INET, &(si4.sin_addr), addr, sizeof(addr));
port = ntohs(si4.sin_port);
}
else
{
// IPv6
::inet_ntop(AF_INET6, &(si6.sin6_addr), addr, sizeof(addr));
port = ntohs(si6.sin6_port);
}
}
else
{
::strcpy(addr, "unknown host...");
port = 0;
}
std::vector<stDriver> &vstDriver = DriversMap[(*it)->m_pDriversMapKey];
len = ::sprintf(buf, "%02d: [%s]:[%d] / [%s][%s] / space[%u] ch[%u]\n", num, addr, port, (*it)->m_pDriversMapKey, vstDriver[(*it)->m_iDriverNo].strBonDriver, (*it)->m_dwSpace, (*it)->m_dwChannel);
if ((size_t)len >= left)
{