diff --git a/libxsync b/libxsync index 33e27f5..c9dc057 160000 --- a/libxsync +++ b/libxsync @@ -1 +1 @@ -Subproject commit 33e27f52d366381c5ef5647020a36b7cb2c5b190 +Subproject commit c9dc057143720efe76a21368ded925a2a0314288 diff --git a/mainwindow.cpp b/mainwindow.cpp index b5f846f..e070288 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -121,11 +121,12 @@ MainWindow::~MainWindow() { delete ui; } void MainWindow::mainWindowsRun() { // XSyncUdpFactoryImpl::Ins()->initialize(); - auto xsudp = XSyncUdpFactoryImpl::Ins()->createXSUDP(); - XS_ASSERT(xsudp->initialize("0.0.0.0", 9999)); - xsudp->startReceive([this, xsudp](XsyncNetAdd &from, uint8_t *data, size_t length) { - ZLOGI(TAG, "receive from <%s:%d> (%d) :%s", from.ip.c_str(), from.port, data, length); - xsudp->sendto(from, "hello\n", 5, NULL); - }); - + // auto xsudp = XSyncUdpFactoryImpl::Ins()->createXSUDP(); + // XS_ASSERT(xsudp->initialize("0.0.0.0", 9999)); + // xsudp->startReceive([this, xsudp](XsyncNetAdd &from, uint8_t *data, size_t length) { + // ZLOGI(TAG, "receive from <%s:%d> (%d) :%s", from.ip.c_str(), from.port, data, length); + // xsudp->sendto(from, "hello\n", 5, NULL); + // }); + + } diff --git a/src/xsync_udp_factory_impl.cpp b/src/xsync_udp_factory_impl.cpp index e40786d..e01ceb6 100644 --- a/src/xsync_udp_factory_impl.cpp +++ b/src/xsync_udp_factory_impl.cpp @@ -28,7 +28,8 @@ class XSUDP : public I_XSUDP { virtual xs_error_code_t sendto(const XsyncNetAdd& to, const char* data, int32_t length, int32_t* sendlength) override; virtual xs_error_code_t receive(char* data, int32_t& length, XsyncNetAdd& from, int overtimems) override; virtual xs_error_code_t startReceive(onMessage_t onMessage) override; - ; + virtual xs_error_code_t stopReceive() override; + virtual ~XSUDP(); }; const char* fmtip(uint32_t ip) { @@ -94,6 +95,10 @@ xs_error_code_t XSUDP::receive(char* data, int32_t& length, XsyncNetAdd& from, i xs_error_code_t XSUDP::startReceive(onMessage_t onMessage) { m_onMessage = onMessage; + + if (m_zq_thread) { + return kxs_ec_success; + } m_rxbuf = (char*)malloc(10240); m_rxbufsize = 10240; @@ -111,6 +116,28 @@ xs_error_code_t XSUDP::startReceive(onMessage_t onMessage) { m_zq_thread->start(); } +xs_error_code_t XSUDP::stopReceive() { + if (m_zq_thread) { + m_zq_thread->quit(); + m_zq_thread->wait(); + m_zq_thread.reset(nullptr); + } + + if (!m_rxbuf) { + free(m_rxbuf); + m_rxbuf = nullptr; + m_rxbufsize = 0; + } +} +XSUDP::~XSUDP() { + stopReceive(); + + if (m_sock_fd > 0) { + closesocket(m_sock_fd); + m_sock_fd = -1; + } +} + /******************************************************************************* * xSyncUdpFactoryImpl * *******************************************************************************/ diff --git a/src/xsync_udp_factory_impl.hpp b/src/xsync_udp_factory_impl.hpp index d075ff3..049eb6e 100644 --- a/src/xsync_udp_factory_impl.hpp +++ b/src/xsync_udp_factory_impl.hpp @@ -14,7 +14,7 @@ namespace iflytop { using namespace std; -class XSyncUdpFactoryImpl : public I_XSyncUDPFactory { +class XSyncUdpFactoryImpl : public I_XSUDPFactory { private: /* data */ XSyncUdpFactoryImpl(){};