void* _send_buffer_monitor(void* arg) { int buf_idx = *(int*)arg; Message_t msg; Client_t* c; for (;;) { sem_wait(&g_executor.send_buffer_num_[buf_idx]); if (Queue_Pop(g_executor.send_buffer_[buf_idx], &msg) == QUEUE_OK) { if ((c = Map_GetPtr(g_executor.clients, &msg.sockfd)) == NULL) { //Clients already gone Log(LOG_NOTICE,"[SEND FAIL]Client Already Gone"); ylfree(msg.package); continue; } if (msg.trysend_cnt) { usleep(100000); } int nsend = netWrite(NULL, msg.sockfd, (char*) msg.package, msg.pkg_len); if (nsend == ERR_NETWORK_TRY_AGAIN&& ++msg.trysend_cnt < MAX_SEND_TRY) { Queue_Push(g_executor.send_buffer_[buf_idx] , &msg); sem_post(&g_executor.send_buffer_num_[buf_idx]); } else { ylfree(msg.package); } } } return NULL; }
void writeTest(int socket) { // connected, send test data int sent = netWrite(socket, "test", 4); printf("sent: %i\n", sent); netSleep(1000); }
void Socket::send(const string &data, bool async){ if(closed) return; writeBuffer += data; size_t size=writeBuffer.size(); const unsigned char *s=(const unsigned char *)writeBuffer.data(); syslog(LOG_INFO,"Sending %lu",(long unsigned int)size); size_t offset=0; struct pollfd devices[1]; devices[0].fd=socket; devices[0].events=POLLOUT; const int wait=1; // 1 milisec const int bad=POLLERR|POLLHUP|POLLNVAL; time_t timeLimit=time(NULL)+JAIL_SOCKET_TIMEOUT; time_t fullTimeLimit=time(NULL)+JAIL_SOCKET_REQUESTTIMEOUT; while(true){ int res=poll(devices,1,wait); if(res==-1) { throw HttpException(internalServerErrorCode ,"Error poll writing data"); //Error } if(res==0 && async) break; //async write Nothing to do if(res==0) continue; //Nothing to do time_t currentTime=time(NULL); if(currentTime>timeLimit || currentTime>fullTimeLimit){ syslog(LOG_ERR,"Socket write timeout"); throw requestTimeoutCode; } if(devices[0].revents & POLLOUT){ //Write to net size_t toWrite=JAIL_NET_BUFFER_SIZE; if(toWrite>size-offset) toWrite=size-offset; int sizeWritten=netWrite(s+offset,toWrite); if(sizeWritten <0) { throw HttpException(internalServerErrorCode ,"Socket write data error"); }if(sizeWritten == 0){ closed=true; break; } else{ offset += sizeWritten; timeLimit=currentTime+JAIL_SOCKET_TIMEOUT; //Reset timeout } } if(devices[0].revents & POLLHUP){ //socket close closed=true; syslog(LOG_INFO,"POLLHUP"); break; } if(devices[0].revents & bad) { syslog(LOG_ERR,"Error writing http data %m"); throw HttpException(internalServerErrorCode ,"Socket write data error"); } if(offset>=size) break; } writeBuffer.erase(0,offset); syslog(LOG_INFO,"Send %lu",(long unsigned int)offset); }