⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 network.cpp

📁 monqueror一个很具有参考价值的源玛
💻 CPP
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <semaphore.h>#include <ctype.h>#include <string.h>#include <minigui/common.h>#include <minigui/minigui.h>#include <minigui/gdi.h>#include <minigui/window.h>#include <minigui/control.h>#include "../include/typedef.h"#include "network.h"#include "mgmessage.h"#if 0#define MSG_OPENURL     MSG_USER+10#define MSG_URLREADY    MSG_USER+11#endif extern MainWndAttr MainWndAttrib;extern void SetAddressForBar(char * strAddress);extern void SetStatusForBar(char * strStatus);ghttp_request * request = NULL; void Cancel_net_thread()				// the ends the application, invoke it.{	void * thread_result;	SetStatusForBar("Network Stopped.");	if(request == NULL)	{		ghttp_request_destroy(request);		request = NULL;	}	pthread_cancel(MainWndAttrib.net_thread);	pthread_join(MainWndAttrib.net_thread, &thread_result);	sem_destroy(&(MainWndAttrib.start_sem));	pthread_mutex_destroy(&(MainWndAttrib.read_head));	RemoveQueue();}BOOL Start_net_thread(){	int res;	request = NULL;	res = sem_init(&(MainWndAttrib.start_sem), 0, 0);	if(res != 0)	{		printf("Can not Create the network thread. \n");		return FALSE;	}	res = pthread_mutex_init(&(MainWndAttrib.read_head), NULL);	if(res != 0)	{		printf("Can not Create the network thread. \n");		return FALSE;	}	SetStatusForBar("Network Started.");	res = pthread_create(&(MainWndAttrib.net_thread), NULL, thread_network, NULL);	if(res != 0)	{		printf("Can not Create the thread. \n");		return FALSE;	}	return TRUE;}void * thread_network(void * arg){	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);	ErrorType error = NO_ERROR;	while(TRUE)    {        sem_wait(&(MainWndAttrib.start_sem));		error = GetHTMLFile();		JobElement *temp = MainWndAttrib.HeadJob;       	GetNextHeadJob();		if(error != NO_ERROR)		{					if(temp->strBuffer != NULL)				delete temp->strBuffer;			temp->strBuffer = (unsigned char *)malloc(200);			sprintf((char *)temp->strBuffer, "<html> <head></head> <body> <H1>Warning: <H1> Transer Error Occur!!!</body> </html>");			//RemoveHeadJob();		}    	SendNotifyMessage(temp->hWndFrom, MSG_URLREADY, 0, (LONG)temp);	}}void GetNextHeadJob()	// the head pointer turn to next, but not release the memory.{	pthread_mutex_lock(&(MainWndAttrib.read_head));	MainWndAttrib.HeadJob = MainWndAttrib.HeadJob->next;	MainWndAttrib.iJobNumber -- ;	pthread_mutex_unlock(&(MainWndAttrib.read_head));}void Cancel_current_job()				// when click "stop" button, invoke it.{	int i = 0;	pthread_cancel(MainWndAttrib.net_thread);	sem_destroy(&(MainWndAttrib.start_sem));	pthread_mutex_destroy(&(MainWndAttrib.read_head));	sem_init(&(MainWndAttrib.start_sem), 0, 0);	pthread_mutex_init(&(MainWndAttrib.read_head),NULL);	RemoveHeadJob();	for(i = 0; i  < MainWndAttrib.iJobNumber; i++)		sem_post(&(MainWndAttrib.start_sem));	Start_net_thread();}void RemoveJob(JobElement *Job){	if(Job->strURL != NULL)	{		free(Job->strURL);		Job->strURL = NULL;	}	if(Job->strBuffer != NULL)	{		free(Job->strBuffer);		Job->strBuffer = NULL;	}	if(Job->strLibBuffer != NULL)	{		free(Job->strLibBuffer);		Job->strLibBuffer = NULL;	}//	if(Job->strMethod != NULL)//	{//		free(Job->strMethod);//		Job->strMethod = NULL;//	}	free(Job);}void RemoveWndJob(HWND hWnd){	int i = 0;	pthread_cancel(MainWndAttrib.net_thread);	sem_destroy(&(MainWndAttrib.start_sem));	pthread_mutex_destroy(&(MainWndAttrib.read_head));	if(MainWndAttrib.HeadJob != NULL)	{		JobElement *preJob, *tempJob;		tempJob = MainWndAttrib.HeadJob;		preJob = tempJob;		while(tempJob != NULL)		{			if(tempJob->hWndFrom == hWnd)			{				if(MainWndAttrib.HeadJob ==  tempJob)				{					MainWndAttrib.HeadJob = tempJob->next;					preJob = MainWndAttrib.HeadJob;				}				else				{					preJob->next = tempJob->next;					preJob = tempJob->next;				}				RemoveJob(tempJob);				MainWndAttrib.iJobNumber --;				tempJob = preJob->next;			}			else			{				preJob = tempJob;				tempJob = tempJob->next;			}		}						for(i = 0; i  < MainWndAttrib.iJobNumber; i++)			sem_post(&(MainWndAttrib.start_sem));	}	sem_init(&(MainWndAttrib.start_sem), 0, 0);	pthread_mutex_init(&(MainWndAttrib.read_head),NULL);	Start_net_thread();}void RemoveHeadJob()	// when on job is accomplished, invoke it.{	JobElement *tempJobElement;	pthread_mutex_lock(&(MainWndAttrib.read_head));	if(MainWndAttrib.HeadJob != NULL)	{		tempJobElement = MainWndAttrib.HeadJob->next;		if(MainWndAttrib.HeadJob->strURL != NULL)		{			free(MainWndAttrib.HeadJob->strURL);			MainWndAttrib.HeadJob->strURL = NULL;		}		if(MainWndAttrib.HeadJob->strBuffer != NULL)		{			free(MainWndAttrib.HeadJob->strBuffer);			MainWndAttrib.HeadJob->strBuffer = NULL;		}		if(MainWndAttrib.HeadJob->strLibBuffer != NULL)		{			free(MainWndAttrib.HeadJob->strLibBuffer);			MainWndAttrib.HeadJob->strLibBuffer = NULL;		}//		if(MainWndAttrib.HeadJob->strMethod != NULL)//		{//			free(MainWndAttrib.HeadJob->strMethod);//			MainWndAttrib.HeadJob->strMethod = NULL;//		}		MainWndAttrib.HeadJob->next = NULL;		free(MainWndAttrib.HeadJob);		MainWndAttrib.HeadJob = tempJobElement;		MainWndAttrib.iJobNumber -- ;	}	pthread_mutex_unlock(&(MainWndAttrib.read_head));}void RemoveQueue()					// when ends the application, invoke it.{	while(MainWndAttrib.HeadJob != NULL)		RemoveHeadJob();}void AddJobToQueue(JobElement *job){	JobElement *tempJobElement;	pthread_mutex_lock(&(MainWndAttrib.read_head));	tempJobElement = MainWndAttrib.HeadJob;	if(tempJobElement != NULL)	{		while(tempJobElement->next != NULL)				tempJobElement = tempJobElement->next;		tempJobElement->next = job;	}	else		MainWndAttrib.HeadJob = job;	MainWndAttrib.iJobNumber ++;	pthread_mutex_unlock(&(MainWndAttrib.read_head));	sem_post(&(MainWndAttrib.start_sem));}																											  ErrorType GetHTMLFile(){	char message[MAX_PATH];	ErrorType Error = NO_ERROR;	int n_httpFileLength;               // Http Length	JobElement * HeadJobElement = MainWndAttrib.HeadJob;	// add some code to get method; and some mutex and semaphore here.	request = ghttp_request_new();		HeadJobElement->strBuffer = NULL;	HeadJobElement->strLibBuffer = NULL;	HeadJobElement->IsFinish = FALSE;		sprintf(message, "Now is transferring %s", HeadJobElement->strURL);	SetStatusForBar(message);	HeadJobElement->strLibBuffer = GetHttpFile(HeadJobElement->strURL, HeadJobElement->strMethod, 					&n_httpFileLength, &Error, request);	sprintf(message, "%s is transferred completed.", HeadJobElement->strURL);	SetStatusForBar(message);	if (Error != NO_ERROR )	{		ghttp_request_destroy(request);		request = NULL;		HeadJobElement->strLibBuffer = NULL;		return Error;	}			HeadJobElement->strBuffer = (unsigned char *)malloc(n_httpFileLength + 1);		HeadJobElement->strLength = n_httpFileLength;					memset(HeadJobElement->strBuffer, 0, n_httpFileLength + 1);	memcpy(HeadJobElement->strBuffer, HeadJobElement->strLibBuffer, n_httpFileLength);	ghttp_request_destroy(request);	request = NULL;	HeadJobElement->strLibBuffer = NULL;	return NO_ERROR;}unsigned char *GetHttpFile(unsigned char *url,unsigned char *method,int *len, ErrorType *error, ghttp_request * request){	ghttp_status status;	unsigned char *parameter = (unsigned char *)strstr((const char *)url,"?");	unsigned char *uri = (unsigned char *)malloc(1024);	bzero(uri,1024);	if(parameter)	{		parameter++;		strncpy((char *)uri, (char *)url, parameter-url-1);		if( !strcmp((char *)method, "get") )		{			if(ghttp_set_uri(request, (char *)url)==-1)				*error = URI_ERROR;		}		else if( !strcmp((char *)method, "post") )		{			if(ghttp_set_uri(request, (char *)uri)==-1)				*error = URI_ERROR;			ghttp_set_type(request, ghttp_type_post);			ghttp_set_body(request, (char *)parameter, strlen((char *)parameter));		 }	}	else	{		if(ghttp_set_uri(request,(char *)url)==-1)		    *error = URI_ERROR;    }    ghttp_set_header(request,http_hdr_Connection,"close");    ghttp_prepare(request);    status = ghttp_process(request);	*len = ghttp_get_body_len(request);	free ((unsigned char *)uri);	//free ((unsigned char *)parameter);	if(status == ghttp_error)		*error = PROCESS_ERROR;	if(status == ghttp_not_done)		*error = PROCESS_ERROR;	if(status == ghttp_done)		*error = NO_ERROR;	return (unsigned char*)ghttp_get_body(request);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -