Пример #1
0
GF_Err PNC_processBIFSGenerator(PNC_CallbackData * data)
{
	const int tmpBufferSize = 2048;
	char *tmpBuffer = (char*)alloca(tmpBufferSize);
	int byteRead=0;

	char *bsBuffer;
	int retour=0;
	GF_Err e;

	if (data->server_socket)
	{
		data->socket = NULL;
		e = gf_sk_accept(data->server_socket, &(data->socket));
		if (e){
			return GF_OK;
		} else {
			dprintf(DEBUG_RTP_serv_generator, "New TCP client connected !\n");
		}
	}

	do
	{
		if (data->socket == NULL)
			return GF_OK;
		e = gf_sk_receive(data->socket, tmpBuffer, tmpBufferSize, 0, & byteRead);
		switch (e) {
			case GF_IP_NETWORK_EMPTY:
				e = GF_OK;
				break;
			case GF_OK:
				if (byteRead > 0){
					dprintf(DEBUG_RTP_serv_generator, "Received %d bytes\n", byteRead);
					/* We copy data in buffer */
					memcpy( &(data->buffer[data->bufferPosition]), tmpBuffer, byteRead );
					data->buffer[data->bufferPosition + byteRead] = '\0';
					retour = findCommand( data->buffer, data->bufferPosition);
					data->bufferPosition += byteRead;
					if (retour >= 0){
						/** OK, it means we found a command ! */
						if (strncmp(&(data->buffer[retour+13]),
							"SEND_CRITICAL", 13)==0){
								bsBuffer = eat_buffer_to_bs( data->buffer, retour, retour + 26, RECV_BUFFER_SIZE_FOR_COMMANDS);
								data->bufferPosition = 0;
								return processSendCritical(data, bsBuffer);
						}
						if (strncmp(&(data->buffer[retour+13]), "SEND", 4)==0){
							bsBuffer = eat_buffer_to_bs( data->buffer, retour, retour + 17, RECV_BUFFER_SIZE_FOR_COMMANDS);
							data->bufferPosition = 0;
							return processSend(data, bsBuffer);
						}
						if (strncmp(&(data->buffer[retour+13]), "RAP", 3)==0){
							bsBuffer = eat_buffer_to_bs( data->buffer, retour, retour + 16, RECV_BUFFER_SIZE_FOR_COMMANDS);
							data->bufferPosition = 0;
							return processRap(data, bsBuffer);
						}
						if (strncmp(&(data->buffer[retour+13]), "RAP_RESET", 9)==0){
							bsBuffer = eat_buffer_to_bs( data->buffer, retour, retour + 22, RECV_BUFFER_SIZE_FOR_COMMANDS);
							data->bufferPosition = 0;
							return processRapReset(data, bsBuffer);
						}
						/** If we are here, it means probably we did not received fully the command */
						break;
					}
				}
				/* No bytes were received */
				break;
			default:
				fprintf(stderr, "Socket error while receiving BIFS data %s\n", gf_error_to_string(e));
				if (data->socket != NULL){
					gf_sk_del(data->socket);
					data->socket = NULL;
				}
				return e;
		}

	} while (e == GF_OK);

	return GF_OK;
}
Пример #2
0
u32 tcp_server(void *par)
{
	TCP_Input *input = par;
	u32 *timer = input->RAPtimer;
	char buffer[MAX_BUF];
	unsigned char temp[MAX_BUF];
	FILE *fp;
	u32 byte_read;
	int ret;
	GF_Config *gf_config_file;
	GF_Socket *TCP_socket;
	GF_Socket *conn_socket;
	GF_Err e;
	
	int debug = input->debug;
	input->status = 1;

	TCP_socket = gf_sk_new(GF_SOCK_TYPE_TCP);
	e = gf_sk_bind(TCP_socket, NULL, input->port, NULL, 0, 0);
	e = gf_sk_listen(TCP_socket, 1);
	e = gf_sk_set_block_mode(TCP_socket, 1);
	e = gf_sk_server_mode(TCP_socket, 0);
	
	while(input->status == 1)
	{	
		memset(buffer, 0, sizeof(buffer));	
		e = gf_sk_accept(TCP_socket, &conn_socket);
		if (e == GF_OK) {
			memset(buffer, 0, sizeof(buffer));
			e = gf_sk_receive(conn_socket, buffer, MAX_BUF, 0, &byte_read);
		}

		switch (e) {
			case GF_IP_NETWORK_EMPTY:
				gf_sleep(33);
				continue;
			case GF_OK:					
				break;
			default:
				fprintf(stderr, "Error with TCP socket : %s\n", gf_error_to_string(e));
				exit(1);
				break;
		}

		if((*(input->config_flag)) == 0)
		{
			u32 num_retry;
			fp = fopen("temp.cfg", "w+");
			if (!fp) {
				fprintf(stderr, "Error opening temp file for the configuration\n");
				exit(1);
			}
			ret = fwrite(buffer, 1, byte_read, fp);
			fclose(fp);
			
			/* parsing config info */
			gf_config_file = gf_cfg_new(".", "temp.cfg");
			if (!gf_config_file) {
				fprintf(stderr, "Error opening the config file %s\n", gf_error_to_string(e));
				exit(-1);
			}
			parse_config(gf_config_file, input->config, debug);

			/* Acknowledging the configuration */ 
			gf_sk_send(conn_socket, "OK\n", 3);

			memset(temp, 0, sizeof(temp));
			fp = fopen(input->config->scene_init_file, "w+");
			if (!fp) {
				fprintf(stderr, "Error opening temp file for reception of the initial scene\n");
				exit(1);
			}
			num_retry=10;

			while (1)
			{
				GF_Err e = gf_sk_receive(conn_socket, temp, sizeof(temp), 0, &byte_read);

				if (e == GF_OK) {
					fwrite(temp, 1, byte_read, fp);
				} else if (e==GF_IP_NETWORK_EMPTY) {
					num_retry--;
					if (!num_retry)
						break;
					gf_sleep(1);
				} else {
					fprintf(stderr, "Error receiving initial scene: %s\n", gf_error_to_string(e));
					break;
				}
			}
			fclose(fp);
			*(input->config_flag) = 1;
		}
		/* we only wait now for the config updates */
		if ( (*(input->config_flag)) == 1) {
			ret = sscanf(buffer, "DelaiMax=%d\n", timer);							
			fprintf(stdout, "RAP timer changed, now : %d\n", *timer);
		}
		gf_sk_del(conn_socket);
	}

	input->status = 2;
	return GF_OK;
}