static Code chainMetaPredicateSupervisor(Definition def, Code post) { if ( true(def, P_META) && true(def, P_TRANSPARENT) ) { tmp_buffer buf; unsigned int i; int count = 0; Code codes; initBuffer(&buf); for(i=0; i < def->functor->arity; i++) { int ma = def->impl.any.args[i].meta; if ( MA_NEEDS_TRANSPARENT(ma) ) { addBuffer(&buf, encode(S_MQUAL), code); addBuffer(&buf, VAROFFSET(i), code); count++; } } if ( count > 0 ) { baseBuffer(&buf, code)[(count-1)*2] = encode(S_LMQUAL); copySuperVisorCode((Buffer)&buf, post); freeCodes(post); codes = allocCodes(entriesBuffer(&buf, code)); copyCodes(codes, baseBuffer(&buf, code), entriesBuffer(&buf, code)); return codes; } else { discardBuffer(&buf); } } return post; }
static Code chainMetaPredicateSupervisor(Definition def, Code post) { if ( true(def, P_META) && true(def, P_TRANSPARENT) ) { tmp_buffer buf; unsigned int i; int count = 0; Code codes; initBuffer(&buf); for(i=0; i < def->functor->arity; i++) { int ma = MA_INFO(def, i); if ( ma <= 9 || ma == MA_META || ma == MA_HAT || ma == MA_DCG ) /* 0..9, :, ^ or // */ { addBuffer(&buf, encode(S_MQUAL), code); addBuffer(&buf, VAROFFSET(i), code); count++; } } if ( count > 0 ) { baseBuffer(&buf, code)[(count-1)*2] = encode(S_LMQUAL); copySuperVisorCode((Buffer)&buf, post); freeCodes(post); codes = allocCodes(entriesBuffer(&buf, code)); copyCodes(codes, baseBuffer(&buf, code), entriesBuffer(&buf, code)); return codes; } else { discardBuffer(&buf); } } return post; }
void Mesh::initBuffer(string name, string linkage, unsigned vertexSize) { if(usedAttributes.contains(name)) { if(!buffers[name].empty()) { addBuffer(buffers[name], vertexSize, linkage); } else { if (vertexSize == 3) { LogWarning << "No" << name << "Buffer found. Will use Position Buffer instead."; addBuffer(buffers["position"], vertexSize, linkage); } else { LogError << "No" << name << "Buffer found."; } } } }
WidgetChatCenter::WidgetChatCenter(QWidget* parent) : QMainWindow(parent), ui(new Ui::WidgetChatCenter) { ui->setupUi(this); quazaaIrc = new QuazaaIRC(); if(quazaaSettings.Chat.ConnectOnStartup) { quazaaIrc->startIrc(quazaaSettings.Chat.IrcUseSSL, quazaaSettings.Profile.IrcNickname, quazaaSettings.Profile.IrcUserName, quazaaSettings.Chat.IrcServerName, quazaaSettings.Chat.IrcServerPort); ui->actionConnect->setEnabled(false); ui->actionDisconnect->setEnabled(true); qDebug() << "Trying to connect to IRC"; } else { ui->actionConnect->setEnabled(true); ui->actionDisconnect->setEnabled(false); } restoreState(quazaaSettings.WinMain.ChatToolbars); connect(&skinSettings, SIGNAL(skinChanged()), this, SLOT(skinChangeEvent())); connect(quazaaIrc, SIGNAL(appendMessage(Irc::Buffer*, QString, QString, QuazaaIRC::Event)), this, SLOT(appendMessage(Irc::Buffer*, QString, QString, QuazaaIRC::Event))); connect(quazaaIrc, SIGNAL(channelNames(QStringList)), this, SLOT(channelNames(QStringList))); connect(quazaaIrc, SIGNAL(bufferAdded(QString)), this, SLOT(addBuffer(QString))); connect(quazaaIrc, SIGNAL(setPrefixes(QString, QString)), this, SLOT(setPrefixes(QString, QString))); //connect(quazaaIrc, SIGNAL(joined(QString)), this, SLOT(joined(QString))); WidgetChatTab* wct = new WidgetChatTab(quazaaIrc, this); ui->tabWidget->addTab(wct, "Status"); wct->setName("*status"); // * is not allowed by RFC (IIRC) skinChangeEvent(); }
Buffer* BufferManager::getCurrentBuffer(__int32 minimumSize, bool forceNewBuffer) { Buffer* result = NULL; if (!_activeBuffers->empty()) { result = _activeBuffers->back(); // because some other action could change the currentPos // we should ensure its in the right place before returning the buffer result->seek(result->currentPos()); } if ((forceNewBuffer) || (result == NULL) || (result->spaceLeft() < minimumSize)) { bool newBuffer; int flag = 0x01; // Active buffer if (_reusableBuffers->empty()) { result = createNewBuffer(); newBuffer = true; } else { result = _reusableBuffers->front(); _reusableBuffers->pop(); result->reset(); newBuffer = false; } addBuffer(result); result->seek(0); registerBufferControlFile(result, newBuffer, flag); } return result; }
int WaylandNativeWindow::setBufferCount(int cnt) { int start = 0; TRACE("cnt:%d", cnt); if (m_bufList.size() == cnt) return NO_ERROR; lock(); if (m_bufList.size() > cnt) { /* Decreasing buffer count, remove from beginning */ std::list<WaylandNativeWindowBuffer*>::iterator it = m_bufList.begin(); for (int i = 0; i <= m_bufList.size() - cnt; i++ ) { destroyBuffer(*it); ++it; m_bufList.pop_front(); } } else { /* Increasing buffer count, start from current size */ for (int i = m_bufList.size(); i < cnt; i++) WaylandNativeWindowBuffer *unused = addBuffer(); } unlock(); return NO_ERROR; }
void BufferPool::getMoreSpace(t_audoNSamples minRequired) { int buffersNeeded = minRequired / m_blockSize + 1; for (int i = 0; i < buffersNeeded; ++i) { addBuffer(); } }
bool loadSoundFile(const std::string &name, const std::string &filepath) { SoundBuffer *buf = loadOggFile(filepath); if(buf) addBuffer(name, buf); return false; }
bool loadSoundData(const std::string &name, const std::string &filedata) { SoundBuffer *buf = load_ogg_from_buffer(filedata, name); if (buf) addBuffer(name, buf); return false; }
void BufferQueueDump::onAcquireBuffer(const int& slot, const sp<GraphicBuffer>& buffer, const sp<Fence>& fence, const int64_t& timestamp) { if (TRACK_CONSUMER == mMode) { addBuffer(slot, buffer, fence, timestamp); } }
std::shared_ptr<SimpleMesh> SimpleMeshCreator::sphere(float radius, unsigned int numLoops, unsigned int segmentsPerLoop, vec4 color) { auto spheremesh = std::make_shared<SimpleMesh>(); numLoops = std::max(4u, numLoops); segmentsPerLoop = std::max(8u, segmentsPerLoop); // Set identity matrix spheremesh->setModelMatrix(mat4(1.f)); // Create Vertices auto normals = std::make_shared<Vec3BufferRAM>((numLoops + 1) * (segmentsPerLoop + 1)); auto normalBuffer = std::make_shared<Buffer<vec3>>(normals); unsigned int pointsPerLine = segmentsPerLoop + 1; for (unsigned int i = 0; i <= numLoops; ++i) { for (unsigned int j = 0; j <= segmentsPerLoop; ++j) { float theta = (i * static_cast<float>(M_PI) / numLoops); // + ((static_cast<float>(M_PI) * j) / (segmentsPerLoop * numLoops)); if (i == numLoops) theta = static_cast<float>(M_PI); float phi = j * 2 * static_cast<float>(M_PI) / segmentsPerLoop; float sinTheta = std::sin(theta); float sinPhi = std::sin(phi); float cosTheta = std::cos(theta); float cosPhi = std::cos(phi); vec3 normal(cosPhi * sinTheta, sinPhi * sinTheta, cosTheta); vec3 vert(normal * radius); vec3 texCoord(static_cast<float>(j) / segmentsPerLoop, static_cast<float>(i) / numLoops, 0.0f); spheremesh->addVertex(vert, texCoord, color); normals->set(i * pointsPerLine + j, normal); } } spheremesh->addBuffer(BufferType::NormalAttrib, normalBuffer); // Create Indices // compute indices spheremesh->setIndicesInfo(DrawType::Triangles, ConnectivityType::None); for (unsigned int y = 0; y < numLoops; ++y) { auto indices = std::make_shared<IndexBufferRAM>(pointsPerLine * 2); auto indexBuf = std::make_shared<IndexBuffer>(indices); unsigned int offset = y * pointsPerLine; std::size_t count = 0; for (unsigned int x = 0; x < pointsPerLine; ++x) { indices->set(count++, offset + x); indices->set(count++, offset + x + pointsPerLine); } spheremesh->addIndicies(Mesh::MeshInfo(DrawType::Triangles, ConnectivityType::Strip), indexBuf); } return spheremesh; }
void BufferManager::loadBuffers() { openLogFile(); __int32 buffers = _controlFile->readInt(); // The buffers are circular, this means that some // buffers are in the front of the control file but // they are at the tail of the queue std::vector<Buffer*> front; std::vector<Buffer*> tail; bool addToTail = true; for (__int32 x = 0; x < buffers; x++) { __int32 controlPosition = _controlFile->currentPos(); char flag = _controlFile->readChar(); __int64 startOffset = _controlFile->readLong(); __int64 bufferLen = _controlFile->readLong(); char* logFileName = _controlFile->readChars(); Buffer* buffer = new Buffer(this, logFileName, startOffset, _buffersSize, _buffersSize / pageSize()); buffer->setControlPosition(controlPosition); if (flag & 0x01) { if (addToTail) { tail.push_back(buffer); } else { front.push_back(buffer); } } else { addToTail = false; addReusable(buffer); } free(logFileName); } for (std::vector<Buffer*>::iterator iter = front.begin(); iter != front.end(); iter++) { Buffer* buffer = *iter; addBuffer(buffer); } for (std::vector<Buffer*>::iterator iter = tail.begin(); iter != tail.end(); iter++) { Buffer* buffer = *iter; addBuffer(buffer); } }
CSVGBuffer * CSVGBufferMgr:: createBuffer(const string &name) { CSVGBuffer *buffer = new CSVGBuffer(svg_, name); buffer->setAntiAlias(anti_alias_); addBuffer(buffer); return buffer; }
size_t DumpFile::readFromFile(const char * filename) { ifstream myFile (filename, ios::binary); cout << endl << "Reading buffers from file: " << filename << endl; cout <<"----------------------------------------" << endl; if(!myFile){ cerr << "file " << filename << " could not be opened!" << endl; exit (-1); } while(myFile.peek() != EOF){ DumpBuffer* baseptr = new DumpBuffer(myFile); DataType ti = baseptr->getTypeInfo(); DumpBuffer* temp; switch(ti){ case T_INT: temp = new DumpWrapper<int>( *baseptr, LOCAL ); break; case T_UINT: temp = new DumpWrapper<unsigned int>( *baseptr, LOCAL ); break; case T_SHORT: temp = new DumpWrapper<short>( *baseptr, LOCAL ); break; case T_USHORT: temp = new DumpWrapper<unsigned short>( *baseptr, LOCAL ); break; case T_DOUBLE: temp = new DumpWrapper<double>( *baseptr, LOCAL ); break; case T_CHAR: temp = new DumpWrapper<char>( *baseptr, LOCAL ); break; case T_FLOAT: default: temp = new DumpWrapper<float>( *baseptr, LOCAL ); break; } addBuffer(temp); delete baseptr; } myFile.close(); return _buffers.size(); }
Buffer * DB::getBuffer(const std::string & netName, const std::string & name, const bool create) { for (Buffer & buf : buffers) { if (buf.name == name && getNetwork(buf.networkid)->name == netName) { return &buf; } } if (create) { Buffer buf; buf.networkid = getNetwork(netName, true)->id; buf.name = name; return addBuffer(buf); } return NULL; }
std::shared_ptr<SimpleMesh> SimpleMeshCreator::plane(glm::vec3 pos, glm::vec2 extent, unsigned int meshResX, unsigned int meshResY) { auto plane = std::make_shared<SimpleMesh>(); // Set identity matrix plane->setModelMatrix(mat4(1.f)); meshResX = std::max(1u, meshResX); meshResY = std::max(1u, meshResY); glm::vec3 p0(pos - glm::vec3(extent, 0.0f) * 0.5f); glm::vec3 texCoordDelta(1.0f / glm::vec2(meshResX, meshResY), 0.0f); glm::vec3 stepDelta(extent.x * texCoordDelta.x, extent.y * texCoordDelta.y, 0.0f); unsigned int pointsPerLine = meshResX + 1; const glm::vec4 color(0.6f, 0.6f, 0.6f, 1.0f); auto normals = std::make_shared<Vec3BufferRAM>((meshResX + 1) * (meshResY + 1)); auto normalBuffer = std::make_shared<Buffer<vec3>>(normals); for (unsigned int y = 0; y <= meshResY; ++y) { for (unsigned int x = 0; x <= meshResX; ++x) { glm::vec3 tCoord(texCoordDelta * glm::vec3(x, y, 0.0f)); plane->addVertex(p0 + stepDelta * glm::vec3(x, y, 0.0f), tCoord, color); normals->set(y * pointsPerLine + x, vec3(0.0f, 0.0f, 1.0f)); } } plane->addBuffer(BufferType::NormalAttrib, normalBuffer); // compute indices plane->setIndicesInfo(DrawType::Triangles, ConnectivityType::None); for (unsigned int y = 0; y < meshResY; ++y) { auto indices = std::make_shared<IndexBufferRAM>(pointsPerLine * 2); auto indexBuf = std::make_shared<IndexBuffer>(indices); unsigned int offset = y * pointsPerLine; std::size_t count = 0; for (unsigned int x = 0; x < pointsPerLine; ++x) { indices->set(count++, offset + x); indices->set(count++, offset + x + pointsPerLine); } plane->addIndicies(Mesh::MeshInfo(DrawType::Triangles, ConnectivityType::Strip), indexBuf); } return plane; }
/* * get buffer from the free list */ void* getBuffer(linkedList* list) { // if no buffer at all, add one and return if (!list->bufferList) { addBuffer(list); } // choose one buffer to assign list->occupy++; buffer* buf = list->bufferList; list->bufferList = (buffer*)buf->head; buf->head = (void*)list; mainList* mainlist = mainPage->ptr; if (list != &mainlist->ll) mainlist->occupy++; void* bufferPoint = (void*)buf + sizeof(buffer); return bufferPoint; }
void Mesh::init() { LogDebug << "Buffersize" << buffers.count() << "Indexsize" << indices.size(); assert(!buffers.empty()); assert(!buffers["position"].empty()); assert(!indices.empty()); /* Allocate and assign a Vertex Array Object to our handle */ glGenVertexArrays(1, &vao); LogDebug << "Generating Vertex Array Object #" << vao; /* Bind our Vertex Array Object as the current used object */ glBindVertexArray(vao); addBuffer(buffers["position"], 3, "in_Vertex"); initBuffer("color", "in_Color", 3); initBuffer("normal", "in_Normal", 3); initBuffer("tangent", "in_Tangent", 3); initBuffer("bitangent", "in_Bitangent", 3); initBuffer("uv", "in_Uv", 2); addElementBuffer(indices); }
int XHCP_server (node_t* argXmlConfig) { static int listener; static int conn; static pid_t pid; static struct sockaddr_in servaddr; static char *additionalDataBuffer=NULL; static int argc = 0; static char *argv[MAX_CMD_ARGS+1]; static char buffer[MAX_REQ_LINE] = {0}; int status, nbCar; switch (XHCP_running_status) { /* ------------------------------------------------------------------------ */ case (XHCPstate_init): XHCP_loadConfig(argXmlConfig); /* Create socket */ if ( (listener = socket (AF_INET, SOCK_STREAM, 0)) < 0 ) Error_Quit ("Couldn't create listening socket."); /* Populate socket address structure */ memset (&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl (INADDR_ANY); servaddr.sin_port = htons (XHCP_SERVER_PORT); /* "Address already in use" error message killer !!!! */ int tr=1; if (setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&tr,sizeof(int)) == -1) { perror("setsockopt"); exit(1); } /* Assign socket address to socket */ if ( bind (listener, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) { perror("Bind"); Error_Quit ("Couldn't bind listening socket."); } /* Make socket a listening socket */ if ( listen (listener, LISTENQ) < 0 ) Error_Quit ("Call to listen failed."); int flags = fcntl(listener, F_GETFL ); fcntl(listener, F_SETFL, flags | O_NONBLOCK ); XHCP_customWelcomeMessage (); /* L'initialisation est terminée, on passe à la suite */ XHCP_running_status = XHCPstate_waitConnect; /* No break, we continue !!!*/ /* ------------------------------------------------------------------------ */ case (XHCPstate_waitConnect): /* Wait for connection */ if ( (conn = accept (listener, NULL, NULL)) < 0 ) { if ( (errno == EWOULDBLOCK) || (errno == EAGAIN) ) { return XHCP_running_status; } else Error_Quit ("Error calling accept()"); } //TODO Gestion du timeout XHCP_printXHCPResponse (conn, RES_HALWELCOM ); // Petit message de bienvenue XHCP_running_status = XHCPstate_waitCommand; /* No break, we continue !!!*/ /* ------------------------------------------------------------------------ */ case (XHCPstate_waitCommand): if ( (nbCar = recv(conn, buffer, MAX_REQ_LINE - 1, MSG_DONTWAIT)) <0 ) { if ( (errno == EWOULDBLOCK) || (errno == EAGAIN) ) { //TODO Gestion du timeout return XHCP_running_status; } else Error_Quit ("Error calling accept()"); } buffer[nbCar]='\0'; Trim (buffer, 0); // We suppress all extra characters if ( buffer[0] == '\0' ) return XHCP_running_status; // We continue.... printf ("Ligne lue : %s\n", buffer); cut_Line (buffer, &argc, argv); printf ("%d arguments :\n", argc); int i; for ( i=0; i<argc; i++ ) printf ( "%d - %s\n", i, argv[i]); status = exec_Line (conn, argc, argv ); // We compute the line... printf("Ligne executee, statut = %d\n",status); switch (status) { case -1: // deconnexion XHCP_running_status = XHCPstate_endConnect; return XHCP_running_status; break; case 0: // Fin de la commande return XHCP_running_status; break; // default : // On continue } XHCP_running_status = XHCPstate_waitData; /* No break, we continue !!!*/ /* ------------------------------------------------------------------------ */ case (XHCPstate_waitData): if ( (nbCar = recv(conn, buffer, MAX_REQ_LINE - 1, MSG_DONTWAIT)) <0 ) { if ( (errno == EWOULDBLOCK) || (errno == EAGAIN) ) { //TODO Gestion du timeout return XHCP_running_status; } else Error_Quit ("Error calling accept()"); } buffer[nbCar]='\0'; /* We suppress all extra characters on the right except '.' and '>' */ Trim (buffer, 1); /* The handler is activate, so all lignes are added in buffer */ if ( buffer[0] == '.' && buffer[1] == '\0') { additionalDataHandler (conn, argc, argv, additionalDataBuffer ); additionalDataHandler = NULL; free (additionalDataBuffer); additionalDataBuffer = NULL; XHCP_running_status = XHCPstate_waitCommand; } else { additionalDataBuffer = addBuffer (additionalDataBuffer, buffer); } break; /* ------------------------------------------------------------------------ */ case (XHCPstate_endConnect): XHCP_printXHCPResponse (conn, RES_CLOCONBYE ); if ( close (conn) < 0 ) Error_Quit ("Error closing connection socket in parent."); XHCP_running_status = XHCPstate_waitConnect; //default : /* (XHCPstate_death) */ /* Do nothing ... */ } return XHCP_running_status; }
SimpleMesh::SimpleMesh(DrawType dt, ConnectivityType ct) : Mesh(dt, ct) { addBuffer(BufferType::PositionAttrib, std::make_shared<Buffer<vec3>>()); // pos 0 addBuffer(BufferType::TexcoordAttrib, std::make_shared<Buffer<vec3>>()); // pos 1 addBuffer(BufferType::ColorAttrib, std::make_shared<Buffer<vec4>>()); // pos 2 }
void BufferQueueDump::onDequeueBuffer(const int& slot, const sp<GraphicBuffer>& buffer, const sp<Fence>& fence) { if (TRACK_PRODUCER == mMode) addBuffer(slot, buffer, fence); }
int WaylandNativeWindow::dequeueBuffer(BaseNativeWindowBuffer **buffer, int *fenceFd){ HYBRIS_TRACE_BEGIN("wayland-platform", "dequeueBuffer", ""); WaylandNativeWindowBuffer *wnb=NULL; TRACE("%p", buffer); lock(); HYBRIS_TRACE_BEGIN("wayland-platform", "dequeueBuffer_wait_for_buffer", ""); HYBRIS_TRACE_COUNTER("wayland-platform", "m_freeBufs", "%i", m_freeBufs); while (m_freeBufs==0) { HYBRIS_TRACE_COUNTER("wayland-platform", "m_freeBufs", "%i", m_freeBufs); pthread_cond_wait(&cond,&mutex); } std::list<WaylandNativeWindowBuffer *>::iterator it = m_bufList.begin(); for (; it != m_bufList.end(); it++) { if ((*it)->busy) continue; if ((*it)->youngest == 1) continue; break; } if (it==m_bufList.end()) { HYBRIS_TRACE_BEGIN("wayland-platform", "dequeueBuffer_worst_case_scenario", ""); HYBRIS_TRACE_END("wayland-platform", "dequeueBuffer_worst_case_scenario", ""); it = m_bufList.begin(); for (; it != m_bufList.end() && (*it)->busy; it++) {} } if (it==m_bufList.end()) { unlock(); HYBRIS_TRACE_BEGIN("wayland-platform", "dequeueBuffer_no_free_buffers", ""); HYBRIS_TRACE_END("wayland-platform", "dequeueBuffer_no_free_buffers", ""); TRACE("%p: no free buffers", buffer); return NO_ERROR; } wnb = *it; assert(wnb!=NULL); HYBRIS_TRACE_END("wayland-platform", "dequeueBuffer_wait_for_buffer", ""); /* If the buffer doesn't match the window anymore, re-allocate */ if (wnb->width != m_window->width || wnb->height != m_window->height || wnb->format != m_format || wnb->usage != m_usage) { TRACE("wnb:%p,win:%p %i,%i %i,%i x%x,x%x x%x,x%x", wnb,m_window, wnb->width,m_window->width, wnb->height,m_window->height, wnb->format,m_format, wnb->usage,m_usage); destroyBuffer(wnb); m_bufList.erase(it); wnb = addBuffer(); } wnb->busy = 1; *buffer = wnb; --m_freeBufs; HYBRIS_TRACE_COUNTER("wayland-platform", "m_freeBufs", "%i", m_freeBufs); HYBRIS_TRACE_BEGIN("wayland-platform", "dequeueBuffer_gotBuffer", "-%p", wnb); HYBRIS_TRACE_END("wayland-platform", "dequeueBuffer_gotBuffer", "-%p", wnb); HYBRIS_TRACE_END("wayland-platform", "dequeueBuffer_wait_for_buffer", ""); unlock(); return NO_ERROR; }
BinaryMemory& BinaryMemory::operator+(const BinaryMemory &ref){ addBuffer(ref.getBuffer(), ref.getBufferSize()); return *this; }
void BinaryMemory::addBuffer(const BinaryMemory &ref){ addBuffer(ref.getBuffer(), ref.getBufferSize()); }
void PacketCreator::addBuffer(PacketCreator &packet) { addBuffer(packet.getBuffer(), packet.getSize()); }
void PacketCreator::addBuffer(PacketReader &packet) { addBuffer(packet.getBuffer(), packet.getBufferLength()); }
SimpleMesh::SimpleMesh(DrawType dt, ConnectivityType ct) : Mesh(dt, ct) { addBuffer(BufferType::POSITION_ATTRIB, std::make_shared<Buffer<vec3>>()); // pos 0 addBuffer(BufferType::TEXCOORD_ATTRIB, std::make_shared<Buffer<vec3>>()); // pos 1 addBuffer(BufferType::COLOR_ATTRIB, std::make_shared<Buffer<vec4>>()); // pos 2 addIndicies(Mesh::MeshInfo(dt, ct), std::make_shared<IndexBuffer>()); }
int XHCP_server (node_t* argXmlConfig) { static const int zero=0; static int listener; static int conn; static pid_t pid; //static struct sockaddr_in servaddr; static struct sockaddr_storage servaddr; static struct timeval time_conn, time_resp; static char *additionalDataBuffer=NULL; static int argc = 0; static char *argv[MAX_CMD_ARGS+1]; static char buffer[MAX_REQ_LINE] = {0}; int status, nbCar; // Variables non persistantes int sockV4, sockV6; struct addrinfo hints, *list_addr, *p_adresse, *aiv4, *aiv6, *choix_ai; switch (XHCP_running_status) { /* ------------------------------------------------------------------------ */ case (XHCPstate_init): XHCP_loadConfig(argXmlConfig); /* Create socket */ // if ( (listener = socket (AF_INET, SOCK_STREAM, 0)) < 0 ) // Error_Quit ("Couldn't create listening socket."); /* Populate socket address structure */ memset (&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if ( (getaddrinfo(NULL, XHCP_SERVER_PORT, &hints, &list_addr)) < 0 ) { //printf("getaddrinfo: %s\n",gai_strerroe(flags)); perror("getaddrinfo"); exit(1); } sockV4=-1; sockV6=-1; p_adresse = list_addr; // Vérification des protocoles en testant l'ouverture d'une socket while (p_adresse) { if ( p_adresse->ai_family == AF_INET6) { if ( sockV6 < 0 ) { printf("Scanning IPV6..."); if ( (sockV6=socket(p_adresse->ai_family, p_adresse->ai_socktype, p_adresse->ai_protocol)) >= 0 ) { aiv6 = p_adresse; printf(" found"); } printf("\n"); } } else if ( p_adresse->ai_family == AF_INET) { if ( sockV4 < 0 ) { printf("Scanning IPV4..."); if ( (sockV4=socket(p_adresse->ai_family, p_adresse->ai_socktype, p_adresse->ai_protocol)) >= 0 ) { aiv4 = p_adresse; printf(" found"); } printf("\n"); } } p_adresse = p_adresse->ai_next; } // Selection de la socket if ( sockV6 >= 0 ) { choix_ai = aiv6; listener = sockV6; // Tentative d'activation de la traduction IPV6->IPV4 if ( setsockopt(sockV6, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) < 0 ) { perror("notice : setsockopt(IPV6_ONLY)"); printf("IPV6 ONLY ! Tentative de passer en IPV4...\n"); if (sockV4 >=0) { close(sockV6); sockV6 = -1; } } else { //Traduction possible, on ferme l'ipv4 if (sockV4 >= 0) { printf("IPV4 over IPV6 => close IPV4\n"); close(sockV4); sockV4=-1; } } } if ( sockV4 >= 0 ) { choix_ai = aiv4; listener = sockV4; } else if ( sockV6 < 0 ) { Error_Quit ("Aucun protocole IPV4 ou IPV6 possible."); } /* "Address already in use" error message killer !!!! */ int tr=1; if (setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&tr,sizeof(int)) == -1) { perror("setsockopt"); exit(1); } /* Assign socket address to socket */ if ( bind (listener, choix_ai->ai_addr, choix_ai->ai_addrlen) < 0 ) { perror("Bind"); Error_Quit ("Couldn't bind listening socket."); } /* Make socket a listening socket */ if ( listen (listener, LISTENQ) < 0 ) Error_Quit ("Call to listen failed."); int flags = fcntl(listener, F_GETFL ); fcntl(listener, F_SETFL, flags | O_NONBLOCK ); freeaddrinfo(list_addr); /* Diverses initialisations */ XHCP_customWelcomeMessage (); /* L'initialisation est terminée, on passe à la suite */ XHCP_running_status = XHCPstate_waitConnect; /* No break, we continue !!!*/ /* ------------------------------------------------------------------------ */ case (XHCPstate_waitConnect): /* Wait for connection */ if ( (conn = accept (listener, NULL, NULL)) < 0 ) { if ( (errno == EWOULDBLOCK) || (errno == EAGAIN) ) { return XHCP_running_status; } else Error_Quit ("Error calling accept()"); } //Gestion du timeout pour la bascule en mode commande gettimeofday(&time_conn, NULL); //XHCP_running_status = XHCPstate_waitCommand; XHCP_running_status = XHCPstate_waitHTTPRequest; /* No break, we continue !!!*/ /* ------------------------------------------------------------------------ */ case (XHCPstate_waitHTTPRequest): if ( (nbCar = recv(conn, buffer, MAX_REQ_LINE - 1, MSG_DONTWAIT)) <0 ) { if ( (errno == EWOULDBLOCK) || (errno == EAGAIN) ) { //TODO Gestion du timeout gettimeofday(&time_resp, NULL); if ( timerdiff(&time_conn, &time_resp) > 100000 ) { // On passe en mode "Commande" XHCP_running_status = XHCPstate_waitCommand; XHCP_printXHCPResponse (conn, RES_HALWELCOM ); // Petit message de bienvenue } return XHCP_running_status; } else Error_Quit ("Error calling accept()"); } buffer[nbCar]='\0'; /* We suppress all extra characters */ Trim (buffer, 0); processHttpRequest(conn, buffer); if ( close (conn) < 0 ) Error_Quit ("Error closing connection socket in parent."); XHCP_running_status = XHCPstate_waitConnect; return XHCP_running_status; /* ------------------------------------------------------------------------ */ case (XHCPstate_waitCommand): if ( (nbCar = recv(conn, buffer, MAX_REQ_LINE - 1, MSG_DONTWAIT)) <0 ) { if ( (errno == EWOULDBLOCK) || (errno == EAGAIN) ) { //TODO Gestion du timeout return XHCP_running_status; } else Error_Quit ("Error calling accept()"); } buffer[nbCar]='\0'; Trim (buffer, 0); // We suppress all extra characters if ( buffer[0] == '\0' ) return XHCP_running_status; // We continue.... printf ("Ligne lue : %s\n", buffer); cut_Line (buffer, &argc, argv); printf ("%d arguments :\n", argc); int i; for ( i=0; i<argc; i++ ) printf ( "%d - %s\n", i, argv[i]); status = exec_Line (conn, argc, argv ); // We compute the line... printf("Ligne executee, statut = %d\n",status); switch (status) { case -1: // deconnexion XHCP_running_status = XHCPstate_endConnect; return XHCP_running_status; break; case 0: // Fin de la commande return XHCP_running_status; break; // default : // On continue } XHCP_running_status = XHCPstate_waitData; /* No break, we continue !!!*/ /* ------------------------------------------------------------------------ */ case (XHCPstate_waitData): if ( (nbCar = recv(conn, buffer, MAX_REQ_LINE - 1, MSG_DONTWAIT)) <0 ) { if ( (errno == EWOULDBLOCK) || (errno == EAGAIN) ) { //TODO Gestion du timeout return XHCP_running_status; } else Error_Quit ("Error calling accept()"); } buffer[nbCar]='\0'; /* We suppress all extra characters on the right except '.' and '>' */ Trim (buffer, 1); /* The handler is activate, so all lignes are added in buffer */ if ( buffer[0] == '.' && buffer[1] == '\0') { additionalDataHandler (conn, argc, argv, additionalDataBuffer ); additionalDataHandler = NULL; free (additionalDataBuffer); additionalDataBuffer = NULL; XHCP_running_status = XHCPstate_waitCommand; } else { additionalDataBuffer = addBuffer (additionalDataBuffer, buffer); } break; /* ------------------------------------------------------------------------ */ case (XHCPstate_endConnect): XHCP_printXHCPResponse (conn, RES_CLOCONBYE ); if ( close (conn) < 0 ) Error_Quit ("Error closing connection socket in parent."); XHCP_running_status = XHCPstate_waitConnect; //default : /* (XHCPstate_death) */ /* Do nothing ... */ } return XHCP_running_status; }