}
//设置网络非阻塞。
void setnonblocking(SOCKET sock)
{
u_long ulArg = 1;
if (::ioctlsocket(sock, FIONBIO, &ulArg) == SOCKET_ERROR) {
log("ioctlsocket error\n");
}
}
//线程中只调用等待即可。
unsigned int STDCALL dispatch(void* arg)
{
event_base_dispatch((struct event_base*)arg);
return NULL;
}
//处理用户发出的请求
void
OpenClient(struct evhttp_request* req, struct evkeyvalq* query_headers, int* response_code, struct evbuffer *response_buf)
{
const char* type = evhttp_find_header(query_headers, "type");
if (type == NULL) {
*response_code = HTTP_INTERNAL;
log("type == NULL");
return;
}
char* response_data ="{\"ret\":0}";//需要返回的信息,可以是JSON等约定类型的串。
evbuffer_add(response_buf, response_data, strlen(response_data) + 1);
free(response_data);
}
//处理接收的命令
void genericHandle(struct evhttp_request* req, void *arg)
{
const struct evhttp_uri* uri = evhttp_request_get_evhttp_uri(req);
const char* path = evhttp_uri_get_path(uri);
const char* query_str = evhttp_uri_get_query(uri);
struct evkeyvalq headers;
if (evhttp_parse_query_str(query_str, &headers) == -1) {
log("evhttp_parse_query_str error!\n");
return;
}
int response_code = HTTP_OK;
struct evbuffer *buf = evbuffer_new();
size_t post_size = EVBUFFER_LENGTH(req->input_buffer);
if (post_size > 0)//用这种方式处理POST中BODY的数据。
{
char* pcBuf = new char[post_size + 1];
memset(pcBuf,0,post_size+1);
memcpy (pcBuf, EVBUFFER_DATA(req->input_buffer), post_size);
if (strcmp(path, "/SendMessageToProcess") == 0) {//这儿解析命令
//--
} else if (strcmp(path, "/StartProcessWithParam") == 0) {
//--
}
else
{
return;//命令错误
}
char* response_data = "";
evbuffer_add(buf, response_data, strlen(response_data) + 1);
free(response_data);
// char buf[1024] = {0};
// evbuffer_remove(req->input_buffer, &buf, sizeof(buf) - 1);
// event_base_loopbreak((struct event_base*)arg);
//这种数据是约定好的,可能是JSON,或纯数据,可以是大文件。
delete []pcBuf;
}
else
{
//解析HTTP请求的命令 GET?
string strTmp = path;
//分割传入的命令。
vector
if (vecInput.size() > 0)
{
if (vecInput[0] == "StartProcessWithParam")//解析命令
{
}
}
}
// No 'Access-Control-Allow-Origin' header is present on the requested resource//解决跨域调用返回的报错问题
evhttp_add_header(evhttp_request_get_output_headers(req), "Access-Control-Allow-Origin","*");
evhttp_send_reply(req, response_code, "ok", buf);
evbuffer_free(buf);
}
//字符串分割函数
vector< string> split( string str, string pattern)
{
vector
if(pattern.empty()) return ret;
size_t start=0,index=str.find_first_of(pattern,0);
while(index!=str.npos)
{
if(start!=index)
ret.push_back(str.substr(start,index-start));
start=index+1;
index=str.find_first_of(pattern,start);
}
if(!str.substr(start).empty())
ret.push_back(str.substr(start));
return ret;
}
//使用方法:将LIBEVENT引入,在UI层开启线程调用 startHttpServer传入端口号即可。
/*
_beginthread(ThreadHttpServer,0,this);
void ThreadHttpServer( void *dummy )
{
startHttpServer(端口号);
}
*/
//网页POST格式:
/*
注意客户端须有: xhrFields: { //允许跨域访问是添加cookie
withCredentials: false
}, 或者不加此项,不能为TRUE。
如下: