SASC
DVBSoftCAM

cNetSocket Class Reference

#include <network.h>

Inheritance diagram for cNetSocket:

List of all members.

Public Member Functions

 cNetSocket (void)
 ~cNetSocket ()
bool Connect (const char *Hostname, int Port, int timeout=-1)
bool Bind (const char *Hostname, int Port)
void Disconnect (void)
int Read (unsigned char *data, int len, int timeout=-1)
int Write (const unsigned char *data, int len, int timeout=-1)
int SendTo (const char *Host, int Port, const unsigned char *data, int len, int timeout=-1)
void Flush (void)
bool Connected (void) const
void SetQuietLog (bool ql)
void SetConnectTimeout (int to)
void SetRWTimeout (int to)
void SetIdleTimeout (int to)
void SetUDP (bool u)
 cNetSocket (void)
 ~cNetSocket ()
bool Connect (const char *Hostname, int Port, int timeout=-1)
bool Bind (const char *Hostname, int Port)
void Disconnect (void)
int Read (unsigned char *data, int len, int timeout=-1)
int Write (const unsigned char *data, int len, int timeout=-1)
int SendTo (const char *Host, int Port, const unsigned char *data, int len, int timeout=-1)
void Flush (void)
bool Connected (void) const
void SetQuietLog (bool ql)
void SetConnectTimeout (int to)
void SetRWTimeout (int to)
void SetIdleTimeout (int to)
void SetUDP (bool u)

Friends

class cNetWatcher

Detailed Description

Definition at line 36 of file network.h.


Constructor & Destructor Documentation

cNetSocket::cNetSocket ( void  )

Definition at line 106 of file network.c.

References DEFAULT_CONNECT_TIMEOUT, DEFAULT_IDLE_TIMEOUT, and DEFAULT_READWRITE_TIMEOUT.

{
  hostname=0; sd=-1; udp=connected=quietlog=false;
  conTimeout=DEFAULT_CONNECT_TIMEOUT; rwTimeout=DEFAULT_READWRITE_TIMEOUT;
  idleTimeout=DEFAULT_IDLE_TIMEOUT;
}
cNetSocket::~cNetSocket ( )

Definition at line 113 of file network.c.

References Disconnect(), and free.

{
  Disconnect();
  free(hostname);
}
cNetSocket::cNetSocket ( void  )
cNetSocket::~cNetSocket ( )

Member Function Documentation

bool cNetSocket::Bind ( const char *  Hostname,
int  Port 
)

Definition at line 202 of file network.c.

References Disconnect(), free, L_CORE_NET, L_GEN_ERROR, cMutex::Lock(), PRINTF, cMutex::Unlock(), and cNetWatcher::Up().

Referenced by cCardClientGbox::Login(), and cCardClientCCcam::Login().

{
  Lock();
  Disconnect();
  if(Hostname) {
    free(hostname);
    hostname=strdup(Hostname); port=Port;
    }
  struct sockaddr_in socketAddr;
  if(GetAddr(&socketAddr,hostname,port) && (sd=GetSocket(udp))>=0) {
    if(!quietlog) PRINTF(L_CORE_NET,"socket: binding to %s:%d/%s (%d.%d.%d.%d)",
               hostname,port,udp?"udp":"tcp",
               (socketAddr.sin_addr.s_addr>> 0)&0xff,(socketAddr.sin_addr.s_addr>> 8)&0xff,(socketAddr.sin_addr.s_addr>>16)&0xff,(socketAddr.sin_addr.s_addr>>24)&0xff);
    int r;
    do { r=bind(sd,(struct sockaddr *)&socketAddr,sizeof(socketAddr)); } while(r<0 && errno==EINTR);
    if(r==0) {
      connected=true;
      Activity(); Unlock(); nw.Up(this);
      return true;
      }
    else PRINTF(L_GEN_ERROR,"socket: bind failed: %s",*StrError(errno));
    }
  Unlock();
  Disconnect();
  return false;
}
bool cNetSocket::Bind ( const char *  Hostname,
int  Port 
)
bool cNetSocket::Connect ( const char *  Hostname,
int  Port,
int  timeout = -1 
)

Definition at line 163 of file network.c.

References Disconnect(), free, L_CORE_NET, L_GEN_ERROR, cMutex::Lock(), PRINTF, cMutex::Unlock(), and cNetWatcher::Up().

Referenced by cCardClientRadegast::Login(), cCardClientNewCamd::Login(), cCardClientCCcam2::Login(), cCardClientCamd35::Login(), cCardClientCommon::Login(), and cCardClientAroureos::Login().

{
  Lock();
  Disconnect();
  if(Hostname) {
    free(hostname);
    hostname=strdup(Hostname); port=Port;
    }
  if(timeout<0) timeout=conTimeout;
  struct sockaddr_in socketAddr;
  if(GetAddr(&socketAddr,hostname,port) && (sd=GetSocket(udp))>=0) {
    if(!quietlog) PRINTF(L_CORE_NET,"connecting to %s:%d/%s (%d.%d.%d.%d)",
               hostname,port,udp?"udp":"tcp",
               (socketAddr.sin_addr.s_addr>> 0)&0xff,(socketAddr.sin_addr.s_addr>> 8)&0xff,(socketAddr.sin_addr.s_addr>>16)&0xff,(socketAddr.sin_addr.s_addr>>24)&0xff);
    int r;
    do { r=connect(sd,(struct sockaddr *)&socketAddr,sizeof(socketAddr)); } while(r<0 && errno==EINTR);
    if(r==0) connected=true;
    else if(errno==EINPROGRESS) {
      if(Select(false,timeout)>0) {
        int r=-1;
        unsigned int l=sizeof(r);
        if(getsockopt(sd,SOL_SOCKET,SO_ERROR,&r,&l)==0) {
          if(r==0)
            connected=true;
          else PRINTF(L_GEN_ERROR,"socket: connect failed (late): %s",*StrError(r));
          }
        else PRINTF(L_GEN_ERROR,"socket: getsockopt failed: %s",*StrError(errno));
        }
      else PRINTF(L_GEN_ERROR,"socket: connect timed out");
      }
    else PRINTF(L_GEN_ERROR,"socket: connect failed: %s",*StrError(errno));

    if(connected) { Activity(); Unlock(); nw.Up(this); return true; }
    }
  Unlock();
  Disconnect();
  return false;
}
bool cNetSocket::Connect ( const char *  Hostname,
int  Port,
int  timeout = -1 
)
bool cNetSocket::Connected ( void  ) const [inline]

Definition at line 60 of file network.h.

{ return connected; }
void cNetSocket::Disconnect ( void  )
void cNetSocket::Disconnect ( void  )

Definition at line 229 of file network.c.

References cNetWatcher::Down().

Referenced by Bind(), Connect(), cCardClient::Logout(), cAuxSrv::Map(), and ~cNetSocket().

{
  nw.Down(this);
  cMutexLock lock(this);
  if(sd>=0) { close(sd); sd=-1; }
  quietlog=connected=false;
}
void cNetSocket::Flush ( void  )
void cNetSocket::Flush ( void  )

Definition at line 237 of file network.c.

Referenced by cCardClientRadegast::ProcessECM(), cCardClientNewCamd::ProcessECM(), cCardClientGbox::ProcessECM(), and cCardClientAroureos::ProcessECM().

{
  cMutexLock lock(this);
  if(sd>=0) {
    unsigned char buff[512];
    while(read(sd,buff,sizeof(buff))>0) Activity();
    }
}
int cNetSocket::Read ( unsigned char *  data,
int  len,
int  timeout = -1 
)

Definition at line 248 of file network.c.

References cTimeMs::Elapsed(), HEXDUMP, L_CORE_NETDATA, L_GEN_DEBUG, L_GEN_ERROR, and PRINTF.

Referenced by cCardClientCCcam::Action(), cCardClientNewCamd::Login(), cAuxSrv::Map(), and cCardClient::RecvMsg().

{
  cMutexLock lock(this);
  if(timeout<0) timeout=rwTimeout;
  bool blockmode=true;
  if(len<0) { len=-len; blockmode=false; }
  else if(len==0) {
    PRINTF(L_GEN_DEBUG,"internal: zero length on socket read");
    errno=EINVAL;
    return -1;
    }
  int cnt=0, r;
  cTimeMs tim;
  do {
    r=timeout;
    if(r>0 && blockmode) { r-=tim.Elapsed(); if(r<10) r=10; }
    r=Select(true,r);
    if(r>0) {
      do { r=read(sd,data+cnt,len-cnt); } while(r<0 && errno==EINTR);
      if(r<0) PRINTF(L_GEN_ERROR,"socket: read failed: %s",*StrError(errno));
      else if(r>0) cnt+=r;
      else {
        PRINTF(L_GEN_ERROR,"socket: EOF on read");
        errno=ECONNRESET;
        }
      }
    } while(r>0 && cnt<len && blockmode);
  Activity();
  if((!blockmode && cnt>0) || cnt>=len) {
    HEXDUMP(L_CORE_NETDATA,data,cnt,"network read");
    return cnt;
    }
  return -1;
}
int cNetSocket::Read ( unsigned char *  data,
int  len,
int  timeout = -1 
)
int cNetSocket::SendTo ( const char *  Host,
int  Port,
const unsigned char *  data,
int  len,
int  timeout = -1 
)
int cNetSocket::SendTo ( const char *  Host,
int  Port,
const unsigned char *  data,
int  len,
int  timeout = -1 
)

Definition at line 307 of file network.c.

References cTimeMs::Elapsed(), HEXDUMP, L_CORE_NETDATA, L_GEN_ERROR, and PRINTF.

Referenced by cCardClientGbox::ProcessECM().

{
  cMutexLock lock(this);
  if(timeout<0) timeout=rwTimeout;
  int cnt=0, r=-1;
  struct sockaddr_in saddr;
  if(GetAddr(&saddr,Host,Port)) {
    cTimeMs tim;
    do {
      r=timeout;
      if(r>0) { r-=tim.Elapsed(); if(r<10) r=10; }
      r=Select(false,r);
      if(r>0) {
        do { r=sendto(sd,data+cnt,len-cnt,0,(struct sockaddr *)&saddr,sizeof(saddr)); } while(r<0 && errno==EINTR);
        if(r<0) PRINTF(L_GEN_ERROR,"socket: sendto %d.%d.%d.%d:%d failed: %s",(saddr.sin_addr.s_addr>> 0)&0xff,(saddr.sin_addr.s_addr>> 8)&0xff,(saddr.sin_addr.s_addr>>16)&0xff,(saddr.sin_addr.s_addr>>24)&0xff,Port,*StrError(errno));
        else if(r>0) cnt+=r;
        }
      } while(r>0 && cnt<len);
    }
  Activity();
  if(cnt>=len) {
    HEXDUMP(L_CORE_NETDATA,data,cnt,"network sendto %d.%d.%d.%d:%d",(saddr.sin_addr.s_addr>> 0)&0xff,(saddr.sin_addr.s_addr>> 8)&0xff,(saddr.sin_addr.s_addr>>16)&0xff,(saddr.sin_addr.s_addr>>24)&0xff,Port);
    return cnt;
    }
  return -1;
}
void cNetSocket::SetConnectTimeout ( int  to) [inline]

Definition at line 62 of file network.h.

{ conTimeout=to; }
void cNetSocket::SetConnectTimeout ( int  to) [inline]

Definition at line 62 of file network.h.

{ conTimeout=to; }
void cNetSocket::SetIdleTimeout ( int  to) [inline]

Definition at line 64 of file network.h.

{ idleTimeout=to; }
void cNetSocket::SetIdleTimeout ( int  to) [inline]

Definition at line 64 of file network.h.

{ idleTimeout=to; }
void cNetSocket::SetQuietLog ( bool  ql) [inline]

Definition at line 61 of file network.h.

Referenced by cCardClientCCcam2::Login(), and cCardClientCCcam::Login().

{ quietlog=ql; }
void cNetSocket::SetQuietLog ( bool  ql) [inline]

Definition at line 61 of file network.h.

{ quietlog=ql; }
void cNetSocket::SetRWTimeout ( int  to) [inline]

Definition at line 63 of file network.h.

{ rwTimeout=to; }
void cNetSocket::SetUDP ( bool  u) [inline]

Definition at line 65 of file network.h.

{ udp=u; }
void cNetSocket::SetUDP ( bool  u) [inline]
int cNetSocket::Write ( const unsigned char *  data,
int  len,
int  timeout = -1 
)
int cNetSocket::Write ( const unsigned char *  data,
int  len,
int  timeout = -1 
)

Definition at line 283 of file network.c.

References cTimeMs::Elapsed(), HEXDUMP, L_CORE_NETDATA, L_GEN_ERROR, and PRINTF.

Referenced by cAuxSrv::Map(), and cCardClient::SendMsg().

{
  cMutexLock lock(this);
  if(timeout<0) timeout=rwTimeout;
  int cnt=0, r;
  cTimeMs tim;
  do {
    r=timeout;
    if(r>0) { r-=tim.Elapsed(); if(r<10) r=10; }
    r=Select(false,r);
    if(r>0) {
      do { r=write(sd,data+cnt,len-cnt); } while(r<0 && errno==EINTR);
      if(r<0) PRINTF(L_GEN_ERROR,"socket: write failed: %s",*StrError(errno));
      else if(r>0) cnt+=r;
      }
    } while(r>0 && cnt<len);
  Activity();
  if(cnt>=len) {
    HEXDUMP(L_CORE_NETDATA,data,cnt,"network write");
    return cnt;
    }
  return -1;
}

Friends And Related Function Documentation

cNetWatcher [friend]

Definition at line 37 of file network.h.


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