CMS 3D CMS Logo

Public Member Functions | Protected Attributes

DTCtcp Class Reference

#include <DTSpyHelper.h>

Inheritance diagram for DTCtcp:
DTSpy

List of all members.

Public Member Functions

DTCtcpAccept ()
unsigned long addr ()
void Connect (unsigned long hostaddr, int port)
void Connect (const char *hostaddr, int port)
int Disconnect ()
 DTCtcp (DTCtcp *)
 DTCtcp (int port)
 DTCtcp (int sock, int opt)
 DTCtcp ()
short Id ()
int Receive (char *buffer, int size)
int Send (char *buffer, int size)
int WaitData (int timeout)
 ~DTCtcp ()

Protected Attributes

struct sockaddr_in clientAddr
int connected
struct sockaddr_in myaddr
int port
int sock

Detailed Description

Definition at line 19 of file DTSpyHelper.h.


Constructor & Destructor Documentation

DTCtcp::DTCtcp ( )

Definition at line 15 of file DTSpyHelper.cc.

Referenced by Accept().

{
   DTCtcp(0);
}
DTCtcp::DTCtcp ( int  port)

Definition at line 20 of file DTSpyHelper.cc.

References connected, myaddr, port, and sock.

{
//  struct sockaddr_in myaddr;
 
    connected=false;

     printf("zeroing...\n");
    bzero ((char *) &myaddr, sizeof(myaddr));
     printf("zeroing done..\n");


   sock = socket (AF_INET, SOCK_STREAM, 0);
     printf("create socket..\n");

   if (sock < 0)
   {
     printf("no socket...\n");
      throw DTtcpExcp(errno);
   }
  

    myaddr.sin_family       = AF_INET;
    myaddr.sin_port         = htons (localport);
 
    //int blen = 65536;
    int blen = 65536*8;

//     printf("setting socket opts buf...\n");
//    if(setsockopt(sock,SOL_SOCKET,SO_SNDBUF,(char *)&blen,sizeof(blen))<0)
//      throw DTtcpExcp(errno);
//    if(setsockopt(sock,SOL_SOCKET,SO_RCVBUF,(char *)&blen,sizeof(blen))<0)
//      throw DTtcpExcp(errno);
     printf("setting socket opts reuse...\n");
    if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&blen,sizeof(blen))<0)
      throw DTtcpExcp(errno);
//     printf("setting socket opts nodelay...\n");
//    if(setsockopt(sock,SOL_SOCKET,TCP_NODELAY,(char *)&blen,sizeof(blen))<0)
//      throw;
     printf("binding...\n");

    port = localport;

    if (port)
        if(bind(sock,(struct sockaddr *)&myaddr,sizeof (myaddr)) < 0)
        { perror ("bind failed");
          throw DTtcpExcp(errno);
        }

          
}
DTCtcp::DTCtcp ( int  sock,
int  opt 
)

Definition at line 71 of file DTSpyHelper.cc.

References connected, port, and sock.

{

   connected = true;
   port =0;

   sock = snew;
}
DTCtcp::DTCtcp ( DTCtcp aconn)

Definition at line 80 of file DTSpyHelper.cc.

References connected, port, and sock.

{

   connected = aconn->connected;
   port = aconn->port;

   sock = aconn->sock;
}
DTCtcp::~DTCtcp ( )

Definition at line 89 of file DTSpyHelper.cc.

References sock.

{
  printf("deleting DTCtcp\n");
  //if (connected) shutdown(sock,2);
  shutdown(sock,SHUT_RDWR);
  close (sock);
}

Member Function Documentation

DTCtcp * DTCtcp::Accept ( )

Definition at line 121 of file DTSpyHelper.cc.

References accept(), clientAddr, DTCtcp(), and sock.

{

 //   struct sockaddr_in  clientAddr; /* client's address */    

    bzero ((char *) &clientAddr, sizeof (clientAddr));

    if (listen (sock, 2) < 0)
        {
        perror ("listen failed");
        throw DTtcpExcp(errno);
        }

    int len = sizeof (clientAddr);

    int snew = accept (sock, (struct sockaddr *) &clientAddr,(socklen_t *) &len);
    if (snew <=0) 
    {
        perror ("accept failed");
        throw DTtcpExcp(errno);
    }
 
    return new DTCtcp(snew,0);
}
unsigned long DTCtcp::addr ( )

Definition at line 106 of file DTSpyHelper.cc.

References clientAddr.

{
    unsigned long maddr = clientAddr.sin_addr.s_addr;
    maddr = htonl (maddr);
    return maddr;
}
void DTCtcp::Connect ( const char *  hostaddr,
int  port 
)

Definition at line 162 of file DTSpyHelper.cc.

References clientAddr, align_tpl::connect, connected, and sock.

Referenced by DTSpy::DTSpy(), and DTSpyReader::DTSpyReader().

{
    clientAddr.sin_family      = AF_INET;
    clientAddr.sin_addr.s_addr = inet_addr (host);
    clientAddr.sin_port        = htons (toport);
  
 if (connect (sock, (struct sockaddr  *)&clientAddr, sizeof (clientAddr)) < 0)
{
        perror ("connect failed");
        throw DTtcpExcp(errno);
}
  connected = true;
}
void DTCtcp::Connect ( unsigned long  hostaddr,
int  port 
)

Definition at line 147 of file DTSpyHelper.cc.

References clientAddr, align_tpl::connect, connected, and sock.

{
    clientAddr.sin_family      = AF_INET;
    clientAddr.sin_addr.s_addr = htonl (in);
    clientAddr.sin_port        = htons (toport);
  
 if (connect (sock, (struct sockaddr  *)&clientAddr, sizeof (clientAddr)) < 0)
{
        perror ("connect failed");
        throw DTtcpExcp(errno);
}
  connected = true;
}
int DTCtcp::Disconnect ( )

Definition at line 114 of file DTSpyHelper.cc.

References connected, and sock.

{
  connected = false;
  return shutdown(sock,SHUT_RDWR);
}
short DTCtcp::Id ( )

Definition at line 98 of file DTSpyHelper.cc.

References clientAddr.

{
    long maddr = clientAddr.sin_addr.s_addr;
    maddr = htonl (maddr);
    return maddr&0xff;
}
int DTCtcp::Receive ( char *  buffer,
int  size 
)

Definition at line 197 of file DTSpyHelper.cc.

References findQualityFiles::size, and sock.

Referenced by DTSpy::getNextBuffer().

{
//    return  read (sock, buffer,size) ;
 
    int howmany = 0;
    int toberead = size;
    do
    {
      //int readnow = recv (sock, &buffer[howmany], toberead,MSG_WAITALL) ;
      int readnow = recv (sock, &buffer[howmany], toberead,0) ;
      //if (readnow < 0 ) {printf("some rrorrs...%d\n",errno); return -1;}
      if (readnow <= 0 ) 
           {printf("some rrorrs...%d\n",errno); throw DTtcpExcp(errno);}
      else { 
        howmany+=readnow; toberead-=readnow;}
    } while (toberead>0);
    return howmany;
}
int DTCtcp::Send ( char *  buffer,
int  size 
)

Definition at line 217 of file DTSpyHelper.cc.

References connected, sock, and TablePrint::write.

{
  if (connected==false) throw DTtcpExcp(EPIPE);
    int myret =  write (sock, buffer, size) ;
    if (myret<0) throw DTtcpExcp(errno);
    return myret;
}
int DTCtcp::WaitData ( int  timeout)

Definition at line 177 of file DTSpyHelper.cc.

References benchmark_cfg::select, and sock.

{
//    return  read (sock, buffer,size) ;
     fd_set rfds;
     struct timeval tv;
   
    FD_ZERO(&rfds);
    FD_SET(sock,&rfds);

    tv.tv_sec = timeout;
    tv.tv_usec = 0;
 
     int retva = select (1,&rfds,0,&rfds,&tv);
     if (retva)
         if (FD_ISSET(0,&rfds)) return 1;
         else return -1;
     else return 0; 
}

Member Data Documentation

struct sockaddr_in DTCtcp::clientAddr [protected]

Definition at line 27 of file DTSpyHelper.h.

Referenced by Accept(), addr(), Connect(), and Id().

int DTCtcp::connected [protected]

Definition at line 26 of file DTSpyHelper.h.

Referenced by Connect(), Disconnect(), DTCtcp(), DTSpy::getNextBuffer(), and Send().

struct sockaddr_in DTCtcp::myaddr [protected]

Definition at line 28 of file DTSpyHelper.h.

Referenced by DTCtcp().

int DTCtcp::port [protected]

Definition at line 23 of file DTSpyHelper.h.

Referenced by DTCtcp().

int DTCtcp::sock [protected]

Definition at line 24 of file DTSpyHelper.h.

Referenced by Accept(), Connect(), Disconnect(), DTCtcp(), Receive(), Send(), WaitData(), and ~DTCtcp().