#include #include #include #include #include "zlib.h" #include "MonitorData.h" //********************************************************************************************** //********************************************************************************************** //********************************** DO NOT EDIT THIS FILE *********************************** //********************************************************************************************** //********************************************************************************************** #define BUFFER_SIZE_START 2048 #define BUFFER_RESIZE_VALUE 1024 extern void set_bit( unsigned char p[], int bit); extern void unset_bit(unsigned char p[], int bit); extern int get_bit( unsigned char p[], int bit); // ------------------ Support Routine ----------------------------- ////////////////////////////////////////////////////////////////////// // qsort compare routine ////////////////////////////////////////////////////////////////////// int compar_name( const void *e1, const void *e2 ) { MON_ELEMENT *data1,*data2; data1 = *(MON_ELEMENT **)e1; data2 = *(MON_ELEMENT **)e2; #ifdef _WIN32 return _stricmp(data1->label, data2->label); #else return strcasecmp(data1->label,data2->label); #endif } int compar_index( const void *e1, const void *e2 ) { MON_ELEMENT *data1,*data2; data1 = (MON_ELEMENT *)e1; data2 = (MON_ELEMENT *)e2; return data1->id - data2->id; } // ------------------ Game Data Object ---------------------------- ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// //CMonitorData::CMonitorData(GenericNotifier *notifier) //: m_notifier(notifier) CMonitorData::CMonitorData() { m_sequence = 1; m_buffer = NULL; m_nbuffer = 0; m_count = 0; m_max = ELEMENT_MAX_START; m_data = new MON_ELEMENT[m_max]; for(int x=0;xSend(cUdpChannelReliable1, p, size + 6); free(p); sequence++; } void CMonitorData::send( UdpConnection *con,short & sequence,short msg,char *data,int size ) { unsigned long compress_len; int len; unsigned short usize; char *p; int rnt; if( CURRENT_API_VERSION != CURRENT_API_3 ) return; p = (char *)malloc(size+6+1000); len = 0; memset(p,0,size+6+1000); packShort(p+len, len, msg); packShort(p+len, len, m_sequence); compress_len = size + 1000; rnt = compress2((Bytef *)p+6,&compress_len,(Bytef *)data,(long)size,2); if( rnt != Z_OK ) { free(p); printf("CMonitor::send-compress failed, error=%d size =%d\n",rnt,size); return; } usize = (unsigned short)compress_len; packShort(p+len, len, usize); usize += 6; con->Send(cUdpChannelReliable1, p, usize); free(p); sequence++; } bool CMonitorData::processHierarchyRequestBlock( UdpConnection *con, short & sequence ) { int x,size,count,len; char temp[512]; if( m_count == 0 ) { send(con,sequence,MON_MSG_REPLY_HIERARCHY,"NOTREADY"); return 0; } memset(m_buffer,0,16); send(con,sequence,MON_MSG_REPLY_HIERARCHY_BLOCK_BEGIN,m_buffer); MON_ELEMENT **me; me = new MON_ELEMENT *[m_count]; for(x=0;xlabel,me[x]->id,me[x]->ping); if( len+size >= m_nbuffer ) resize_buffer(size+BUFFER_RESIZE_VALUE); strcpy(&m_buffer[size],temp); size += len; if( size > 64000 ) { send(con,sequence,MON_MSG_REPLY_HIERARCHY_BLOCK,m_buffer,size+1); memset(m_buffer,0,size); count++; size = 0; } x++; } delete [] me; if( size > 0 ) { send(con,sequence,MON_MSG_REPLY_HIERARCHY_BLOCK,m_buffer,size+1); memset(m_buffer,0, 16); count++; size = 0; } memset(m_buffer,0, 16); send(con,sequence,MON_MSG_REPLY_HIERARCHY_BLOCK_END,m_buffer); return 1; } bool CMonitorData::processElementsRequest( UdpConnection *con, short & sequence, char * data, int /* dataLen */ , long lastUpdateTime ) { char tmp[200]; int x,id; int size; int count; char **list; memset(m_buffer,0,16); if( !strcmp(data,"-1") ) { size = 0; for (x=0;x= m_nbuffer ) resize_buffer(size+BUFFER_RESIZE_VALUE); strcat(m_buffer,tmp); if( size > 65000 ) { send(con,sequence,MON_MSG_REPLY_ELEMENTS,m_buffer); memset(m_buffer,0,16); } } if( size ) send(con,sequence,MON_MSG_REPLY_ELEMENTS,m_buffer); return 1; } if( !strcmp(data,"-2") ) { size = 0; count = 0; for (x=0;x= m_nbuffer ) resize_buffer(size+BUFFER_RESIZE_VALUE); strcat(m_buffer,tmp); count++; } if( size > 65000 ) { send(con,sequence,MON_MSG_REPLY_ELEMENTS,m_buffer); memset(m_buffer,0,16); } } if ( count ) { send(con,sequence,MON_MSG_REPLY_ELEMENTS,m_buffer); return 1; } return 0; } list = new char * [m_max]; count = parseList( list, data,'|',m_max); memset(m_buffer,0,16); size = 0; for(x=0;x= m_nbuffer ) resize_buffer(size+BUFFER_RESIZE_VALUE); strcat(m_buffer,tmp); } } delete [] list; if( count > 0 ) send(con,sequence,MON_MSG_REPLY_ELEMENTS,m_buffer); return 0; } bool CMonitorData::processDescriptionRequest(UdpConnection *con, short & sequence, char * userData, int , unsigned char *mark) { char line[4096]; char tmp[400]; int x,id,size; int flag; char *p; size = 0; memset(m_buffer,0,16); strcpy(line,userData); p = strtok(line,"|"); flag = 0; if( strstr(p,"next") ) { for(x=0;x= m_nbuffer ) resize_buffer(size+BUFFER_RESIZE_VALUE); strcat(m_buffer,tmp); send(con,sequence,MON_MSG_REPLY_DESCRIPTION,m_buffer); return 1; } } } } else { if( p ) { size = 0; flag = 0; id = atoi(p); for(x=0;x= m_nbuffer ) resize_buffer(size+BUFFER_RESIZE_VALUE); strcat(m_buffer,tmp); if( size >= 2000 || m_data[x].discription ) { send(con,sequence,MON_MSG_REPLY_DESCRIPTION,m_buffer); return 1; } } } } } } if( flag == 2 ) send(con,sequence,MON_MSG_REPLY_DESCRIPTION,m_buffer); send(con,sequence,MON_MSG_REPLY_DESCRIPTION,"none"); return 0; } int CMonitorData::add(const char *label, int id, int ping, const char *des ) { int x; if( strlen(label) >= 127 ) { printf("MonAPI Error: add() label exceeds length of 127. ->%s\n",label); return 0; } for(x=0;x %s\n",id,m_data[x].label,label); m_data[x].ping = pingValue( ping ); m_data[x].value = 0; m_data[x].id = id; //******* Label *********** if( m_data[x].label ) delete [] m_data[x].label; m_data[x].label = new char [strlen(label)+1]; strcpy(m_data[x].label,label); //******* Discription ****** if( m_data[x].discription != NULL ) delete [] m_data[x].discription; m_data[x].discription = NULL; if( des ) { m_data[x].discription = new char [strlen(des)+1]; strcpy(m_data[x].discription,des); } qsort(m_data,m_count,sizeof(MON_ELEMENT),compar_index); return 1; } if( !strcmp(label,m_data[x].label) ) { printf("MonAPI ERROR: Label already found %s Id: old(%d) new(%d).\n",label,m_data[x].id,id); return 0; } } if( m_max == m_count + 1 ) { printf("MonitorObject: max element reached [%d].\n%s\nContact David Taylor (858) 577-3155.\n",m_max,label); return 0; } m_data[m_count].ping = pingValue( ping ); m_data[m_count].value = 0; m_data[m_count].id = id; if(m_data[m_count].label) delete [] m_data[m_count].label; m_data[m_count].label = new char [strlen(label)+1]; strcpy(m_data[m_count].label,label); if( des ) { if( m_data[x].discription ) delete [] m_data[x].discription; m_data[x].discription = new char [strlen(des)+1]; strcpy(m_data[x].discription,des); } m_count++; qsort(m_data,m_count,sizeof(MON_ELEMENT),compar_index); return 1; } int CMonitorData::setDescription( int Id, const char *Description , int & mode) { int x; for(x=0;x 1; ) { i = (high+low) / 2; if ( Id <= m_data[i].id ) high = i; else low = i; } if ( high < m_count && Id==m_data[high].id ) { if( m_data[high].ChangedTime == 0 || m_data[high].value != value ) m_data[high].ChangedTime = (long)time(NULL); m_data[high].value = value; } } void CMonitorData::remove(int Id) { int x; if( m_count == 1 ) { if( m_data[0].label ) delete [] m_data[0].label; if( m_data[0].discription ) delete [] m_data[0].discription; m_data[0].label = 0; m_data[0].id = 0; m_data[0].value = 0;; m_data[0].ping = 0;; m_count--; return; } for(x=0;x 0 ) { if( *data == tok ) { *data = 0; data++; cnt++; list[count] = data; if( *data ) count++; } cnt++; if( *data ) data++; } return count; } void CMonitorData::dump() { printf("********** Monitor API Dump *******************\n"); printf(" Version: %d\n",CURRENT_API_VERSION); printf("\ncount: %d\n", m_count); printf("\t%-40s %8s\t%s\t%s\n","Label","Id","Ping","Value"); for(int x=0;x