CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DTSpyHelper.cc
Go to the documentation of this file.
1 #include "DTSpyHelper.h"
2 #include <errno.h>
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 <string.h>
10 
11 #ifdef __wasAPPLE__
12 typedef int socklen_t;
13 #endif
14 
16 {
17  DTCtcp(0);
18 }
19 
20 DTCtcp::DTCtcp(int localport)
21 {
22 // struct sockaddr_in myaddr;
23 
24  connected=false;
25 
26  printf("zeroing...\n");
27  bzero ((char *) &myaddr, sizeof(myaddr));
28  printf("zeroing done..\n");
29 
30 
31  sock = socket (AF_INET, SOCK_STREAM, 0);
32  printf("create socket..\n");
33 
34  if (sock < 0)
35  {
36  printf("no socket...\n");
37  throw DTtcpExcp(errno);
38  }
39 
40 
41  myaddr.sin_family = AF_INET;
42  myaddr.sin_port = htons (localport);
43 
44  //int blen = 65536;
45  int blen = 65536*8;
46 
47 // printf("setting socket opts buf...\n");
48 // if(setsockopt(sock,SOL_SOCKET,SO_SNDBUF,(char *)&blen,sizeof(blen))<0)
49 // throw DTtcpExcp(errno);
50 // if(setsockopt(sock,SOL_SOCKET,SO_RCVBUF,(char *)&blen,sizeof(blen))<0)
51 // throw DTtcpExcp(errno);
52  printf("setting socket opts reuse...\n");
53  if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&blen,sizeof(blen))<0)
54  throw DTtcpExcp(errno);
55 // printf("setting socket opts nodelay...\n");
56 // if(setsockopt(sock,SOL_SOCKET,TCP_NODELAY,(char *)&blen,sizeof(blen))<0)
57 // throw;
58  printf("binding...\n");
59 
60  port = localport;
61 
62  if (port)
63  if(bind(sock,(struct sockaddr *)&myaddr,sizeof (myaddr)) < 0)
64  { perror ("bind failed");
65  throw DTtcpExcp(errno);
66  }
67 
68 
69 }
70 
71 DTCtcp::DTCtcp(int snew, int opt)
72 {
73 
74  connected = true;
75  port =0;
76 
77  sock = snew;
78 }
79 
81 {
82 
83  connected = aconn->connected;
84  port = aconn->port;
85 
86  sock = aconn->sock;
87 }
88 
90 {
91  printf("deleting DTCtcp\n");
92  //if (connected) shutdown(sock,2);
93  shutdown(sock,SHUT_RDWR);
94  close (sock);
95 }
96 
97 short
99 {
100  long maddr = clientAddr.sin_addr.s_addr;
101  maddr = htonl (maddr);
102  return maddr&0xff;
103 }
104 
105 unsigned long
107 {
108  unsigned long maddr = clientAddr.sin_addr.s_addr;
109  maddr = htonl (maddr);
110  return maddr;
111 }
112 
113 int
115 {
116  connected = false;
117  return shutdown(sock,SHUT_RDWR);
118 }
119 
120 DTCtcp *
122 {
123 
124  // struct sockaddr_in clientAddr; /* client's address */
125 
126  bzero ((char *) &clientAddr, sizeof (clientAddr));
127 
128  if (listen (sock, 2) < 0)
129  {
130  perror ("listen failed");
131  throw DTtcpExcp(errno);
132  }
133 
134  int len = sizeof (clientAddr);
135 
136  int snew = accept (sock, (struct sockaddr *) &clientAddr,(socklen_t *) &len);
137  if (snew <=0)
138  {
139  perror ("accept failed");
140  throw DTtcpExcp(errno);
141  }
142 
143  return new DTCtcp(snew,0);
144 }
145 
146 void
147 DTCtcp::Connect(unsigned long in,int toport)
148 {
149  clientAddr.sin_family = AF_INET;
150  clientAddr.sin_addr.s_addr = htonl (in);
151  clientAddr.sin_port = htons (toport);
152 
153  if (connect (sock, (struct sockaddr *)&clientAddr, sizeof (clientAddr)) < 0)
154 {
155  perror ("connect failed");
156  throw DTtcpExcp(errno);
157 }
158  connected = true;
159 }
160 
161 void
162 DTCtcp::Connect(const char *host,int toport)
163 {
164  clientAddr.sin_family = AF_INET;
165  clientAddr.sin_addr.s_addr = inet_addr (host);
166  clientAddr.sin_port = htons (toport);
167 
168  if (connect (sock, (struct sockaddr *)&clientAddr, sizeof (clientAddr)) < 0)
169 {
170  perror ("connect failed");
171  throw DTtcpExcp(errno);
172 }
173  connected = true;
174 }
175 
176 int
178 {
179 // return read (sock, buffer,size) ;
180  fd_set rfds;
181  struct timeval tv;
182 
183  FD_ZERO(&rfds);
184  FD_SET(sock,&rfds);
185 
186  tv.tv_sec = timeout;
187  tv.tv_usec = 0;
188 
189  int retva = select (1,&rfds,0,&rfds,&tv);
190  if (retva)
191  if (FD_ISSET(0,&rfds)) return 1;
192  else return -1;
193  else return 0;
194 }
195 
196 int
197 DTCtcp::Receive(char *buffer,int size)
198 {
199 // return read (sock, buffer,size) ;
200 
201  int howmany = 0;
202  int toberead = size;
203  do
204  {
205  //int readnow = recv (sock, &buffer[howmany], toberead,MSG_WAITALL) ;
206  int readnow = recv (sock, &buffer[howmany], toberead,0) ;
207  //if (readnow < 0 ) {printf("some rrorrs...%d\n",errno); return -1;}
208  if (readnow <= 0 )
209  {printf("some rrorrs...%d\n",errno); throw DTtcpExcp(errno);}
210  else {
211  howmany+=readnow; toberead-=readnow;}
212  } while (toberead>0);
213  return howmany;
214 }
215 
216 int
217 DTCtcp::Send(char *buffer,int size)
218 {
219  if (connected==false) throw DTtcpExcp(EPIPE);
220  int myret = write (sock, buffer, size) ;
221  if (myret<0) throw DTtcpExcp(errno);
222  return myret;
223 }
int Receive(char *buffer, int size)
Definition: DTSpyHelper.cc:197
int Send(char *buffer, int size)
Definition: DTSpyHelper.cc:217
int Disconnect()
Definition: DTSpyHelper.cc:114
struct sockaddr_in myaddr
Definition: DTSpyHelper.h:28
int timeout
Definition: mps_check.py:51
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:30
short Id()
Definition: DTSpyHelper.cc:98
void Connect(const char *hostaddr, int port)
Definition: DTSpyHelper.cc:162
int sock
Definition: DTSpyHelper.h:24
int connected
Definition: DTSpyHelper.h:26
int port
Definition: DTSpyHelper.h:23
string host
Definition: query.py:114
DTCtcp * Accept()
Definition: DTSpyHelper.cc:121
unsigned long addr()
Definition: DTSpyHelper.cc:106
struct sockaddr_in clientAddr
Definition: DTSpyHelper.h:27
tuple size
Write out results.
int WaitData(int timeout)
Definition: DTSpyHelper.cc:177