// $Id$ // // File: BORroc.cc // Created: Fri Jan 22 10:43:59 EST 2016 // Creator: davidl (on Linux gluon104.jlab.org 2.6.32-358.23.2.el6.x86_64 x86_64) // #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DEBUG 2 #include using namespace std; #include "BORroc.h" //--------------------------------- // ReadROCshmem //--------------------------------- void BORroc::ReadROCshmem(void) { int sd; struct sockaddr_in pin; struct hostent *hp; int HEADER[10]; int nleft,nread; unsigned int time0; char *rcv; unsigned int MARKER,REQUEST,REQUESTED; int evtTrigID, evtModID, evtSize; int lenDATA = 15000000; int Nr_Modules = 0; int ModuleID = 0; int TriggerID = 0; time((time_t*)&time0); if ((hp = gethostbyname(hostname.c_str())) == 0) { perror("gethostbyname"); return; } printf( "%s IP=%u.%u.%u.%u \n", hostname.c_str(), (unsigned char) hp->h_addr_list[0][0], (unsigned char) hp->h_addr_list[0][1], (unsigned char) hp->h_addr_list[0][2], (unsigned char) hp->h_addr_list[0][3]); //-------- fill in the socket structure with host information memset(&pin, 0, sizeof(pin)); pin.sin_family = AF_INET; pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; pin.sin_port = htons(port); //-------- grab an Internet domain socket if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); return; } printf("TCPclient:: try to connect to %s port=%d\n",hostname.c_str(),port); //------- connect to PORT on HOST if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) { perror("connect"); return; } printf("TCPclient:: CONNECTED to %s port=%d local_sock=%d\n",hostname.c_str(),port,sd); if(DEBUG>3) printf("REQUEST=%#X(%d) REQUESTED=%d\n",REQUEST,REQUEST,REQUESTED); HEADER[0]=0x2; //-- request for 1 event default -- HEADER[1]=0xAABBCCDD; HEADER[2]=lenDATA; HEADER[3]=0; HEADER[4]=0; HEADER[5]=Nr_Modules; HEADER[6]=ModuleID; if(DEBUG>3) printf(" tcp_event_get(): ask DATA max lenDATA=%d\n",lenDATA); if (send(sd, (char*) HEADER, sizeof(HEADER), 0) == -1) { perror("send"); return; } //----------------------------------------------------- //-- recv HEADER --- if(DEBUG>3) printf(" tcp_event_get(): wait for HEADER \n"); nread=0; nleft=sizeof(HEADER); rcv=(char*)HEADER; while(nleft>0){ nread=recv(sd,rcv,nleft, 0); if (nread <=0) {perror("recv_header"); return;} nleft-=nread; rcv+=nread; } MARKER= HEADER[1]; lenDATA=HEADER[2]; TriggerID=HEADER[4]; Nr_Modules=HEADER[5]; ModuleID=HEADER[6]; if(DEBUG>3) printf("HEADER OK=%#X, Nr_MOD=%d, ModID=%d, lenDATA=%d\n",MARKER,Nr_Modules,ModuleID,lenDATA); //-- recv DATA --- if(DEBUG>3) printf(" tcp_event_get(): wait for DATA \n"); N = 0; nleft=lenDATA*4; DATA = new uint32_t[lenDATA+1]; rcv=(char*)DATA; while(nleft>0){ nread=recv(sd,rcv, nleft, 0); if (nread <=0) { perror("recv_data"); return;} nleft-=nread; rcv+=nread; N+=nread; if(DEBUG>3) printf("DATA recv=%d nleft=%d\n",nread,nleft); } evtTrigID=DATA[1]; evtModID=(DATA[0]>>24)&0xf; evtSize=DATA[0]&0xfffff; if(DEBUG>3) printf("OK, TCP recv=%d nleft=%d Trg=%d(%d) Mod=%d siz=%d\n",nread,nleft,evtTrigID,TriggerID,evtModID,evtSize); data_valid = true; }