Example #1
0
void SceneClientManager::init(const std::vector<ServerEntryPtr>& scenesentry)
{
	if (scenesentry.empty())
	{
		LOG(INFO)<<("can't find scene entry");
		return ;
	}
	else
	{
		LOG(INFO)<<"scene entry num="<<scenesentry.size();
	}

	for (size_t i=0;i<scenesentry.size(); ++i)
	{
		ServerEntryPtr e = scenesentry[i];
		connection_ptr conn(new connection(GatewayServer::instance().get_io_service(), std::string("sceneclient") + boost::lexical_cast<std::string>(i)));
		context_ptr client(new SceneClient(conn->get_io_service(), conn, e));
		conn->set_context(client);

		conn->set_compressor(std::shared_ptr<loki::compressor>(new loki::compressor_zlib()));

		char key[32]= {'a', 'b', 'c'};
		std::shared_ptr<loki::encryptor_aes> aes(new loki::encryptor_aes());
		aes->setkey((const unsigned char*)key,256);
		conn->set_encryptor(aes);
		/////////////////////

		conn->set_connected_handler(std::bind(&SceneClientManager::on_connected, this, std::placeholders::_1));
		conn->set_error_handler(std::bind(&SceneClientManager::on_error, this, std::placeholders::_1));

		//直接放入主逻辑线程处理
		conn->set_msg_handler(std::bind(&GatewayServer::on_msg, GatewayServer::getSingletonPtr(), std::placeholders::_1));

		conn->async_connect(e->ip(), e->port());

		//start timer
		//timer_.expires_from_now(boost::posix_time::seconds(5));
		//timer_.async_wait(boost::bind(&SceneClientManager::check_reconnect, SceneClientManager::instance_ptr(), _1, conn));
	}

}
Example #2
0
bool GatewayServer::start_server()
{
	new LoginSessionManager();
	new GatewayTaskManager();
	new SceneClientManager();

	notify_login_server();

	//as client
	recordclient_.reset(new connection(pool_.get_io_service(),"recordclient"));
	recordclient_->set_msg_handler(std::bind(&GatewayServer::on_msg, this, std::placeholders::_1));
	recordclient_->set_error_handler(std::bind(&GatewayServer::handle_stop, this));			//TODO: change another handler
	ServerEntryPtr recordEntry = serverlist_.getEntryByType(RECORDSERVER);
	if (!recordEntry)
	{
		LOG(ERROR)<<("can't find record entry");
		return false;
	}
	if (!recordclient_->connect(recordEntry->ip(), recordEntry->port()))
	{
		LOG(ERROR)<<("can't find record entry");
		return false;
	}
	else
	{
		Record::t_LoginRecord send;
		send.set_id(myentry_->id());
		send.set_type(myentry_->type());
		codec_.send(recordclient_, &send);
	}
			
	//as client
	sessionclient_.reset(new connection(pool_.get_io_service(), "sessionclient"));
	sessionclient_->set_msg_handler(std::bind(&GatewayServer::on_msg, this, std::placeholders::_1));
	sessionclient_->set_error_handler(std::bind(&GatewayServer::handle_stop, this));			//TODO: change another handler
	ServerEntryPtr sessionEntry = serverlist_.getEntryByType(SESSIONSERVER);
	if (!sessionEntry)
	{
		LOG(INFO)<<("can't find session entry");
		return false;
	}
	if (!sessionclient_->connect(sessionEntry->ip(), sessionEntry->port()))
	{
		LOG(INFO)<<("connect session error");
		return false;
	}
	else
	{
		Session::t_LoginSession send;
		send.set_id(myentry_->id());
		send.set_type(myentry_->type());
		codec_.send(sessionclient_, &send);
	}

	//as client
	std::vector<ServerEntryPtr> scenesentry = serverlist_.getEntrysByType(SCENESERVER);
	if (scenesentry.empty())
	{
		LOG(INFO)<<("can't find scene entry");
		return false;
	}
	else
	{
		LOG(INFO)<<"scene entry num="<<scenesentry.size();
	}

	SceneClientManager::instance().init(scenesentry);

	started_ = true;

	one_second_timer_.expires_from_now(boost::posix_time::seconds(1));
	one_second_timer_.async_wait(boost::bind(&GatewayServer::one_second_callback, this, _1));

	return true;
}