Example #1
0
void CStartServerWindow::OnBnClickedButton2()
{
	CString s;

	if(m_layoutlist.GetCurSel() < 0 || m_layoutlist.GetCurSel() > m_layoutlist.GetCount())
	{
		::AfxMessageBox(L"please select a layout!");
		return;
	}

	m_layoutlist.GetLBText(m_layoutlist.GetCurSel(), s);

	char* cs = KBEngine::strutil::wchar2char(s.GetBuffer(0));
	KBEUnordered_map< std::string, std::vector<CStartServerWindow::LAYOUT_ITEM> >::iterator iter = 
		layouts_.find(cs);

	free(cs);

	if(iter == layouts_.end())
	{
		::AfxMessageBox(L"please select a layout!");
		return;
	}

	std::vector<CStartServerWindow::LAYOUT_ITEM>::iterator iter1 = iter->second.begin();
	for(; iter1 != iter->second.end(); iter1++)
	{
		LAYOUT_ITEM& item = (*iter1);

		KBEngine::COMPONENT_TYPE ctype = KBEngine::ComponentName2ComponentType(item.componentName.c_str());

		std::vector<std::string> vec;
		KBEngine::strutil::kbe_split(item.addr, ':', vec);
		if(vec.size() != 2)
		{
			continue;
		}

		KBEngine::Network::EndPoint* endpoint = KBEngine::Network::EndPoint::ObjPool().createObject();
		
		KBEngine::u_int32_t address;
		KBEngine::Network::Address::string2ip(vec[0].c_str(), address);
		KBEngine::Network::Address addr(address, htons(atoi(vec[1].c_str())));

		if(addr.ip == 0)
		{
			::AfxMessageBox(L"address is error!");
			KBEngine::Network::EndPoint::ObjPool().reclaimObject(endpoint);
			continue;
		}

		endpoint->socket(SOCK_STREAM);
		if (!endpoint->good())
		{
			AfxMessageBox(L"couldn't create a socket\n");
			KBEngine::Network::EndPoint::ObjPool().reclaimObject(endpoint);
			continue;
		}

		endpoint->addr(addr);
		if(endpoint->connect(addr.port, addr.ip) == -1)
		{
			CString err;
			err.Format(L"connect server is error! %d", ::WSAGetLastError());
			AfxMessageBox(err);
			KBEngine::Network::EndPoint::ObjPool().reclaimObject(endpoint);
			continue;
		}
		
		endpoint->setnonblocking(true);

		KBEngine::uint64 cid = KBEngine::genUUID64();
		KBEngine::int16 gus = -1;

		KBEngine::Network::Bundle bundle;
		bundle.newMessage(KBEngine::MachineInterface::startserver);
		bundle << KBEngine::getUserUID();
		bundle << ctype;
		endpoint->send(&bundle);
		KBEngine::Network::TCPPacket packet;
		packet.resize(1024);

		fd_set	fds;
		struct timeval tv = { 0, 1000000 }; // 1000ms

		FD_ZERO( &fds );
		FD_SET((int)(*endpoint), &fds);
		
		int selgot = select((*endpoint)+1, &fds, NULL, NULL, &tv);
		if(selgot == 0)
		{
			KBEngine::Network::EndPoint::ObjPool().reclaimObject(endpoint);
			continue;	// 超时可能对方繁忙
		}
		else if(selgot == -1)
		{
			KBEngine::Network::EndPoint::ObjPool().reclaimObject(endpoint);
			continue;
		}
		else
		{
			endpoint->recv(packet.data(), 1024);
		}

		bool success = true;
		packet << success;

		if(success)
		{
			for(int row = 0; row < m_list.GetItemCount(); row++)
			{
				CString name = m_list.GetItemText(row, 0);
				CString addr = m_list.GetItemText(row, 1); 
				CString running = m_list.GetItemText(row, 2); 

				char* cs1 = KBEngine::strutil::wchar2char(name.GetBuffer(0));
				char* cs2 = KBEngine::strutil::wchar2char(addr.GetBuffer(0));

				if(item.componentName == cs1 && item.addr == cs2 && running == L"false")
				{
					free(cs1);
					free(cs2);
					m_list.SetItemText(row, 2, L"true");
					break;
				}

				free(cs1);
				free(cs2);
			}
		}

		KBEngine::Network::EndPoint::ObjPool().reclaimObject(endpoint);
	}
}
void CConnectRemoteMachineWindow::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	CguiconsoleDlg* dlg = static_cast<CguiconsoleDlg*>(theApp.m_pMainWnd);
	byte ips[4];

	if (0 == m_ip.GetAddress(ips[0],ips[1],ips[2],ips[3]))
	{
		AfxMessageBox(L"address is error!");
		return;
	}
	
	char strip[256];
	sprintf_s(strip, 256, "%d.%d.%d.%d", ips[0],ips[1],ips[2],ips[3]);
	
	KBEngine::u_int16_t port = 0;
	CString sport;
	m_port.GetWindowTextW(sport);
	char* csport = KBEngine::strutil::wchar2char(sport.GetBuffer(0));
	port = atoi(csport);
	std::string command = strip;
	command += ":";
	command += csport;
	free(csport);

	KBEngine::Network::EndPoint* endpoint = KBEngine::Network::EndPoint::ObjPool().createObject();

	KBEngine::u_int32_t address;
	Network::Address::string2ip(strip, address);
	KBEngine::Network::Address addr(address, htons(port));

	if(addr.ip == 0)
	{
		::AfxMessageBox(L"address is error!");
		KBEngine::Network::EndPoint::ObjPool().reclaimObject(endpoint);
		return;
	}

	endpoint->socket(SOCK_STREAM);
	if (!endpoint->good())
	{
		AfxMessageBox(L"couldn't create a socket\n");
		KBEngine::Network::EndPoint::ObjPool().reclaimObject(endpoint);
		return;
	}

	endpoint->addr(addr);
	if(endpoint->connect(addr.port, addr.ip) == -1)
	{
		CString err;
		err.Format(L"connect server is error! %d", ::WSAGetLastError());
		AfxMessageBox(err);
		KBEngine::Network::EndPoint::ObjPool().reclaimObject(endpoint);
		return;
	}

	endpoint->setnonblocking(false);
	int8 findComponentTypes[] = {LOGGER_TYPE, BASEAPP_TYPE, CELLAPP_TYPE, BASEAPPMGR_TYPE, CELLAPPMGR_TYPE, LOGINAPP_TYPE, DBMGR_TYPE, BOTS_TYPE, UNKNOWN_COMPONENT_TYPE};
	int ifind = 0;

	while(true)
	{
		int8 findComponentType = findComponentTypes[ifind++];
		if(findComponentType == UNKNOWN_COMPONENT_TYPE)
		{
			//INFO_MSG("Componentbridge::process: not found %s, try again...\n",
			//	COMPONENT_NAME_EX(findComponentType));
			break;
		}

		KBEngine::Network::Bundle bhandler;
		bhandler.newMessage(KBEngine::MachineInterface::onFindInterfaceAddr);

		KBEngine::MachineInterface::onFindInterfaceAddrArgs7::staticAddToBundle(bhandler, KBEngine::getUserUID(), KBEngine::getUsername(), 
			CONSOLE_TYPE, g_componentID, (COMPONENT_TYPE)findComponentType, 0, 0);

		endpoint->send(&bhandler);

		KBEngine::Network::TCPPacket packet;
		packet.resize(65535);

		endpoint->setnonblocking(true);
		KBEngine::sleep(300);
		packet.wpos(endpoint->recv(packet.data(), 65535));

		while(packet.length() > 0)
		{
			MachineInterface::onBroadcastInterfaceArgs24 args;
			
			try
			{
				args.createFromStream(packet);
			}catch(MemoryStreamException &)
			{
				goto END;
			}

			INFO_MSG(fmt::format("CConnectRemoteMachineWindow::OnBnClickedOk: found {}, addr:{}:{}\n",
				COMPONENT_NAME_EX((COMPONENT_TYPE)args.componentType), inet_ntoa((struct in_addr&)args.intaddr), ntohs(args.intport)));

			Components::getSingleton().addComponent(args.uid, args.username.c_str(), 
				(KBEngine::COMPONENT_TYPE)args.componentType, args.componentID, args.globalorderid, args.grouporderid, 
				args.intaddr, args.intport, args.extaddr, args.extport, args.extaddrEx, args.pid, args.cpu, args.mem, args.usedmem, 
				args.extradata, args.extradata1, args.extradata2, args.extradata3);

		}
	}
END:
	dlg->updateTree();

	KBEngine::Network::EndPoint::ObjPool().reclaimObject(endpoint);
	wchar_t* wcommand = KBEngine::strutil::char2wchar(command.c_str());
	bool found = false;
	std::deque<CString>::iterator iter = m_historyCommand.begin();
	for(; iter != m_historyCommand.end(); iter++)
	{
		if((*iter) == wcommand)
		{
			found = true;
			break;
		}
	}
	
	if(!found)
	{
		m_historyCommand.push_front(wcommand);
		if(m_historyCommand.size() > 10)
			m_historyCommand.pop_back();

		saveHistory();
	}

	free(wcommand);

	OnOK(); 
}