/** ****************************************************************************** @file /net/queue.cpp @brief Trida fronty zprav @author Vta @version 1.0 ******************************************************************************/ #include "net/queue.h" #include "common/xml/xml.h" #include "net/compress.h" #include namespace net { TMessageQueue::TMessageQueue() { this->end=QUEUE_RUNNING; this->first=NULL; this->last=NULL; this->num=0; this->compressbuff=new DA(1000); if ( (this->mutex=SDL_CreateMutex()) == NULL ) { GLOBALLOGID(PRIORITY_FATAL,"Queue - Can not create the mutex"); THROW(E_8K_NET,"Queue - Can not create the mutex"); } } TMessageQueue::~TMessageQueue() { delete(this->compressbuff); SDL_DestroyMutex(mutex); } int TMessageQueue::isEnding() { return this->end; } void TMessageQueue::shutdownQueue() { this->end=QUEUE_ENDING; } int TMessageQueue::addMessage(char * data,mysocket to) { this->lock(); // printf("ADD"); if (this->last==NULL) { this->last=(struct TMessageBody*)KMemAlloc(sizeof(struct TMessageBody)); this->first=this->last; } else { this->last->next=(struct TMessageBody*)KMemAlloc(sizeof(struct TMessageBody)); this->last=this->last->next; } this->last->next=NULL; this->last->to=to; size_t len;// =strlen(data)+1; #if COMPRESS_ACTIVE == 1 if (to!=0) { this->compressbuff->reset(); MyCompress(data,this->compressbuff,COMPRESS_LEVEL); len=this->compressbuff->getMaxId()+1; this->last->data=(char*)KMemAlloc(sizeof(char)*len); strcpy(this->last->data,this->compressbuff->getData()); } else { len=strlen(data)+1; this->last->data=(char*)KMemAlloc(sizeof(char)*len); strcpy(this->last->data,data); } #else len=strlen(data)+1; this->last->data=(char*)KMemAlloc(sizeof(char)*len); strcpy(this->last->data,data); #endif this->last->len=len; this->last->part_sent=0; this->num++; int ret=this->num; this->unlock(); return ret; } TMessageBody * TMessageQueue::messageToSend() { this->lock(); TMessageBody * m=this->first; this->unlock(); return m; } void TMessageQueue::messageWasSend() { this->lock(); if (this->first->part_sent==this->first->len) // uz odesla cela { TMessageBody * old=this->first; if (this->first->next!=NULL) { this->first=this->first->next; KMemFree(old->data); KMemFree(old); // printf("WAS SEND 1"); } else { KMemFree(this->first->data); KMemFree(this->first); this->first=NULL; this->last=NULL; // printf("WAS SEND 2"); } this->num--; } this->unlock(); } void TMessageQueue::lock(void) { if ( SDL_mutexP(this->mutex) < 0 ) { GLOBALLOGID(PRIORITY_FATAL, "Queue - Can not lock the mutex"); THROW(E_8K_NET,"Queue - Can not lock the mutex"); } GLOBALLOGID(PRIORITY_MUTEX,"Lock %i\n",SDL_ThreadID()); // printf("<"); } void TMessageQueue::unlock(void) { if ( SDL_mutexV(this->mutex) < 0 ) { GLOBALLOGID(PRIORITY_FATAL, "Queue - Can not unlock the mutex"); THROW(E_8K_NET,"Queue - Can not unlock the mutex"); } GLOBALLOGID(PRIORITY_MUTEX,"Unlock %i\n",SDL_ThreadID()); // printf(">"); } }