//设置队列写入点 static int synchttp_now_putpos() { int readpos_value = 0; int putpos_value = 0; char queue_input[32] = {0}; readpos_value = synchttp_get_readpos(); putpos_value = synchttp_get_putpos(); putpos_value = putpos_value + 1; if(putpos_value == readpos_value){ //消息队列已满 putpos_value = 0; }else if(readpos_value <=0 && putpos_value > SYNCHTTP_DEFAULT_MAXQUEUE){ //如果队列没有出操作并且队列已满 putpos_value = 0; }else if(putpos_value > SYNCHTTP_DEFAULT_MAXQUEUE){ if(tcbdbput2(synchttp_db_tcbdb, "PP", "1")) { putpos_value = 1; } }else{ sprintf(queue_input,"%d", putpos_value); tcbdbput2(synchttp_db_tcbdb, "PP", (char *)queue_input); } return putpos_value; }
static int synchttp_now_readpos() { int putpos_value = 0; int readpos_value = 0; char queue_name[300] = {0}; /* 队列名称的总长度,用户输入的队列长度少于256字节 */ /* 读取当前队列写入位置点 */ putpos_value = synchttp_get_putpos(); /* 读取当前队列读取位置点 */ readpos_value = synchttp_get_readpos(); /* 如果readpos_value的值不存在,重置为1 */ if (readpos_value == 0 && putpos_value > 0) { readpos_value = 1; tcbdbput2(synchttp_db_tcbdb, "RP", "1"); /* 如果队列的读取值(出队列)小于队列的写入值(入队列) */ } else if (readpos_value < putpos_value) { readpos_value = readpos_value + 1; char queue_input[32] = {0}; sprintf(queue_input, "%d", readpos_value); tcbdbput2(synchttp_db_tcbdb, "RP", queue_input); /* 如果队列的读取值(出队列)大于队列的写入值(入队列),并且队列的读取值(出队列)小于最大队列数量 */ } else if (readpos_value > putpos_value && readpos_value < SYNCHTTP_DEFAULT_MAXQUEUE) { readpos_value = readpos_value + 1; char queue_input[32] = {0}; sprintf(queue_input, "%d", readpos_value); tcbdbput2(synchttp_db_tcbdb, "RP", queue_input); /* 如果队列的读取值(出队列)大于队列的写入值(入队列),并且队列的读取值(出队列)等于最大队列数量 */ } else if (readpos_value > putpos_value && readpos_value == SYNCHTTP_DEFAULT_MAXQUEUE) { readpos_value = 1; tcbdbput2(synchttp_db_tcbdb, "RP", "1"); /* 队列的读取值(出队列)等于队列的写入值(入队列),即队列中的数据已全部读出 */ } else { readpos_value = 0; } return readpos_value; }
/* * Maps a key to a string. * * ARGUMENTS: * backend Pointer to the database. Must have been returned by * "beOpen()". * key Pointer to the 0-terminated key. * value Pointer to the string value. * RETURNS: * 0 Success. * RDB_DBERR Backend database error. */ RdbStatus bePut( Backend* backend, const char* const key, const char* const value) { assert(backend != NULL); assert(backend->bdb != NULL); assert(key != NULL); assert(value != NULL); return tcbdbput2(backend->bdb, key, value) ? 0 : RDB_DBERR; }
/* 处理模块 */ void synchttp_handler(struct evhttp_request *req, void *arg) { struct evbuffer *buf; buf = evbuffer_new(); /* 分析URL参数 */ char *decode_uri = strdup((char*) evhttp_request_uri(req)); struct evkeyvalq synchttp_http_query; evhttp_parse_query(decode_uri, &synchttp_http_query); free(decode_uri); /* 接收GET表单参数 */ const char *synchttp_input_charset = evhttp_find_header(&synchttp_http_query, "charset"); /* 编码方式 */ const char *synchttp_input_data = evhttp_find_header(&synchttp_http_query, "data"); /* 数据 */ const char *synchttp_input_auth = evhttp_find_header(&synchttp_http_query, "auth"); /* 验证密码 */ /* 返回给用户的Header头信息 */ if (synchttp_input_charset != NULL && strlen(synchttp_input_charset) <= 40) { char content_type[64] = { 0x00 }; sprintf(content_type, "text/plain; charset=%s", synchttp_input_charset); evhttp_add_header(req->output_headers, "Content-Type", content_type); } else { evhttp_add_header(req->output_headers, "Content-Type", "text/plain"); } evhttp_add_header(req->output_headers, "Connection", "keep-alive"); evhttp_add_header(req->output_headers, "Cache-Control", "no-cache"); /* 权限校验 */ bool is_auth_pass = false; /* 是否验证通过 */ if (synchttp_settings_auth != NULL) { /* 如果命令行启动参数设置了验证密码 */ if (synchttp_input_auth != NULL && strcmp(synchttp_settings_auth, synchttp_input_auth) == 0) { is_auth_pass = true; } else { is_auth_pass = false; } } else { /* 如果命令行启动参数没有设置验证密码 */ is_auth_pass = true; } if (is_auth_pass == false) { /* 校验失败 */ evbuffer_add_printf(buf, "%s", "SYNCHTTP_AUTH_FAILED"); goto output; } int now_putpos = synchttp_now_putpos(); if(now_putpos > SYNCHTTP_DEFAULT_MAXQUEUE){ evbuffer_add_printf(buf, "%s", "SYNCHTTP_PUT_FULL"); goto output; } int buffer_data_len; char queue_name[32] = { 0x00 }; sprintf(queue_name,"Q:%d", now_putpos); /*请求消息入库*/ char *synchttp_input_buffer; char *buffer_data; if (synchttp_input_data != NULL) { /*GET请求*/ buffer_data_len = strlen(synchttp_input_data); buffer_data = (char *) tccalloc(1, buffer_data_len + 3); memcpy(buffer_data, synchttp_input_data, buffer_data_len); strcat(buffer_data, "#1"); } else { /*POST请求*/ buffer_data_len = EVBUFFER_LENGTH(req->input_buffer); buffer_data = (char *) tccalloc(1, buffer_data_len + 3); memcpy(buffer_data, EVBUFFER_DATA(req->input_buffer), buffer_data_len); strcat(buffer_data, "#2"); } synchttp_input_buffer = urldecode(buffer_data); /*参数是否存在判断 */ if (strlen(synchttp_input_buffer) < 3){ /* 参数错误 */ evbuffer_add_printf(buf, "%s", "SYNCHTTP_ERROR"); goto output; } if(tcbdbput2(synchttp_db_tcbdb, queue_name, synchttp_input_buffer) == true){ fprintf(stderr, "in:%s--->%s\n", queue_name, synchttp_input_buffer); }else{ evbuffer_add_printf(buf, "%s", "SYNCHTTP_ERROR"); goto output; } evbuffer_add_printf(buf, "%s", "SYNCHTTP_SET_OK"); free(synchttp_input_buffer); free(buffer_data); output: /* 输出内容给客户端 */ evhttp_send_reply(req, HTTP_OK, "OK", buf); /* 内存释放 */ evhttp_clear_headers(&synchttp_http_query); evbuffer_free(buf); }