Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:21:23

0001 #include "DTSpyHelper.h"
0002 #include <cerrno>
0003 #include <sys/time.h>
0004 #include <sys/types.h>
0005 #include <unistd.h>
0006 #include <sys/socket.h>
0007 #include <netinet/in.h>
0008 #include <arpa/inet.h>
0009 #include <cstring>
0010 
0011 #ifdef __wasAPPLE__
0012 typedef int socklen_t;
0013 #endif
0014 
0015 DTCtcp::DTCtcp() { DTCtcp(0); }
0016 
0017 DTCtcp::DTCtcp(int localport) {
0018   //  struct sockaddr_in myaddr;
0019 
0020   connected = false;
0021 
0022   printf("zeroing...\n");
0023   bzero((char *)&myaddr, sizeof(myaddr));
0024   printf("zeroing done..\n");
0025 
0026   sock = socket(AF_INET, SOCK_STREAM, 0);
0027   printf("create socket..\n");
0028 
0029   if (sock < 0) {
0030     printf("no socket...\n");
0031     throw DTtcpExcp(errno);
0032   }
0033 
0034   myaddr.sin_family = AF_INET;
0035   myaddr.sin_port = htons(localport);
0036 
0037   //int blen = 65536;
0038   int blen = 65536 * 8;
0039 
0040   //     printf("setting socket opts buf...\n");
0041   //    if(setsockopt(sock,SOL_SOCKET,SO_SNDBUF,(char *)&blen,sizeof(blen))<0)
0042   //      throw DTtcpExcp(errno);
0043   //    if(setsockopt(sock,SOL_SOCKET,SO_RCVBUF,(char *)&blen,sizeof(blen))<0)
0044   //      throw DTtcpExcp(errno);
0045   printf("setting socket opts reuse...\n");
0046   if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&blen, sizeof(blen)) < 0)
0047     throw DTtcpExcp(errno);
0048   //     printf("setting socket opts nodelay...\n");
0049   //    if(setsockopt(sock,SOL_SOCKET,TCP_NODELAY,(char *)&blen,sizeof(blen))<0)
0050   //      throw;
0051   printf("binding...\n");
0052 
0053   port = localport;
0054 
0055   if (port)
0056     if (bind(sock, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
0057       perror("bind failed");
0058       throw DTtcpExcp(errno);
0059     }
0060 }
0061 
0062 DTCtcp::DTCtcp(int snew, int opt) {
0063   connected = true;
0064   port = 0;
0065 
0066   sock = snew;
0067 }
0068 
0069 DTCtcp::DTCtcp(DTCtcp *aconn) {
0070   connected = aconn->connected;
0071   port = aconn->port;
0072 
0073   sock = aconn->sock;
0074 }
0075 
0076 DTCtcp::~DTCtcp() {
0077   printf("deleting DTCtcp\n");
0078   //if (connected) shutdown(sock,2);
0079   shutdown(sock, SHUT_RDWR);
0080   close(sock);
0081 }
0082 
0083 short DTCtcp::Id() {
0084   long maddr = clientAddr.sin_addr.s_addr;
0085   maddr = htonl(maddr);
0086   return maddr & 0xff;
0087 }
0088 
0089 unsigned long DTCtcp::addr() {
0090   unsigned long maddr = clientAddr.sin_addr.s_addr;
0091   maddr = htonl(maddr);
0092   return maddr;
0093 }
0094 
0095 int DTCtcp::Disconnect() {
0096   connected = false;
0097   return shutdown(sock, SHUT_RDWR);
0098 }
0099 
0100 DTCtcp *DTCtcp::Accept() {
0101   //   struct sockaddr_in  clientAddr; /* client's address */
0102 
0103   bzero((char *)&clientAddr, sizeof(clientAddr));
0104 
0105   if (listen(sock, 2) < 0) {
0106     perror("listen failed");
0107     throw DTtcpExcp(errno);
0108   }
0109 
0110   int len = sizeof(clientAddr);
0111 
0112   int snew = accept(sock, (struct sockaddr *)&clientAddr, (socklen_t *)&len);
0113   if (snew <= 0) {
0114     perror("accept failed");
0115     throw DTtcpExcp(errno);
0116   }
0117 
0118   return new DTCtcp(snew, 0);
0119 }
0120 
0121 void DTCtcp::Connect(unsigned long in, int toport) {
0122   clientAddr.sin_family = AF_INET;
0123   clientAddr.sin_addr.s_addr = htonl(in);
0124   clientAddr.sin_port = htons(toport);
0125 
0126   if (connect(sock, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0) {
0127     perror("connect failed");
0128     throw DTtcpExcp(errno);
0129   }
0130   connected = true;
0131 }
0132 
0133 void DTCtcp::Connect(const char *host, int toport) {
0134   clientAddr.sin_family = AF_INET;
0135   clientAddr.sin_addr.s_addr = inet_addr(host);
0136   clientAddr.sin_port = htons(toport);
0137 
0138   if (connect(sock, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0) {
0139     perror("connect failed");
0140     throw DTtcpExcp(errno);
0141   }
0142   connected = true;
0143 }
0144 
0145 int DTCtcp::Receive(char *buffer, int size) {
0146   //    return  read (sock, buffer,size) ;
0147 
0148   int howmany = 0;
0149   int toberead = size;
0150   do {
0151     //int readnow = recv (sock, &buffer[howmany], toberead,MSG_WAITALL) ;
0152     int readnow = recv(sock, &buffer[howmany], toberead, 0);
0153     //if (readnow < 0 ) {printf("some rrorrs...%d\n",errno); return -1;}
0154     if (readnow <= 0) {
0155       printf("some rrorrs...%d\n", errno);
0156       throw DTtcpExcp(errno);
0157     } else {
0158       howmany += readnow;
0159       toberead -= readnow;
0160     }
0161   } while (toberead > 0);
0162   return howmany;
0163 }
0164 
0165 int DTCtcp::Send(char *buffer, int size) {
0166   if (connected == false)
0167     throw DTtcpExcp(EPIPE);
0168   int myret = write(sock, buffer, size);
0169   if (myret < 0)
0170     throw DTtcpExcp(errno);
0171   return myret;
0172 }