// // Created by zx on 22-12-22. // #include "ServerTcpStatu.h" ServerTcpStatu::ServerTcpStatu(){ } bool ServerTcpStatu::Bind(int port) { vector opts = { SO_REUSEPORT, SO_REUSEADDR }; if(setup(port,opts) == 0) { exit_=true; if(thread_accept) { if(thread_accept->joinable()) thread_accept->join(); } if(thread_recv) { if(thread_recv->joinable()) thread_recv->join(); } exit_=false; thread_accept=new std::thread(&ServerTcpStatu::accept,this); thread_recv=new std::thread(&ServerTcpStatu::recv,this); return true; } return false; } void ServerTcpStatu::SetRecvCallback(RecvCallback callback) { recv_callback_=callback; } void ServerTcpStatu::accept() { while(exit_==false) { accepted(); cerr << "Accepted" << endl; } } void ServerTcpStatu::recv() { while(exit_==false) { vector desc; while(1) { desc = getMessage(); for(unsigned int i = 0; i < desc.size(); i++) { if( desc[i] ) { if(recv_callback_!= nullptr) { recv_callback_(desc[i]->ip,desc[i]->socket,desc[i]->message); } clean(i); } } usleep(1000); } } } void ServerTcpStatu::Publish(std::string data) { for(int i=0;i