@@ -142,13 +142,13 @@ class Connection : public EvbufCallbackBase<Connection> {
142142
143143 void SetPeerInfo (std::unique_ptr<PeerInfo> &&peer_info) { peer_info_ = std::move (peer_info); }
144144
145- const PeerInfo * GetPeerInfo () {
145+ const PeerInfo & GetPeerInfo () {
146146 if (peer_info_) {
147- return peer_info_. get () ;
147+ return * peer_info_;
148148 }
149149
150150 SetPeerInfo (std::make_unique<PeerInfo>(ip_, port_, " " , -1 ));
151- return peer_info_. get () ;
151+ return * peer_info_;
152152 }
153153
154154 uint64_t GetClientType () const ;
@@ -235,35 +235,30 @@ class PeerInfo {
235235 ~PeerInfo () = default ;
236236
237237 PeerInfo (std::string_view ip, uint32_t port, std::string_view peer_id, int64_t peer_version)
238- : port_(port), peer_version_(peer_version) {
239- if (peer_id.empty ()) {
240- str_ = fmt::format (" {}:{}" , ip, port);
241- } else {
242- str_ = fmt::format (" {}:{} ({}@{})" , ip, port, peer_id, peer_version);
243- }
244-
245- addr_ = std::string_view (str_).substr (0 , str_.find (' ' ));
246- ip_ = std::string_view (str_).substr (0 , ip.length ());
247-
248- if (!peer_id.empty ()) {
249- peer_id_ = std::string_view (str_).substr (addr_.length () + 1 , peer_id.length ());
250- }
238+ : ip_(ip), port_(port), peer_id_(peer_id), peer_version_(peer_version) {
239+ addr_ = fmt::format (" {}:{}" , ip, port);
251240 }
252241
253- std::string_view GetIP () const { return ip_; }
242+ std::string GetIP () const { return ip_; }
254243 uint32_t GetPort () const { return port_; }
255244
256- std::string_view GetStringView () const { return str_; }
257- std::string_view GetPeerID () const { return peer_id_; }
258- std::string_view GetAddr () const { return addr_; }
245+ std::string ToString () const {
246+ if (peer_id_.empty ()) {
247+ return fmt::format (" {}:{}" , ip_, port_);
248+ } else {
249+ return fmt::format (" {}:{} ({}@{})" , ip_, port_, peer_id_, peer_version_);
250+ }
251+ }
252+ std::string GetPeerID () const { return peer_id_; }
253+ std::string GetAddr () const { return addr_; }
259254 int64_t GetPeerVersion () const { return peer_version_; }
260255
261256 private:
262- std::string_view ip_;
263- std::string_view addr_;
257+ std::string ip_;
258+ std::string addr_;
264259 uint32_t port_ = 0 ;
265260
266- std::string_view peer_id_;
261+ std::string peer_id_;
267262 int64_t peer_version_ = 0 ;
268263
269264 std::string str_;
0 commit comments