Esempio n. 1
0
/**
* @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());
}
Esempio n. 2
0
///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();
}