void OnMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp time_) { LOG_DEBUG << conn->name(); size_t len = buf->readableBytes(); while (len >= kCells + 2) { const char* crlf = buf->findCRLF(); if (crlf) { kimgbo::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 onPubSubMessage( const TcpConnectionPtr& connectionPtr, const MessagePtr& messagePtr, Timestamp) { const PubSubMessage& message = *dynamic_cast<PubSubMessage*>(messagePtr.get()); counters_.incrementInBoundMessage(); switch(message.op()) { case Op::PUB: distributePublish( string(message.topic().data()), string(message.content().data()) ); break; case Op::SUB: doSubscribe(connectionPtr, string(message.topic().data())); break; case Op::UNSUB: doUnsubscribe(connectionPtr, string(message.topic().data())); break; default: LOG(ERROR) << "Unsupported message op: " << message.op(); connectionPtr->shutdown(); break; } }
void EchoServer::onClose(const TcpConnectionPtr &conn) { cout << "client : " << conn->getPeerAddr().toIp() << " : " << conn->getPeerAddr().toPort() << " close" << endl; conn->shutdown(); }
void onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp receiveTime) { if (buf->findCRLF()) { conn->send("No such user\r\n"); conn->shutdown(); } }
void onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp receiveTime) { ParseResult result = kSuccess; while (result == kSuccess) //while循环,当result一直为kSuccess { string cmd; string topic; string content; result = parseMessage(buf, &cmd, &topic, &content); if (result == kSuccess) { if (cmd == "pub") { doPublish(conn->name(), topic, content, receiveTime); } else if (cmd == "sub") { LOG_INFO << conn->name() << " subscribes " << topic; doSubscribe(conn, topic); } else if (cmd == "unsub") { doUnsubscribe(conn, topic); } else { conn->shutdown(); result = kError; } } else if (result == kError) { conn->shutdown(); } } }
void onConnection(const TcpConnectionPtr& conn){ LOG_INFO << "FileServer - " << conn->peerAddress().toIpPort() << " -> " << conn->localAddress().toIpPort() << " is " << (conn->connected() ? "UP" : "DOWN"); if(conn->connected()){ LOG_INFO << "FileServer - Sending file " << g_file << " to " << conn->peerAddress().toIpPort(); conn->setHighWaterMarkCallback(onHighWaterMark, 64*1024); string fileContent = readFile(g_file); conn->send(fileContent); conn->shutdown(); LOG_INFO << "FileServer - done"; } }
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); } }
void EchoServer::onConnection(const TcpConnectionPtr& conn){ LOG_INFO << "EchoServer - " << conn->peerAddress().toIpPort() << " -> " << conn->localAddress().toIpPort() << " is " << (conn->connected() ? "UP" : "DOWN"); if(conn->connected()){ ++numConnected_; if(numConnected_ > kMaxConnections_){ conn->shutdown(); conn->forceCloseWithDelay(3.0); // > round trip of the whole Internet. } }else{ --numConnected_; } LOG_INFO << "numConnected = " << numConnected_; }
void HttpServer::onMessage(const TcpConnectionPtr& conn, ByteBuffer *buf, Timestamp receiveTime) { LOG_INFO("HttpServer::onConnection recv data [%d][%s]", conn->fd(), buf->toString().c_str()); HttpContext *context = zl::stl::any_cast<HttpContext>(conn->getMutableContext()); assert(context); if(!context->parseRequest(buf, receiveTime)) { conn->send("HTTP/1.1 400 Bad Request\r\n\r\n"); conn->send("HTTP/1.1 400 Bad Request\r\n\r\n"); conn->shutdown(); } if (context->gotAll()) { LOG_INFO("HttpServer::onMessage parse request over."); response(conn, context->request()); context->reset(); // process request and return response, then reset, for long-connection } }
void ZGWServer::onClientConnection(const TcpConnectionPtr& conn) { LOG_INFO << "Client " << conn->peerAddress().toIpPort() << " -> " << conn->localAddress().toIpPort() << " is " << (conn->connected() ? "UP" : "DOWN"); if( conn->connected() ) { int id = -1; if( !idQueue_.empty() ) { id = idQueue_.front(); idQueue_.pop(); id2conn_[id] = conn; } if( -1 == id ) { LOG_ERROR << "[过载保护]系统到达最大连接数,拒绝连接"; conn->shutdown(); } else { conn->setContext(id); LOG_INFO << "新连接[" << conn->peerAddress().toIpPort() << "]流水ID: " << id; } } else { if(!conn->getContext().empty()) { int id = boost::any_cast<int>(conn->getContext()); assert(id > 0 && id <= static_cast<int>(kMaxConns)); // 回收连接的key idQueue_.push(id); id2conn_.erase(id); } } }
void HttpServer::response(const TcpConnectionPtr& conn, const HttpRequest& req) { const string& connection = req.getHeader("Connection"); bool close = connection == "close" || (req.version() == HTTP_VERSION_1_0 && connection != "Keep-Alive"); HttpResponse response(close); response.setStatusCode(HttpStatusOk); response.setServerName("MyHttpServer"); methodCallback(req, &response); // callback, for init response ByteBuffer buf; response.compileToBuffer(&buf); //printf("[%s]\n", buf.toString().c_str()); conn->send(&buf); LOG_INFO("HttpServer::response send data [%d]", conn->fd()); if (response.closeConnection()) { LOG_INFO("HttpServer::response close this[%d]", conn->fd()); conn->shutdown(); } }
void P2PServer::onStringMessage(const TcpConnectionPtr& conn, const string& message, Timestamp) { char type = message[0]; int id = message[1]; string ip = message.substr(2,4); int reqfile = message[6]; //catalogue_->insertUser(ip, reqfile); cout << endl << "test" << catalogue_->hasNeighbor(ip) << message << endl; string ret; if(type == 'a') { catalogue_->insertUser(ip, reqfile); ret.append("b0"); ret.append(processMessage(ip)); cout << endl << ret << endl; codec_.send(conn, ret); conn->shutdown(); } else if(type == 'c') { ret.append("d01111"); ret.append(1, (char)100); cout << endl << "222test" << endl << ret; codec_.send(conn, ret); } else { cout << "error 123" << endl; } }
/* * private functions * */ void tcp_server::onConnection(const TcpConnectionPtr& conn) { LOG_DEBUG << "tcp_server - " << conn->peerAddress().toIpPort() << " -> " << conn->localAddress().toIpPort() << " is " << (conn->connected() ? "UP" : "DOWN"); if (conn->connected()) { ++numConnected_; if (numConnected_ > kMaxConnections_) { conn->shutdown(); } else { conn->setTcpNoDelay(true); } } else { --numConnected_; } LOG_DEBUG << "numConnected = " << numConnected_; }
void PubSubClient::onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp receiveTime) { ParseResult result = kSuccess; while (result == kSuccess) { string cmd; string topic; string content; result = parseMessage(buf, &cmd, &topic, &content); if (result == kSuccess) { if (cmd == "pub" && subscribeCallback_) { subscribeCallback_(topic, content, receiveTime); } } else if (result == kError) { conn->shutdown(); } } }
void onClose(const TcpConnectionPtr &conn) { cout<<"close"<<endl; conn->shutdown(); }
void ProxyServer::onUnknownMessage(const TcpConnectionPtr& conn,const MessagePtr& message,Timestamp) { conn->shutdown(); m_conns.erase(std::string(conn->name().c_str())); }
void NetworkProxy::close_connection(const TcpConnectionPtr& connection) { connection->shutdown(); connection->close(); }