Пример #1
0
int Spider_Http_Client_Base::recv_package(int sock, Http_Response_Package& response)
{
	char recvbuffer[4096]={0};
	int count=recv_header(sock,recvbuffer);
	if ( count<0 )
	{
		LLOG(L_ERROR,"recv_header error.");
		return -1;
	}

	response.prase_package(recvbuffer);
	std::string result=response.get_field("Transfer-Encoding");
	if( result.compare("chunked")==0)
	{   //chunk的下载方式
		char* body=NULL;
		int length=recv_body_bychunk(sock,body);
		response.set_body(body,length);
	}
	else
	{   //Content-Length的下载方式
		int length=atoi(response.get_field("Content-Length").c_str());
		if ( length>0 )
		{
			char* body=(char*)malloc(length);
			count=recv_body_bylength(sock,body,length);
			if (count!=length)
			{
				LLOG(L_ERROR,"recv_package:recv_body_bylength error.");
				return -1;
			}
			response.set_body(body, length);
		}
	}
	return 0;
}
Пример #2
0
char *
recv_subrequest(int sock, unsigned short request_id)
{
    unsigned short content_len;
    ssize_t recv_header_ret = recv_header(&sock, request_id, &content_len);
    if (!recv_header_ret) {
        pthread_exit((void *)5);
    }
    char *content = (char *)emalloc(sizeof(char)*content_len);
    memset(content, '\0', sizeof(char)*content_len);
    ssize_t recv_ret = recv_content(&sock, request_id, content_len, content);
    struct epoll_event epv = {0, {0}};
    epoll_ctl(g_epoll_fd, EPOLL_CTL_DEL, sock, &epv);
    close(sock);
    g_req_count--;
    return content;
}
Пример #3
0
/**
	establishes connection to ethernet

	@param port: port number for videotransfer
	@param region: pointer to image parameters
	@return returns '0' if connection is established, else '1'
*/
int establish_connection(int port, image_params_t * params)
{
	int i,i_end;
	// *** init network interfaces ******************************************	
	printf("\n\n");
	printf("########################################\n");
	printf("#       START TO RECEIVE FRAMES        #\n");
	printf("########################################\n");
	printf("\n\n");
	
	// *** tcp/ip connect ****************************************************
	printf("waiting for connection...\n");
	fd = accept_connection(port);
	if(fd < 0){
		printf("connection failed\naborting\n");
		return 1;
	}
	printf("connection established\n");
	
	if(!recv_header(fd, &img_param)){
		printf("failed reading image parameters (header)\n");
		return 1;
	}

	memcpy(params,&img_param,sizeof(image_params_t));
	
	printf("\n");
	printf("Image stream header:  width = %u\n", (unsigned int) img_param.width);
	printf("                     height = %u\n", (unsigned int) img_param.height);
	printf("                        bpc = %u\n", (unsigned int) img_param.depth);
	printf("                   channels = %u\n", (unsigned int) img_param.nChannels);

	// set height and width of frames to maximum x and y values
	SIZE_X = MIN ( params->width, MAX_SIZE_X);
	SIZE_Y = MIN ( params->height, MAX_SIZE_Y);

	framebuffer = malloc((params->width*params->height*(params->depth/8)*(params->nChannels+1)) + 4096);
	framebuffer = (unsigned int *)((((unsigned int)framebuffer) / 4096) * 4096);
	i_end = ((params->width*params->height*(params->depth/8)*(params->nChannels+1)) + 4096)/4096;
	for (i=0; i<i_end;i++ )
  	{
		framebuffer[i*1024] = 0;
	}
	return 0;
}
Пример #4
0
int toreceive(int fd, int flags, packet_header_p *pkt_hdr, void **data, long sec, long usec) {
	fd_set fdset;
	struct timeval tv;
	int bytes_recvd = 0;
	int retval;
	packet_header_p ph;

	FD_ZERO(&fdset);
	FD_SET(fd, &fdset);

	tv.tv_sec = sec;
	tv.tv_usec = usec;

	retval = select(fd+1, &fdset, NULL, NULL, &tv);

	if (retval < 0) {
		return -1;
	} else if (retval == 0) {
		return 0;
	} else {
		if (FD_ISSET(fd, &fdset)) {
			if (*pkt_hdr == NULL) {
				*pkt_hdr = create_packet_header();
			}
			retval = recv_header(fd, flags, *pkt_hdr);
			if (retval <= 0) {
				return -1;
			}
			ph = *pkt_hdr;
			bytes_recvd += retval;
			if (ph->length > 0) {
				if (*data == NULL) {
					*data = malloc(ph->length);
				}
				retval = recv_data(fd, flags, *data, ph->length);
				if (retval <= 0) {
					return -1;
				}
				bytes_recvd += retval;
			}
		}
		return bytes_recvd;
	}
}
Пример #5
0
int main(void)
{
	static struct ImageParams imageParams;
	
	// *** init display ******************************************************
	tft_init();
	tft_test_image();
	
	// *** init network interfaces *******************************************
	init_all_network_interfaces();
	if(!eth0_up){
		diag_printf("failed to initialize eth0\naborting\n");
		return 1;
	}
	else{
		diag_printf(" eth0 up\n");
	}
	
	diag_printf("\n\n");
	diag_printf("########################################\n");
	diag_printf("#        Netimage receiver Demo        #\n");
	diag_printf("########################################\n");
	diag_printf("\n\n");
	
	// *** tcp/ip connect ****************************************************
	diag_printf("waiting for connection...\n");
	int fd = accept_connection();
	if(fd < 0){
		diag_printf("connection failed\naborting\n");
		return 1;
	}
	diag_printf("connection established\n");
	
	if(!recv_header(fd, &imageParams)){
		diag_printf("failed reading image parameters (header)\n");
		return 1;
	}
	
	diag_printf("\n");
	diag_printf("Image stream header:  width = %d\n", imageParams.width);
	diag_printf("                     height = %d\n", imageParams.height);
	diag_printf("                        bpc = %d\n", imageParams.depth);
	diag_printf("                   channels = %d\n", imageParams.nChannels);
	
	while(1){
		// current pixel coordinates
		int x,y;
		// number of bytes between the end of a line in the framebuffer
		// and the beginning of the next line.
		int step = tft_info.rlen/4 - imageParams.width;
		// buffer for incomming pixel data
		uint8_t line[4096];
		// frame buffer pointer
		uint32_t * fb_pos = tft_info.fb;
		
		for(y = 0; y < imageParams.height; y++){
			// read a line into the buffer
			tcp_read(fd, line, imageParams.depth/8*imageParams.nChannels*imageParams.width);
			// current position
			uint8_t * p = line;
			
			for(x = 0; x < imageParams.width; x++){
				// write pixel to framebuffer
				*fb_pos = p[0] | (p[1] << 8) | (p[2] << 16);
				// next pixel
				p += 3;
				fb_pos++;
			}
			// next line
			fb_pos += step;
		}
	}
	
	return 0; // never
}
Пример #6
0
bool WINAPI TCPIPDoWriteRead(void* ipTCPIPSystem, HEADER*& hdr, char*& buffer, int& bufcount, unsigned int timeout)
{
	HEADER close_hdr;
	HEADER* error_hdr;
	short error = 0;
	short error_detail = 0;
	int wcount;
	int data_length = 0;
	CTCPIPSystemDrvr* pTCPIPSystem = (CTCPIPSystemDrvr*)ipTCPIPSystem;

	if (pTCPIPSystem->m_hSocket == NULL)
	{
		(fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_CHECKCONNECTION, 999, (int)0);
		return false;
	}

	switch(hdr->hdr_type)
	{
	default:
	case WRITE_REQUEST_FIRST:
		memcpy(pTCPIPSystem->m_IObuffer,hdr,sizeof(HEADER));
		HEADER_swap((HEADER*)pTCPIPSystem->m_IObuffer);
		data_length = sizeof(HEADER);
	case WRITE_REQUEST_NEXT:
		if (buffer != NULL)
			memcpy(pTCPIPSystem->m_IObuffer + data_length, buffer, bufcount);
		data_length += bufcount;
		if ((wcount = send_nblk(pTCPIPSystem->m_hSocket, 
								pTCPIPSystem->m_IObuffer, 
								data_length, 
								0,
								timeout,
								pTCPIPSystem)) == SOCKET_ERROR|| wcount == 0)
		{
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		break;
	case READ_RESPONSE_FIRST:
	case READ_RESPONSE_NEXT:
		if ((wcount = recv_nblk(pTCPIPSystem->m_hSocket, 
								pTCPIPSystem->m_IObuffer, 
								MAX_TCP_BUFFER_LENGTH, 
								0,
								timeout,
								pTCPIPSystem)) == SOCKET_ERROR || wcount == 0)
		{
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		if (hdr->hdr_type == READ_RESPONSE_FIRST && wcount < sizeof HEADER)
		{
			if ((wcount = recv_header(pTCPIPSystem->m_hSocket, 
									pTCPIPSystem->m_IObuffer, 
									wcount,
									MAX_TCP_BUFFER_LENGTH,
									sizeof HEADER,
									0,
									timeout,
									pTCPIPSystem)) == SOCKET_ERROR || wcount == 0)
			{
				TCPIPCloseSession (ipTCPIPSystem);
				return false;
			}
		}
		break;
	case CLOSE_TCPIP_SESSION:
		memcpy(&close_hdr,hdr,sizeof(HEADER));
		HEADER_swap((HEADER*)&close_hdr);
		data_length = sizeof(HEADER);
		send_close_session(pTCPIPSystem->m_hSocket, 
								(char*)&close_hdr, 
								data_length, 
								0,
								30,
								pTCPIPSystem);
		return true;
	}
	bufcount = 0;
	switch(hdr->hdr_type)
	{
	case WRITE_REQUEST_FIRST:
		bufcount -= sizeof(HEADER);
	case WRITE_REQUEST_NEXT:
		bufcount += wcount;
		if (bufcount < 0)
		{
			error = DRVR_ERR_INCORRECT_LENGTH;
			(fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_SEND, error, bufcount);
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		break;
	case READ_RESPONSE_FIRST:
		bufcount = wcount - sizeof(HEADER);
		if (bufcount < 0)
		{
			error = DRVR_ERR_INCORRECT_LENGTH;
			(fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, error, bufcount);
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		buffer = pTCPIPSystem->m_IObuffer + sizeof(HEADER);
		hdr = (HEADER*)pTCPIPSystem->m_IObuffer;
		if (hdr->signature != SIGNATURE)
		{
			error = DRVR_ERR_WRONGSIGNATURE;
			(fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, error, (int)0);
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		if (hdr->error != 0 )
		{
			error = DRVR_ERR_ERROR_FROM_SERVER;
			(fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, hdr->error, hdr->error_detail);
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		break;
	case READ_RESPONSE_NEXT:
		bufcount = wcount;
		memcpy(buffer, pTCPIPSystem->m_IObuffer, bufcount); 
		break;
	case SRVR_TRANSPORT_ERROR:
		error_hdr = (HEADER*)(pTCPIPSystem->m_IObuffer);
		error = error_hdr->error;
		error_detail = error_hdr->error_detail;
		(fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_SRVR_TRANSPORT_ERROR, error, error_detail);
		TCPIPCloseSession (ipTCPIPSystem);
		break;
	default:
		error = 1000;
		(fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_HDR_TYPE, error, (int)0);
		TCPIPCloseSession (ipTCPIPSystem);
		break;
	}
	return error == 0;
}
Пример #7
0
bool WINAPI TCPIPDoWriteRead(void* ipTCPIPSystem, HEADER*& hdr, char*& buffer, int& bufcount, unsigned int timeout)
{
	HEADER close_hdr;
	HEADER* error_hdr;
	short error = 0;
	short error_detail = 0;
	int wcount;
	int data_length = 0;
	CTCPIPUnixDrvr* pTCPIPSystem = (CTCPIPUnixDrvr*)ipTCPIPSystem;

	if (pTCPIPSystem->m_hSocket == INVALID_SOCKET)
	{
		(fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_CHECKCONNECTION, 999, (int)0, "Invalid Socket");
		return false;
	}
	
	switch(hdr->hdr_type)
	{
	default:	
	case WRITE_REQUEST_FIRST:
		memcpy(pTCPIPSystem->m_IObuffer,hdr,sizeof(HEADER));
		if(pTCPIPSystem->swap() == SWAP_YES)
		HEADER_swap((HEADER*)pTCPIPSystem->m_IObuffer);
		data_length = sizeof(HEADER);
	case WRITE_REQUEST_NEXT:
		if (buffer != NULL)
			memcpy(pTCPIPSystem->m_IObuffer + data_length, buffer, bufcount);
		data_length += bufcount;
		if ((wcount = send_nblk(pTCPIPSystem->m_hSocket, 
								pTCPIPSystem->m_IObuffer, 
								data_length, 
								0,
								timeout,
								pTCPIPSystem)) == SOCKET_ERROR|| wcount == 0)
		{
			(fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_SEND, 0, wcount, "WRITE_REQUEST_FIRST/NEXT Error");
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		break;
	case READ_RESPONSE_FIRST:
	case READ_RESPONSE_NEXT:
		wcount = recv_nblk(pTCPIPSystem->m_hSocket, 
						   pTCPIPSystem->m_IObuffer, 
						   MAX_TCP_BUFFER_LENGTH, 
						   0,
						   timeout,
						   pTCPIPSystem); 
		/*
		 * recv_nblk returns a negative -ETIMEDOUT value in case of a timeout
		 * for anything other than a connect/disconnect/getobject reference or a stop server request, we need
		 * to send a Cancel request and wait for either a graceful cancel completion or a communication link failure 
		 * because of the process_stop on the MXOSRVR
		 * A negative ETIMEDOUT value is returned -- otherwise the caller will interpret it as that many bytes read (all *NIX has a +ve value for ETIMEDOUT)
		 */

		if(wcount == -ETIMEDOUT)
		   return false;

		if(wcount == SOCKET_ERROR || wcount == 0)
		{
			(fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, 0, wcount, "READ_RESPONSE_FIRST/NEXT Error");
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		if ((hdr->hdr_type == READ_RESPONSE_FIRST) && (wcount < sizeof(HEADER)))
		{
			if ((wcount = recv_header(pTCPIPSystem->m_hSocket, 
									pTCPIPSystem->m_IObuffer, 
									wcount,
									MAX_TCP_BUFFER_LENGTH,
									sizeof(HEADER),
									0,
									timeout,
									pTCPIPSystem)) == SOCKET_ERROR || wcount == 0)
			{
				(fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, 0, wcount, "READ_RESPONSE_FIRST/NEXT recv_header() Error");
				TCPIPCloseSession (ipTCPIPSystem);
				return false;
			}
		}
		break;
	case CLOSE_TCPIP_SESSION:
		memcpy(&close_hdr,hdr,sizeof(HEADER));
		if(pTCPIPSystem->swap() == SWAP_YES)
		HEADER_swap((HEADER*)&close_hdr);
		data_length = sizeof(HEADER);
		send_close_session(pTCPIPSystem->m_hSocket, 
								(char*)&close_hdr, 
								data_length, 
								0,
								30,
								pTCPIPSystem);
		return true;
	}
	bufcount = 0;
	switch(hdr->hdr_type)
	{
	case WRITE_REQUEST_FIRST:
		bufcount -= sizeof(HEADER);
	case WRITE_REQUEST_NEXT:
		bufcount += wcount;
		if (bufcount < 0)
		{
			error = DRVR_ERR_INCORRECT_LENGTH;
			(fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_SEND, error, bufcount, "Driver Incorrect Length Error");
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		break;
	case READ_RESPONSE_FIRST:
		bufcount = wcount - sizeof(HEADER);
		if (bufcount < 0)
		{
			error = DRVR_ERR_INCORRECT_LENGTH;
			(fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, error, bufcount, "Driver Incorrect Length Error");
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		buffer = pTCPIPSystem->m_IObuffer + sizeof(HEADER);
		hdr = (HEADER*)pTCPIPSystem->m_IObuffer;
		if (hdr->signature != SIGNATURE)
		{
			error = DRVR_ERR_WRONGSIGNATURE;
		(fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, error, (int)0, "Wrong Socket Signature");
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		if (hdr->error != 0 )
		{
			error = DRVR_ERR_ERROR_FROM_SERVER;
			(fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_RECV, hdr->error, hdr->error_detail, "Error From Server");
			TCPIPCloseSession (ipTCPIPSystem);
			return false;
		}
		break;
	case READ_RESPONSE_NEXT:
		bufcount = wcount;
		memcpy(buffer, pTCPIPSystem->m_IObuffer, bufcount); 
		break;
	case SRVR_TRANSPORT_ERROR:
		error_hdr = (HEADER*)(pTCPIPSystem->m_IObuffer);
		error = error_hdr->error;
		error_detail = error_hdr->error_detail;
		(fpTCPIPSET_ERROR)((long)pTCPIPSystem, UNX, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_SRVR_TRANSPORT_ERROR, error, error_detail, "Server Transport Error");
		TCPIPCloseSession (ipTCPIPSystem);
		break;
	default:
		error = 1000;
		(fpTCPIPSET_ERROR)((long)pTCPIPSystem, PC, TCPIP, pTCPIPSystem->odbcAPI, E_DRIVER, pTCPIPSystem->m_object_ref, O_DO_WRITE_READ, F_HDR_TYPE, error, (int)0, "Unknown Header Type" );
		TCPIPCloseSession (ipTCPIPSystem);
		break;
	}
	return error == 0;
}