CMS 3D CMS Logo

DTSpyHelper.cc
Go to the documentation of this file.
1 #include "DTSpyHelper.h"
2 #include <cerrno>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <cstring>
10 
11 #ifdef __wasAPPLE__
12 typedef int socklen_t;
13 #endif
14 
16 
17 DTCtcp::DTCtcp(int localport) {
18  // struct sockaddr_in myaddr;
19 
20  connected = false;
21 
22  printf("zeroing...\n");
23  bzero((char *)&myaddr, sizeof(myaddr));
24  printf("zeroing done..\n");
25 
26  sock = socket(AF_INET, SOCK_STREAM, 0);
27  printf("create socket..\n");
28 
29  if (sock < 0) {
30  printf("no socket...\n");
31  throw DTtcpExcp(errno);
32  }
33 
34  myaddr.sin_family = AF_INET;
35  myaddr.sin_port = htons(localport);
36 
37  //int blen = 65536;
38  int blen = 65536 * 8;
39 
40  // printf("setting socket opts buf...\n");
41  // if(setsockopt(sock,SOL_SOCKET,SO_SNDBUF,(char *)&blen,sizeof(blen))<0)
42  // throw DTtcpExcp(errno);
43  // if(setsockopt(sock,SOL_SOCKET,SO_RCVBUF,(char *)&blen,sizeof(blen))<0)
44  // throw DTtcpExcp(errno);
45  printf("setting socket opts reuse...\n");
46  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&blen, sizeof(blen)) < 0)
47  throw DTtcpExcp(errno);
48  // printf("setting socket opts nodelay...\n");
49  // if(setsockopt(sock,SOL_SOCKET,TCP_NODELAY,(char *)&blen,sizeof(blen))<0)
50  // throw;
51  printf("binding...\n");
52 
53  port = localport;
54 
55  if (port)
56  if (bind(sock, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
57  perror("bind failed");
58  throw DTtcpExcp(errno);
59  }
60 }
61 
62 DTCtcp::DTCtcp(int snew, int opt) {
63  connected = true;
64  port = 0;
65 
66  sock = snew;
67 }
68 
70  connected = aconn->connected;
71  port = aconn->port;
72 
73  sock = aconn->sock;
74 }
75 
77  printf("deleting DTCtcp\n");
78  //if (connected) shutdown(sock,2);
79  shutdown(sock, SHUT_RDWR);
80  close(sock);
81 }
82 
83 short DTCtcp::Id() {
84  long maddr = clientAddr.sin_addr.s_addr;
85  maddr = htonl(maddr);
86  return maddr & 0xff;
87 }
88 
89 unsigned long DTCtcp::addr() {
90  unsigned long maddr = clientAddr.sin_addr.s_addr;
91  maddr = htonl(maddr);
92  return maddr;
93 }
94 
96  connected = false;
97  return shutdown(sock, SHUT_RDWR);
98 }
99 
101  // struct sockaddr_in clientAddr; /* client's address */
102 
103  bzero((char *)&clientAddr, sizeof(clientAddr));
104 
105  if (listen(sock, 2) < 0) {
106  perror("listen failed");
107  throw DTtcpExcp(errno);
108  }
109 
110  int len = sizeof(clientAddr);
111 
112  int snew = accept(sock, (struct sockaddr *)&clientAddr, (socklen_t *)&len);
113  if (snew <= 0) {
114  perror("accept failed");
115  throw DTtcpExcp(errno);
116  }
117 
118  return new DTCtcp(snew, 0);
119 }
120 
121 void DTCtcp::Connect(unsigned long in, int toport) {
122  clientAddr.sin_family = AF_INET;
123  clientAddr.sin_addr.s_addr = htonl(in);
124  clientAddr.sin_port = htons(toport);
125 
126  if (connect(sock, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0) {
127  perror("connect failed");
128  throw DTtcpExcp(errno);
129  }
130  connected = true;
131 }
132 
133 void DTCtcp::Connect(const char *host, int toport) {
134  clientAddr.sin_family = AF_INET;
135  clientAddr.sin_addr.s_addr = inet_addr(host);
136  clientAddr.sin_port = htons(toport);
137 
138  if (connect(sock, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0) {
139  perror("connect failed");
140  throw DTtcpExcp(errno);
141  }
142  connected = true;
143 }
144 
145 int DTCtcp::Receive(char *buffer, int size) {
146  // return read (sock, buffer,size) ;
147 
148  int howmany = 0;
149  int toberead = size;
150  do {
151  //int readnow = recv (sock, &buffer[howmany], toberead,MSG_WAITALL) ;
152  int readnow = recv(sock, &buffer[howmany], toberead, 0);
153  //if (readnow < 0 ) {printf("some rrorrs...%d\n",errno); return -1;}
154  if (readnow <= 0) {
155  printf("some rrorrs...%d\n", errno);
156  throw DTtcpExcp(errno);
157  } else {
158  howmany += readnow;
159  toberead -= readnow;
160  }
161  } while (toberead > 0);
162  return howmany;
163 }
164 
165 int DTCtcp::Send(char *buffer, int size) {
166  if (connected == false)
167  throw DTtcpExcp(EPIPE);
168  int myret = write(sock, buffer, size);
169  if (myret < 0)
170  throw DTtcpExcp(errno);
171  return myret;
172 }
DTSpyHelper.h
DTCtcp::port
int port
Definition: DTSpyHelper.h:20
runTheMatrix.opt
opt
Definition: runTheMatrix.py:291
query.host
string host
Definition: query.py:115
edmScanValgrind.buffer
buffer
Definition: edmScanValgrind.py:171
accept
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:30
DTCtcp::addr
unsigned long addr()
Definition: DTSpyHelper.cc:89
DTCtcp
Definition: DTSpyHelper.h:18
DTCtcp::Send
int Send(char *buffer, int size)
Definition: DTSpyHelper.cc:165
DTCtcp::myaddr
struct sockaddr_in myaddr
Definition: DTSpyHelper.h:25
DTCtcp::clientAddr
struct sockaddr_in clientAddr
Definition: DTSpyHelper.h:24
DTtcpExcp
Definition: DTSpyHelper.h:11
DTCtcp::~DTCtcp
~DTCtcp()
Definition: DTSpyHelper.cc:76
DTCtcp::Id
short Id()
Definition: DTSpyHelper.cc:83
recoMuon::in
Definition: RecoMuonEnumerators.h:6
DTCtcp::Disconnect
int Disconnect()
Definition: DTSpyHelper.cc:95
DBConfiguration_cff.connect
connect
Definition: DBConfiguration_cff.py:18
writeEcalDQMStatus.write
write
Definition: writeEcalDQMStatus.py:48
DTCtcp::connected
int connected
Definition: DTSpyHelper.h:23
DTCtcp::sock
int sock
Definition: DTSpyHelper.h:21
DTCtcp::Connect
void Connect(const char *hostaddr, int port)
Definition: DTSpyHelper.cc:133
DTCtcp::Accept
DTCtcp * Accept()
Definition: DTSpyHelper.cc:100
DTCtcp::DTCtcp
DTCtcp()
Definition: DTSpyHelper.cc:15
DTCtcp::Receive
int Receive(char *buffer, int size)
Definition: DTSpyHelper.cc:145
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443