Exemplo n.º 1
0
void *child(void *arg) {

  MySocket *client = (MySocket *) arg;

  bool requestActive = true;
  while (requestActive) {
    HTTPRequest *request = new HTTPRequest(client, PORT);
    HTTPResponse *response = new HTTPResponse();
    IptvService *service = new IptvService();

    if (!request->readRequest()) {
      // XXX FIXME throw an exception
      break;
    } else if (request->isHead()) {
      service->head(request, response);
      client->write(response->response());
    } else if (request->isGet()) {
      service->get(request, response);
      client->write(response->response());
      
      shared_ptr<BlockingQueue<string>> queue =
	shared_ptr<BlockingQueue<string>>(new BlockingQueue<string>());
      struct ThreadData *threadData = new struct ThreadData;
      threadData->queue = queue;
      threadData->service = service;
      pthread_t tid;
      pthread_create(&tid, NULL, streamService, threadData);
      pthread_detach(tid);

      try {
	string data;      
	while (true) {
	  data = queue->popFront();
	  HttpUtils::writeChunk(client, data.c_str(), data.length());
	}
      } catch (SocketWriteError swe) {
	requestActive = false;
      } catch (...) {
	// swallow it
      }
      queue->close();
    }
    
    delete response;
    delete request;
  }
  
  client->close();
  delete client;

  return NULL;
}
void MessagesSender::iniciarPartida(string puerto,string nombreUsuario){
    MySocket* socketServidor;
    MySocket* socketCliente;
    Jugador *jug1,*jug2;
    QMutex mutex;
    jug1=new Jugador(COLOR_ROJO);
    jug2=new Jugador(COLOR_BLANCO);
    jug1->setNombre(nombreUsuario);

    try{
        socketServidor=new MySocket(puerto);
        socketServidor->setFlags(O_NONBLOCK);
        socketServidor->listen(1);
        int i,j,indiceMensaje,bytes;
        string nombreJugador1;
        char firmaDelProtocolo[TAMANIO_FIRMA_DEL_PROTOCOLO+1];
        char nombreDelJugador[TAMANIO_NOMBRE_JUGADOR+1];
        char mensaje[BUFFER+1];
        char mensajeRecibido[BUFFER+1];
        int tamanioNombreJugador;
        cout<<"Esperando jugador..."<<endl;
        esperarConecciones=true;
        while( (socketCliente=socketServidor->accept())==nullptr )
        {
            if(!esperarConecciones){
                estaConectado=false;
                delete socketServidor;
                cout<<"Aqui"<<endl;
                return;
            }
        }
        mensaje[0]=FIRMA_DEL_PROTOCOLO[0];
        mensaje[1]=FIRMA_DEL_PROTOCOLO[1];
        mensaje[2]=INICIO_PARTIDA;
        mensaje[3]=VERSION_DEL_PROTOCOLO;
        nombreJugador1=jug1->getNombre();
        indiceMensaje=4;
        for(i=0,j=nombreJugador1.size();i<j;i++){
            mensaje[indiceMensaje++]=nombreJugador1.at(i);
        }
        mensaje[indiceMensaje]=0;
        socketCliente->write(mensaje,indiceMensaje);
        bytes=socketCliente->read(mensajeRecibido,BUFFER);
        memcpy(firmaDelProtocolo,mensajeRecibido,2);
        firmaDelProtocolo[TAMANIO_FIRMA_DEL_PROTOCOLO]='\0';
        memcpy(nombreDelJugador,&mensajeRecibido[2],tamanioNombreJugador=bytes-2);
        nombreDelJugador[tamanioNombreJugador]='\0';
        cout<<"Firma del protocolo:"<<firmaDelProtocolo<<endl;
        cout<<"Nombre del jugador 2:"<<nombreDelJugador<<endl;
        if(strncmp(firmaDelProtocolo,FIRMA_DEL_PROTOCOLO,2)!=0){
            estaConectado=false;
            cout<<"No se recibio la bandera adecuada"<<endl;
            delete socketCliente;
            return;
        }
        /*cout<<socketCliente<<endl;
        cout<<socketCliente->getIp()<<endl;
        jug2->setHost(socketCliente->getIp());
        cout<<"IP:"<< jug2->getHost()<<endl;
        cout<<"Puerto:"<<socketCliente->getPort()<<endl;*/
        jug2->setNombre(string(nombreDelJugador));
        mutex.lock();
        socketCliente->setFlags(O_NONBLOCK);
        MessagesSender::socketCliente=socketCliente;
        MessagesSender::socketServidor=socketServidor;
        Tablero tablero(jug1,jug2);
        ::tablero=move(tablero);
        mutex.unlock();
        estaConectado=true;
    }catch(SocketException& ex){
        cout<<ex.getMessage()<<endl;
    }
}