1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- //
- // Created by zx on 22-12-22.
- //
- #include "ServerTcpStatu.h"
- ServerTcpStatu::ServerTcpStatu(){
- }
- bool ServerTcpStatu::Bind(int port)
- {
- vector<int> 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<descript_socket*> 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<num_client;++i)
- {
- Send(data,i);
- }
- }
|