/**
 * 开始引擎
 * 成功返回true,失败返回false
 */
bool STNetEngine::Start()
{
	if ( !m_stop ) return true;
	m_stop = false;	
	
	int memoryCount = 2;
	for ( memoryCount = 2; memoryCount * memoryCount < m_averageConnectCount * 2; memoryCount++ );
	if ( memoryCount < 200 ) memoryCount = 200;
	if ( NULL != m_pConnectPool )//之前Stop()过,又重新启动
	{
		delete m_pConnectPool;
		m_pConnectPool = NULL;
	}
	m_pConnectPool = new MemoryPool( sizeof(STNetConnect), memoryCount );
	if ( NULL == m_pConnectPool )
	{
		m_startError = "内存不足,无法创建NetConnect内存池";
		Stop();
		return false;
	}
	if ( !m_pNetMonitor->Start( MAXPOLLSIZE ) ) 
	{
		m_startError = m_pNetMonitor->GetInitError();
		Stop();
		return false;
	}
	if ( !ListenAll() )
	{
		Stop();
		return false;
	}
	ConnectAll();
	return m_mainThread.Run( Executor::Bind(&STNetEngine::Main), this, 0 );
}
void NetEngine::ReConnectAll()
{
	if ( 0 >= m_nReconnectTime ) return;//无重连机制
	static time_t lastConnect = time(NULL);
	time_t curTime = time(NULL);
	if ( m_nReconnectTime > curTime - lastConnect ) return;
	lastConnect = curTime;
	ConnectAll();
}
ribi::QtMusicTheoryMultiScaleDialog::QtMusicTheoryMultiScaleDialog(QWidget *parent) :
    QtHideAndShowDialog(parent),
    ui(new Ui::QtMusicTheoryMultiScaleDialog)
{
  ui->setupUi(this);

  //Put all available scales in the scales combobox
  {
    ui->scale_1->clear();
    ui->scale_2->clear();
    const std::vector<boost::shared_ptr<Music::Scale> > v
      = Music::Scale::CreateScales(Music::Note(0));
    std::for_each(v.begin(),v.end(),
      [this](const boost::shared_ptr<Music::Scale>& scale)
      {
        ui->scale_1->addItem(scale->GetName().c_str());
        ui->scale_2->addItem(scale->GetName().c_str());
      }
    );
  }

  ConnectAll();
}
/**
 * 开始引擎
 * 成功返回true,失败返回false
 */
bool NetEngine::Start()
{
	if ( !m_stop ) return true;
	m_stop = false;	
	int memoryCount = 2;
	for ( memoryCount = 2; memoryCount * memoryCount < m_averageConnectCount * 2; memoryCount++ );
	if ( memoryCount < 200 ) memoryCount = 200;
	if ( NULL != m_pConnectPool )//之前Stop了又重新Start
	{
		delete m_pConnectPool;
		m_pConnectPool = NULL;
	}
	m_pConnectPool = new MemoryPool( sizeof(NetConnect), memoryCount );
	if ( NULL == m_pConnectPool )
	{
		m_startError = "内存不足,无法创建NetConnect内存池";
		Stop();
		return false;
	}
	if ( !m_pNetMonitor->Start( MAXPOLLSIZE ) ) 
	{
		m_startError = m_pNetMonitor->GetInitError();
		Stop();
		return false;
	}
	m_workThreads.Start( m_workThreadCount );
	int i = 0;
	for ( ; i < m_ioThreadCount; i++ ) m_ioThreads.Accept( Executor::Bind(&NetEngine::NetMonitorTask), this, NULL );
	m_ioThreads.Start( m_ioThreadCount );
	if ( !ListenAll() )
	{
		Stop();
		return false;
	}
	ConnectAll();
	return m_mainThread.Run( Executor::Bind(&NetEngine::Main), this, 0 );
}