/** * @param : conn : the GNE::Connection which contains the GNE::Packets to be read */ void NetMgr::OnReceive(GNE::Connection& conn) { LOG_DEBUG("Received packet"); GNE::Packet* next = conn.stream().getNextPacket(); while (next != NULL) { int type = next->getType(); if (type == GNE::PingPacket::ID) { GNE::PingPacket& ping = *((GNE::PingPacket*)next); if (ping.isRequest()) { ping.makeReply(); conn.stream().writePacket(ping, true); } else { LOG_INFO("Ping: " + ping.getPingInformation().pingTime.toString()); } } delete next; next = conn.stream().getNextPacket(); } }
///One or more GNE::Packets was received, let's do something with them void MyNet::OnReceive( GNE::Connection &conn ) { mMutex.acquire(); GNE::Packet* next = conn.stream().getNextPacket(); while( next != 0 ) { int type = next->getType(); if( type == PositionPacket::ID ) { if ( !GetIsServer() ) { //aha, this is one of our custom packets. Decompose it and update our slave. PositionPacket *pos = static_cast<PositionPacket*>(next); osg::Vec3 newXYZ = pos->mXYZ; osg::Vec3 newHPR = pos->mHPR; std::string ownerID = pos->mOwnerID; Transform xform; xform.SetTranslation(newXYZ); xform.SetRotation(newHPR); mCamera->SetTransform(xform, Transformable::REL_CS); }else LOG_INFO("Host sending position to itself\n"); } else if( type == PlayerQuitPacket::ID ) { PlayerQuitPacket* playerQuitPacket = static_cast<PlayerQuitPacket*>(next); //mIDsToRemove.push( playerQuitPacket->mPlayerID ); if ( !GetIsServer() ) { LOG_INFO("Host sending Quit-packet\n"); //Shutdown(); mNode->Quit(); }else LOG_ERROR("Host sending Quit-packet to itself\n"); } delete next; next = conn.stream().getNextPacket(); } mMutex.release(); }
/** * @param conn: The GNE::Connection that caused the failure * @param error : The error describing the failure */ void NetMgr::OnConnectFailure(GNE::Connection& conn, const GNE::Error& error) { LOG_DEBUG(error.toString() + "from " + conn.getRemoteAddress(true).toString()); }
///One or more GNE::Packets was received, let's do something with them void MyNetwork::OnReceive( GNE::Connection &conn ) { mMutex.acquire(); GNE::Packet* next = conn.stream().getNextPacket(); while (next != 0) { int type = next->getType(); if (type == GNE::PingPacket::ID) { //it's a PingPacket so lets process it. GNE::PingPacket& ping = *static_cast<GNE::PingPacket*>(next); if (ping.isRequest()) { ping.makeReply(); conn.stream().writePacket(ping, true); } else { LOG_INFO("Ping: " + ping.getPingInformation().pingTime.toString()); } } else if (type == PositionPacket::ID) { //aha, this is one of our custom packets. Decompose it and move our remote //player representation. PositionPacket* pos = static_cast<PositionPacket*>(next); osg::Vec3 newXYZ = pos->mXYZ; osg::Vec3 newHPR = pos->mHPR; std::string ownerID = pos->mOwnerID; Transform xform; xform.SetTranslation(newXYZ); xform.SetRotation(newHPR); StringObjectMap::iterator iter = mOtherPlayerMap.find(ownerID); if (iter != mOtherPlayerMap.end()) { //the player ID is already in our list so lets update it's position iter->second->SetTransform(xform, Transformable::ABS_CS); } else { //this player isn't in our list, so let's create him MakePlayer(ownerID); } if (GetIsServer()) { //rebroadcast the packet to all connections except for the //one who sent the packet in the first place ConnectionIterator conns = mConnections.begin(); while (conns != mConnections.end()) { if (conns->first != conn.getRemoteAddress(true).toString()) { SendPacket(conns->first, *next); } ++conns; } } } else if (type == PlayerQuitPacket::ID) { PlayerQuitPacket* playerQuitPacket = static_cast<PlayerQuitPacket*>(next); mIDsToRemove.push(playerQuitPacket->mPlayerID); } delete next; next = conn.stream().getNextPacket(); } mMutex.release(); }