コード例 #1
0
ファイル: clientobject.cpp プロジェクト: mengdizhang/kbengine
//-------------------------------------------------------------------------------------
bool ClientObject::initCreate()
{
	Network::EndPoint* pEndpoint = new Network::EndPoint();
	
	pEndpoint->socket(SOCK_STREAM);
	if (!pEndpoint->good())
	{
		ERROR_MSG("ClientObject::initNetwork: couldn't create a socket\n");
		delete pEndpoint;
		error_ = C_ERROR_INIT_NETWORK_FAILED;
		return false;
	}
	
	ENGINE_COMPONENT_INFO& infos = g_kbeSrvConfig.getBots();
	u_int32_t address;

	pEndpoint->convertAddress(infos.login_ip, address);
	if(pEndpoint->connect(htons(infos.login_port), address) == -1)
	{
		ERROR_MSG(fmt::format("ClientObject::initNetwork({1}): connect server({2}:{3}) is error({0})!\n",
			kbe_strerror(), name_, infos.login_ip, infos.login_port));

		delete pEndpoint;
		// error_ = C_ERROR_INIT_NETWORK_FAILED;
		state_ = C_STATE_INIT;
		return false;
	}

	Network::Address addr(infos.login_ip, infos.login_port);
	pEndpoint->addr(addr);

	pServerChannel_->endpoint(pEndpoint);
	pEndpoint->setnonblocking(true);
	pEndpoint->setnodelay(true);

	pServerChannel_->pMsgHandlers(&ClientInterface::messageHandlers);
	Bots::getSingleton().pEventPoller()->registerForRead((*pEndpoint), this);

	Network::Bundle* pBundle = Network::Bundle::ObjPool().createObject();
	(*pBundle).newMessage(LoginappInterface::hello);
	(*pBundle) << KBEVersion::versionString() << KBEVersion::scriptVersionString();

	if(Network::g_channelExternalEncryptType == 1)
	{
		pBlowfishFilter_ = new Network::BlowfishFilter();
		(*pBundle).appendBlob(pBlowfishFilter_->key());
	}
	else
	{
		std::string key = "";
		(*pBundle).appendBlob(key);
	}

	pServerChannel_->pushBundle(pBundle);

	this->pEndpoint_ = pEndpoint;
	return true;
}
コード例 #2
0
ファイル: clientobject.cpp プロジェクト: mengdizhang/kbengine
//-------------------------------------------------------------------------------------
bool ClientObject::initLoginGateWay()
{
	Bots::getSingleton().pEventPoller()->deregisterForRead(*pServerChannel_->endpoint());
	Network::EndPoint* pEndpoint = new Network::EndPoint();
	
	pEndpoint->socket(SOCK_STREAM);
	if (!pEndpoint->good())
	{
		ERROR_MSG("ClientObject::initLogin: couldn't create a socket\n");
		delete pEndpoint;
		error_ = C_ERROR_INIT_NETWORK_FAILED;
		return false;
	}
	
	u_int32_t address;

	pEndpoint->convertAddress(ip_.c_str(), address);
	if(pEndpoint->connect(htons(port_), address) == -1)
	{
		ERROR_MSG(fmt::format("ClientObject::initLogin({}): connect server is error({})!\n",
			kbe_strerror(), name_));

		delete pEndpoint;
		// error_ = C_ERROR_INIT_NETWORK_FAILED;
		state_ = C_STATE_LOGIN_GATEWAY_CREATE;
		return false;
	}

	Network::Address addr(ip_.c_str(), port_);
	pEndpoint->addr(addr);

	pServerChannel_->endpoint(pEndpoint);
	pEndpoint->setnonblocking(true);
	pEndpoint->setnodelay(true);

	Bots::getSingleton().pEventPoller()->registerForRead((*pEndpoint), this);
	connectedGateway_ = true;

	Network::Bundle* pBundle = Network::Bundle::ObjPool().createObject();
	(*pBundle).newMessage(BaseappInterface::hello);
	(*pBundle) << KBEVersion::versionString() << KBEVersion::scriptVersionString();
	
	if(Network::g_channelExternalEncryptType == 1)
	{
		pBlowfishFilter_ = new Network::BlowfishFilter();
		(*pBundle).appendBlob(pBlowfishFilter_->key());
		pServerChannel_->pFilter(NULL);
	}
	else
	{
		std::string key = "";
		(*pBundle).appendBlob(key);
	}

	pServerChannel_->pushBundle(pBundle);
	return true;
}
コード例 #3
0
ファイル: clientobject.cpp プロジェクト: warmwine/kbengine
//-------------------------------------------------------------------------------------
bool ClientObject::initLoginGateWay()
{
	Bots::getSingleton().networkInterface().dispatcher().deregisterReadFileDescriptor(*pTCPPacketReceiverEx_->pEndPoint());
	pServerChannel_->stopSend();
	pServerChannel_->pPacketSender(NULL);
	SAFE_RELEASE(pTCPPacketSenderEx_);
	SAFE_RELEASE(pTCPPacketReceiverEx_);

	Network::EndPoint* pEndpoint = new Network::EndPoint();
	
	pEndpoint->socket(SOCK_STREAM);
	if (!pEndpoint->good())
	{
		ERROR_MSG("ClientObject::initLogin: couldn't create a socket\n");
		delete pEndpoint;
		error_ = C_ERROR_INIT_NETWORK_FAILED;
		return false;
	}
	
	u_int32_t address;

	pEndpoint->convertAddress(ip_.c_str(), address);
	if(pEndpoint->connect(htons(port_), address) == -1)
	{
		ERROR_MSG(fmt::format("ClientObject::initLogin({}): connect server is error({})!\n",
			kbe_strerror(), name_));

		delete pEndpoint;
		// error_ = C_ERROR_INIT_NETWORK_FAILED;
		state_ = C_STATE_LOGIN_GATEWAY_CREATE;
		return false;
	}

	Network::Address addr(ip_.c_str(), port_);
	pEndpoint->addr(addr);
	pServerChannel_->pEndPoint(pEndpoint);
	pEndpoint->setnonblocking(true);
	pEndpoint->setnodelay(true);

	pTCPPacketSenderEx_ = new Network::TCPPacketSenderEx(*pEndpoint, this->ninterface_, this);
	pTCPPacketReceiverEx_ = new Network::TCPPacketReceiverEx(*pEndpoint, this->ninterface_, this);
	Bots::getSingleton().networkInterface().dispatcher().registerReadFileDescriptor((*pEndpoint), pTCPPacketReceiverEx_);

	//²»ÔÚÕâÀï×¢²á
	//Bots::getSingleton().networkInterface().dispatcher().registerWriteFileDescriptor((*pEndpoint), pTCPPacketSenderEx_);
	pServerChannel_->pPacketSender(pTCPPacketSenderEx_);

	connectedGateway_ = true;

	Network::Bundle* pBundle = Network::Bundle::ObjPool().createObject();
	(*pBundle).newMessage(BaseappInterface::hello);
	(*pBundle) << KBEVersion::versionString() << KBEVersion::scriptVersionString();
	
	if(Network::g_channelExternalEncryptType == 1)
	{
		pBlowfishFilter_ = new Network::BlowfishFilter();
		(*pBundle).appendBlob(pBlowfishFilter_->key());
		pServerChannel_->pFilter(NULL);
	}
	else
	{
		std::string key = "";
		(*pBundle).appendBlob(key);
	}

	pEndpoint->send(pBundle);
	Network::Bundle::ObjPool().reclaimObject(pBundle);
	return true;
}