//------------------------------------------------------------------------------------ void PlayerManager::processDatabasePlayer(NetPack* pPack) { ///把数据转发给客户端 Tag_PlayerCollect* players=static_cast<Tag_PlayerCollect*>(pPack->getData()); if(players->m_Count==0)///表示这个帐号还没有新建角色 { }else ///把所有角色发给客户端 { } RakNet::SystemAddress address; address.SetBinaryAddress(players->m_ip); NetWorkServer::getSingleton().send(GM_REQUEST_PLAYERS,(const char*)pPack->getData(),pPack->getDataLength(),address); return ; }
void SimulateClientMainFrame::OnSend( wxCommandEvent& event ) { wxString serverName=m_serverList->GetStringSelection(); wxString messageID=m_messagelist->GetStringSelection(); wxString message= m_MessageText->GetValue(); //if(serverName.empty()||messageID.empty()||messageID.empty()) //{ // wxMessageBox("请选择服务器,消息id,和消息内容"); // return ; //} unsigned int id=stringToMessageID(messageID.c_str()); RakNet::SystemAddress address; address.SetBinaryAddress(serverName.c_str()); if(id==GM_TEXT_MESSAGE) { NetWorkClient::getSingletonPtr()->getConnectInstance("statserver")->send(id,message.c_str(),message.Length()+1); m_MessageText->Clear(); addSendMessage(message); }else if(GM_ACCOUNT_REQUEST==id) { UserLogin userlogin; ::ZeroMemory(&userlogin,sizeof(userlogin)); if(sscanf(message.c_str(),"%s = %s",userlogin.m_account,userlogin.m_password)>0) { NetWorkClient::getSingleton().getConnectInstance("statserver")->send(id,userlogin); }else { wxMessageBox("格式不正确请用;分格开用户名和密码"); } // memcpy(userlogin.m_account,"palyer1",strlen("player1")); // memcpy(userlogin.m_password,"palyer1",strlen("player1")); }else if(id==GM_CHATMESSAGE) { NetWorkClient::getSingletonPtr()->getConnectInstance(GAMESERVER)->send(id,message.c_str(),message.Length()+1); m_MessageText->Clear(); addSendMessage(message); }else if(id==GM_CONNECT_GAMESERVER) { ///发送帐号id组游戏逻辑服务器 long accountid=0; if(message.ToLong(&accountid)) { UINT temid=accountid; NetWorkClient::getSingletonPtr()->getConnectInstance(GAMESERVER)->send(id,temid); m_MessageText->Clear(); } } return ; }
void Server::update(running_machine *machine) { for( map<RakNet::RakNetGUID,vector< string > >::iterator it = unknownPeerInputs.begin(); it != unknownPeerInputs.end(); ) { cout << "GOT UNKNOWN PEER INPUT\n"; if(peerIDs.find(it->first)!=peerIDs.end()) { int ID = peerIDs[it->first]; for(int a=0;a<it->second.size();a++) { peerInputs[ID].push_back(it->second[a]); } map<RakNet::RakNetGUID,vector< string > >::iterator itold = it; it++; unknownPeerInputs.erase(itold); } else { it++; } } //cout << "SERVER TIME: " << RakNet::GetTimeMS()/1000.0f/60.0f << endl; //printf("Updating server\n"); RakNet::Packet *p; for (p=rakInterface->Receive(); p; rakInterface->DeallocatePacket(p), p=rakInterface->Receive()) { // We got a packet, get the identifier with our handy function unsigned char packetIdentifier = GetPacketIdentifier(p); //printf("GOT PACKET %d\n",int(packetIdentifier)); // Check if this is a network message packet switch (packetIdentifier) { case ID_CONNECTION_LOST: // Couldn't deliver a reliable packet - i.e. the other system was abnormally // terminated case ID_DISCONNECTION_NOTIFICATION: // Connection lost normally printf("ID_DISCONNECTION_NOTIFICATION from %s\n", p->systemAddress.ToString(true)); removePeer(p->guid,machine); break; case ID_NEW_INCOMING_CONNECTION: // Somebody connected. We have their IP now printf("ID_NEW_INCOMING_CONNECTION from %s with GUID %s\n", p->systemAddress.ToString(true), p->guid.ToString()); if(syncHappend) { //Sorry, too late //rakInterface->CloseConnection(p->systemAddress,true); } break; case ID_CLIENT_INFO: cout << "GOT ID_CLIENT_INFO\n"; //This client is requesting candidacy, set their info { char buf[4096]; strcpy(buf,(char*)(p->data+1)); candidateNames[p->systemAddress] = buf; } //Find a session index for the player { char buf[4096]; buf[0] = ID_SETTINGS; memcpy(buf+1,&secondsBetweenSync,sizeof(int)); strcpy(buf+1+sizeof(int),username.c_str()); rakInterface->Send( buf, 1+sizeof(int)+username.length()+1, HIGH_PRIORITY, RELIABLE_ORDERED, ORDERING_CHANNEL_SYNC, p->guid, false ); } if(acceptedPeers.size()>=maxPeerID-1) { //Sorry, no room rakInterface->CloseConnection(p->systemAddress,true); } else if(acceptedPeers.size()) { printf("Asking other peers to accept %s\n",p->systemAddress.ToString()); waitingForAcceptFrom[p->systemAddress] = std::vector<RakNet::RakNetGUID>(); for(int a=0; a<acceptedPeers.size(); a++) { RakNet::RakNetGUID guid = acceptedPeers[a]; waitingForAcceptFrom[p->systemAddress].push_back(guid); cout << "SENDING ADVERTIZE TO " << guid.ToString() << endl; char buf[4096]; buf[0] = ID_ADVERTISE_SYSTEM; strcpy(buf+1,p->systemAddress.ToString(true)); rakInterface->Send(buf,1+strlen(p->systemAddress.ToString(true))+1,HIGH_PRIORITY,RELIABLE_ORDERED,ORDERING_CHANNEL_SYNC,guid,false); } printf("Asking other peers to accept\n"); } else { //First client, automatically accept acceptPeer(p->systemAddress,machine); } break; case ID_INCOMPATIBLE_PROTOCOL_VERSION: printf("ID_INCOMPATIBLE_PROTOCOL_VERSION\n"); break; case ID_ACCEPT_NEW_HOST: { printf("Accepting new host\n"); RakNet::SystemAddress saToAccept; saToAccept.SetBinaryAddress(((char*)p->data)+1); for(int a=0; a<waitingForAcceptFrom[saToAccept].size(); a++) { if(waitingForAcceptFrom[saToAccept][a]==p->guid) { waitingForAcceptFrom[saToAccept].erase(waitingForAcceptFrom[saToAccept].begin()+a); break; } } if(waitingForAcceptFrom[saToAccept].empty()) { cout << "Accepting: " << saToAccept.ToString() << endl; waitingForAcceptFrom.erase(waitingForAcceptFrom.find(saToAccept)); acceptPeer(saToAccept,machine); } } break; case ID_REJECT_NEW_HOST: { RakNet::SystemAddress saToReject; saToReject.SetBinaryAddress(((char*)p->data)+1); printf("Rejecting new client\n"); cout << p->guid.ToString() << " REJECTS " << saToReject.ToString() << endl; if(waitingForAcceptFrom.find(saToReject)==waitingForAcceptFrom.end()) printf("Could not find waitingForAcceptFrom for this GUID, weird"); else waitingForAcceptFrom.erase(waitingForAcceptFrom.find(saToReject)); rakInterface->CloseConnection(saToReject,true); } break; case ID_CLIENT_INPUTS: if(peerIDs.find(p->guid)==peerIDs.end() || peerInputs.find(peerIDs[p->guid])==peerInputs.end()) { cout << __FILE__ << ":" << __LINE__ << " OOPS!!!!\n"; } //cout << "GOT CLIENT INPUTS\n"; peerInputs[peerIDs[p->guid]].push_back(string((char*)GetPacketData(p),(int)GetPacketSize(p))); break; default: printf("UNEXPECTED PACKET ID: %d\n",int(packetIdentifier)); break; } } }