SASC-NG
DVBSoftCAM

cMsgCache Class Reference

#include <system.h>

List of all members.

Public Member Functions

 cMsgCache (int NumCache, int StoreSize)
 ~cMsgCache ()
int Cache (int id, bool result, const unsigned char *store)
int Get (const unsigned char *msg, int len, unsigned char *store)
void Clear (void)
void SetMaxFail (int max)

Detailed Description

Definition at line 201 of file system.h.


Constructor & Destructor Documentation

cMsgCache::cMsgCache ( int  NumCache,
int  StoreSize 
)

Definition at line 451 of file system.c.

References Clear(), L_GEN_ERROR, MALLOC, and PRINTF.

{
  numCache=NumCache;
  storeSize=StoreSize;
  ptr=0; stores=0; maxFail=2;
  caches=MALLOC(struct Cache,numCache);
  if(caches) {
    Clear();
    if(storeSize>0) {
      stores=MALLOC(unsigned char,numCache*storeSize);
      if(!stores) PRINTF(L_GEN_ERROR,"msgcache: no memory for store area");
      }
    }
  else PRINTF(L_GEN_ERROR,"msgcache: no memory for cache");
}
cMsgCache::~cMsgCache ( )

Definition at line 467 of file system.c.

References free.

{
  free(caches);
  free(stores);
}

Member Function Documentation

int cMsgCache::Cache ( int  id,
bool  result,
const unsigned char *  store 
)

Definition at line 553 of file system.c.

References cCondVar::Broadcast(), FAIL1, FAIL2, FAILN, FREE, GOOD, L_CORE_MSGCACHE, LBEND, LBPUT, LBSTARTF, MASK, Cache::mode, and WAIT.

Referenced by cEcmHandler::Process(), cSystemCardClient::ProcessECM(), cCardClientRadegast::ProcessEMM(), cCardClientNewCamd::ProcessEMM(), cCardClientCCcam2::ProcessEMM(), cCardClientCamd35::ProcessEMM(), cCardClientCommon::ProcessEMM(), and cCardClientAroureos::ProcessEMM().

{
  cMutexLock lock(&mutex);
  if(id<1 || !caches || (storeSize>0 && !stores)) return 0; // sanity
  struct Cache *s=&caches[id-1];
  LBSTARTF(L_CORE_MSGCACHE);
  LBPUT("%d/%p: de-queued msg with id=%d ",getpid(),this,id);
  if(s->mode&WAIT) wait.Broadcast();
  if(result) {
    if(store && storeSize>0)
      memcpy(&stores[(id-1)*storeSize],store,storeSize);
    s->mode=GOOD;
    LBPUT("(GOOD)");
    return 0;
    }
  else {
    int m=s->mode&MASK;
    if(m==GOOD)
      m=s->mode=FREE;
    if(m==FREE || (m>=FAIL1 && m<=FAIL2)) {
      s->mode=(s->mode&MASK)+1;
      if(s->mode<maxFail)
        LBPUT("(FAIL%d)",s->mode);
      else
        m=s->mode=FAILN;
      }
    if(m==FAILN)
      LBPUT("(FAILN)");
    return s->mode;
    }
  LBEND();
}
void cMsgCache::Clear ( void  )

Definition at line 479 of file system.c.

References L_CORE_MSGCACHE, and PRINTF.

Referenced by cMsgCache(), and cEcmHandler::Process().

{
  cMutexLock lock(&mutex);
  memset(caches,0,sizeof(struct Cache)*numCache);
  ptr=0;
  PRINTF(L_CORE_MSGCACHE,"%d/%p: clear",getpid(),this);
}
int cMsgCache::Get ( const unsigned char *  msg,
int  len,
unsigned char *  store 
)

Definition at line 504 of file system.c.

References Cache::crc, crc32_le(), FAIL1, FAIL2, GOOD, id, L_CORE_MSGCACHE, Cache::mode, PRINTF, QUEUED, cCondVar::Wait(), and WAIT.

Referenced by cEcmHandler::Process(), cSystemCardClient::ProcessECM(), cCardClientRadegast::ProcessEMM(), cCardClientNewCamd::ProcessEMM(), cCardClientCCcam2::ProcessEMM(), cCardClientCamd35::ProcessEMM(), cCardClientCommon::ProcessEMM(), and cCardClientAroureos::ProcessEMM().

{
  unsigned char md[16];
  int crc=crc32_le(0,MD5(msg,len,md),16);
  cMutexLock lock(&mutex);
  if(!caches || (storeSize>0 && !stores)) return -1; // sanity
  struct Cache *s;
  while((s=FindMsg(crc))) {
    if(!(s->mode&QUEUED)) break;
    s->mode|=WAIT;
    PRINTF(L_CORE_MSGCACHE,"%d/%p: msg already queued. waiting to complete",getpid(),this);
    wait.Wait(mutex);
    }
  int id;
  if(!s) {
    while(1) {
      s=&caches[ptr];
      if(!(s->mode&QUEUED)) break;
      s->mode|=WAIT;
      PRINTF(L_CORE_MSGCACHE,"%d/%p: queue overwrite protection id=%d",getpid(),this,ptr+1);
      wait.Wait(mutex); // don't overwrite queued msg's
      }
    id=ptr+1;
    s->crc=crc;
    s->mode=QUEUED;
    PRINTF(L_CORE_MSGCACHE,"%d/%p: queued msg with id=%d",getpid(),this,id);
    ptr++; if(ptr>=numCache) { ptr=0; PRINTF(L_CORE_MSGCACHE,"msgcache: roll-over (%d)",numCache); }
    return id;
    }
  else {
    id=(s-&caches[0])+1;
    if(s->mode==GOOD) {
      if(store && storeSize>0)
        memcpy(store,&stores[(id-1)*storeSize],storeSize);
      PRINTF(L_CORE_MSGCACHE,"%d/%p: msg is cached as GOOD (%d)",getpid(),this,id);
      return 0;
      }
    else if(s->mode>=FAIL1 && s->mode<=FAIL2) {
      PRINTF(L_CORE_MSGCACHE,"%d/%p: msg is cached as FAIL%d (%d)",getpid(),this,s->mode,id);
      s->mode|=QUEUED;
      return id;
      }
    else {
      PRINTF(L_CORE_MSGCACHE,"%d/%p: msg is cached as FAILN (%d)",getpid(),this,id);
      return -1;
      }
    }
}
void cMsgCache::SetMaxFail ( int  max)

Definition at line 473 of file system.c.

References FAILN, L_CORE_MSGCACHE, min(), and PRINTF.

{
  maxFail=min(maxfail,FAILN);
  PRINTF(L_CORE_MSGCACHE,"%d/%p: maxFail set to %d",getpid(),this,maxFail);
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines