Fix ServerChannel 65/264565/3
authorSangyoon Jang <jeremy.jang@samsung.com>
Fri, 24 Sep 2021 10:38:07 +0000 (19:38 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Tue, 28 Sep 2021 03:01:01 +0000 (03:01 +0000)
- Fix to use std::erase() std::remove_if combination to remove item safely
- Capture std::shared_ptr itself instead of using raw pointer

Change-Id: Ib151a9ea52814dca99adb313124ec2a82a6ebf51
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
cion/channel/server_channel.cc

index 604e48fa4d72c91403e3b44c484670a2c28a54fd..d07fb8e62dfd99959f61b991cb4a2d59a979c477 100644 (file)
@@ -129,17 +129,23 @@ void ServerChannel::Impl::Disconnect(std::shared_ptr<PeerInfo> peer) {
   std::shared_ptr<ServerPeerInfo> speer =
       std::dynamic_pointer_cast<ServerPeerInfo>(peer);
   LOG(INFO) << "Disconnect !! " << speer->GetClientDp();
-  for (std::shared_ptr<PeerInfo> i : peerlist_) {
-    std::shared_ptr<ServerPeerInfo> si =
-      std::dynamic_pointer_cast<ServerPeerInfo>(i);
-    if (*si->GetClientDp() == *speer->GetClientDp())
-      peerlist_.remove(i);
-  }
 
-  for (VineDpPtr ptr : accepted_dp_list_) {
-    if (*ptr == *speer->GetClientDp())
-      accepted_dp_list_.remove(ptr);
-  }
+  peerlist_.erase(std::remove_if(peerlist_.begin(), peerlist_.end(),
+      [speer](std::shared_ptr<PeerInfo> i) -> bool {
+        std::shared_ptr<ServerPeerInfo> si =
+            std::dynamic_pointer_cast<ServerPeerInfo>(i);
+        if (*si->GetClientDp() == *speer->GetClientDp())
+          return true;
+        return false;
+      }));
+
+  accepted_dp_list_.erase(
+      std::remove_if(accepted_dp_list_.begin(), accepted_dp_list_.end(),
+          [speer](VineDpPtr ptr) -> bool {
+            if (*ptr == *speer->GetClientDp())
+              return true;
+            return false;
+          }));
 }
 
 void ServerChannel::Impl::Accept(std::shared_ptr<PeerInfo> peer_info) {
@@ -509,10 +515,9 @@ void ServerChannel::Accept(std::shared_ptr<PeerInfo> peer) {
     return;
   }
 
-  InvokeAtIdle(this, [this, peer_copy = *peer]() -> void {
+  InvokeAtIdle(this, [this, peer]() -> void {
         ConnectionResult result(ConnectionResult::OK);
-        auto p = std::make_shared<PeerInfo>(peer_copy);
-        this->OnConnectionResult(p, result);
+        this->OnConnectionResult(peer, result);
       });
 }
 
@@ -524,10 +529,9 @@ void ServerChannel::Reject(std::shared_ptr<PeerInfo> peer, std::string reason) {
     return;
   }
 
-  InvokeAtIdle(this, [this, peer_copy = *peer]() -> void {
+  InvokeAtIdle(this, [this, peer]() -> void {
         ConnectionResult result(ConnectionResult::Rejected);
-        auto p = std::make_shared<PeerInfo>(peer_copy);
-        this->OnConnectionResult(p, result);
+        this->OnConnectionResult(peer, result);
       });
 }