void ClientManage::readySend(const QByteArray &data, const quint32 &flag) { ClientSocket *socket = NULL; for(auto now = m_sockets.begin(); now != m_sockets.end(); now++) { if(!now.value()) { socket = (ClientSocket *)now.key(); } } if(!socket && (m_sockets.size() <= m_clientSettings.connectCount())) { for(int now = 0; now < m_clientSettings.connectTryMax(); now++) { socket = new ClientSocket(m_clientSettings.connectAddress(), m_clientSettings.connectPort(), m_socketSettings); connect(socket, SIGNAL(accepted(Socket*,QByteArray)), this, SLOT(accepted(Socket*,QByteArray)), Qt::DirectConnection); connect(socket, SIGNAL(disconnected(Socket*,SocketExitFlag,int)), this, SLOT(disconnected(Socket*)), Qt::DirectConnection); if(socket->begin()) { break; } delete socket; socket = NULL; } } if(!socket) { qDebug("ClientManage: No more available client"); if(flag) { emit finished(flag, QByteArray(), false); } } else { socket->send(data); m_sockets[socket] = flag; } }
int main(int argc, char* argv[]) { ClientSocket client; bool succ; if(argc > 1) succ = client.create(argv[1], 27016, NULL); else succ = client.create("127.0.0.1", 27016, NULL); if(!succ) printf("connent failed"); int i = 0; while(true) { std::stringstream ss; ss << client.get_id() << "|" << i++ ; client.send(ss.str().c_str()); Sleep(100); } }
int ChatWindow::handleInput(SDL_Event &event, ClientSocket& sock) { // Control del input de texto int res = input_box.handleInput(event); if(res == SInput::RES_CLOSE) { this->hide(); }else if(res == SInput::RES_ENTER) { BitStream bs; bs << PROTO::CHAT << nick_local << nick_destino << input_box.getText(); sock.send(bs.str()); } // Control del input del mouse if(event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) { if( event.button.x < x + this->w && event.button.x > x + this->w - CLOSE_BUTTON_SIDE && event.button.y > y && event.button.y < y + CLOSE_BUTTON_SIDE) { res = SInput::RES_CLOSE; this->hide(); } } return res; }
void* clientThread (void* arg) { ClientSocket* client = (ClientSocket*)arg; client->setReceiveTimeout(5); char buffer[(1024 * 4) + 1]; ssize_t size = client->receive(buffer, 1024 * 4); buffer[size] = 0; if (size > 0) { HttpRequest* request = new HttpRequest(buffer, (size_t)size); HttpResponse* response = new HttpResponse(); if ((request->uri != NULL) && (request->method != NULL)) { std::string path = documentRoot; path += request->uri; struct stat fileInfo; int statRes = stat(path.c_str(), &fileInfo); if ((statRes == -1) || ((fileInfo.st_mode & S_IFMT) != S_IFREG)) { response->status = "404"; response->reason = "Not found"; response->contentLength = 9; response->headers.push_back("Content-Type: text/html"); std::string answer = response->compileHeader(); client->send(answer.c_str(), answer.length()); client->send("Not found", response->contentLength); } else { response->status = "200"; response->reason = "OK"; // Get content-type. const char * contentType = mineMapping.parsePath(path.c_str()); if (contentType != NULL) { std::string contentTypeHeader = "Content-Type: "; contentTypeHeader += contentType; response->headers.push_back(contentTypeHeader.c_str()); response->contentLength = fileInfo.st_size; std::string answer = response->compileHeader(); client->send(answer.c_str(), answer.length()); client->sendFile(path.c_str(), fileInfo.st_size); } else { FcgiBridge* fcgi = new FcgiBridge(client, response); fcgi->query(request->method, path.c_str(), request->queryString, request->headerDataBlock); delete fcgi; } } } delete request; delete response; } client->close(); delete client; return NULL; }
void FileTransferSocketThread::execute() { if(info.hostType == eServer) { ServerSocket serverSocket; serverSocket.bind(this->info.serverPort); serverSocket.listen(1); Socket *clientSocket = serverSocket.accept(); char data[513]=""; memset(data, 0, 256); clientSocket->receive(data,256, true); if(*data == SEND_FILE) { FileInfo file; memcpy(&file, data+1, sizeof(file)); *data=ACK; clientSocket->send(data,256); Checksum checksum; checksum.addFile(file.fileName); file.filecrc = checksum.getSum(); ifstream infile(file.fileName.c_str(), ios::in | ios::binary | ios::ate); if(infile.is_open() == true) { file.filesize = infile.tellg(); infile.seekg (0, ios::beg); memset(data, 0, 256); *data=SEND_FILE; memcpy(data+1,&file,sizeof(file)); clientSocket->send(data,256); clientSocket->receive(data,256, true); if(*data != ACK) { //transfer error } int remain=file.filesize % 512 ; int packs=(file.filesize-remain)/512; while(packs--) { infile.read(data,512); //if(!ReadFile(file,data,512,&read,NULL)) // ; //read error //if(written!=pack) // ; //read error clientSocket->send(data,512); clientSocket->receive(data,256, true); if(*data!=ACK) { //transfer error } } infile.read(data,remain); //if(!ReadFile(file,data,remain,&read,NULL)) // ; //read error //if(written!=pack) // ; //read error clientSocket->send(data,remain); clientSocket->receive(data,256, true); if(*data!=ACK) { //transfer error } infile.close(); } } delete clientSocket; } else { Ip ip(this->info.serverIP); ClientSocket clientSocket; clientSocket.connect(this->info.serverIP, this->info.serverPort); if(clientSocket.isConnected() == true) { FileInfo file; file.fileName = this->info.fileName; //file.filesize = //file.filecrc = this->info. string path = extractDirectoryPathFromFile(file.fileName); createDirectoryPaths(path); ofstream outFile(file.fileName.c_str(), ios_base::binary | ios_base::out); if(outFile.is_open() == true) { char data[513]=""; memset(data, 0, 256); *data=SEND_FILE; memcpy(data+1,&file,sizeof(file)); clientSocket.send(data,256); clientSocket.receive(data,256, true); if(*data!=ACK) { //transfer error } clientSocket.receive(data,256,true); if(*data == SEND_FILE) { memcpy(&file, data+1, sizeof(file)); *data=ACK; clientSocket.send(data,256); int remain = file.filesize % 512 ; int packs = (file.filesize-remain) / 512; while(packs--) { clientSocket.receive(data,512,true); outFile.write(data, 512); if(outFile.bad()) { //int ii = 0; } //if(!WriteFile(file,data,512,&written,NULL)) // ; //write error //if(written != pack) // ; //write error *data=ACK; clientSocket.send(data,256); } clientSocket.receive(data,remain,true); outFile.write(data, remain); if(outFile.bad()) { //int ii = 0; } //if(!WriteFile(file,data,remain,&written,NULL)) // ; //write error //if(written!=pack) // ; //write error *data=ACK; clientSocket.send(data,256); Checksum checksum; checksum.addFile(file.fileName); uint32 crc = checksum.getSum(); if(file.filecrc != crc) { //int ii = 0; } //if(calc_crc(file)!=info.crc) // ; //transfeer error } outFile.close(); } } } }
void Tile::blit(SDL_Surface* pantalla, Camara& cam, Personaje* personaje, Mapa* mapa){ Tile* tilePersonaje = mapa->getTilePorPixeles(personaje->getX(), personaje->getY()); int deltaX = personaje -> getXAnclajeNiebla() - x; int deltaY = personaje -> getYAnclajeNiebla() - y; if((abs(deltaX) <= personaje->getRadioX()) && (abs(deltaY) <= personaje->getRadioY())){ //dibujo en colores normales for(auto it = entidades.begin(); it != entidades.end(); ++it) { if ( (!(*it)-> isInTile(this->x , this -> y)) && (!(*it)->isCompartido())){ // es un ancla deltaX = personaje -> getXAnclajeNiebla() - (*it)->getX(); deltaY = personaje -> getYAnclajeNiebla() - (*it)->getY(); if((abs(deltaX) <= personaje->getRadioX()) && (abs(deltaY) <= personaje->getRadioY())){ (*it)->setColor(true,x,y, mapa, (personaje -> getXAnclajeNiebla()), (personaje -> getYAnclajeNiebla()), personaje); (*it)->blit(pantalla, &cam, NULL,x, y,true); (*it)->setDibujada(true, mapa,personaje); } }else{ (*it)->setColor(true,x,y, mapa, (personaje -> getXAnclajeNiebla()), (personaje -> getYAnclajeNiebla()), personaje); (*it)->blit(pantalla, &cam, NULL,x, y,true); (*it)->setDibujada(true, mapa,personaje); } } bool agregoTile = personaje->agregarTilesExplorados(this); if(agregoTile){ BitStream bs; bs << PROTO::NIEBLA_SYNC << this->u << this->v; sock.send(bs.str()); } personaje->agregarTilesExplorados(this); }else{ std::vector<Tile*> tilesExplorados = personaje->getTilesExplorados(); if(std::find(tilesExplorados.begin(), tilesExplorados.end(), this) != tilesExplorados.end()){ //lo bliteo en gris for(auto it = entidades.begin(); it != entidades.end(); ++it) { if((*it)->noDibujaFueraDelRadio()) continue; if((!((*it)->getDibujada())) || ((*it)->get_nombre() == "tierraDefault")){ (*it)->setColor(true,x,y, mapa, (personaje -> getXAnclajeNiebla()), (personaje -> getYAnclajeNiebla()), personaje); (*it)->blit(pantalla, &cam, NULL,x, y,false); (*it)->setDibujada(true, mapa,personaje); }else{ (*it)->blit(pantalla, &cam, NULL,x, y,false); } } } } //bliteo el personaje por si el ancla no estaba en el radio (PASA SI RADIO ES 1)pero el personaje se blitea arriba de las cosas //Tile* tileAnclaPersonaje = personaje->getTileAncla(); //for(auto it = tileAnclaPersonaje->entidades.begin(); it != tileAnclaPersonaje->entidades.end(); ++it){ // if((*it) == personaje){ // (*it)->blit(pantalla, &cam, NULL, x, y); // } //} //asi estaba antes //for(auto it = entidades.begin(); it != entidades.end(); ++it) { // (*it)->blit(pantalla, &cam, NULL,x, y); //} }