Exemplo n.º 1
0
/*
 * Setup the Simple Client
 * @returns true on success, false on failure
 */
bool BaseClientWrapper::Setup() {
  if (!m_ss)
    m_ss = new SelectServer();

  if (!m_socket) {
    if (m_auto_start)
      m_socket = ola::client::ConnectToServer(OLA_DEFAULT_PORT);
    else
    m_socket = TcpSocket::Connect("127.0.0.1", OLA_DEFAULT_PORT);

    if (!m_socket) {
      delete m_socket;
      delete m_ss;
      m_socket = NULL;
      m_ss = NULL;
      return false;
    }
    m_socket->SetOnClose(
        ola::NewSingleCallback(this, &OlaClientWrapper::SocketClosed));
  }

  CreateClient();
  m_ss->AddReadDescriptor(m_socket);
  return StartupClient();
}
Exemplo n.º 2
0
bool BaseClientWrapper::Setup() {
  if (!m_socket.get()) {
    InitSocket();

    if (!m_socket.get())
      return false;
  }

  CreateClient();

  m_ss.AddReadDescriptor(m_socket.get());
  return StartupClient();
}
Exemplo n.º 3
0
	CErrno NetThread::Init(Json::Value & conf)
	{   
		if (!m_bInitFirst)
		{
			m_objInitConf = conf;

			if (!m_pNetReactor)
			{
				m_pNetReactor = new Net::NetReactorDefault();
				m_pNetReactor->SetNetThread(this);

				if (CErrno::Success() != m_pNetReactor->Init())
				{
					SAFE_DELETE(m_pNetReactor);
					MsgAssert_ReF(0, "NetReactor fail.");
				}
			}

			Json::Value netThead = conf.get("net_thread", Json::Value());
			INT32 nPriority = netThead.get("priority", -1).asInt();
			if (nPriority == -1)
			{
				while (ThreadPool::ThreadPoolInterface::GetInstance().HasPriorityThread(nPriority))
				{
					nPriority = CUtil::random(RAND_MIN_PRIORITY , RAND_MAX_PRIORITY);
				}
			}

			SetThreadPriority(nPriority);
			ThreadPool::ThreadPoolInterface::GetInstance().CreateThread(nPriority);
			ThreadPool::ThreadPoolInterface::GetInstance().AddTask(this);

		}
		else
		{
			Json::Value server = conf.get("server", Json::Value());
			std::string strType = server.get("listen_type", "tcp").asCString();
			std::string strAddress = server.get("listen_address", "127.0.0.1").asCString();
			INT32 nPort = server.get("listen_port", 8003).asInt();
			std::string strNodeName = server.get("net_node_name", "").asCString();
			BOOL		bResueAddr = server.get("resue_addr", FALSE).asInt();

			StartupServer(strNodeName, strType, strAddress, nPort , bResueAddr);

			Json::Value clients = conf.get("clients", Json::Value());
			StartupClient(clients);
		}
		return CErrno::Success(); 
	}