Beispiel #1
0
int NetLib::clientStart(ServerNode* stub,script::tixmlCodeNode * node)
{
	if (!stub || !node) return 0;
	int id = 0;
	std::string ip = node->getAttr("ip");
	WORD port = 0;
	node->getAttr("id",id);
	const char *name = node->getAttr("name");
	node->getAttr("port",port);
	std::string type = node->getAttr("type");
	if (id == 0)
	{
		if (type == "local")
		{
			ClientNode * client = theClient.addLocalClient(name);
			if (client)
			{
				client->useLocal();
			}
			return 1;
		}
		ClientNode * client = theClient.addRemoteClient(name);
		if (client)
		{
			if (!client->connect(ip.c_str(),port))
			{
				//client->useLocal();
				client->getSocket()->setInvalid();
				printf("服务器连接失败 %s:%u \n",ip.c_str(),port);
				return 0;
			}
			else
			{
				stTestCmd testCmd;
				cmd::Stream ss = testCmd.toStream();
				client->getSocket()->sendPacket(ss.content(),ss.size());
				printf("服务器连接成功<%s:%u>\n",ip.c_str(),port);
				return 1;
			}
		}
		else
		{
			printf("该类型的连接不存在:%d\n",id);
		}
	}
	return 0;
}