libt2n: (gerd) socket reconnect added (incl. unit tests)
[libt2n] / src / command_client.cpp
index 376d3f9..0f44203 100644 (file)
@@ -51,6 +51,35 @@ command_client::command_client(client_connection& _c, long long _command_timeout
     read_hello();
 }
 
+/** @brief replace the connection currently in use with a new one
+
+    @param _c reference of the new connection
+
+    @note the old connection must still be valid when this method is called,
+          it can safely be deleted after this method returned
+
+    @note all callbacks registered on the old connection will be copied over
+          to the new one
+*/
+void command_client::replace_connection(client_connection& _c)
+{
+    // copy all callbacks registered on the old connection
+    for(callback_event_type e=static_cast<callback_event_type>(0);
+        e < __events_end;
+        e=static_cast<callback_event_type>(static_cast<int>(e)+1))
+    {
+        list<boost::function<void ()> > evcb=c.get_callback_list(e);
+
+        for (list<boost::function<void ()> >::iterator i=evcb.begin(); i != evcb.end(); i++)
+            _c.add_callback(e,*i);
+    }
+
+    // replace the connection
+    c=_c;
+
+    read_hello();
+}
+
 std::string command_client::read_packet(const long long &usec_timeout)
 {
     string resultpacket;