1010#include < boost/date_time/posix_time/posix_time.hpp>
1111#include < unordered_map>
1212#include < thread>
13+ #include < regex>
1314
14- VectorworksMVR::CMVRxchangeServiceImpl::CMVRxchangeServiceImpl ()
15+ VectorworksMVR::CMVRxchangeServiceImpl::CMVRxchangeServiceImpl (): fmdns_long_timer(fmdns_IO_Context)
1516{
1617 fServer = nullptr ;
1718}
1819
1920VectorworksMVR::CMVRxchangeServiceImpl::~CMVRxchangeServiceImpl ()
2021{
2122 this ->mDNS_Client_Stop ();
23+ if (fmdns_Thread.joinable ())
24+ {
25+ fmdns_Thread.join ();
26+ }
2227 this ->TCP_Stop ();
2328}
2429
30+ void FilterServiceName (MVRxchangeString& service)
31+ {
32+ std::string s (service.fBuffer );
33+ std::string out;
34+
35+ if (s.empty ())
36+ {
37+ return ;
38+ }
39+
40+ std::regex_replace (std::back_inserter (out), s.begin (), s.end (), std::regex (" [^a-zA-Z0-9\\ -_]" ), " " );
41+
42+ service = out.c_str ();
43+ }
44+
45+
2546VCOMError VectorworksMVR::CMVRxchangeServiceImpl::ConnectToLocalService (const ConnectToLocalServiceArgs& service)
2647{
2748 this ->LeaveLocalService ();
@@ -30,6 +51,7 @@ VCOMError VectorworksMVR::CMVRxchangeServiceImpl::ConnectToLocalService(const Co
3051 fCurrentService = service;
3152 this ->TCP_Start ();
3253
54+ FilterServiceName (fCurrentService .Service );
3355
3456 // ---------------------------------------------------------------------------------------------
3557 // Start mDNS Service
@@ -95,10 +117,10 @@ VCOMError VectorworksMVR::CMVRxchangeServiceImpl::ConnectToLocalService(const Co
95117
96118 if (fmdns_Thread.joinable ())
97119 {
98- fmdns_IO_Context.stop ();
99- fmdns_Thread.join ();
120+ mDNS_Client_Stop ();
100121 }
101- fmdns_Thread = std::thread (&CMVRxchangeServiceImpl::mDNS_Client_Start , this );
122+
123+ mDNS_Client_Start ();
102124
103125 return kVCOMError_NoError ;
104126}
@@ -112,6 +134,7 @@ VCOMError VectorworksMVR::CMVRxchangeServiceImpl::LeaveLocalService()
112134 leaveMessage.Message .Type = MVRxchangeMessageType::MVR_LEAVE;
113135 leaveMessage.Message .LEAVE .FromStationUUID = fCurrentService .StationUUID ;
114136 this ->Send_message (leaveMessage);
137+ mDNS_Client_Stop ();
115138
116139 for (auto & s : fmdns)
117140 {
@@ -126,8 +149,21 @@ VCOMError VectorworksMVR::CMVRxchangeServiceImpl::LeaveLocalService()
126149
127150VCOMError VCOM_CALLTYPE CMVRxchangeServiceImpl::QueryLocalServices (size_t & out_Count)
128151{
129- this ->mDNS_Client_Task ();
130-
152+
153+ if (fmdns_Thread.joinable ())
154+ {
155+ std::condition_variable cv;
156+ std::mutex mut;
157+ std::unique_lock<std::mutex> lock (mut);
158+ boost::asio::post (fmdns_IO_Context, [this , &cv](){
159+ this ->mDNS_Client_Task ();
160+ cv.notify_all ();
161+ });
162+ cv.wait (lock);
163+ }else {
164+ this ->mDNS_Client_Task ();
165+ }
166+
131167 {
132168 std::lock_guard<std::mutex> lock (fQueryLocalServicesResult_mtx );
133169 out_Count = fQueryLocalServicesResult .size ();
@@ -303,9 +339,12 @@ IMVRxchangeService::IMVRxchangeMessage CMVRxchangeServiceImpl::TCP_OnIncommingMe
303339 if (GetSingleMemberOfService (in.LEAVE .FromStationUUID , newItem) != kVCOMError_NoError )
304340 {
305341 std::lock_guard<std::mutex> lock (fMvrGroupMutex );
306- std::remove_if (fMVRGroup .begin (), fMVRGroup .end (), [&newItem](const MVRxchangeGroupMember& it){
307- return it.stationUUID == newItem.stationUUID ;
308- });
342+ fMVRGroup .erase (
343+ std::remove_if (fMVRGroup .begin (), fMVRGroup .end (), [&newItem](const MVRxchangeGroupMember& it){
344+ return it.stationUUID == newItem.stationUUID ;
345+ }),
346+ fMVRGroup .end ()
347+ );
309348 }
310349 }
311350
@@ -315,6 +354,7 @@ IMVRxchangeService::IMVRxchangeMessage CMVRxchangeServiceImpl::TCP_OnIncommingMe
315354 ret = fCallBack .IncomingCallback (in, fCallBack .Context );
316355 }
317356
357+
318358 return ret;
319359}
320360
@@ -400,34 +440,40 @@ VCOMError CMVRxchangeServiceImpl::GetSingleMemberOfService(const MvrUUID& statio
400440 return kVCOMError_Failed ;
401441}
402442
403-
404- void CMVRxchangeServiceImpl::mDNS_Client_Tick (boost::asio::deadline_timer* t)
443+ void CMVRxchangeServiceImpl::mDNS_Client_Tick ()
405444{
406445 MVRXCHANGE_DEBUG (" mDNS_Client_Tick" );
407446 mDNS_Client_Task ();
408447
409- t->async_wait (boost::bind (&CMVRxchangeServiceImpl::mDNS_Client_Tick , this , t));
448+ fmdns_long_timer.expires_from_now (boost::posix_time::seconds (45 ));
449+ fmdns_long_timer.async_wait (boost::bind (&CMVRxchangeServiceImpl::mDNS_Client_Tick , this ));
410450}
411451
412- void CMVRxchangeServiceImpl::mDNS_Client_Start ()
413- {
414- boost::asio::deadline_timer t_long (fmdns_IO_Context, boost::posix_time::seconds ( 45 ));
452+ void CMVRxchangeServiceImpl::mDNS_Client_RFun ()
453+ {
454+ while (!fmdns_stop_flag)
415455 {
416- boost::asio::deadline_timer t_short (fmdns_IO_Context, boost::posix_time::seconds (1 ));
417- t_short.async_wait (boost::bind (&CMVRxchangeServiceImpl::mDNS_Client_Tick , this , &t_long));
456+ fmdns_IO_Context.run_for (std::chrono::seconds (1 ));
418457 }
419- fmdns_IO_Context.run ();
458+ }
459+
460+ void CMVRxchangeServiceImpl::mDNS_Client_Start ()
461+ {
462+ fmdns_stop_flag = false ;
463+ boost::asio::post (fmdns_IO_Context, boost::bind (&CMVRxchangeServiceImpl::mDNS_Client_Tick , this ));
464+ fmdns_Thread = std::thread (&CMVRxchangeServiceImpl::mDNS_Client_RFun , this );
420465}
421466
422467void CMVRxchangeServiceImpl::mDNS_Client_Stop ()
423468{
424- if (fmdns_Thread.joinable ())
469+ fmdns_stop_flag = true ;
470+ if (fmdns_Thread.joinable ())
425471 {
426- fmdns_IO_Context.stop ();
427472 fmdns_Thread.join ();
428473 }
429474}
430475
476+
431477mdns_cpp::QueryResList CMVRxchangeServiceImpl::mDNS_Filter_Queries (mdns_cpp::QueryResList &input)
432478{
433479 // IPv6 is currently not supported
@@ -493,8 +539,6 @@ mdns_cpp::QueryResList CMVRxchangeServiceImpl::mDNS_Filter_Queries(mdns_cpp::Que
493539
494540void CMVRxchangeServiceImpl::mDNS_Client_Task ()
495541{
496-
497-
498542 mdns_cpp::mDNS mdns;
499543 auto query_res = mdns.executeQuery2 (MVRXChange_Service);
500544 std::vector<ConnectToLocalServiceArgs> result;
0 commit comments