Browse Source

Store Connection's address & port on construction, so that we do not log a zero address later if the socket has already been destroyed.

Lasse Öörni 11 years ago
parent
commit
a06c328305

+ 7 - 12
Source/Engine/Network/Connection.cpp

@@ -70,6 +70,13 @@ Connection::Connection(Context* context, bool isClient, kNet::SharedPtr<kNet::Me
     logStatistics_(false)
 {
     sceneState_.connection_ = this;
+    
+    // Store address and port now for accurate logging (kNet may already have destroyed the socket on disconnection,
+    // in which case we would log a zero address:port on disconnect)
+    kNet::EndPoint endPoint = connection_->RemoteEndPoint();
+    ///\todo Not IPv6-capable.
+    address_ = Urho3D::ToString("%d.%d.%d.%d", endPoint.ip[0], endPoint.ip[1], endPoint.ip[2], endPoint.ip[3]);
+    port_ = endPoint.port;
 }
 
 Connection::~Connection()
@@ -998,18 +1005,6 @@ bool Connection::IsConnected() const
     return connection_->GetConnectionState() == kNet::ConnectionOK;
 }
 
-String Connection::GetAddress() const
-{
-    kNet::EndPoint endPoint = connection_->RemoteEndPoint();
-    ///\todo Not IPv6-capable.
-    return Urho3D::ToString("%d.%d.%d.%d", endPoint.ip[0], endPoint.ip[1], endPoint.ip[2], endPoint.ip[3]);
-}
-
-unsigned short Connection::GetPort() const
-{
-    return connection_->RemoteEndPoint().port;
-}
-
 String Connection::ToString() const
 {
     return GetAddress() + ":" + String(GetPort());

+ 6 - 2
Source/Engine/Network/Connection.h

@@ -159,9 +159,9 @@ public:
     /// Return whether to log data in/out statistics.
     bool GetLogStatistics() const { return logStatistics_; }
     /// Return remote address.
-    String GetAddress() const;
+    String GetAddress() const { return address_; }
     /// Return remote port.
-    unsigned short GetPort() const;
+    unsigned short GetPort() const { return port_; }
     /// Return an address:port string.
     String ToString() const;
     /// Return number of package downloads remaining.
@@ -238,6 +238,10 @@ private:
     String sceneFileName_;
     /// Statistics timer.
     Timer statsTimer_;
+    /// Remote endpoint address.
+    String address_;
+    /// Remote endpoint port.
+    unsigned short port_;
     /// Client connection flag.
     bool isClient_;
     /// Connection pending flag.

+ 1 - 1
Source/Engine/Network/Network.cpp

@@ -166,11 +166,11 @@ bool Network::Connect(const String& address, unsigned short port, Scene* scene,
     kNet::SharedPtr<kNet::MessageConnection> connection = network_->Connect(address.CString(), port, kNet::SocketOverUDP, this);
     if (connection)
     {
-        LOGINFO("Connecting to server " + address + ":" + String(port));
         serverConnection_ = new Connection(context_, false, connection);
         serverConnection_->SetScene(scene);
         serverConnection_->SetIdentity(identity);
         serverConnection_->SetConnectPending(true);
+        LOGINFO("Connecting to server " + serverConnection_->ToString());
         return true;
     }
     else