CCacheEntity* CHttpCache::get_cache_entity(const char* filename, int filename_length) { // 得到文件名的MD5码 struct MD5Context ctx; unsigned char md5[16]; MD5Init(&ctx); MD5Update(&ctx, (const unsigned char *)filename, filename_length); MD5Final(md5, &ctx); TCacheEntityTable::iterator iter; uint32_t index = *(uint64_t *)md5 % _cache_entity_table_number; { sys::CLockHelper<sys::CLock> lock(_lock[index]); iter = _cache_entity_table[index].find(md5); if (iter != _cache_entity_table[index].end()) { iter->second->inc_refcount(); return iter->second; } } int fd = open(filename, O_RDONLY); if (-1 == fd) { MYLOG_DEBUG("Can not open %s for %s.\n", filename, strerror(errno)); return NULL; } try { sys::mmap_t* m = sys::CMMap::map(fd, getpagesize()); CCacheEntity* cache_entity = new CCacheEntity(md5, m); cache_entity->inc_refcount(); sys::CLockHelper<sys::CLock> lock(_lock[index]); std::pair<TCacheEntityTable::iterator, bool> retval = _cache_entity_table[index].insert(std::make_pair(md5, cache_entity)); if (!retval.second) // 已经存在 { delete cache_entity; retval.first->second->inc_refcount(); return retval.first->second; } cache_entity->inc_refcount(); return cache_entity; } catch (sys::CSyscallException& ex) { MYLOG_DEBUG("Map %s error: %s.\n", filename, strerror(ex.get_errcode())); return NULL; } }
MY_NAMESPACE_BEGIN bool CHttpTranslator::translate(IProtocolParser* protocol_parser, IResponsor* request_responsor) { CHttpResponsor* responsor = (CHttpResponsor *)request_responsor; CHttpRequester* requster = (CHttpRequester *)protocol_parser; const CHttpHeader* header = requster->get_http_event()->get_http_header(); uint16_t domain_name_length; const char* domain_name = header->get_domain_name(domain_name_length); const CVirtualHost* host = CHostManager::get_singleton()->find_host(domain_name, domain_name_length); if (NULL == host) { MYLOG_ERROR("Can not find host for %.*s.\n", domain_name_length, domain_name); return false; } uint16_t url_length; char full_filename[FILENAME_MAX]; const char* url = header->get_url(url_length); int full_filename_length = host->get_full_filename(url, url_length, full_filename, sizeof(full_filename)-1); if (0 == full_filename_length) { MYLOG_ERROR("Can not find file for %.*s/%.*s.\n", domain_name_length, domain_name, url_length, url); return false; } MYLOG_DEBUG("GET %.*s:%s.\n", domain_name_length, domain_name, full_filename); responsor->set_filename(full_filename, full_filename_length); return true; }
int session_manage::add_session(std::string sessionid, STU_SESSION_INFO *psession ) { if (psession == NULL || m_predis_client == NULL) { return -1; } std::string key; std::string value; sessioninfo2json(value, psession); key = key+SESSION_INFO_PREFIX+sessionid; MYLOG_DEBUG("addsession, key=%s, value=%s",key.c_str(), value.c_str()); int result; result = m_predis_client->set_value(key, value); if (result == -1) { MYLOG_ERROR("failed to set sessioninfo to redis, sessionid=%s", sessionid.c_str()); return -1; } result = m_predis_client->set_expire(key); if (result == -1) { MYLOG_ERROR("failed to set expire to sessionid, sessionid=%s", sessionid.c_str()); return -1; } return 0; }
int HttpRequestPacket::decode_get( string& sHttpHead ) { //LOG4CPLUS_DEBUG(logger, "http head: " << sHttpHead); //get first line, GET /xx?xxx string::size_type requestPos = sHttpHead.find("\r\n"); if ( requestPos == string::npos ) { // unfinished recv data, need more MYLOG_ERROR( "error http request : %s" , sHttpHead.c_str()); return -1; } //ready to parse param string sRequestLine = sHttpHead.substr(0, requestPos); string::size_type pos1 = sRequestLine.find("/"); if ( pos1 == string::npos ) { MYLOG_DEBUG( "can't find /"); return -1; } string::size_type pos2 = sRequestLine.find(" ", pos1);//find space if ( pos2 == string::npos ) { MYLOG_DEBUG( "can't find space"); return -1; } m_str_GetRequest = sRequestLine.substr(pos1, pos2-pos1); ParseParam(m_str_GetRequest); //LOG4CPLUS_DEBUG(logger, "m_str_GetRequest: " << m_str_GetRequest); ParseHead(sHttpHead); /*std::map<std::string, std::string>::iterator iter; for (iter = mapHead.begin(); iter!= mapHead.end(); iter++) { LOG4CPLUS_DEBUG(logger, "key="<<iter->first<<" value="<<iter->second); }*/ //ready to parse cookie string sCookie = GetLine("COOKIE", sHttpHead); ParseCookie(sCookie); return 0; }
void CDbProxyHandler::query(DBTable& _return, const std::string& sign, const int32_t seq, const int32_t query_index, const std::vector<std::string> & tokens, const int32_t limit, const int32_t limit_start) { CConfigLoader* config_loader = CConfigLoader::get_singleton(); struct QueryInfo query_info; if (check_token(seq, tokens)) { throw apache::thrift::TApplicationException("invalid parameter"); } if (!config_loader->get_query_info(query_index, &query_info)) { MYLOG_ERROR("query_index[%d] not exists\n", query_index); throw apache::thrift::TApplicationException("query_index not exists"); } if (sign != query_info.sign) { MYLOG_ERROR("sign[%s] error: %s\n", sign.c_str(), query_info.sign.c_str()); throw apache::thrift::TApplicationException("sign error"); } try { sys::DBConnection* db_connection = config_loader->get_db_connection(query_info.database_index); if (NULL == db_connection) { MYLOG_ERROR("database_index[%d] not exists\n", query_info.database_index); throw apache::thrift::TApplicationException("database_index not exists"); } else if (tokens.size() > utils::FORMAT_STRING_SIZE) { MYLOG_ERROR("[%d]too big: %d\n", seq, (int)tokens.size()); throw apache::thrift::TApplicationException("tokens too many"); } else { std::string sql = utils::format_string(query_info.sql_template.c_str(), tokens); MYLOG_DEBUG("%s LIMIT %d,%d\n", sql.c_str(), limit_start, limit); if (limit_start > 0) db_connection->query(_return, "%s LIMIT %d,%d", sql.c_str(), limit_start, limit); else db_connection->query(_return, "%s LIMIT %d", sql.c_str(), limit); } } catch (sys::CDBException& db_ex) { MYLOG_ERROR("[%d]%s\n", seq, db_ex.str().c_str()); throw apache::thrift::TApplicationException(db_ex.str()); } }
MOOON_NAMESPACE_USE static void foo() { for (int i=0; i<100000; ++i) { MYLOG_DEBUG("[%d]MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM", i); MYLOG_DETAIL("[%d]KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK", i+1); MYLOG_INFO("[%d]BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", i+2); MYLOG_ERROR("[%d]TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT", i+3); MYLOG_WARN("[%d]PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", i+4); MYLOG_TRACE("[%d]AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", i+5); MYLOG_STATE("[%d]ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ", i+6); //sys::CUtils::millisleep(10); } }
bool CHttpEvent::on_name_value_pair(const char* name_begin, const char* name_end ,const char* value_begin, const char* value_end) { MYLOG_DEBUG("Name: %.*s, Value: %.*s.\n", (int)(name_end-name_begin), name_begin, (int)(value_end-value_begin), value_begin); size_t name_len = name_end - name_begin; size_t value_len = value_end - value_begin; if ((name_len < 3) || (name_len>sizeof(_on_name_value_pair_xxx)/sizeof(on_name_value_pair_xxx))) { MYLOG_ERROR("Error NV - %.*s: %.*s.\n", (int)name_len, name_begin, (int)value_len, value_begin); return false; } if (NULL == _on_name_value_pair_xxx[name_len]) return true; return (this->*_on_name_value_pair_xxx[name_len])(name_begin, name_len, value_begin, value_len); }
int CDbProxyHandler::update(const std::string& sign, const int32_t seq, const int32_t update_index, const std::vector<std::string>& tokens) { CConfigLoader* config_loader = CConfigLoader::get_singleton(); struct UpdateInfo update_info; if (check_token(seq, tokens)) { throw apache::thrift::TApplicationException("invalid parameter"); } if (!config_loader->get_update_info(update_index, &update_info)) { MYLOG_ERROR("[%d]update_index[%d] not exists\n", seq, update_index); throw apache::thrift::TApplicationException("update_index not exists"); } try { sys::DBConnection* db_connection = config_loader->get_db_connection(update_info.database_index); if (NULL == db_connection) { MYLOG_ERROR("[%d]database_index[%d] not exists\n", seq, update_info.database_index); throw apache::thrift::TApplicationException("database_index not exists"); } else if (tokens.size() > utils::FORMAT_STRING_SIZE) { MYLOG_ERROR("[%d]too big: %d\n", seq, (int)tokens.size()); throw apache::thrift::TApplicationException("tokens too many"); } else { std::string sql = utils::format_string(update_info.sql_template.c_str(), tokens); MYLOG_DEBUG("%s\n", sql.c_str()); int affected_rows = db_connection->update("%s", sql.c_str()); return affected_rows; } } catch (sys::CDBException& db_ex) { MYLOG_ERROR("[%d]%s\n", seq, db_ex.str().c_str()); throw apache::thrift::TApplicationException(db_ex.str()); } }
void zmq_client::send_msg_doit( void ) { MYLOG_DEBUG("send_msg_doit thread start"); int result; sleep(1); zmq::context_t *pcontext = new zmq::context_t(1); zmq::socket_t *psocket_cmd = new zmq::socket_t(*pcontext, ZMQ_DEALER); result = psocket_cmd->connect(m_ipc_addr.c_str()); if (result == -1) { MYLOG_ERROR("failed to connect to ipc, addr=%s",m_ipc_addr.c_str()); return; } while (true) { boost::shared_ptr<STU_ZMQ_CLIENT_MSG> pstu; result = m_queue_send.dequeue_expand(pstu); if (result != 1) { continue; } //MY_LOGDEBUG("recv msg from m_queue_send, data="<<pstu->data<<" dst="<<pstu->destination); m_queue_ipc.add_queue(pstu); STU_ZMQ_INFO stu; stu.cmd_type=ZMQ_CMD_TYPE_DATA; stu.data = pstu->data; stu.destination = pstu->destination; socket_wrapper wrapper; wrapper.send_pack(psocket_cmd, &stu); } }
bool CHttpEvent::on_method(const char* begin, const char* end) { MYLOG_DEBUG("Http method: %.*s.\n", (int)(end-begin), begin); int len = end-begin; switch (len) { case 3: if (0 == strncasecmp(begin, "GET", len)) { _header.set_method(CHttpHeader::hm_get); return true; } case 4: if (0 == strncasecmp(begin, "POST", len)) { _header.set_method(CHttpHeader::hm_post); return true; } break; } return false; }
void zmq_client::reactor_doit( void ) { MYLOG_DEBUG("zmq_client::reactor_doit started"); int result; zmq::context_t *pcontext_client=new zmq::context_t(1); std::string str_connection; str_connection = "tcp://"+m_router_ip+":"+SDUtility::format(m_router_port); MYLOG_DEBUG("conn=%s",str_connection.c_str()); m_psocket_router = new zmq::socket_t(*pcontext_client,ZMQ_DEALER); m_psocket_router->setsockopt(ZMQ_IDENTITY, m_client_ientity.c_str(),m_client_ientity.size()); result = m_psocket_router->connect(str_connection.c_str()); if (result == -1) { MYLOG_ERROR("connect to router failed ,conn str=%s",str_connection.c_str()); return; } m_psocket_cmd=new zmq::socket_t(*pcontext_client, ZMQ_ROUTER); result = m_psocket_cmd->bind(m_ipc_addr.c_str()); MYLOG_DEBUG("ipc=%s",m_ipc_addr.c_str()); if (result == -1) { MYLOG_ERROR("failed to bind socket to ipc,ipc=%s",m_ipc_addr.c_str()); return; } zmq_pollitem_t items [] = { { m_psocket_router->get(), 0, ZMQ_POLLIN, 0 }, { m_psocket_cmd->get(), 0, ZMQ_POLLIN, 0 }, }; time_t updatetime=time(NULL); socket_wrapper sockwrapper; while (true) { int result; zmq::message_t message; int poll_result = zmq_poll (items, 2, 1000); if (poll_result == -1) { MYLOG_ERROR("failed to zmq_poll in dispatch_transport_doit,errstr=%s, errno=%d",ZMQ_ERROR,zmq_errno()); continue; } time_t currenttime = time(NULL); if (currenttime>updatetime) { time_t diff=currenttime-updatetime; if (diff>=1) { updatetime=time(NULL); STU_ZMQ_INFO stu_heartbeat; stu_heartbeat.cmd_type=ZMQ_CMD_TYPE_HEARTBEAT; //stu_heartbeat.data="unkownunkownunkoownunkownunkoownunkownunkoownunkownunkoownunkownunkoownunkownunkoownunkownunkoownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkownunkown"; stu_heartbeat.data="unkown"; stu_heartbeat.destination="unkown"; sockwrapper.send_pack(m_psocket_router, &stu_heartbeat); //MYLOG_DEBUG("ientity=%s send heartbeat finished",m_client_ientity.c_str()); } } if (items [0].revents & ZMQ_POLLIN) // router, send package to client { STU_ZMQ_INFO stu; result = sockwrapper.recv_pack(m_psocket_router,&stu); if (result == -1) { continue; } if (stu.cmd_type == ZMQ_CMD_TYPE_DATA) { m_callback_notify(stu.data); } else if(stu.cmd_type == ZMQ_CMD_TYPE_HEARTBEAT) { MYLOG_DEBUG("identity=%s recv heartbeat, cmdtype=%s",m_client_ientity.c_str(),stu.cmd_type.c_str()); } } if (items [1].revents & ZMQ_POLLIN) // ipc send package to reactor { //MY_LOGDEBUG("recv ipc message"); STU_ZMQ_INFO_IDENTITY stu_zmq_info_identity; result = sockwrapper.recv_pack_identity(m_psocket_cmd, &stu_zmq_info_identity); if (result == -1) { MYLOG_ERROR("sockwrapper.recv_pack_identity error"); continue; } boost::shared_ptr<STU_ZMQ_CLIENT_MSG> pstu_msg; result = m_queue_ipc.dequeue_nodelay(pstu_msg); if (result != 1) { MYLOG_ERROR("m_queue_ipc.dequeue_nodelay error"); continue; } STU_ZMQ_INFO stu; stu.data = pstu_msg->data; stu.cmd_type=ZMQ_CMD_TYPE_DATA; stu.destination = pstu_msg->destination; //MY_LOGDEBUG("data="<<stu.data<<" dst="<<stu.destination); result = sockwrapper.send_pack(m_psocket_router, &stu); if (result == -1) { MYLOG_ERROR("failed to send msg to router"); } } } }
void tcpserver::epoll_reactor_doit( void ) { uint32_t sendbuf = 1024*30; uint32_t sendbufsize=sizeof(uint32_t); int result = 0; int epoll_fd = epoll_create(m_epoll_maxsize); m_epoll_fd = epoll_fd; if(epoll_fd == -1) { return; } // add listen fd to epoll epoll_event event_listen; event_listen.data.fd = m_listen_fd; event_listen.events = EPOLLIN; result = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, m_listen_fd, &event_listen); if (result == -1) { MYLOG_ERROR("failed to add listen fd to epoll fd"); return; } // add pipe fd to epoll epoll_event event_pipe; event_pipe.data.fd = m_pipe_read; event_pipe.events = EPOLLIN; result = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, m_pipe_read, &event_pipe); if (result == -1) { MYLOG_ERROR("failed to add pipe fd to epoll fd"); return; } epoll_event *pepoll_events = new epoll_event[m_epoll_maxsize]; uint64_t timeout_check_before =time(NULL); uint64_t currenttime = time(NULL); while (true) { currenttime = time(NULL); if (currenttime < timeout_check_before) { timeout_check_before = currenttime; } else { if (currenttime-timeout_check_before > m_timeout_check_interval_seconds) { //MYLOG_DEBUG("check timeout scan reach, ready to start timeout check"); timeout_check_before = currenttime; // 需要做超时检查轮训 for (int i=0; i<m_epoll_maxsize; i++) { if (m_array_epoll_data[i]->status == enum_socket_close) { continue; } //MYLOG_DEBUG("fd %d in conencted, timeout diff=%d", i, currenttime - m_array_epoll_data[i]->updatetime); if (currenttime > m_array_epoll_data[i]->updatetime ) { if (currenttime - m_array_epoll_data[i]->updatetime > m_fd_timeout_threshold) { MYLOG_DEBUG("socket timeout, close socket"); close_socket_internal(i); } } } //MYLOG_DEBUG("check timeout scan reach, timeout check finished"); } } int fds = epoll_wait(epoll_fd, pepoll_events, m_epoll_maxsize, 500); if (fds == 0) { //MYLOG_INFO("epoll wait timeout notify"); continue; } if (fds == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { continue; } MYLOG_ERROR("epoll wait error, errno=%d, strerr=%s", errno, strerror(errno)); return; } int i; for (i=0; i<fds; i++) { // check if new client connection if (pepoll_events[i].data.fd == m_listen_fd) { struct sockaddr_in new_addr; memset(&new_addr, 0, sizeof(new_addr)); socklen_t new_addr_len = sizeof(new_addr); int new_client_fd; new_client_fd = accept(m_listen_fd, (struct sockaddr*)&new_addr, &new_addr_len); if (new_client_fd == -1) { MYLOG_ERROR("failed to accept new client from listen fd"); continue; } if (new_client_fd >= m_epoll_maxsize) { close(new_client_fd); MYLOG_ERROR("clientfd larger than max epoll count, fd=%d",new_client_fd); continue; } int noblock_result = set_nonblocking(new_client_fd); if (noblock_result == -1) { MYLOG_ERROR("failed to set new client fd to nonblocking"); close(new_client_fd); continue; } // set sendbuffer; int optresult = setsockopt(new_client_fd,SOL_SOCKET,SO_SNDBUF,(char*) &sendbuf,sendbufsize); if (optresult == -1) { MYLOG_ERROR("failed to set send buffer size,errno=%d, errstr=%s",errno, strerror(errno)); close(new_client_fd); continue; } epoll_event new_client_event; new_client_event.data.fd = new_client_fd; new_client_event.events = EPOLLIN; int add_result = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, new_client_fd, &new_client_event); if (add_result == -1) { MYLOG_ERROR("failed to add new client fd to epoll fd, errno=%d, strerr=%s",errno, strerror(errno)); close(new_client_fd); continue; } m_array_epoll_data[new_client_fd]->status = enum_socket_conn; m_array_epoll_data[new_client_fd]->client_socket_addr = new_addr; m_array_epoll_data[new_client_fd]->clientfd = new_client_fd; m_array_epoll_data[new_client_fd]->fd_unify_tag = get_unify_tag(new_client_fd); m_array_epoll_data[new_client_fd]->str_data_recved.clear(); m_array_epoll_data[new_client_fd]->str_data_write.clear(); m_array_epoll_data[new_client_fd]->updatetime = time(NULL); m_clientcount++; m_notify_callback(enum_sock_new_conn, NULL, 0, m_array_epoll_data[new_client_fd]); continue; } // ----------------- new pipe cmd arrive ------------------- if (pepoll_events[i].data.fd == m_pipe_read) { char pipe_data; int pipe_result = read(m_pipe_read, &pipe_data, 1); if (pipe_result == -1) { MYLOG_ERROR("get cmd from pipe read failed, continue"); continue; } boost::shared_ptr<STU_EPOLL_RTN> stu_epoll_rtn; int queue_result = m_queue_send.dequeue_nodelay(stu_epoll_rtn); if (queue_result != 1) { MYLOG_ERROR("failed to get data from queue"); continue; } // check if this socket is closed already if (m_array_epoll_data[stu_epoll_rtn->clientfd]->status == enum_socket_close) { MYLOG_ERROR("socket already closed, drop send or close cmd"); continue; } // check fd validation if (m_array_epoll_data[stu_epoll_rtn->clientfd]->clientfd == stu_epoll_rtn->clientfd && m_array_epoll_data[stu_epoll_rtn->clientfd]->fd_unify_tag == stu_epoll_rtn->fd_unify_tag) { } else { MYLOG_ERROR("different clientfd or unify tag, skip close or send"); continue; } // 处理pipe发送过来的命令 switch (stu_epoll_rtn->cmd_type) { case PIPE_CMD_SEND: { int send_result = send_internal(stu_epoll_rtn.get()); if (send_result == -1) { close_socket_internal(stu_epoll_rtn->clientfd); } else if (send_result > 0) { // 需要将socket添加到epollfd中,监听可写事件 epoll_event client_event; client_event.data.fd = stu_epoll_rtn->clientfd; client_event.events = EPOLLOUT | EPOLLIN; int mod_result = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, stu_epoll_rtn->clientfd, &client_event); if (mod_result == -1) { close_socket_internal(stu_epoll_rtn->clientfd); continue; } } else { // 此次没有数据需要提交到协议栈缓冲区,需要修改该socket的可写标记 send_result = 0; /*epoll_event client_event; client_event.data.fd = stu_epoll_rtn->clientfd; client_event.events = EPOLLIN; int mod_result = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, stu_epoll_rtn->clientfd, &client_event); if (mod_result == -1) { close_socket_internal(stu_epoll_rtn->clientfd); continue; }*/ } } break; case PIPE_CMD_CLOSE: { MYLOG_DEBUG("ready to close socket from pipe cmd"); close_socket_internal(stu_epoll_rtn->clientfd); } break; default: MYLOG_ERROR("unkown pipe cmd type, type=%d",stu_epoll_rtn->cmd_type); continue; break; } continue; } if (pepoll_events[i].events & POLL_IN) { // 可读事件触发 int recv_result = dealwith_recv(pepoll_events[i].data.fd); if (recv_result == -1) { MYLOG_ERROR("dealwith_recv return errors, ready to close socket, fd=%d", pepoll_events[i].data.fd); close_socket_internal(pepoll_events[i].data.fd); } } if (pepoll_events[i].events & POLL_OUT) { // 可写事件触发 int send_result = dealwith_send(pepoll_events[i].data.fd); if (send_result == -1) { MYLOG_ERROR("dealwith_send return errors, ready to close socket, fd=%d", i); close_socket_internal(pepoll_events[i].data.fd); } } } // end of for (i=0; i<fds; i++) } // end of while (true) }
void CHttpEvent::on_error(const char* errmsg) { MYLOG_DEBUG("Http parsing error: %s.\n", errmsg); }
/* 返回值: -1: 失败,需要关闭socket 0: 成功 */ int tcpserver::dealwith_recv( int clientfd ) { m_array_epoll_data[clientfd]->updatetime = time(NULL); char buffer[1024*10]; size_t result; int headlength = m_notify_callback(enum_sock_head_length, NULL, NULL, NULL); int data_already_recved = m_array_epoll_data[clientfd]->str_data_recved.size(); if (headlength > data_already_recved) { // 还需要接受头部 size_t size_needs_recv = headlength-data_already_recved; result = recv(clientfd,buffer, size_needs_recv, 0); if (result == -1) { if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) { MYLOG_DEBUG("recv get -1, but errno=%s", strerror(errno)); return 0; } MYLOG_DEBUG("recv get -1, errno=%s", strerror(errno)); return -1; } if (result == 0) // 对端关闭 { MYLOG_ERROR("terminal socket actively closed, fd=%d",clientfd); return -1; } std::string str_data; str_data.append(buffer, result); m_array_epoll_data[clientfd]->str_data_recved = m_array_epoll_data[clientfd]->str_data_recved+str_data; return 0; } else { // uint32_t already_recved_size = m_array_epoll_data[clientfd]->str_data_recved.size(); uint32_t bodylength=m_notify_callback(enum_sock_body_length, m_array_epoll_data[clientfd]->str_data_recved.c_str(), already_recved_size, m_array_epoll_data[clientfd]); if (bodylength >= 1024*10) { return -1; } uint32_t need_recv_size = bodylength-(already_recved_size-headlength); if (need_recv_size == 0) { m_notify_callback(enum_sock_data_recv, m_array_epoll_data[clientfd]->str_data_recved.c_str(), m_array_epoll_data[clientfd]->str_data_recved.size(), m_array_epoll_data[clientfd]); m_array_epoll_data[clientfd]->str_data_recved.clear(); return 0; } else { result = recv(clientfd, buffer, need_recv_size, 0); if (result == 0) { return -1; } if (result == -1) { if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) { return 0; } return -1; } std::string str_data; str_data.append(buffer, result); m_array_epoll_data[clientfd]->str_data_recved = m_array_epoll_data[clientfd]->str_data_recved+str_data; if (result == need_recv_size) { m_notify_callback(enum_sock_data_recv, m_array_epoll_data[clientfd]->str_data_recved.c_str(), m_array_epoll_data[clientfd]->str_data_recved.size(), m_array_epoll_data[clientfd]); m_array_epoll_data[clientfd]->str_data_recved.clear(); } return 0; } } return 0; }
int HttpRequestPacket::decode_post( string& sHttpHead ) { string::size_type requestPos = sHttpHead.find("\r\n"); if ( requestPos == string::npos ) { // unfinished recv data, need more MYLOG_ERROR("error http request :%s " ,sHttpHead.c_str()); return -1; } //ready to parse param string sRequestLine = sHttpHead.substr(0, requestPos); string::size_type pos1 = sRequestLine.find("/"); if ( pos1 == string::npos ) { MYLOG_DEBUG( "can't find /"); return -1; } string::size_type pos2 = sRequestLine.find(" ", pos1);//find space if ( pos2 == string::npos ) { MYLOG_DEBUG( "can't find space"); return -1; } m_str_GetRequest = sRequestLine.substr(pos1, pos2-pos1); ParseParam(m_str_GetRequest); //LOG4CPLUS_DEBUG(logger, "m_str_GetRequest: " << m_str_GetRequest); ParseHead(sHttpHead); std::map<std::string, std::string>::iterator iter; iter = mapHead.find("CONTENT-LENGTH"); if (iter == mapHead.end()) { MYLOG_DEBUG( "failed to find CONTENT-LENGTH from headers"); return -1; } std::string str_length = iter->second; boost::trim(str_length); int intlength = SDUtility::atouint32(str_length); if (sHttpHead.size() < intlength) { return -1; } std::string str_params; str_params = sHttpHead.substr(sHttpHead.length()-intlength, intlength); string2map(str_params, mapParam); return 0; /*std::map<std::string, std::string>::iterator iter; for (iter = mapHead.begin(); iter!= mapHead.end(); iter++) { LOG4CPLUS_DEBUG(logger, "key="<<iter->first<<" value="<<iter->second); }*/ //ready to parse cookie //string sCookie = GetLine("COOKIE", sHttpHead); //ParseCookie(sCookie); }
bool CHttpEvent::on_url(const char* begin, const char* end) { MYLOG_DEBUG("URL: %.*s.\n", (int)(end-begin), begin); _header.set_url(begin, end-begin); return true; }
bool CHttpEvent::on_version(const char* begin, const char* end) { MYLOG_DEBUG("Version: %.*s.\n", (int)(end-begin), begin); return true; }