void leerSiguienteRecurso(t_personaje *personaje) {
	if(!personaje->nivel_actual) {
		return;
	}
	if(!list_size(personaje->nivel_actual->datos_nivel->recursos)) {
		// Terminó el nivel
		log_info(logger, "Nivel terminado.");
		terminarNivel(personaje);
	}
	else {
		char* recursoSimbolo = (char *) list_remove(personaje->nivel_actual->datos_nivel->recursos, 0);
		log_info(logger, "El recurso es: %s.", recursoSimbolo);

		t_mensaje* requestRecurso = mensaje_create(POS_RECURSO, recursoSimbolo);
		socketSend(personaje->connNivel, requestRecurso, handleConnectionError);

		t_mensaje *responseRecurso = socketRecv(personaje->connNivel, handleConnectionError);
		if(responseRecurso->id != POS_RECURSO) {
			log_error(logger, "Se esperaba la posicion del recurso, llegó: id: %d, payload: %s.", responseRecurso->id, responseRecurso->payload);
			exit(EXIT_FAILURE);
		}

		point* punto = fromString(responseRecurso->payload);

		t_recurso_personaje* recurso = malloc(sizeof(t_recurso_personaje));
		recurso->pos = punto;
		recurso->simbolo = recursoSimbolo;

		personaje->recursoActual = recurso;
	}
}
char* pedirRecurso(t_personaje* personaje) {
	log_info(logger, "Pedir el recurso: %s.", personaje->recursoActual->simbolo);
	t_mensaje* requestRecurso = mensaje_create(PEDIR_RECURSO, personaje->recursoActual->simbolo);
	socketSend(personaje->connNivel, requestRecurso, handleConnectionError);
	
	t_mensaje* responseRecurso = socketRecv(personaje->connNivel, handleConnectionError);
	if(responseRecurso->id != RECURSO) {
		log_error(logger, "No se esperaba lo que llegó, llegó: id: %d, payload: %s.", responseRecurso->id, responseRecurso->payload);
		exit(EXIT_FAILURE);
	}

	char* recursoAnterior = personaje->recursoActual->simbolo;

	personaje->recursoActual = NULL;
	return string_duplicate(string_equals_ignore_case(responseRecurso->payload, "1") ? "0" : recursoAnterior);
}
void esperarMensaje(t_personaje *personaje) {
	t_mensaje* mensaje = socketRecv(personaje->connPlanificador, handleConnectionError);
	if(!mensaje) {
		return;
	}
	if(mensaje->id == SIGUIENTE_TURNO) {
		hacerMovimiento(personaje);
	}
	else if(mensaje->id == MUERTO_ESTAS) {
		morir(personaje, MUERTE_PLANIFICADOR);
	}
	else {
		log_error(logger, "No se esperaba lo que llegó, llegó: id: %d, payload: %s.", mensaje->id, mensaje->payload);
		exit(EXIT_FAILURE);
	}
}
Esempio n. 4
0
int EXPORT_THIS d_socket_recv(void *sock, void *buff, int len) {
	return socketRecv(sock, buff, len);
}
Esempio n. 5
0
// prepareSong: Receive a song with network
Music* prepareSong(Socket* Client, u32 idx)
{
	// Init resources
	u16 audiotype;
	u64 size;
	Music* songFile = (Music*)malloc(sizeof(Music));
	Packet* pkg = NULL;
	
	// Sending command to client
	char cmd[10];
	sprintf(cmd, "exec1:%i", (idx - 1));
	cmd[9] = 0;
	socketSend(Client, cmd);
	
	// Getting file info
	while (pkg == NULL) pkg = socketRecv(Client, 64);
	Songlist* song = getSong(idx);
	
	u64 fileSize = atoi((char*)pkg->message);
	socketSend(Client, "OK");
	if (song->format == WAV_PCM16){
		
		// Getting and parsing header file
		linearFree(pkg->message);
		free(pkg);
		pkg = NULL;
		while (pkg == NULL) pkg = socketRecv(Client, 256);
		socketSend(Client, "OK");
		u8* header = pkg->message;
		u32 header_size = pkg->size;
		u32 samplerate,jump,chunk=0x00000000;
		strcpy(songFile->author,"");
		strcpy(songFile->title,"");
		songFile->big_endian = false;
		u32 pos = 16;
		while (chunk != 0x61746164){
			memcpy(&jump, &header[pos], 4);
			pos=pos+4+jump;
			memcpy(&chunk, &header[pos], 4);
			pos=pos+4;
	
			//Chunk LIST detection
			if (chunk == 0x5453494C){
				u32 chunk_size;
				u32 subchunk;
				u32 subchunk_size;
				u32 sub_pos = pos+4;
				memcpy(&subchunk, &header[sub_pos], 4);
				if (subchunk == 0x4F464E49){
					sub_pos = sub_pos+4;
					memcpy(&chunk_size, &header[pos], 4);
					while (sub_pos < (chunk_size + pos + 4)){
						memcpy(&subchunk, &header[sub_pos], 4);
						memcpy(&subchunk_size, &header[sub_pos + 4], 4);
						if (subchunk == 0x54524149){
							strncpy(songFile->author, (char*)&header[sub_pos + 8], subchunk_size);
							songFile->author[subchunk_size] = 0;
						}else if (subchunk == 0x4D414E49){
							strncpy(songFile->title, (char*)&header[sub_pos + 8], subchunk_size);
							songFile->title[subchunk_size] = 0;
						}
						sub_pos = sub_pos + 8 + subchunk_size;
						u8 checksum;
						memcpy(&checksum, &header[sub_pos], 1);
						if (checksum == 0) sub_pos++;
					}
				}
			}
		
		}
		memcpy(&audiotype, &header[22], 2);
		memcpy(&samplerate, &header[24], 4);
		memcpy(&(songFile->bytepersample), &header[32], 2);
		u16 raw_enc;
		memcpy(&raw_enc, &header[20], 2);
		songFile->wavebuf = NULL;
		songFile->wavebuf2 = NULL;
		if (raw_enc == 0x01) songFile->encoding = CSND_ENCODING_PCM16;
		else if (raw_enc == 0x11) songFile->encoding = CSND_ENCODING_ADPCM;
		songFile->mem_size = fileSize - header_size;
		songFile->size = songFile->mem_size;
		u32 REAL_STREAM_MAX_ALLOC;
		if (audiotype == 1) REAL_STREAM_MAX_ALLOC = STREAM_MAX_ALLOC;
		else REAL_STREAM_MAX_ALLOC = STREAM_MAX_ALLOC * 10;
		while (songFile->mem_size > REAL_STREAM_MAX_ALLOC){
			if ((songFile->mem_size % 2) == 1) songFile->mem_size++;
			songFile->mem_size = songFile->mem_size / 2;
		}
		if ((songFile->mem_size % 2) == 1) songFile->mem_size++;
		linearFree(pkg->message);
		free(pkg);
		pkg = NULL;
		if (raw_enc == 0x11){ // TODO: ADPCM support
		}else if (raw_enc == 0x01){
			if (audiotype == 1){
				while (pkg == NULL) pkg = socketRecv(Client, 32768);
				u32 processedBytes = 0;
				songFile->audiobuf = (u8*)linearAlloc(songFile->mem_size);
				while (processedBytes < songFile->mem_size  - 52000){
					if (pkg == NULL){
						pkg = socketRecv(Client, 32768);
						continue;
					}
					memcpy(&songFile->audiobuf[processedBytes], pkg->message, pkg->size);
					processedBytes = processedBytes + pkg->size;
					linearFree(pkg->message);
					free(pkg);
					pkg = socketRecv(Client, 32768);
				}
				//svcSleepThread(1000000000);
				processedBytes = 0;
				socketSend(Client, "exec2:0000");
				bool secondBlock = false;
				pkg = NULL;
				while (pkg == NULL) pkg = socketRecv(Client, 32768);
				streamCache = (u8*)linearAlloc(songFile->mem_size);
				while (processedBytes < songFile->mem_size){
					if (pkg == NULL){
						pkg = socketRecv(Client, 32768);
						continue;
					}
					memcpy(&streamCache[processedBytes], pkg->message, pkg->size);
					processedBytes = processedBytes + pkg->size;
					if ((!secondBlock) && (processedBytes >= (songFile->mem_size / 2))){
						socketSend(Client, "exec2:0000");
						secondBlock = true;
					}
					linearFree(pkg->message);
					free(pkg);
					pkg = socketRecv(Client, 32768);
				}
				songFile->audiobuf2 = NULL;
			}
		}
		songFile->samplerate = samplerate;
		songFile->isPlaying = false;
		songFile->encoding = CSND_ENCODING_PCM16;
		linearFree(header);
	}
	
	return songFile;
		/*}else{
			// I must reordinate my buffer in order to play stereo sound (Thanks CSND/FS libraries .-.)
			u32 size_tbp;
			if (mem_size){
				wav_file->moltiplier = 1;
				//wav_file->sourceFile = fileHandle;
				wav_file->isPlaying = false;
				wav_file->startRead = (pos+4);
				wav_file->size = size;
				wav_file->mem_size = (size-(pos+4));
				while (wav_file->mem_size > STREAM_MAX_ALLOC * 10){
					wav_file->mem_size = wav_file->mem_size / 2;
				}
				tmp_buf = (u8*)linearAlloc(wav_file->mem_size);
				wav_file->audiobuf = (u8*)linearAlloc(wav_file->mem_size/2);
				wav_file->audiobuf2 = (u8*)linearAlloc(wav_file->mem_size/2);
				//FSFILE_Read(fileHandle, &bytesRead, wav_file->startRead, tmp_buf, wav_file->mem_size);
				size_tbp = wav_file->mem_size;
			}else{
				tmp_buf = (u8*)linearAlloc((size-(pos+4)));
				size_tbp = size-(pos+4);
				wav_file->startRead = 0;
				wav_file->size = (size_tbp)/2;
				//FSFILE_Read(fileHandle, &bytesRead, pos+4, tmp_buf, size-(pos+4));
			}
			u32 off=0;
			u32 i=0;
			u16 z;
			if (raw_enc == 0x01){ //PCM16 Decoding
				wav_file->audiobuf = (u8*)linearAlloc((size-(pos+4))/2);
				wav_file->audiobuf2 = (u8*)linearAlloc((size-(pos+4))/2);
				while (i < size_tbp){
					z=0;
					while (z < (wav_file->bytepersample/2)){
						wav_file->audiobuf[off+z] = tmp_buf[i+z];
						wav_file->audiobuf2[off+z] = tmp_buf[i+z+(wav_file->bytepersample/2)];
						z++;
					}
					i=i+wav_file->bytepersample;
					off=off+(wav_file->bytepersample/2);
				}
			}else if (raw_enc == 0x11){ //ADPCM Decoding
				u32 headers_num = (size_tbp) / wav_file->bytepersample;
				wav_file->audiobuf = (u8*)linearAlloc((size_tbp-headers_num*8)/2);
				wav_file->audiobuf2 = (u8*)linearAlloc((size_tbp-headers_num*8)/2);
				int z=0,i=0;
				while (i < size_tbp){
					wav_file->audiobuf[z] = tmp_buf[i++];
					wav_file->audiobuf2[z++] = tmp_buf[i+3];
					wav_file->audiobuf[z] = tmp_buf[i++];
					wav_file->audiobuf2[z++] = tmp_buf[i+3];
					wav_file->audiobuf[z] = tmp_buf[i++];
					wav_file->audiobuf2[z++] = tmp_buf[i+3];
					wav_file->audiobuf[z] = tmp_buf[i++];
					wav_file->audiobuf2[z++] = tmp_buf[i+3];
					i=i+4;
					if ((i % wav_file->bytepersample) == 0) i=i+8;
				}
			}
		}
		wav_file->magic = 0x4C534E44;*/
}