00001 /* 00002 * DTSpy.cc 00003 * 00004 * 00005 * Created by Sandro Ventura on 7/28/07. 00006 * Copyright 2007 __MyCompanyName__. All rights reserved. 00007 * 00008 */ 00009 00010 #include "DTSpy.h" 00011 #include <stdlib.h> 00012 #include <string.h> 00013 00014 #define DTSPY_HEAD_SIZE 36 00015 #define DTSPY_MAX_MSG 512*1024 00016 00017 00018 DTSpy::DTSpy():DTCtcp(0) 00019 { 00020 spybuf=(char *)malloc(DTSPY_MAX_MSG); 00021 givenonce=0; 00022 } 00023 00024 00025 DTSpy::~DTSpy() 00026 { 00027 free(spybuf); 00028 } 00029 00030 00031 DTSpy::DTSpy(char *hostaddr,int port):DTCtcp(0) 00032 { 00033 Connect(hostaddr,port); 00034 givenonce=0; 00035 } 00036 00037 00038 int 00039 DTSpy::getNextBuffer() 00040 { 00041 00042 if (!connected) return -1; 00043 if (givenonce) 00044 if ((lastpointer - spybuf) < getBuffSize()) 00045 return (getBuffSize() - (lastpointer - spybuf)); 00046 givenonce=0; 00047 00048 memset(spybuf,0x0,DTSPY_MAX_MSG); /* init buffer */ 00049 00050 int howm=Receive(spybuf,DTSPY_HEAD_SIZE); 00051 00052 unsigned short *i2ohea = (unsigned short *) (spybuf+2); 00053 if (howm == DTSPY_HEAD_SIZE ) 00054 { 00055 howm = Receive(spybuf+DTSPY_HEAD_SIZE,((*i2ohea) * 4)-DTSPY_HEAD_SIZE); 00056 00057 // for (int ii=0; ii<12; ii++) 00058 // printf("%d: %x %x %x %x \n",ii*4,spybuf[ii*4], 00059 // spybuf[ii*4+1],spybuf[ii*4+2],spybuf[ii*4+3]); 00060 00061 return howm+DTSPY_HEAD_SIZE;; 00062 } 00063 else return -1; 00064 } 00065 00066 int 00067 DTSpy::getBuffSize() 00068 { 00069 unsigned short *i2ohea = (unsigned short *) (spybuf+2); 00070 return *i2ohea; 00071 } 00072 00073 00074 00075 int 00076 DTSpy::getRunNo() 00077 { 00078 unsigned short *i2ohea = (unsigned short *) (spybuf+28); 00079 return *i2ohea; 00080 } 00081 00082 const char * 00083 DTSpy::getEventPointer() 00084 { 00085 if (givenonce) return lastpointer; 00086 lastpointer = spybuf+DTSPY_HEAD_SIZE; 00087 givenonce = 1; 00088 return lastpointer; 00089 } 00090 00091 void 00092 DTSpy::setlastPointer(char * thep) 00093 { 00094 lastpointer=thep; 00095 }