Esempio n. 1
0
int dm_read(int handle, unsigned char *netbuf, int readlen)
{
	int status=-1;
	int rBytes = -1;
	if(ReadNewCamdUsecond==0)
	{
		if (SelectTimeOutSecond(handle,1, ReadNewCamdTimeOut) == 1) // this calling will be failed, return 1 when there is data to be read
		{
		    rBytes=S_READ(handle, netbuf, readlen); /* return the bytes read */   // may be block here if no data return
		}
		//libc_printf("---dmread , bytes is %d ---\r\n",rBytes);
		if(rBytes<=0)
		{
			libc_printf("---dmread , error bytes is %d ---\r\n",rBytes);
		}
	}
	else
	{
		if(SelectTimeOutUS(handle,1, ReadNewCamdUsecond)==1)
		{
		    rBytes=S_READ(handle, netbuf, readlen); /* return the bytes read */   // may be block here if no data return
		}
		//libc_printf("---dmread , bytes is %d ---\r\n",rBytes);
		if(rBytes<=0)
		{
			libc_printf("---dmread , error bytes is %d ---\r\n",rBytes);
		}
	}
	return rBytes;
}
Esempio n. 2
0
/*--成功返回0,失败返回-1---*/
int cccam_read(int handle, unsigned char *netbuf, int readlen)
{
	int status=-1;
	int rBytes=-1 ;
#if 1
	if(ReadCccamUsecond==0)
	{
		if (SelectTimeOutSecond(handle,1, ReadCccamTimeOut) == 1)// this calling will be failed, return 1 when there is data to be read
		{
			rBytes=S_READ(handle, netbuf, readlen); /* return the bytes read */
			//rBytes=recv(handle, netbuf, readlen, MSG_WAITALL);
		}
	}
	else
	{
		if(SelectTimeOutUS(handle,1, ReadCccamUsecond)==1)
		{
			rBytes=S_READ(handle, netbuf, readlen); /* return the bytes read */
			//rBytes=recv(handle, netbuf, readlen, MSG_WAITALL);
		}
	}
	//libc_printf("---cccam_read , bytes is %d ---\r\n",rBytes);
#endif
	return rBytes;
}
Esempio n. 3
0
//size_t url_read(void* ptr,size_t size)
size_t url_read(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
	int bytes_read   = 0;
	char *buf = NULL;
	char *buf_handle = NULL;
	//int i=0;
	int total_bytes_read   = 0;

	buf = MALLOC(size);
	buf_handle = buf;
	//libc_printf("url_read  %d!\n",size);
	while(size >0)
	{
		if(libnet_get_wait_flag())
		{
			bytes_read = S_READ(g_sock, buf, size);
			if(bytes_read>0)
			{
				MEMCPY(ptr, buf, bytes_read);
				size-=bytes_read;
				buf+=bytes_read;
				ptr+=bytes_read;
				total_bytes_read+=bytes_read;
				libnet_cb(NET_MSG_DOWNLOAD_PROGRESS, total_bytes_read);
			}
			/*
			This time out should start from video decoder stop, but now we just estimate
			around 60 sec (but can depend on video quality), even video can play over 60sec in buffer !!!
				*/
			if(bytes_read==0 || abort_url_read)
			//if(bytes_read==0)
			{
				if(abort_url_read) total_bytes_read =-1;
				if(bytes_read==0) LIBNET_DEBUG("bytes_read==0!\n");
				else LIBNET_DEBUG("url_timeout");
				FREE(buf_handle);
				libnet_set_wait_flag(FALSE);
				abort_url_read = FALSE;
			    	//progress_destroy(p);
				return total_bytes_read;
			}
			if(bytes_read<0) osal_task_sleep(1);
		}
		else // wait flag = FALSE mean net work terminate
		{
				LIBNET_DEBUG("Url Read terminate!\n");
				abort_url_read = FALSE;
				FREE(buf_handle);
				return total_bytes_read;
		}
	}
	//LIBNET_DEBUG("url_read Finish!\n");
	abort_url_read = FALSE;
	FREE(buf_handle);
	return total_bytes_read;
}
Esempio n. 4
0
int cccam_read_timeout_sec(int handle, unsigned char *netbuf, int readlen, int timeout)
{
	int rBytes=-1 ;

	if(SelectTimeOutSecond(handle,1, timeout)==1)
	{
		rBytes=S_READ(handle, netbuf, readlen); /* return the bytes read */
	}
	return rBytes;
}
Esempio n. 5
0
static char *get_raw_header(int fd)
{
	char *header = NULL;
	char *tmpheader = NULL;

	char buf[8];

	int bytes_read = 0;
	int total_read = 0;
	int buf_len = 128;
	int pos = 0;


	header = MALLOC(buf_len);
	header[0] = 0;


	while ( (bytes_read = S_READ(fd, buf, 1)) )
	{
		total_read += bytes_read;

		if (pos + bytes_read >= buf_len)
		{
			tmpheader = MALLOC(buf_len * 2);
			MEMCPY(tmpheader, header, pos);
			FREE(header);
			header = tmpheader;
			buf_len = 2 * buf_len;
		}
		MEMCPY(header + pos, buf, bytes_read);
		pos += bytes_read;
		header[pos] = 0;

		if ( total_read > 1)
		{
			if ( STRCMP(header + (total_read - 2), "\n\n") == 0 )
				break;
		}

		if ( total_read > 3 )
		{
			if ( STRCMP(header + (total_read - 4), "\r\n\r\n")
					== 0 )
				break;
		}
	}

	return header;
}
Esempio n. 6
0
static char *get_line(UrlResource *rsrc, int control)
{
	int bytes_read	= 0;
	char *end;
	char buf[BUFSIZE+1];

	while ( (bytes_read = S_READ(control, buf, BUFSIZE)) )
	{
		//if ( rsrc->options & OPT_VERBOSE )
		//	fwrite(buf, 1, bytes_read, stderr);

		if ( (buf[0] == '4' || buf[0] == '5') &&
				!((rsrc->options & (OPT_VERBOSE | OPT_QUIET))) )
		{
			//fwrite(buf, 1, bytes_read, stderr);
			return NULL;
		}

		/* in case there's a partial read */
		buf[bytes_read] = '\0';

		if ( buf[bytes_read - 1] == '\n' )
			buf[bytes_read - 1] = '\0';

		if ( buf[bytes_read - 2] == '\r' )
			buf[bytes_read - 2] = '\0';

		if ( isdigit(buf[0]) && buf[3] == ' ' )
		{
			return strdup(buf);
		}

		/* skip to last line of possibly multiple line input */

		if ( (end = strrchr(buf, '\n')) )
		{
			end++;
			if ( isdigit(end[0]) && end[3] == ' ' )
				return strdup(end);
		}
	}

	return NULL;
}
Esempio n. 7
0
/*--成功返回0,失败返回-1---*/
#if 0 /*---vachel--*/
int dm_read(int handle, unsigned char *netbuf, int readlen)
{
		int status=-1;
		int rBytes ;
		SelectTimeOutSecond(handle,1, 30);
		rBytes=S_READ(handle, netbuf, readlen); /* return the bytes read */
		if((rBytes   ==   0)||(rBytes   ==   -1))   
	        {   
#ifdef ENABLE_NEW_CAMD	        
	        	;
	              /*   这两种情况都可认为是链路关闭*/   
			newcamd_disconnectHandle_notify(handle);
#endif				  
	        }   
		else
		{
			status=0;
		}
		return rBytes;
}
Esempio n. 8
0
int dump_data(UrlResource *rsrc, int sock, FILE *out, libnet_callback notify)
{
	int bytes_read   = 0;

	int len = 1024;
	char *buf = MALLOC(len);
	int sum = 0;
	while (rsrc->running)
	{
		
		libc_printf("reading... ", bytes_read);
		bytes_read = S_READ(sock, buf, len);
		sum += bytes_read;
		libc_printf("read %d, total = %d\n", bytes_read, sum);
		if(bytes_read <= 0)
			break;
		osal_task_sleep(300);
	}
	FREE(buf);
	libc_printf("download ok!\n");
}
int main(int argc, char** argv) {

	campo schema_client[] = {{"ID", UNSIGNED | INT, 1}, {"NOMBRE", CHAR, 50}, {"APELLIDO", CHAR, 50},
							 {"EMAIL", CHAR, 80}, {0,0,0}};
	campo primary_key[] = {{"ID", UNSIGNED | INT, 1}, {0,0,0}};

	int client_key = 0; //puedo llegar hasta 999999
	int record_counter = 0;

	//argv[1] nombre del archivo directo original
	//argv[2] nombre del archivo directo de salida
	if (argc != 3) {
		return -1;
	}

	//pedimos memoria para el registro de intercambio
	record = malloc(REG_SIZEOF(schema_book));
	if (record == NULL) {
		return -2; 
	}

	//apertura del directo original
	fd_dir = int D_OPEN(argv[1], READ);
	if (fd_dir == RES_NULL) {
		free(record);
		return -3;
	}

	//creacion de archivo secuencial de intercambio
	if (S_CREATE("temp.sec") == RES_ERROR) {	
		D_CLOSE(fd_dir);
		free(record);
		return -4;	
	}

	//a partir podria hacerse mejoras con las validaciones pertinentes
	//similares a las anteriores
	fd_sec = S_OPEN("temp.sec",WRITE);

	while (client_key <= 999999) {
		REG_SET(record,schema_client,"ID",client_key);
		if (D_READ(fd_dir, record) != RES_NO_EXISTE) {
			//es posible agregar a la logica codigos de error
			//se mantiene por simplicidad asi
			S_WRITE(fd_sec, record, REG_SIZEOF(schema_client));
			record_counter++;
		}

		client_key++;
	}

	D_CLOSE(fd_dir);
	S_CLOSE(fd_sec);

	fd_sec = S_OPEN("temp.sec",READ);

	if (D_CREATE(argv[2], schema_client, primary_key, 
		record_counter + ceil(record_counter * 0.35)) == RES_ERROR) {
		//mantengo secuencial para posterior procesamiento 
		S_CLOSE(fd_sec);
		free(record);
		return -5;
	}

	fd_dir = D_OPEN(argv[2], WRITE);

	while (S_READ(fd_sec, record) != RES_EOF) {
		D_WRITE(fd_dir, record);
	}

	S_CLOSE(fd_sec);
	S_DESTROY("temp.sec");

	D_CLOSE(fd_dir);

	free(record);

	return 0;

	
} 
Esempio n. 10
0
int NwebRead(int handle, unsigned char *netbuf, int readlen)
{
	int ret=-1;
	ret=S_READ(handle, netbuf, readlen); /* return the bytes read */
	return ret;
}
Esempio n. 11
0
int http_transfer(UrlResource *rsrc, libnet_callback notify)
{
	FILE *out 		= NULL;
	Url *u			= NULL;
	Url *proxy_url		= NULL;
	Url *redir_u		= NULL;
	char *request		= NULL;
	//char *raw_header	= NULL;
	HttpHeader *header	= NULL;
	//char *len_string 	= NULL;
	//char *new_location	= NULL;
    char *tmp_string    = NULL;
	//char buf[BUFSIZE];
	int sock 		= -1;
	ssize_t bytes_read	= 0;
    int msg_code    = 0;
	int retval		= 0;
	int i;

    char *buf = MALLOC(BUFSIZE);
    if (!buf)
    {
        LIBNET_DEBUG("No enough memory!\n");
        return 0;
    }
	/* make sure we haven't recursed too much */
	if ( redirect_count > REDIRECT_MAX )
	{
		LIBNET_DEBUG("redirection max count exceeded (looping redirect?)");
		redirect_count = 0;
        msg_code = -NET_ERR_CONNECT_FAILED;
        goto cleanup;
	}


	/* make sure everything's initialized to something useful */
	u = rsrc->url;
	if ( !u->host )
	{
		LIBNET_DEBUG("no host specified");
        msg_code = -NET_ERR_CONNECT_FAILED;
        goto cleanup;
	}

	/* fill in proxyness */
	if ( !rsrc->proxy )
	{
		rsrc->proxy = get_proxy("HTTP_PROXY");
	}

	if (( NULL == rsrc->outfile ) && (NULL == rsrc->buffer))
	{
		if ( u->file )
			rsrc->outfile = strdup(u->file);
		else
			rsrc->outfile = strdup("index.html");
	}

	if ( !u->path )
		u->path = strdup("/");

	if ( !u->file )
		u->file = strdup("");  /* funny looking */

	if ( !u->port )
		u->port = 80;

	rsrc->options |= default_opts;

	/* send the request to either the proxy or the remote host */
	if ( rsrc->proxy )
	{
		proxy_url = url_new();
		url_init(proxy_url, rsrc->proxy);

		if ( !proxy_url->port )
			proxy_url->port = 80;

		if ( !proxy_url->host )
		{
			LIBNET_DEBUG( "bad proxy `%s'", rsrc->proxy);
            msg_code = -NET_ERR_CONNECT_FAILED;
            goto cleanup;
		}

		if ( proxy_url->username )
			rsrc->proxy_username = strdup(proxy_url->username);
		if ( proxy_url->password )
			rsrc->proxy_password = strdup(proxy_url->password);

#ifdef LIB_W5300
		sock = w5300_tcp_connect(proxy_url->host, proxy_url->port, rsrc->srcip, rsrc->srcport);		
#else
		sock = util_tcp_connect(proxy_url->host, proxy_url->port);
#endif
		if (sock < 0)
		{
            msg_code = -NET_ERR_CONNECT_FAILED;
            goto cleanup;            
		}

		safe_free(u->path);
		safe_free(u->file);
		u->path = strdup("");
		u->file = strdup(u->full_url);
		request = get_request(rsrc);
        LIBNET_DEBUG("sending request:\n%s", request);
		S_WRITE(sock, request, STRLEN(request));

		FREE(request);
	}
	else /* no proxy */
	{
#ifdef LIB_W5300
		sock = w5300_tcp_connect(u->host, u->port, rsrc->srcip, rsrc->srcport);		
#else
		sock = util_tcp_connect(u->host, u->port);
#endif
		if (sock < 0)
		{
            msg_code = -NET_ERR_CONNECT_FAILED;
            goto cleanup;            
		}
		request = get_request(rsrc);
        LIBNET_DEBUG("sending request:\n%s", request);
		S_WRITE(sock, request, STRLEN(request));

		FREE(request);
	}

	if (rsrc->outfile)
	{
		if ((get_file_size(rsrc->outfile) > 0) && (rsrc->options & OPT_RESUME))
			out = fopen(rsrc->outfile, "rb+");
		else
			out = fopen(rsrc->outfile, "wb");

		if ( !out )
		{
			LIBNET_DEBUG( "opening %s: %s", rsrc->outfile, "");			
            msg_code = -NET_ERR_FILE_SAVE_ERROR;
            goto cleanup;
			
		}
	}

	/* check to see if it returned a HTTP 1.x response */
	MEMSET(buf, '\0', 5);

#ifdef LIB_W5300
	bytes_read = S_READ(sock, buf, BUFSIZE);
    buf[bytes_read] = '\0';
    LIBNET_DEBUG("receive respond:(%d)\n%s", bytes_read, buf);
#else
	bytes_read = S_READ(sock, buf, 8);
#endif

	if ( bytes_read <= 0 )
	{
        msg_code = -NET_ERR_HTTP_SERVER_ERROR;
        goto cleanup;
	}

	if ( ! (buf[0] == 'H' && buf[1] == 'T'
			&& buf[2] == 'T' && buf[3] == 'P') )
	{
		if((rsrc->options & OPT_RESUME) &&	rsrc->outfile_offset)
		{
			LIBNET_DEBUG("server does not support resume!");
            msg_code = -NET_ERR_OPERATION_NOT_PERMIT;
			goto cleanup;
		}
		//fwrite(buf, bytes_read, 1,out);
	}
	else
	{
		/* skip the header */
#ifdef LIB_W5300
        buf[bytes_read] = '\0';
#else
		char *raw_header1 = NULL;
		buf[bytes_read] = '\0';
		raw_header1 = get_raw_header(sock);
		strconcat2(buf, raw_header1, NULL);
		FREE(raw_header1);
        LIBNET_DEBUG("receive respond:(%d)\n%s", bytes_read, buf);
#endif
		header = make_http_header(buf);

		/* check for redirects */
		tmp_string = get_header_value("location", header);

		if (buf[9] == '3' && tmp_string )
		{
#if 1			//diable redirec function
			redir_u = url_new();

			/* make sure we still send user/password along */
			redir_u->username = safe_strdup(u->username);
			redir_u->password = safe_strdup(u->password);

			url_init(redir_u, tmp_string);
			rsrc->url = redir_u;
			redirect_count++;
			//retval = transfer(rsrc, notify);
			transfer((UINT32)rsrc, (UINT32)notify);
			rsrc->url =u;
			redirect_count--;
			if(redirect_count<0) redirect_count=0;
			if (redir_u)
			{
				url_destroy(redir_u);
				FREE(redir_u);
			}
#endif			
            FREE(tmp_string);
            tmp_string = NULL;
            //msg_code = -NET_ERR_OPERATION_NOT_PERMIT;//we can support redirect now, remove this line.
			goto cleanup;
		}
        if (tmp_string)
        {
            FREE(tmp_string);
            tmp_string = NULL;
        }

		if (buf[9] == '4' || buf[9] == '5')
		{
			for (i = 0; buf[i] && buf[i] != '\n'; i++);
			buf[i] = '\0';
			LIBNET_DEBUG("HTTP error from server: %s\n", buf);

			if( buf[9] == '4' && buf[10] == '0' && buf[11] == '4') 
				msg_code = -NET_ERR_FILE_NOT_FOUND;
			else
				msg_code = -NET_ERR_HTTP_SERVER_ERROR;
			
			goto cleanup;
		}

		tmp_string = get_header_value("content-length", header);

		if (tmp_string)
		{
			rsrc->outfile_size = (off_t )ATOI(tmp_string);
	            FREE(tmp_string);
	            tmp_string = NULL;		
			if(rsrc->use_pecache ==0)
			{
				if ((rsrc->buffer) && (rsrc->buffer_len < rsrc->outfile_size))
				{
					LIBNET_DEBUG("the buffer length less than the file fize (%d < %d)\n", rsrc->buffer, (int)rsrc->outfile_size);
				    msg_code = -NET_ERR_FILE_SAVE_ERROR;
					goto cleanup;
				}
			}				
		}

        tmp_string = get_header_value("content-range", header);
		if (tmp_string)
		{
            FREE(tmp_string);
            tmp_string = NULL;
			rsrc->outfile_size += rsrc->outfile_offset;
		}

		if ((!rsrc->outfile_size) &&
				(rsrc->options & OPT_RESUME) &&
				!(rsrc->options & OPT_NORESUME)
				&& rsrc->outfile_offset )
		{
			LIBNET_DEBUG("unable to determine remote file size");
            msg_code = -NET_ERR_FILE_SAVE_ERROR;
			goto cleanup;
		}
	}

if(rsrc->use_pecache ==0)
	retval = dump_data(rsrc, sock, out, notify);
else
	retval = dump_data_to_pecache(rsrc, sock, out, notify);

cleanup:
	free_http_header(header);

    //if (raw_header)
	//	FREE(raw_header);
		

	if (proxy_url)
	{
		url_destroy(proxy_url);
		FREE(proxy_url);
	}

    if (sock >= 0)
    {
	    S_CLOSE(sock);
    }

	if (out)
		fclose(out);

    if (buf)
    {
        FREE(buf);
    }

#ifndef WIN32
	if (rsrc->outfile)
		fs_sync(rsrc->outfile);
#endif

    if (msg_code < 0 && rsrc->running)
    { 
		notify(NET_MSG_DOWNLOAD_FINISH, (UINT32)msg_code);
		libnet_abort_url_read(TRUE);//to break url_open while loop //retval!=1 means transfer fail
    }

	return retval;

}
Esempio n. 12
0
int dump_data(UrlResource *rsrc, int sock, FILE *out, libnet_callback notify)
{
	Progress *p = NULL;
	int bytes_read   = 0;
	ssize_t written	   = 0;
	char *buf = NULL;
	int lenth, final, final2 = 0;
	int buflen;
	if ((out == NULL) && (rsrc->buffer == NULL) && (NULL == rsrc->strfile_handle))
		return 0;

	/* if we already have all of it */
	if ( !(rsrc->options ) )
	{
		if ( rsrc->outfile_size &&(rsrc->outfile_offset >= rsrc->outfile_size) )
		{
			LIBNET_DEBUG( "you already have all of `%s', skipping", rsrc->outfile);
			//S_CLOSE(sock);
			return 0;
		}
	}

	p = progress_new();
	p->tranfer_callback = notify;
	if(NULL == rsrc->strfile_handle)
	{
		progress_init(p, rsrc, rsrc->outfile_size);
		progress_update(p, rsrc->outfile_offset);
		buflen = BUFSIZE;
	}else
	{
		progress_init(p, rsrc, rsrc->outfile_size);
		buflen = BUFSIZE * 3;
	}
	p->offset = rsrc->outfile_offset;

	if ( out && (rsrc->outfile_offset > 0) && (rsrc->options & OPT_RESUME))
	{
		fseek(out, rsrc->outfile_offset, SEEK_SET);
		LIBNET_DEBUG("ftell = %d \n", ftell(out));
	}
	buf = MALLOC(buflen);
	if(rsrc->strfile_handle)
	{
		fd_set set;
		struct timeval tv = {30, 0};
		FD_ZERO(&set);
		FD_SET(sock, &set);
		while (lwip_select(sock + 1, &set, NULL, NULL, &tv) > 0 && (rsrc->running))
		{
			bytes_read = S_READ(sock, buf, buflen);
			if(0 >= bytes_read)
				break;
			lenth = bytes_read;
			final += bytes_read;
			final2 += bytes_read;
			while(lenth)
			{
				written = httpstrfile_writedatatobuff(rsrc->strfile_handle, buf + (bytes_read - lenth), (UINT32)lenth);
				lenth -= written;
				osal_task_sleep(1);
			}
			if(0x20000 <= final2)
			{
//				libc_printf("final = %d/%d\n", final, rsrc->outfile_size);
				progress_update(p, final2);
				final2 = 0;
			}			
		}