void doUnsubscribe(const TcpConnectionPtr& conn, const string& topic) { LOG_OK << conn->name() << " unsub " << topic; getTopic(topic).remove(conn); // topic could be the one to be destroyed, so don't use it after erasing. //客户端不订阅某个topic,就销毁 ConnectionSubscription* connSub = boost::any_cast<ConnectionSubscription>(conn->getMutableContext()); connSub->erase(topic); }
void ProxyServer::onGetIncrusrstk(const TcpConnectionPtr& conn,const IncrstkcfgPtr& message,Timestamp) { LOG_INFO << "onGetIncrusrstk protobuf received..."; std::string taskid = message->taskid(); int id = boost::lexical_cast<int>(message->incrementid()); int count = boost::lexical_cast<int>(message->count()); char sztask[512]; sprintf(sztask,"%s:%d;%s:%s;%s:%d;%s:%d",TASK_TYPE,GET_INCR_USERSTK,TASK_ID,taskid.c_str(),MOBILE_INCR,id,QUERY_COUNT,count); LOG_INFO << "taskid " <<taskid << " id " << id; // m_redis.command(boost::bind(&ProxyServer::onPushTask,this,_1,_2),"lpush test %s",sztask); m_redis.command(boost::bind(&ProxyServer::onPushTask,this,_1,_2),"lpush %s %s",m_redis.getPushTitle().c_str(),sztask); m_taskconns[taskid] = std::string(conn->name().c_str()); }
void onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp receiveTime) { printf("onMessage(): tid=%d received %d bytes from connection [%s] at %s\n", CurrentThread::tid(), buf->readableBytes(), conn->name().c_str(), receiveTime.toFormattedString().c_str()); string str(buf->peek(), buf->readableBytes()); printf("str:%s\n", str.c_str()); buf->retrieveAll(); }
void LClient::newConnection( const TcpConnectionPtr& session ) { connLost_ = 0; connection_ = session; if( connection_ ) { tagHello hello; strncpy(hello.msg, "Copyright 2010 by backkom. All rights reserved." "This software is the confidential and proprietary information of backkom." "('Confidential Information'). You shall not disclose such Confidential -" "Information and shall use it only in accordance with the terms of the - " "license agreement you entered into with backkom", 128); connection_->sendCmd(&hello); ++display_; } LOGD("Hello-%s", session->name().c_str()); }
void ProxyServer::onSetsingleusrstk(const TcpConnectionPtr& conn,const SetsingleusrstkPtr& message,Timestamp) { LOG4CXX_INFO(log4cxx::Logger::getLogger(PROXYSERVER),"recv SetSingleUsrStkCfg!"); LOG_INFO << "onSetsingleusrstk protobuf received..."; // LOG_INFO << message->DebugString() ; std::string taskid = message->taskid(); std::string pushdata = pb2json(*message); stringReplace(pushdata," ",""); char szconn[128] = {0}; sprintf(szconn,"\"conn\":\"%s\",\"%s\":\"%d\",",m_redis.getPushTitle().c_str(),TASK_TYPE,SET_USER_STOCK); // sprintf(szconn,"\"conn\":\"%s\",\"%s\":\"%d\",",conn->name().c_str(),TASK_TYPE,SET_USER_STOCK); pushdata.insert(1,szconn); LOG_INFO << m_redis.getPushTitle() << ": " << pushdata; m_redis.command(boost::bind(&ProxyServer::onPushTask,this,_1,_2), "lpush %s %s",m_redis.getPushTitle().c_str(),pushdata.c_str()); m_taskconns[taskid] = std::string(conn->name().c_str()); }
//TODO:这个函数没有处理客户端sub的topic不存在的情况 void doSubscribe(const TcpConnectionPtr& conn, const string& topic) { //getMutableContext返回的是:boost::any* //为什么any_cast用的是ConnectionSubscripton,而不是指针???看boost:any_cast原型 /* template<typename ValueType> ValueType * any_cast(any * operand) */ LOG_OK << conn->name() << " sub " << topic; ConnectionSubscription* connSub = boost::any_cast<ConnectionSubscription>(conn->getMutableContext()); connSub->insert(topic);//保存这个客户端订阅的主题 getTopic(topic).add(conn); }
void ProxyServer::onClientConnection(const TcpConnectionPtr& conn) { LOG_TRACE << "Client " << conn->peerAddress().toIpPort() << " -> " << conn->localAddress().toIpPort() << " is " << (conn->connected() ? "online" : "offline"); std::string connname(conn->name().c_str()); if(conn->connected()) { m_conns[connname] = conn; // m_redis.command(boost::bind(&ProxyServer::onRedisGetResult,this,_1,_2),"BRPOP test 0"); } else { LOG_INFO << "connname offline " << connname; m_conns.erase(connname); } }
void MyTcpClient::onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp receiveTime) { if (buf->readableBytes() >= sizeof(int32_t)) { /* const void* data = buf->peek(); int32_t be32 = *static_cast<const int32_t*>(data); buf->retrieve(sizeof(int32_t)); time_t time = sockets::networkToHost32(be32); Timestamp ts(implicit_cast<uint64_t>(time) * Timestamp::kMicroSecondsPerSecond); LOG_INFO << "Server time = " << time << ", " << ts.toFormattedString(); */ std::string str = static_cast<std::string>(buf->retrieveAllAsString()); LOG_INFO << "Server time = " << str; } else { LOG_INFO << conn->name() << " no enough data " << buf->readableBytes() << " at " << receiveTime.toFormattedString(); } }
void onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp){ LOG_DEBUG << conn->name(); size_t len = buf->readableBytes(); while(len >= kCells + 2){ const char* crlf = buf->findCRLF(); if(crlf){ string request(buf->peek(), crlf); buf->retrieveUntil(crlf + 2); len = buf->readableBytes(); if(!processRequest(conn, request)){ conn->send("Bad Request! \r\n"); conn->shutdown(); break; } }else if(len > 100){ // id + ":" + kCells + "\r\n" conn->send("Id too long!\r\n"); conn->shutdown(); break; }else{ break; } } }
void BaseSvr::onMessage(const TcpConnectionPtr& conn, Buffer* buf) { LOG_INFO << "accept msg from <" << conn->name() << ">"; string request(buf->peek(), buf->beginWrite()); LOG_INFO << "received msg from client<" << conn->peerAddress().IpAndPort() << "> : <" << request << ">"; conn->send(request); size_t len = 0; while((len = buf->readableBytes()) > 1) { // 本消息包除头部外剩余部分的长度 size_t packetLen = endian::networkToHost16(buf->peekInt16()); if(packetLen > len) {///< 缓冲区剩余空间反而大于消息包长度了 > conn->send("peek msg fail: packetLen > len of buffer\r\n"); conn->shutdown(); break; } if(false == protobufapi::checksum(buf->peek() + HEADER_LENGTH, packetLen)) {///< 校验结果不匹配 > conn->send("server check sum error. your msg is discard"); conn->shutdown(); break; } Buffer *pMsg = new Buffer(); pMsg->retrieveAll(); pMsg->append(buf->peek() + sizeof int16_t, sizeof int16_t + packetLen - TAIL_LENGTH); m_msgQueue.put(pMsg); buf->retrieve(packetLen + HEADER_LENGTH); } }
bool processRequest(const TcpConnectionPtr& conn, const kimgbo::string& request) { kimgbo::string id; kimgbo::string puzzle; bool goodRequest = true; kimgbo::string::const_iterator colon = find(request.begin(), request.end(), ':'); if (colon != request.end()) { id.assign(request.begin(), colon); puzzle.assign(colon+1, request.end()); } else { puzzle = request; } if (puzzle.size() == implicit_cast<size_t>(kCells)) { LOG_DEBUG << conn->name(); kimgbo::string result = solveSudoku(puzzle); if (id.empty()) { conn->send(result+"\r\n"); } else { conn->send(id+":"+result+"\r\n"); } } else { goodRequest = false; } return goodRequest; }
void ProxyServer::onUnknownMessage(const TcpConnectionPtr& conn,const MessagePtr& message,Timestamp) { conn->shutdown(); m_conns.erase(std::string(conn->name().c_str())); }
void ProxyServer::dealRedisIncrstkcfg(std::string result) { dataserver::IncrUsrStkCfg incr_stkcfg; size_t count = 0; std::stringstream stream; stream<<result; std::string type,user,stk,bulletin,maxprice,minprice,run,range,incr; try { ptree pt,p1,p2,p3,p4; read_json<ptree>(stream,pt); type = pt.get<std::string>(TASK_TYPE); std::cout<<"tasktype: "<<type<<std::endl; p1 = pt.get_child(USER); incr_stkcfg.set_taskid(boost::lexical_cast<std::string>(GET_INCR_USERSTK)); for(ptree::iterator it = p1.begin(); it != p1.end(); it++) { p2 = it->second; user = p2.get<std::string>(USERSTOCK_UID); std::cout<<"UID: "<<user<<std::endl; dataserver::SingleUserStkCfg* user_stkcfg = incr_stkcfg.add_items(); user_stkcfg->set_usrid(user); p3 = p2.get_child(USERSTOCK_STOCKS); for(ptree::iterator iter = p3.begin(); iter != p3.end(); iter++) { p4 = iter->second; stk = p4.get<std::string>(USERSTOCK_STOCKCODE); bulletin = p4.get(USERSTOCK_BULLETIN,"1"); incr = p4.get<std::string>(USERSTOCK_INCR); maxprice = p4.get(USERSTOCK_MAXPRICE,"-1.0"); minprice = p4.get(USERSTOCK_MINPRICE,"-1.0"); run = p4.get(USERSTOCK_RUN,"1"); range = p4.get(USERSTOCK_RANGE,"-10000"); std::cout<<stk<<","<<bulletin<<","<<incr<<std::endl; dataserver::UserStockCfg* pUsercfg = user_stkcfg->add_stkcfg(); pUsercfg->set_stockcode(stk); pUsercfg->set_bulletin(boost::lexical_cast<int>(bulletin)); pUsercfg->set_max_price(boost::lexical_cast<double>(maxprice)); pUsercfg->set_min_price(boost::lexical_cast<double>(minprice)); pUsercfg->set_run(boost::lexical_cast<int>(run)); pUsercfg->set_incrementid(boost::lexical_cast<long long int>(incr)); count++; } } } catch(ptree_error& e) { std::cout << "error for ptree parse.." << std::endl; } google::protobuf::Message* protomsg = NULL; protomsg = &incr_stkcfg; std::string task = ""; std::string connname = m_taskconns[task]; LOG_INFO << "connname " << connname << ", task "<<task; for(ConnectionMap::iterator it(m_conns.begin()); it != m_conns.end(); ++it) { TcpConnectionPtr conn = it->second; if(connname == std::string(conn->name().c_str())) { m_codec.send(conn,*protomsg); break; } } }
void ProxyServer::onRemoveConnection(const TcpConnectionPtr& conn) { m_conns.erase(std::string(conn->name().c_str())); }
void LClient::delConnection( const TcpConnectionPtr& session ) { connLost_ = 1; LOGD("Byte-%s", session->name().c_str()); }