void readImage(unsigned char * data, int len) { mutex.lock(); transImg = new (std::nothrow) sgct_core::Image(); char type = static_cast<char>(data[0]); bool result = false; switch( type ) { case IM_JPEG: result = transImg->loadJPEG(reinterpret_cast<unsigned char*>(data+headerSize), len-headerSize); break; case IM_PNG: result = transImg->loadPNG(reinterpret_cast<unsigned char*>(data+headerSize), len-headerSize); break; } if(!result) { //clear if failed delete transImg; transImg = NULL; } mutex.unlock(); }
void run_server(ServerArg &r) { /* socket bind TCP listen accept recv close UDP redcvfrom close */ struct sockaddr_in clientAddr; SOCKET sockfd; int result; unsigned long long ttlByteRecv = 0; initTCPServer(sockfd, r, clientAddr); listen(sockfd, MAX_ACCEPT_SIZE); cout << "listen() complete" << endl; cout << "wait for incoming message" << endl; #ifdef WIN32 int addrlen = sizeof(struct sockaddr_in); #else socklen_t addrlen = sizeof(struct sockaddr_in); #endif //vector<SOCKET> sockPool; SOCKET sockPool[MAX_LISTEN_SIZE]; //while(true) for(int i=0; i<MAX_ACCEPT_SIZE; i++) { //cout << " accept() start:" << sockfd << endl; SOCKET newSockfd = accept(sockfd, (sockaddr*) &clientAddr, &addrlen); //sockPool.push_back(newSockfd); sockPool[i] = newSockfd; if(newSockfd == -1) { cout << "accept failed: " << WSAGetLastError() << endl; return; } //mutex mutx.lock(); serData.tcpCount = (serData.tcpCount) + 1; mutx.unlock(); cout << endl; cout << "accept() complete (" << sockPool[i] << ")" << endl; //mutex tthread::thread th(runTCPServer, (void*) &(sockPool[i])); th.detach(); //th.join(); //runTCPServer(); } cout << "sockPool is full" << endl; return; }
void printServerData(void* f) { string unit; //int* freq = (int*) f; int freq = 1000; double r; long now; int tcp, udp; //cout << "serData.ttlByteRecv: " << serData.ttlByteRecv << endl; cout << " printServerData ready" << endl; do{ //mutex mutx.lock(); r = serData.ttlByteRecv + 0.0; tcp = serData.tcpCount; udp = serData.udpCount; mutx.unlock(); //mutex now = x_serTimer.ElapsedX(); if(r>0) r = (r*1000.0)/((double)now - (double)time_serBegin); //byte per ms unit = "Bps"; if(r >= 1024) { r /= 1024; //KBps unit = "KBps"; } if (r >= 1024) { r /= 1024; //MBps; unit = "MBps"; } //mutex printf("Rate [%.4g%s] ", r, unit.c_str()); printf("TCP Clients [%d] ", tcp); printf("UDP Clients [%d]", udp); fflush(stdout); //mutex //cout << "got now" << endl; goToSleep(freq); #ifdef WIN32 cout << '\r' ; #else printf("\033[2K\r"); #endif }while(freq>=0); if(freq<0) { cout << endl; exit(0); } return; }
void runTCPServer(void* newSockfd) { SOCKET* sfd = (SOCKET*) newSockfd; char recvbuf[RECV_BUF_SIZE]; unsigned long long ttlByteRecv; int result; //while(1){cout << "runTCPServer" << endl;}; do { memset(recvbuf, '\0', sizeof(recvbuf)); result = recv(*sfd, recvbuf, sizeof(recvbuf)-1, 0); if(result > 0) { //ttlByteRecv += result; //mutex mutx.lock(); serData.ttlByteRecv += result; //cout << "recv() complete" << endl; mutx.unlock(); //mutex } //add ttlByteRecv to function... }while(result > 0); if(result <= 0) { cout << " removed from queue(" << *sfd << ")"<< endl; closesocket(*sfd); WSACleanup(); //mutex mutx.lock(); serData.tcpCount = (serData.tcpCount) - 1; mutx.unlock(); //mutex return; } }
static int echoCallback( struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { // reason for callback switch (reason) { case LWS_CALLBACK_ESTABLISHED: { int n; unsigned int id = Webserver::instance()->generateSessionIndex(); gSendMutex.lock(); #if (_MSC_VER >= 1400) n = sprintf_s(gBuffer, sizeof(gBuffer), "%u\n", id); #else n = sprintf(gBuffer, "%u\n", id); #endif lws_write(wsi, reinterpret_cast<unsigned char *>(gBuffer), n, LWS_WRITE_TEXT); gSendMutex.unlock(); fprintf(stderr, "connection %u established\n", id); } break; case LWS_CALLBACK_RECEIVE: { // log what we recieved and what we're going to send as a response. if (Webserver::instance()->mWebMessageCallbackFn) Webserver::instance()->mWebMessageCallbackFn(reinterpret_cast<const char *>(in), len); } break; } return 0; }
void uploadTexture() { mutex.lock(); if( transImg ) { glfwMakeContextCurrent( hiddenWindow ); //create texture GLuint tex; glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); glPixelStorei(GL_PACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); GLenum internalformat; GLenum type; switch( transImg->getChannels() ) { case 1: internalformat = GL_R8; type = GL_RED; break; case 2: internalformat = GL_RG8; type = GL_RG; break; case 3: default: internalformat = GL_RGB8; type = GL_BGR; break; case 4: internalformat = GL_RGBA8; type = GL_BGRA; break; } int mipMapLevels = 8; glTexStorage2D(GL_TEXTURE_2D, mipMapLevels, internalformat, transImg->getWidth(), transImg->getHeight()); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, transImg->getWidth(), transImg->getHeight(), type, GL_UNSIGNED_BYTE, transImg->getData()); //glTexImage2D(GL_TEXTURE_2D, 0, internalformat, transImg->getWidth(), transImg->getHeight(), 0, type, GL_UNSIGNED_BYTE, transImg->getData()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mipMapLevels-1); glGenerateMipmap( GL_TEXTURE_2D ); //allocate the mipmaps glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //unbind glBindTexture(GL_TEXTURE_2D, GL_FALSE); sgct::MessageHandler::instance()->print("Texture id %d loaded (%dx%dx%d).\n", tex, transImg->getWidth(), transImg->getHeight(), transImg->getChannels()); texIds.addVal(tex); delete transImg; transImg = NULL; glFinish(); //restore glfwMakeContextCurrent( NULL ); } else //if invalid load { texIds.addVal(GL_FALSE); } mutex.unlock(); }