Browse Source

Use std::unique_ptr for LpdMessage instead of std::shared_ptr

Tatsuhiro Tsujikawa 12 years ago
parent
commit
c36370ef60

+ 7 - 9
src/LpdMessageReceiver.cc

@@ -55,7 +55,7 @@ LpdMessageReceiver::~LpdMessageReceiver() {}
 bool LpdMessageReceiver::init(const std::string& localAddr)
 {
   try {
-    socket_.reset(new SocketCore(SOCK_DGRAM));
+    socket_ = std::make_shared<SocketCore>(SOCK_DGRAM);
 #ifdef __MINGW32__
     // Binding multicast address fails under Windows.
     socket_->bindWithFamily(multicastPort_, AF_INET);
@@ -77,9 +77,8 @@ bool LpdMessageReceiver::init(const std::string& localAddr)
   return false;
 }
 
-std::shared_ptr<LpdMessage> LpdMessageReceiver::receiveMessage()
+std::unique_ptr<LpdMessage> LpdMessageReceiver::receiveMessage()
 {
-  std::shared_ptr<LpdMessage> msg;
   while(1) {
     unsigned char buf[200];
     std::pair<std::string, uint16_t> peerAddr;
@@ -87,11 +86,11 @@ std::shared_ptr<LpdMessage> LpdMessageReceiver::receiveMessage()
     try {
       length = socket_->readDataFrom(buf, sizeof(buf), peerAddr);
       if(length == 0) {
-        return msg;
+        return std::unique_ptr<LpdMessage>{};
       }
     } catch(RecoverableException& e) {
       A2_LOG_INFO_EX("Failed to receive LPD message.", e);
-      return msg;
+      return std::unique_ptr<LpdMessage>{};
     }
     HttpHeaderProcessor proc(HttpHeaderProcessor::SERVER_PARSER);
     try {
@@ -103,7 +102,7 @@ std::shared_ptr<LpdMessage> LpdMessageReceiver::receiveMessage()
       A2_LOG_INFO_EX("Failed to parse LPD message.", e);
       continue;
     }
-    const std::shared_ptr<HttpHeader>& header = proc.getResult();
+    auto header = proc.getResult();
     const std::string& infoHashString = header->find(HttpHeader::INFOHASH);
     uint32_t port = 0;
     if(!util::parseUIntNoThrow(port, header->find(HttpHeader::PORT)) ||
@@ -123,12 +122,11 @@ std::shared_ptr<LpdMessage> LpdMessageReceiver::receiveMessage()
                       infoHashString.c_str()));
       continue;
     }
-    std::shared_ptr<Peer> peer(new Peer(peerAddr.first, port, false));
+    auto peer = std::make_shared<Peer>(peerAddr.first, port, false);
     if(util::inPrivateAddress(peerAddr.first)) {
       peer->setLocalPeer(true);
     }
-    msg.reset(new LpdMessage(peer, infoHash));
-    return msg;
+    return make_unique<LpdMessage>(peer, infoHash);
   }
 }
 

+ 1 - 1
src/LpdMessageReceiver.h

@@ -63,7 +63,7 @@ public:
   // sender(peer) and infohash. If no data is available on socket,
   // returns std::shared_ptr<LpdMessage>().  If received data is bad,
   // then returns std::shared_ptr<LpdMessage>(new LpdMessage())
-  std::shared_ptr<LpdMessage> receiveMessage();
+  std::unique_ptr<LpdMessage> receiveMessage();
 
   const std::shared_ptr<SocketCore>& getSocket() const
   {

+ 6 - 6
src/LpdReceiveMessageCommand.cc

@@ -73,12 +73,12 @@ bool LpdReceiveMessageCommand::execute()
     return true;
   }
   for(size_t i = 0; i < 20; ++i) {
-    std::shared_ptr<LpdMessage> m = receiver_->receiveMessage();
+    auto m = receiver_->receiveMessage();
     if(!m) {
       break;
     }
-    std::shared_ptr<BtRegistry> reg = e_->getBtRegistry();
-    std::shared_ptr<DownloadContext> dctx = reg->getDownloadContext(m->infoHash);
+    auto& reg = e_->getBtRegistry();
+    auto& dctx = reg->getDownloadContext(m->infoHash);
     if(!dctx) {
       A2_LOG_DEBUG(fmt("Download Context is null for infohash=%s.",
                        util::toHex(m->infoHash).c_str()));
@@ -90,11 +90,11 @@ bool LpdReceiveMessageCommand::execute()
     }
     RequestGroup* group = dctx->getOwnerRequestGroup();
     assert(group);
-    const std::shared_ptr<BtObject>& btobj = reg->get(group->getGID());
+    auto& btobj = reg->get(group->getGID());
     assert(btobj);
-    const std::shared_ptr<PeerStorage>& peerStorage = btobj->peerStorage;
+    auto& peerStorage = btobj->peerStorage;
     assert(peerStorage);
-    std::shared_ptr<Peer> peer = m->peer;
+    auto& peer = m->peer;
     if(peerStorage->addPeer(peer)) {
       A2_LOG_DEBUG(fmt("LPD peer %s:%u local=%d added.",
                        peer->getIPAddress().c_str(), peer->getPort(),

+ 1 - 1
test/LpdMessageReceiverTest.cc

@@ -50,7 +50,7 @@ void LpdMessageReceiverTest::testReceiveMessage()
                       LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
 
   rcv.getSocket()->isReadable(5);
-  std::shared_ptr<LpdMessage> msg = rcv.receiveMessage();
+  auto msg = rcv.receiveMessage();
   CPPUNIT_ASSERT(msg);
   CPPUNIT_ASSERT_EQUAL(std::string("cd41c7fdddfd034a15a04d7ff881216e01c4ceaf"),
                        util::toHex(msg->infoHash));