DWORD WINAPI monitorSocket(LPVOID whyatall){ int ret, length; struct sockaddr_in addr; SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); while(1) { length = sizeof(addr); struct packet *p = (struct packet *)malloc(sizeof(packet)); if(!p) break; ret = netRead(p->data, 2000, &(p->address), &(p->port)); if(ret > 0) //get current window handle PostMessage(wndMain, WM_UDP_AVAILABLE, ret, (LPARAM)p); else free(p); if(ret <= 0){ int error = WSAGetLastError(); WSASetLastError(0); //take a deep breath Sleep(100); } } return 0; }
void onRead(int socket) { char buf[BUF_SIZE]; int received; // server accepted incoming socket, echo data printf("onRead\n"); received = netRead(socket, buf, BUF_SIZE); printf("received: %i\n", received); writeTest(socket); }
void readSocketData(LtpInterfaceType *localLtpInterfaceObjectP) { int length; while((length = netRead(localLtpInterfaceObjectP->socketID, localLtpInterfaceObjectP->ltpReceType.bufferUChar, localLtpInterfaceObjectP->ltpReceType.bufferLength, &localLtpInterfaceObjectP->ltpReceType.address, &localLtpInterfaceObjectP->ltpReceType.port))>0) { mutexLockInterface(); #ifdef _LTP_ ltpOnPacket(localLtpInterfaceObjectP->ltpObjectP, (char *)localLtpInterfaceObjectP->ltpReceType.bufferUChar, length, localLtpInterfaceObjectP->ltpReceType.address, localLtpInterfaceObjectP->ltpReceType.port); #endif mutexUnLockInterface(); } }
string Socket::receive(int sizeToReceive){ //=0 async read if(closed) { string ret=readBuffer; readBuffer=""; if(ret.size()){ syslog(LOG_INFO,"Received %lu",(long unsigned int)ret.size()); } return ret; } if(header.size()==0) sizeToReceive = JAIL_HEADERS_SIZE_LIMIT; if(sizeToReceive){ syslog(LOG_INFO,"Receiving until %d bytes",sizeToReceive); } //If already read, return data if(sizeToReceive>0 && readBuffer.size()>=sizeToReceive && header.size()!=0){ string ret = readBuffer.substr(0,sizeToReceive); readBuffer.erase(0,sizeToReceive); return ret; } struct pollfd devices[1]; devices[0].fd=socket; devices[0].events=POLLIN; const int MAX=JAIL_NET_BUFFER_SIZE; char buf[MAX]; const int wait=10; // 10 milisec const int bad=POLLERR|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) { syslog(LOG_INFO,"poll fail reading %m"); throw HttpException(internalServerErrorCode ,"Error poll reading data"); //Error } time_t currentTime=time(NULL); if(currentTime>timeLimit || currentTime>fullTimeLimit){ if(sizeToReceive==0){ syslog(LOG_DEBUG,"Socket read timeout, closed connection?"); return ""; }else throw HttpException(requestTimeoutCode ,"Socket read timeout"); } if(res==0 && sizeToReceive==0) break; //Nothing to read if(res==0) continue; //Nothing to do syslog(LOG_DEBUG,"poll return: %d",res); if(devices[0].revents & POLLIN){ //Read from net int sizeRead=netRead(buf,MAX); if(sizeRead >0) { string sread(buf,sizeRead); if(sread.size()!=sizeRead){ syslog(LOG_ERR,"read net decode error"); } readBuffer += sread; if(header.size()==0){ size_t pos; if((pos=readBuffer.find("\r\n\r\n")) != string::npos){ header=readBuffer.substr(0,pos+4); syslog(LOG_INFO,"Received header %lu",(long unsigned int)pos+4); processHeaders(header); readBuffer.erase(0,pos+4); return ""; //End of process }else if(readBuffer.size()>JAIL_HEADERS_SIZE_LIMIT){ throw HttpException(requestEntityTooLargeCode,"Http headers too large"); } }else if(sizeToReceive==0 || readBuffer.size()>=sizeToReceive){ //Receive complete break; } timeLimit=currentTime+JAIL_SOCKET_TIMEOUT; //Reset timeout } else if(sizeRead<0){ throw HttpException(badRequestCode ,"Error reading data"); } else { syslog(LOG_INFO,"sizeRead==0"); closed=true; break; } } if(devices[0].revents & POLLHUP){ //socket close closed=true; syslog(LOG_INFO,"POLLHUP"); break; } if(devices[0].revents & bad) { throw HttpException(internalServerErrorCode ,"Error reading data"); } } string ret=readBuffer; readBuffer=""; if(ret.size()){ syslog(LOG_INFO,"Received %lu",(long unsigned int)ret.size()); } return ret; }
static void _read_query_from_client(EventLoop_t evloop , int fd , void* clientData , int mask) { AVOID_NOUSED(evloop); AVOID_NOUSED(mask); // AVOID_NOUSED(clientData); // Client_t* c = Map_GetPtr(g_executor.clients , &fd); Client_t* c = (Client_t*)clientData; int nread; char errmsg[MAX_NET_ERR_LEN]; StdMutex_Lock(c->oplock); nread = netRead(errmsg, c->fd, c->query_buffer + c->buffer_len, CLIENTS_BUFFER_LEN - c->buffer_len); if (nread == ERR_NETWORK_EXCEPTION) { //ERR HAPPEND StdMutex_Unlock(c->oplock); Log(LOG_ERROR,"Read query from client failed.reason:##%s##", errmsg); _release_client(fd, RELEASE_BY_ERROR); return; } else if (nread == 0) { //Client Close the connection Log(LOG_NOTICE,"[CLIENT_DISCONNECT][%s:%d]", c->addr, c->port); StdMutex_Unlock(c->oplock); _release_client(fd, RELEASE_BY_CLIENT); return; } c->buffer_len += nread; //try to extract as many package as possible char* iterator = c->query_buffer; while (1) { if (c->buffer_len < MSGHEAD_SIZE) { break; } if (!isValidRequest(iterator)) { //not a valid request, clear client Log(LOG_NOTICE,"invalid request received from [%s:%d], close the connection" , c->addr , c->port); StdMutex_Unlock(c->oplock); _release_client(fd , RELEASE_BY_ERROR); return; } Message_t new_msg; new_msg.msgid = *((int32_t*)iterator + 1); new_msg.pkg_len = *((int32_t*)iterator + 2); if (c->status == CLI_INIT && new_msg.msgid != MSG_EXCU_AUTH && new_msg.msgid != MSG_EXCU_PLUSE) { //client not login, kick out Log(LOG_NOTICE,"client [%s:%d] should get authentication first before anyother request" , c->addr , c->port); StdMutex_Unlock(c->oplock); _release_client(fd , RELEASE_BY_ERROR); return; } c->last_seen = time(NULL); if (new_msg.pkg_len + MSGHEAD_SIZE > c->buffer_len) { //not a complete package int offset = iterator - c->query_buffer; for (int pos = 0; pos != c->buffer_len; ++pos) { c->query_buffer[pos] = c->query_buffer[pos + offset]; } break; } //extract one package new_msg.sockfd = c->fd; uint8_t* pkg = ylmalloc(new_msg.pkg_len); memcpy(pkg , iterator + MSGHEAD_SIZE , new_msg.pkg_len); new_msg.package = pkg; uint8_t recv_buf = atomic_add_then_fetch((int*)&g_executor.cur_recv_buffer_ , 1)%CLIENT_RECV_QUEUES; Queue_Push(g_executor.recv_buffer_[recv_buf] , &new_msg); sem_post(&g_executor.recv_buffer_num_[recv_buf]); int msglen = new_msg.pkg_len + MSGHEAD_SIZE; c->buffer_len -= msglen; iterator += msglen; } StdMutex_Unlock(c->oplock); }