Example #1
0
	/// <summary>
	/// Handle_accepts the specified error.
	/// </summary>
	/// <param name="error">The error.</param>
	/// <param name="conn">The conn.</param>
	void TcpServer::handle_accept( const boost::system::error_code& error, const TcpConnectionPtr conn)
	{
		if (!error)	/// ´¦Àí¼àÌýʼþ
		{
			conn->SetMessageCallBack(messageCallBack_);
			conn->SetErrorCallBack(errorCallBack_);
			conn->SetWriteCompleteCallBack(writecompleteCallBack_);
			if (acceptedCallBack_)
			{
				acceptedCallBack_(conn);
			}

			boost::weak_ptr<WheelEntry<TcpConnection> > weak_ptr = timing_wheel_.Register(conn, 
				TimeOutCallBackT<TcpConnection>(boost::bind(&TcpServer::handle_timeout, this, _1)));

			conn->SetContent(boost::any(weak_ptr));

			conn->Start();
			TcpConnectionPtr tcpconnPtr(new TcpConnection(ioservicepool_.get_io_service(), messageCallBack_, &timing_wheel_));
			acceptor_.async_accept(tcpconnPtr->socket(),
				boost::bind(&TcpServer::handle_accept, this,
				boost::asio::placeholders::error,tcpconnPtr));
		}
		else
		{
			if (errorCallBack_)
			{
				errorCallBack_(conn, error);
			}
		}

	}
Example #2
0
	void OnConnection(const TcpConnectionPtr& conn)
	{
		LOG_INFO << conn->localAddress().toIpPort() << " -> "
             << conn->peerAddress().toIpPort() << " is "
             << (conn->connected() ? "UP" : "DOWN");
             	
    if(conn->connected())
    {
    	m_connection = conn;
    	if(g_aliveConnections.incrementAndGet() == g_connections)
    	{
    		LOG_INFO << "all connected";
    	}
    	else
    	{
    		m_connection.reset();
    		
    		if(g_aliveConnections.incrementAndGet() == g_connections)
    		{
    			LOG_INFO << "all disconnected";
    			g_loop->quit();
    		}
    	}
    }
	}
Example #3
0
 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;
         }
     }
 }
Example #4
0
void EchoServer::onConnect(const TcpConnectionPtr &conn)
{
	cout << "client : " << conn->getPeerAddr().toIp() << " : "
		 << conn->getPeerAddr().toPort() << " on" << endl;
	conn->send("hello, welcome to Echo Server!\r\nenter word to look for, or q to quit: ");

}
Example #5
0
NAMESPACE_ZL_NET_START

void defaultConnectionCallback(const TcpConnectionPtr& conn)
{
  LOG_INFO("defaultConnectionCallback : [%s]<->[%s] [%s]\n", conn->localAddress().ipPort().c_str(),
        conn->peerAddress().ipPort().c_str(), conn->connected() ? "UP" : "DOWN");
}
Example #6
0
void ProxyServer::queue_task(const std::string& task,std::string& result)
{

	dataserver::SetUsrStkCfg stkcfg;
	stkcfg.set_taskid("1000001");
   	stkcfg.set_usrid("zhangqi");
    dataserver::UserStockCfg* pUserscfg = stkcfg.add_stkcfg();
    pUserscfg->set_stockcode("600123");
    pUserscfg->set_bulletin(1);
    pUserscfg->set_max_price(12.11);
    pUserscfg->set_run(1);
    pUserscfg->set_incrementid(12345);

    dataserver::UserStockCfg* pUserscfg1 = stkcfg.add_stkcfg();
    pUserscfg1->set_stockcode("600223");
    pUserscfg1->set_bulletin(1);
    pUserscfg1->set_max_price(42.85);
    pUserscfg1->set_run(1);
    pUserscfg1->set_incrementid(9812);

	google::protobuf::Message* protomsg = NULL;
	protomsg = &stkcfg;
	std::string connname = m_taskconns[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;
		}
	}
}
Example #7
0
        bool processRequest(const TcpConnectionPtr& conn, const string& request){
            string id;
            string puzzle;
            bool goodRequest = true;

            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();
                string result = solveSudoku(puzzle);
                if(id.empty()){
                    conn->send(result+"\r\n");
                }else{
                    conn->send(id +":"+ result+"\r\n" );
                }
            }else
                goodRequest = false;
            return goodRequest;
        }
Example #8
0
 void onConnection(const TcpConnectionPtr& conn){
     LOG_INFO << conn->localAddress().toIpPort() << " -> "
              << conn->peerAddress().toIpPort() << " is "
              << (conn->connected() ? "UP" : "DOWN");
     if(!conn->connected())
         loop_->quit();
 }
Example #9
0
void Sender::onConnection(const TcpConnectionPtr& conn)
{
	  LOG_INFO << "EchoServer - " << conn->peerAddress().toIpPort() << " -> "
           << conn->localAddress().toIpPort() << " is "
           << (conn->connected() ? "UP" : "DOWN");
	  
}
Example #10
0
void ZGWServer::responseMsg(zmq_msg_t& msg_t)
{
    size_t msg_size = zmq_msg_size(&msg_t);
    assert( msg_size > 0 );

    std::string str_msg(static_cast<char*>(zmq_msg_data(&msg_t)), msg_size);

    ZMSG msg;
    int rc = msg.deserialize(str_msg);
    if( rc != 0 )
    {
        LOG_ERROR << "PULL线程反序列化消息失败, ret: " << rc;
        return;
    }

    //LOG_INFO << "PULL线程反序列化消息成功, msg[id]: " << msg.flow_id << ", msg[type]: "
    //         << msg.msg_type << ", msg[body size]:" << msg.msg_body.size();

    std::map<uint32_t, TcpConnectionPtr>::iterator iter = id2conn_.find(msg.flow_id);
    if( iter == id2conn_.end() )
    {
        LOG_ERROR << "PULL线程查找flow_id: " << msg.flow_id << "对应的连接失败";
        return;
    }
    TcpConnectionPtr conn = iter->second;

    muduo::net::Buffer buf;
    buf.prependInt8(msg.msg_type);
    buf.prependInt32(static_cast<int32_t>(msg.msg_body.size()));
    buf.append(msg.msg_body.c_str(), msg.msg_body.size());
    conn->send(&buf);

    stat_.msg_recv_cnt.increment();
    stat_.msg_recv_bytes.addAndGet(msg_size);
}
Example #11
0
void EchoServer::onClose(const TcpConnectionPtr &conn)
{
	cout << "client : " << conn->getPeerAddr().toIp() << " : "
		 << conn->getPeerAddr().toPort() << " close" << endl;
	conn->shutdown();
	
}
Example #12
0
void EchoServer::onMessage(const TcpConnectionPtr& conn,
                           Buffer* buf,
                           Timestamp time){
    string msg(buf->retrieveAllAsString());
    LOG_INFO << conn->name() << " echo " << msg.size() << " bytes at " << time.toString();
    conn->send(msg);
}
Example #13
0
void P2PServer::onConnection(const TcpConnectionPtr& conn)
{
  cout << conn->localAddress().toIpPort() << " -> "
       << conn->peerAddress().toIpPort() << " is "
       << (conn->connected() ? "UP" : "DOWN") 
       << endl;

}
Example #14
0
void HttpServer::onConnection(const TcpConnectionPtr& conn)
{
     LOG_INFO("HttpServer::onConnection get one client %d", conn->fd());
     if (conn->connected())
     {
         conn->setContext(HttpContext());
     }
}
Example #15
0
void AppBusiness::onTcpConnected(const TcpConnectionPtr& connection)
{
    logger().writeFmt("onTcpConnected (%s) (ConnCount: %d)",
        connection->getPeerAddr().getDisplayStr().c_str(),
        connection->getServerConnCount());

    connection->recv();
}
Example #16
0
void TcpServer::onClose(const TcpConnectionPtr& conn)
{
    if (closeCallback_) closeCallback_(conn);
//    EventLoop* ioLoop = conn->getLoop();
  //  ioLoop->removeInLoop(conn->connfd());
    LOG_TRACE("client close|ip=%s|port=%u", conn->peerAddr().ip().c_str(), conn->peerAddr().port());
    connectionManager_->Remove(conn->id());
}
Example #17
0
void ServerModule_Chargen::onTcpRecvComplete(const TcpConnectionPtr& connection, void *packetBuffer,
    int packetSize, const Context& context)
{
    logger().writeFmt("[%s] Discarded %u bytes.",
        connection->getConnectionName().c_str(), packetSize);

    connection->recv();
}
Example #18
0
/***************************************************
Description:    处理新的json连接或者连接断开情况
Input:          conn:TCP连接
Output:         无
Return:         无
***************************************************/
void TCPServer::onJsonConnection(const TcpConnectionPtr &conn)
{
    LOG_DEBUG <<"JSONConnection: " << conn->peerAddress().toIpPort() << " -> "
             << conn->localAddress().toIpPort() << " is "
             << (conn->connected() ? "UP" : "DOWN");

    if(!conn->connected())
        jsonCodec_.cleanup(conn);
}
Example #19
0
//-----------------------------------------------------------------------------
// 描述: 接受了一个新的TCP连接
//-----------------------------------------------------------------------------
void AppBusiness::onTcpConnected(const TcpConnectionPtr& connection)
{
    logger().writeFmt("onTcpConnected (%s) (ConnCount: %d)",
                      connection->getPeerAddr().getDisplayStr().c_str(),
                      connection->getServerConnCount());

    string msg = "Welcome to the simple echo server, type 'quit' to exit.\r\n";
    connection->send(msg.c_str(), msg.length());
}
Example #20
0
void LServer::newConnection( const TcpConnectionPtr& session )
{
	if ( 1 )
	{
		ScopedLock lock(lock_);
		connections_[session->name()] = session;
		LOGD("Hello-%s", session->name().c_str());
	}
}
Example #21
0
void LServer::delConnection( const TcpConnectionPtr& session )
{
	if ( 1 )
	{
		ScopedLock lock(lock_);
		connections_.erase(session->name());
		LOGD("Byte-%s", session->name().c_str());
	}
}
Example #22
0
  void onConnection(const TcpConnectionPtr& conn)
  {
    LOG_TRACE << conn->peerAddress().toString() << " -> "
        << conn->localAddress().toString() << " is "
        << (conn->connected() ? "UP" : "DOWN");
    LOG_INFO << conn->getTcpInfoString();

   // conn->send("hello\n");
  }
Example #23
0
void ServerModule_Daytime::onTcpConnected(const TcpConnectionPtr& connection)
{
    logger().writeFmt("onTcpConnected (%s) (ConnCount: %d)",
                      connection->getPeerAddr().getDisplayStr().c_str(),
                      connection->getServerConnCount());

    string msg = DateTime::now().toDateTimeString() + "\n";
    connection->send(msg.c_str(), msg.length());
}
Example #24
0
void onMessage(const TcpConnectionPtr& conn,
                Buffer* buf,
                Timestamp receiveTime)
{
    if (buf->findCRLF())
    {
        conn->send("No such user\r\n");
        conn->shutdown();
    }
}
void onConnection(const TcpConnectionPtr& pTcpConn) {
	if (pTcpConn->IsConnected()) {
		printf("onConnection(): tid=%d new connection [%s] from %s\n"
			, CurrentThread::GetTid()
			, pTcpConn->GetName().c_str()
			, pTcpConn->GetPeerAddress().ToHostPort().c_str());
	} else {
		printf("onConnection(): tid=%d connection [%s] is down\n", CurrentThread::GetTid(), pTcpConn->GetName().c_str());
	}
}
Example #26
0
std::vector<std::string> *tcp_server::connectedAddresses()
{
	mConnectedAddresses.clear();
	for (TcpServer::ConnectionMap::iterator it(server_.connections_.begin()); it != server_.connections_.end(); ++it)
	{
		TcpConnectionPtr conn = it->second;
		mConnectedAddresses.push_back(conn->peerAddress().toIp());
	}
	return &mConnectedAddresses;
}
Example #27
0
	void onConnection(const TcpConnectionPtr& conn)
	{
		if (conn->connected())
		{
			//channel_.reset(new RpcChannel(conn));
			conn->setTcpNoDelay(true);
			channel_->setConnection(conn);
			allConnected_->countDown();
		}
	}
Example #28
0
void TcpServer::removeConnectionInLoop(const TcpConnectionPtr& conn)
{
  _loop->assertInLoopThread();
  size_t n = _connections.erase(conn->name());
  (void)n;
  assert(n == 1);
  EventLoop* ioLoop = conn->getLoop();
  ioLoop->queueInLoop(
      std::bind(&TcpConnection::connectDestroyed, conn));
}
Example #29
0
void ServerModule_Chargen::onTcpConnected(const TcpConnectionPtr& connection)
{
    logger().writeFmt("onTcpConnected (%s) (ConnCount: %d)",
        connection->getPeerAddr().getDisplayStr().c_str(),
        connection->getServerConnCount());

    connection->setNoDelay(true);

    connection->recv();
    connection->send(message_.c_str(), message_.length());
}
Example #30
0
//-----------------------------------------------------------------------------
// 描述: 接受了一个新的TCP连接
//-----------------------------------------------------------------------------
void AppBusiness::onTcpConnected(const TcpConnectionPtr& connection)
{
    logger().writeFmt("onTcpConnected (%s) (ConnCount: %d)",
        connection->getPeerAddr().getDisplayStr().c_str(),
        connection->getServerConnCount());

    //string msg = "Welcome to the simple echo server, type 'quit' to exit.\r\n";

	//ConnetManager::instance().add(connection);

    connection->recv(SELF_PACKET_SPLITTER, EMPTY_CONTEXT);
}