コード例 #1
0
ファイル: gateServer.cpp プロジェクト: elenno/MyServer
void my::GateServer::handle_connect(ConnectionPtr conn, boost::system::error_code err)
{
	if (err)
	{
		LogW << "server name: " << conn->getNetId() << "  connect error: " << err.message() << LogEnd;
		if (conn->getSocket().is_open())
		{
			LogW << "Close Socket!!" << LogEnd;
			conn->getSocket().close();
		}
		return;
	}
	else
	{
		//输出一下这个是成功连接了哪个服务器
		LogD << "server name: " << conn->getNetId() << "  connect success!" << LogEnd;
		try
		{
			static ip::tcp::no_delay option(true);
			conn->getSocket().set_option(option);
			//start
			conn->start();
		}catch(std::exception& e)
		{
			LogE << "Connect to server EXCEPTION!!! server=" << conn->getNetId() << "  reason=" << e.what() << LogEnd;
		}	
	}
}
コード例 #2
0
ファイル: gateServer.cpp プロジェクト: elenno/MyServer
void my::GateServer::handle_accept(ConnectionPtr conn, boost::system::error_code err)
{
	if (err)
	{
		LogE << err.message() << LogEnd;
		conn->getSocket().close();
		ConnectionPtr nextConn = boost::shared_ptr<TcpConnection>(new TcpConnection(core.getService(), m_GateHandler, shared_from_this()));
		m_pAcceptor->async_accept(nextConn->getSocket(), boost::bind(&GateServer::handle_accept, this, nextConn, boost::asio::placeholders::error));
	}
	else
	{
		//new incomming player!!!
		LogD << conn->getSocket().remote_endpoint().address() << " " << conn->getSocket().remote_endpoint().port() << LogEnd;

		boost::recursive_mutex::scoped_lock lock(mtx);
		conn->setNetId(m_nNetIdHolder);
		ip::tcp::no_delay option(true);
		conn->getSocket().set_option(option);
		m_ConnMap.insert(std::make_pair<int, ConnectionPtr>(m_nNetIdHolder, conn));		
		conn->start();
		m_nNetIdHolder = (m_nNetIdHolder + 1) % MAX_NET_ID;
		m_nConnCount++;
			
		ConnectionPtr nextConn = boost::shared_ptr<TcpConnection>(new TcpConnection(core.getService(), m_GateHandler, shared_from_this()));
		m_pAcceptor->async_accept(nextConn->getSocket(), boost::bind(&GateServer::handle_accept, this, nextConn, boost::asio::placeholders::error));
	}
}
コード例 #3
0
ファイル: gateServer.cpp プロジェクト: elenno/MyServer
void my::GateServer::connect(std::string ipaddr, std::string port, ConnectionPtr conn)
{
	boost::system::error_code err;
	ip::tcp::resolver rslv(core.getService());
	ip::tcp::resolver::query q(ipaddr, port);
	ip::tcp::resolver::iterator iter = rslv.resolve(q, err);
	if (err)
	{
		LogE << err.message() << LogEnd;
		return;
	}
	async_connect(conn->getSocket(), iter, boost::bind(&GateServer::handle_connect, this, conn, boost::asio::placeholders::error));
}
コード例 #4
0
ファイル: gateServer.cpp プロジェクト: elenno/MyServer
void my::GateServer::init()
{
	boost::shared_ptr<TcpServer> serverPtr(this); //make sure that shared_from_this() can run perfectly ok!
	Json::Value gateConf = util::fileSystem::loadJsonFileEval(jsonconf::server_config);
	if (gateConf == Json::nullValue)
	{
		LogW << "Error init GateServer, null gateConf" << LogEnd;
		return;
	}
	//初始化httpSvr
	{
		m_HttpServerPtr = HttpServerPtr(new http::HttpServer());
		m_HttpServerPtr->init(gateConf);
		m_HttpServerPtr->run();
	}
	m_GateConf = gateConf;
	int	port = gateConf["gateSvrPort"].asInt();
	std::string gameSvrIp = gateConf["gameSvrIp"].asString();
	int gameSvrPort = gateConf["gameSvrPort"].asInt();
	std::string accountSvrIp = gateConf["accountSvrIp"].asString();
	int accountSvrPort = gateConf["accountSvrPort"].asInt();

	m_nConnCount = 0;
	m_nNetIdHolder = 0;
	m_pEndpoint = EndpointPtr(new boost::asio::ip::tcp::endpoint(ip::tcp::v4(), port));
	m_pAcceptor = AcceptorPtr(new boost::asio::ip::tcp::acceptor(core.getService(), *m_pEndpoint));

	m_GateHandler = boost::shared_ptr<GateHandler>(new GateHandler());
	ConnectionPtr nextConn = boost::shared_ptr<TcpConnection>(new TcpConnection(core.getService(), m_GateHandler, shared_from_this()));
	m_pAcceptor->async_accept(nextConn->getSocket(), boost::bind(&GateServer::handle_accept, this, nextConn, boost::asio::placeholders::error));

	connectToGameSvr(gameSvrIp, gameSvrPort);
	connectToAccountSvr(accountSvrIp, accountSvrPort);

	LogD << "Init Gate Server Ok!!!" << LogEnd;

	update();
}
コード例 #5
0
ファイル: gateServer.cpp プロジェクト: elenno/MyServer
void my::GateServer::sendToPlayer(NetMessage& msg)
{
	int playerId = msg.getPlayerId();
	int netId = msg.getNetId();
	ConnectionMap::iterator it = m_PlayerMap.find(playerId);
	bool flag = false;
	if (it == m_PlayerMap.end())
	{
		LogW << "| Player not online, try ConnMap, playerId=" << playerId << LogEnd;
		it = m_ConnMap.find(netId);
		flag = true;
		if (it == m_ConnMap.end())
		{
			LogW << "| ConnectionMap not found netId:" << netId << LogEnd;
		    return;
		}
	}
	ConnectionPtr playerConn = it->second;
	if (!playerConn->getSocket().is_open())
	{
		//socket already closed!!!
		LogW << "Socket closed! netId=" << netId << LogEnd;
		//kick player
		kickConnection(playerConn);
		return;
	}
	std::string msgstr = msg.getMessage();
	NetMessage tmp(msgstr, msg.getProto(), msg.getPlayerId(), msg.getNetId());
	if (tmp.serialize())
	{
		if (0 != playerConn->sendMessage(tmp))
		{
			LogW << "Send Msg To Player Failed, playerId=" << playerId << LogEnd;
		}
	}
}