-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathth-ecm.c
704 lines (628 loc) · 21.6 KB
/
th-ecm.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
void clients_check_sendcw(ECM_DATA *ecm)
{
#ifdef TESTCHANNEL
int testchannel = ( (ecm->caid==cfg.testchn.caid) && (ecm->provid==cfg.testchn.provid) && (ecm->sid==cfg.testchn.sid) );
if (testchannel) {
char dump[64];
array2hex( ecm->cw, dump, 16);
mlogf(LOGINFO,0," Send DCW to clients ch %04x:%06x:%04x/%02x:%08x => %s\n", ecm->caid, ecm->provid, ecm->sid, ecm->ecm[0], ecm->hash, dump);
}
#endif
cs_check_sendcw(ecm);
#ifdef MGCAMD_SRV
mg_check_sendcw(ecm);
#endif
#ifdef CCCAM_SRV
cc_check_sendcw(ecm);
#endif
#ifdef FREECCCAM_SRV
freecccam_check_sendcw(ecm);
#endif
#ifdef CS378X_SRV
cs378x_check_sendcw(ecm);
#endif
#ifdef CAMD35_SRV
camd35_check_sendcw(ecm);
#endif
}
#include "loadbalance.c"
#include "setdcw.c"
///////////////////////////////////////////////////////////////////////////////
// Check sending ecm to servers
///////////////////////////////////////////////////////////////////////////////
void wakeup_sendecm() // not needed in mono-thread
{
pipe_wakeup( prg.pipe.ecm[1] );
}
void ecm_faileddcw( ECM_DATA *ecm )
{
ecm->dcwstatus = STAT_DCW_FAILED;
ecm->checktime = 0;
ecm->waitserver = 0;
sid_newecm(ecm);
clients_check_sendcw(ecm); // send decode failed to clients
#ifdef TESTCHANNEL
int testchannel = ( (ecm->caid==cfg.testchn.caid) && (ecm->provid==cfg.testchn.provid) && (ecm->sid==cfg.testchn.sid) );
if (testchannel) {
mlogf(LOGINFO,0," <Decode_Failed> ch %04x:%06x:%04x/%02x:%08x (%s)\n", ecm->caid, ecm->provid, ecm->sid, ecm->ecm[0], ecm->hash, ecm->statusmsg);
}
#endif
}
void check_ecm(ECM_DATA *ecm, uint32_t ticks)
{
ecm->checktime = 0; // invalid
ecm->waitserver = 0;
// CACHE(fallowcache = 1)
if (ecm->dcwstatus==STAT_DCW_WAITCACHE) {
struct cardserver_data *cs = ecm->cs;
if (!cs) {
ecm->statusmsg = "Invalid profile id";
ecm_faileddcw( ecm );
return;
}
if (cs->option.fallowcache) {
if (!ecm->waitcache) { // Not done
if (cs->option.fallowcache) {
pipe_cache_find(ecm, cs);
ecm->waitcache = 1;
ecm->checktime = ecm->recvtime + cs->option.cachetimeout; // wait for cache
}
else ecm->dcwstatus = STAT_DCW_WAIT;
}
else {
if ( ( (ecm->recvtime+cs->option.cachetimeout)<=ticks ) ) ecm->dcwstatus = STAT_DCW_WAIT;
else {
//mlogf(LOGDEBUG,0," Wait for cache\n");
ecm->checktime = ecm->recvtime+cs->option.cachetimeout;
}
}
}
else ecm->dcwstatus = STAT_DCW_WAIT;
}
// SEND ECM
if ( (ecm->dcwstatus==STAT_DCW_WAIT) ) {
// Check Profile
struct cardserver_data *cs = ecm->cs;
if (!cs) {
ecm->statusmsg = "Invalid profile id";
ecm_faileddcw( ecm );
return;
}
//check for decode failed
// Check for Max used Servers
if ( (cs->option.server.max>0) && (ecm->server_totalsent>=cs->option.server.max) ) {
if (!ecm->server_totalwait) {
ecm->statusmsg = "Decode failed, max servers is reached and no more servers to wait";
ecm_faileddcw( ecm );
// Send DCW to Cache if not sent
if ( cs->option.fallowcache && cs->option.cachesendrep && (ecm->cachestatus!=ECM_CACHE_REP) ) {
//pipe_send_cache_reply(ecm,cs); // Send failed Cache Reply
ecm->cachestatus = ECM_CACHE_REP;
}
}
else ecm->checktime = ecm->recvtime + cs->option.dcw.timeout*ecm->period;
}
// Check for ECM TimeOut
else if ( (ticks-ecm->recvtime) >= cs->option.dcw.timeout*ecm->period ) {
ecm->statusmsg = "Decode failed, dcw timeout";
ecm_faileddcw( ecm );
// Send DCW to Cache if not sent
if ( cs->option.fallowcache && cs->option.cachesendrep && (ecm->cachestatus!=ECM_CACHE_REP) ) {
//pipe_send_cache_reply(ecm,cs); // Send failed Cache Reply
ecm->cachestatus = ECM_CACHE_REP;
}
}
// Check for ECM sending
else if ( (ticks-ecm->recvtime) <= (cs->option.server.timeout*ecm->period) ) { // ~ cfg.cardserver.option.dcw.timeout*2/3 is the cardserver timeout
// Check for cache request
if ( (ecm->cachestatus==ECM_CACHE_NONE) && cs->option.fallowcache && cs->option.cachesendreq ) {
pipe_cache_request(ecm,cs);
ecm->cachestatus = ECM_CACHE_REQ;
}
// check for decode failed with no remaining server to wait, send to new cardserver
if ( !ecm->server_totalwait
|| (cs->option.server.first>ecm->server_totalsent)
|| ((ticks-ecm->lastsendtime)>=cs->option.server.interval)
) {
//mlogf(LOGDEBUG,0," check_sendecm[%s] ch %04x:%06x:%04x\n", cs->name,ecm->caid,ecm->provid,ecm->sid);
struct server_data *newsrv = NULL;
if ( srvtab_arrange(cs, ecm, ecm->server_totalsent > 0 )==-1 ) { // No more servers to decode
//mlogf(LOGDEBUG,0," sendecm: no servers found to decode\n");
/*#ifdef PUBLIC
if (!ecm->server_totalwait) {
ecm->statusmsg = "Decode failed, No servers found to decode";
ecm_faileddcw( ecm );
// Send DCW to Cache if not sent
if ( cs->option.fallowcache && cs->option.cachesendrep && (ecm->cachestatus!=ECM_CACHE_REP) ) {
//pipe_send_cache_reply(ecm,cs); //Send Failed Cache Reply
ecm->cachestatus=ECM_CACHE_REP;
}
}
else
#endif */
ecm->checktime = ecm->recvtime + cs->option.dcw.timeout*ecm->period;
return;
}
else if (psrvlist[0] && psrvlist[0]->srv) {
// SEND ECM
newsrv = psrvlist[0]->srv;
newsrv->busycard = psrvlist[0]->card; // dont save pointers!!!
newsrv->busycardid = psrvlist[0]->shareid;
//mlogf(LOGDEBUG,0," sendecm: selected server to decode (%s:%d)\n", newsrv->host->name, newsrv->port);
if (newsrv->type==TYPE_NEWCAMD) {
if (cs_sendecm_srv(cs, newsrv, ecm)>0) {
ecm->lastsendtime = ticks;
mlogf(LOGINFO,getdbgflagpro(DBG_SERVER,0,newsrv->id,cs->id)," -> ecm to Newcamd server%d (%s:%d) ch %04x:%06x:%04x:%08x\n",(1+ecm->server_totalsent),newsrv->host->name,newsrv->port,ecm->caid,ecm->provid,ecm->sid,ecm->hash);
newsrv->lastecmtime = ticks;
newsrv->ecmnb++;
newsrv->busy=1;
newsrv->ecm.request = ecm;
newsrv->ecm.hash = ecm->hash;
newsrv->retry=0;
ecm_addsrv(ecm, newsrv->id);
ecm_addsrvip(ecm, newsrv->host->ip);
}
}
#ifdef CCCAM_CLI
else if (newsrv->type==TYPE_CCCAM) {
if (cc_sendecm_srv(newsrv, ecm)>0) {
ecm->lastsendtime = ticks;
mlogf(LOGINFO,getdbgflagpro(DBG_SERVER,0,newsrv->id,cs->id)," -> ecm to CCcam server%d (%s:%d) ch %04x:%06x:%04x:%08x\n",(1+ecm->server_totalsent),newsrv->host->name,newsrv->port,ecm->caid,ecm->provid,ecm->sid,ecm->hash);
newsrv->lastecmtime = ticks;
newsrv->ecmnb++;
struct cs_card_data *card = cc_getcardbyid( newsrv, newsrv->busycardid );
if (card) card->ecmnb++;
newsrv->busy=1;
newsrv->ecm.request = ecm;
newsrv->ecm.hash = ecm->hash;
newsrv->retry=0;
ecm_addsrv(ecm, newsrv->id);
ecm_addsrvip(ecm, newsrv->host->ip);
}
}
#endif
#ifdef RADEGAST_CLI
else if (newsrv->type==TYPE_RADEGAST) {
if (rdgd_sendecm_srv(newsrv, ecm)>0) {
ecm->lastsendtime = ticks;
mlogf(LOGINFO,getdbgflagpro(DBG_SERVER,0,newsrv->id,cs->id)," -> ecm to Radegast server%d (%s:%d) ch %04x:%06x:%04x:%08x\n",(1+ecm->server_totalsent),newsrv->host->name,newsrv->port,ecm->caid,ecm->provid,ecm->sid,ecm->hash);
newsrv->lastecmtime = ticks;
newsrv->ecmnb++;
newsrv->busy=1;
newsrv->ecm.request = ecm;
newsrv->ecm.hash = ecm->hash;
newsrv->retry=0;
ecm_addsrv(ecm, newsrv->id);
ecm_addsrvip(ecm, newsrv->host->ip);
}
}
#endif
#ifdef CAMD35_CLI
else if (newsrv->type==TYPE_CAMD35) {
if (camd35_sendecm_srv(newsrv, ecm)>0) {
ecm->lastsendtime = ticks;
mlogf(LOGINFO,getdbgflagpro(DBG_SERVER,0,newsrv->id,cs->id)," -> ecm to camd35 server%d (%s:%d) ch %04x:%06x:%04x:%08x\n",(1+ecm->server_totalsent),newsrv->host->name,newsrv->port,ecm->caid,ecm->provid,ecm->sid,ecm->hash);
newsrv->lastecmtime = ticks;
newsrv->ecmnb++;
newsrv->busy=1;
newsrv->ecm.request = ecm;
newsrv->ecm.hash = ecm->hash;
newsrv->retry=0;
ecm_addsrv(ecm, newsrv->id);
ecm_addsrvip(ecm, newsrv->host->ip);
}
}
#endif
#ifdef CS378X_CLI
else if (newsrv->type==TYPE_CS378X) {
if (cs378x_sendecm_srv(newsrv, ecm)>0) {
ecm->lastsendtime = ticks;
mlogf(LOGINFO,getdbgflagpro(DBG_SERVER,0,newsrv->id,cs->id)," -> ecm to cs378x server%d (%s:%d) ch %04x:%06x:%04x:%08x\n",(1+ecm->server_totalsent),newsrv->host->name,newsrv->port,ecm->caid,ecm->provid,ecm->sid,ecm->hash);
newsrv->lastecmtime = ticks;
newsrv->ecmnb++;
newsrv->busy=1;
newsrv->ecm.request = ecm;
newsrv->ecm.hash = ecm->hash;
newsrv->retry=0;
ecm_addsrv(ecm, newsrv->id);
ecm_addsrvip(ecm, newsrv->host->ip);
}
}
#endif
ecm->statusmsg = "Waiting for servers...";
if ( cs->option.server.first > (ecm->server_totalsent+1) ) ecm->checktime = ticks + 10;
else {
ecm->checktime = ticks + cs->option.server.interval;
if ( (ecm->checktime-ecm->recvtime) > (cs->option.server.timeout*ecm->period) ) ecm->checktime = ecm->recvtime + cs->option.dcw.timeout*ecm->period;
}
}
else {
ecm->statusmsg = "Wait for available servers";
//mlogf(LOGDEBUG, getdbgflag(DBG_NEWCAMD,cs->id,0), " check_sendecm[%s] ch %04x:%06x:%04x, Wait for available server...\n", cs->name,ecm->caid,ecm->provid,ecm->sid);
// PROBLEM HERE ??????
ecm->waitserver = 1; // XXX
ecm->checktime = ecm->recvtime + cs->option.dcw.timeout*ecm->period; // till the end if there is no server
#ifdef BUSY_SERVER
if (!ecm->server_totalwait && !ecm->server_totalsent) {
ecm->statusmsg = "Decode failed, no free server";
ecm_faileddcw( ecm );
cs->ecmbusysrv++;
}
#endif
return;
}
}
else {
ecm->checktime = ecm->lastsendtime + cs->option.server.interval;
if ( (ecm->checktime-ecm->recvtime) > (cs->option.server.timeout*ecm->period) ) ecm->checktime = ecm->recvtime + cs->option.dcw.timeout*ecm->period;
}
}
#ifndef PUBLIC
else if (!ecm->server_totalwait) {
ecm->statusmsg = "Decode failed, no server open this channel";
ecm_faileddcw( ecm );
// Send DCW to Cache if not sent
if ( cs->option.fallowcache && cs->option.cachesendrep && (ecm->cachestatus!=ECM_CACHE_REP) ) {
//pipe_send_cache_reply(ecm,cs); //Send Failed Cache Reply
ecm->cachestatus = ECM_CACHE_REP;
}
}
else if ( cs->option.fallowcache && cs->option.cacheresendreq && (ecm->cachestatus==ECM_CACHE_REQ) ) {
pipe_cache_resendreq(ecm, cs);
ecm->cachestatus = ECM_CACHE_REQ2;
ecm->checktime = ecm->recvtime + cs->option.dcw.timeout*ecm->period;
}
#endif
else ecm->checktime = ecm->recvtime + cs->option.dcw.timeout*ecm->period;
}
}
inline void check_sendecm(int newserver)
{
uint32_t ticks = GetTickCount();
struct ecm_request *req = ecmdata;
while (req) {
if ( (req->recvtime+TIME_ECMALIVE) < ticks ) break;
if (req->checktime) { // (checktime==0) --> do nothing
if ( (req->checktime < ticks) || (req->waitserver && newserver) ) {
check_ecm(req, ticks);
ticks = GetTickCount();
}
}
req = req->next;
if (req==ecmdata) break;
}
}
/// recalculate ecmcheck wakeup time
uint32_t getecmwakeuptime()
{
uint32_t ticks = GetTickCount();
uint32_t waketime = ticks+10000;
struct ecm_request *req = ecmdata;
while (req) {
if ( (req->recvtime+TIME_ECMALIVE) < ticks ) break;
if ( req->checktime )
if ( waketime > req->checktime ) waketime = req->checktime;
req = req->next;
if (req==ecmdata) break;
}
return waketime;
}
///////////////////////////////////////////////////////////////////////////////
// CACHE PIPE RECV MESSAGES
///////////////////////////////////////////////////////////////////////////////
int newserver = 0;
void recv_ecm_pipe()
{
uint8_t buf[1024];
struct cache_data req;
uint8_t cw[16];
int peerid;
ECM_DATA *ecm;
struct pollfd pfd;
do {
int len = pipe_recv( prg.pipe.ecm[0], buf);
if (len>0) {
switch(buf[0]) {
case PIPE_WAKEUP:
break;
case PIPE_LOCK:
pthread_mutex_lock(&prg.lockmain);
pthread_mutex_unlock(&prg.lockmain);
break;
case PIPE_CACHE_FIND_FAILED:
get_cache2ecm(buf, &req, NULL);
pthread_mutex_lock(&prg.lockecm);
ecm = req.ecm; //search_ecmdata_byhash( req.caid, req.sid, req.hash );
if (ecm) {
if ( (ecm->caid==req.caid)&&(ecm->hash==req.hash)&&(ecm->sid==req.sid)&&(ecm->dcwstatus==STAT_DCW_WAITCACHE) ) {
#ifndef PUBLIC
struct cardserver_data *cs = ecm->cs;
if ( cs && (!cs->option.cachestatic) )
#endif
ecm->dcwstatus = STAT_DCW_WAIT;
ecm->checktime = ecm->recvtime;
}
}
pthread_mutex_unlock(&prg.lockecm);
break;
case PIPE_CACHE_FIND_SUCCESS: // SET DCW
peerid = get_cache2ecm(buf, &req, cw);
pthread_mutex_lock(&prg.lockecm);
ecm = req.ecm; //search_ecmdata_byhash( req.caid, req.sid, req.hash );
if (ecm) {
if ( (ecm->caid==req.caid)&&(ecm->hash==req.hash)&&(ecm->sid==req.sid) ) {
struct cardserver_data *cs = ecm->cs;
if (cs && cs->option.fallowcache) {
int isnanoe0=ecm_isnanoe0(ecm->ecm,ecm->caid);
if (!acceptDCW(cw, isnanoe0)) {
mlogf(LOGDEBUG,0," [!] dcw error from cachepeer %d, bad dcw!!! ch %04x:%06x:%04x nanoe0=%d\n",peerid,ecm->caid, ecm->provid, ecm->sid, isnanoe0);
}
else
ecm_setdcw( ecm, cw, DCW_SOURCE_CACHE, peerid );
}
}
}
pthread_mutex_unlock(&prg.lockecm);
break;
case PIPE_SRV_CONNECTED:
newserver = 1;
#ifdef EPOLL_ECM
struct server_data *srv;
memcpy( &srv, buf+1, sizeof(void*) );
// Add to events
struct epoll_event ev; // epoll event
ev.events = EPOLLIN;
ev.data.fd = srv->handle;
ev.data.ptr = srv;
if ( epoll_ctl(prg.epoll.ecm, EPOLL_CTL_ADD, srv->handle, &ev) == -1 ) mlogf(LOGERROR,DBG_ERROR,"Err! EPOLL_CTL_ADD %s (%d)\n", srv->user, srv->handle);
//else mlogf(LOGDEBUG,0,"EPOLL_CTL_ADD %s (%d)\n", cli->user, cli->handle);
#endif
break;
case PIPE_SRV_AVAILABLE:
newserver = 1;
break;
}
}
pfd.fd = prg.pipe.ecm[0];
pfd.events = POLLIN | POLLPRI;
} while ( poll(&pfd, 1, 0)>0 );
}
///////////////////////////////////////////////////////////////////////////////
// RECEIVE MESSAGES THREAD
///////////////////////////////////////////////////////////////////////////////
inline void srv_recvmsg( struct server_data *srv )
{
if (srv->type==TYPE_NEWCAMD) cs_srv_recvmsg(srv);
#ifdef CCCAM_CLI
else if (srv->type==TYPE_CCCAM) cc_srv_recvmsg(srv);
#endif
#ifdef RADEGAST_CLI
else if (srv->type==TYPE_RADEGAST) rdgd_srv_recvmsg(srv);
#endif
#ifdef CAMD35_CLI
else if (srv->type==TYPE_CAMD35) camd35_srv_recvmsg(srv);
#endif
#ifdef CS378X_CLI
else if (srv->type==TYPE_CS378X) cs378x_srv_recvmsg(srv);
#endif
}
#ifdef EPOLL_ECM
void *recv_msg_thread(void *param)
{
#ifndef PUBLIC
prg.pid_msg = syscall(SYS_gettid);
prg.tid_msg = pthread_self();
prctl(PR_SET_NAME,"ECM Thread",0,0,0);
#endif
struct epoll_event evlist[MAX_EPOLL_EVENTS]; // epoll recv events
prg.epoll.ecm = epoll_create( MAX_EPOLL_EVENTS );
// Add PIPE
struct epoll_event ev; // epoll event
ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP;
ev.data.ptr = NULL;
if ( epoll_ctl( prg.epoll.ecm, EPOLL_CTL_ADD, prg.pipe.ecm[0], &ev) == -1 ) mlogf(LOGERROR,0,"epoll_ctl ecm recv_msg_thread error -1\n");
while (!prg.restart) {
newserver = 0;
// getmintime
uint32_t mintime = ecm_check_time;
uint32_t ticks = GetTickCount();
uint32_t ms;
if (mintime>(ticks+10)) ms = mintime-ticks; else ms = 10;
int ready = epoll_wait( prg.epoll.ecm, evlist, MAX_EPOLL_EVENTS, ms);
if (ready == -1) {
if ( (errno==EINTR)||(errno==EAGAIN) ) {
usleep(1000);
continue;
}
else {
usleep(99000);
mlogf(LOGERROR,DBG_ERROR,"Err! epoll_wait (%d)", errno);
}
}
int i;
for (i=0; i < ready; i++) {
if ( evlist[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR) ) { // EPOLLRDHUP
if (evlist[i].data.ptr == NULL) mlogf(LOGERROR,DBG_ERROR,"Err! epoll_wait() pipe\n"); // error !!!
else disconnect_srv(evlist[i].data.ptr);
}
else if ( evlist[i].events & (EPOLLIN|EPOLLPRI) ) {
if (evlist[i].data.ptr == NULL) recv_ecm_pipe();
else {
pthread_mutex_lock(&prg.locksrv);
srv_recvmsg(evlist[i].data.ptr);
pthread_mutex_unlock(&prg.locksrv);
}
}
}
pthread_mutex_lock(&prg.locksrv);
pthread_mutex_lock(&prg.lockecm);
check_sendecm( newserver );
ecm_check_time = getecmwakeuptime();
pthread_mutex_unlock(&prg.lockecm);
pthread_mutex_unlock(&prg.locksrv);
usleep(cfg.delay.thread);
}
return NULL;
}
#else
void *recv_msg_thread(void *param)
{
struct pollfd pfd[MAX_PFD];
int pfdcount;
#ifndef PUBLIC
prg.pid_msg = syscall(SYS_gettid);
prg.tid_msg = pthread_self();
prctl(PR_SET_NAME,"ECM Thread",0,0,0);
#endif
while (!prg.restart) {
// getmintime
uint32_t mintime = ecm_check_time;
newserver = 0;
uint32_t ticks = GetTickCount();
uint32_t ms;
if (mintime>(ticks+10)) ms = mintime-ticks; else ms = 10;
pfdcount = 0;
// Cache Data// Clients/Servers WakeUP
pfd[pfdcount].fd = prg.pipe.ecm[0];
pfd[pfdcount++].events = POLLIN | POLLPRI;
//Servers
struct server_data *srv = cfg.server;
while (srv && (pfdcount<SERVER_MAX_PFD)) {
if ( !IS_DISABLED(srv->flags)&&(srv->handle>0) ) {
srv->ipoll = pfdcount;
pfd[pfdcount].fd = srv->handle;
pfd[pfdcount++].events = POLLIN | POLLPRI;
} else srv->ipoll = -1;
srv = srv->next;
}
int retval = poll(pfd, pfdcount, ms);
if ( retval>0 ) {
/// SERVERS
pthread_mutex_lock(&prg.locksrv);
struct server_data *srv = cfg.server;
while (srv) {
if ( !IS_DISABLED(srv->flags)&&(srv->handle>0)&&(srv->ipoll>=0)&&(srv->handle==pfd[srv->ipoll].fd) ) {
if ( pfd[srv->ipoll].revents & (POLLHUP|POLLNVAL) ) disconnect_srv(srv);
else if ( pfd[srv->ipoll].revents & (POLLIN|POLLPRI) ) srv_recvmsg(srv);
}
srv = srv->next;
}
pthread_mutex_unlock(&prg.locksrv);
//
if ( pfd[0].revents & (POLLIN|POLLPRI) ) recv_ecm_pipe();
}
else if ( (retval<0) && (errno!=EINTR) ) {
mlogf(LOGERROR,0," thread receive messages: poll error %d(errno=%d)\n", retval, errno);
usleep(91000);
}
pthread_mutex_lock(&prg.locksrv);
pthread_mutex_lock(&prg.lockecm);
check_sendecm( newserver );
ecm_check_time = getecmwakeuptime();
pthread_mutex_unlock(&prg.lockecm);
pthread_mutex_unlock(&prg.locksrv);
usleep(cfg.delay.thread*2);
}
return NULL;
}
#endif
// Check for keepalive with servers/clients
void *thread_keepalive(void *param)
{
while (1) {
sleep(5);
uint32_t ticks = GetTickCount();
// Check Servers
struct server_data *srv = cfg.server;
while (srv) {
if ( !IS_DISABLED(srv->flags)&&(srv->connection.status>0) ) {
if (srv->type==TYPE_NEWCAMD) cs_check_keepalive(srv);
else if (srv->type==TYPE_CCCAM) {
if ( !srv->keepalive.status && ((srv->keepalive.time+75000)<ticks) ) {
srv->keepalive.status = 1; // Sent and waiting for reply
srv->keepalive.time = ticks;
if ( !cc_msg_send( srv->handle, &srv->sendblock, CC_MSG_KEEPALIVE, 0, NULL) ) disconnect_srv( srv );
}
}
#ifdef CAMD35_CLI
else if (srv->type==TYPE_CAMD35) {
if ( !srv->keepalive.status && ((srv->keepalive.time+30000)<ticks) ) {
camd35_send_keepalive(srv);
srv->keepalive.status = 1; // Sent and waiting for reply
srv->keepalive.time = ticks;
}
else if ( (srv->keepalive.status==1) && ((srv->keepalive.time+10000)<ticks) ) {
camd35_send_keepalive(srv);
srv->keepalive.status = 2;
srv->keepalive.time = ticks;
}
else if ( (srv->keepalive.status>1) && ((srv->keepalive.time+10000)<ticks) ) {
mlogf(LOGWARNING,getdbgflag(DBG_SERVER,0,srv->id)," ??? no keepalive response from camd35 server (%s:%d)\n",srv->host->name,srv->port);
disconnect_srv( srv );
}
}
#endif
}
srv = srv->next;
}
// Check Cacheex Servers
srv = cfg.cacheexserver;
while (srv) {
if ( !IS_DISABLED(srv->flags)&&(srv->connection.status>0) ) {
if (srv->type==TYPE_CCCAM) {
if ( !srv->keepalive.status && ((srv->keepalive.time+75000)<ticks) ) {
srv->keepalive.status = 1; // Sent and waiting for reply
srv->keepalive.time = ticks;
if ( !cc_msg_send( srv->handle, &srv->sendblock, CC_MSG_KEEPALIVE, 0, NULL) ) disconnect_srv( srv );
}
}
#ifdef CAMD35_CLI
else if (srv->type==TYPE_CAMD35) {
if ( !srv->keepalive.status && ((srv->keepalive.time+30000)<ticks) ) {
camd35_send_keepalive(srv);
srv->keepalive.status = 1; // Sent and waiting for reply
srv->keepalive.time = ticks;
}
else if ( (srv->keepalive.status==1) && ((srv->keepalive.time+10000)<ticks) ) {
camd35_send_keepalive(srv);
srv->keepalive.status = 2;
srv->keepalive.time = ticks;
}
else if ( (srv->keepalive.status>1) && ((srv->keepalive.time+10000)<ticks) ) {
mlogf(LOGWARNING,getdbgflag(DBG_SERVER,0,srv->id)," ??? no keepalive response from camd35 server (%s:%d)\n",srv->host->name,srv->port);
disconnect_srv( srv );
}
}
#endif
}
srv = srv->next;
}
#ifdef CAMD35_SRV
sleep(1);
// Check camd35 Clients
ticks = GetTickCount();
struct camd35_server_data *camd35 = cfg.camd35.server;
while (camd35) {
struct camd35_client_data *cli = camd35->client;
while (cli) {
if (cli->connection.status>0) {
if ( (cli->lastactivity+300000) < ticks) {
camd35_disconnect_cli(cli);
}
}
cli = cli->next;
}
camd35 = camd35->next;
}
#endif
}
}
int start_thread_recv_msg()
{
create_thread(&prg.tid_msg, (threadfn)recv_msg_thread,NULL);
create_thread(&prg.tid_msg, (threadfn)thread_keepalive,NULL);
#ifdef THREAD_DCW
create_thread(&prg.tid_msg, (threadfn)setdcw_thread,NULL);
#endif
return 0;
}