This socket server can:
- Non-blocking processing of multiple socket connections.
- You can receive a ping message from the client and remove the inactive client within 5 seconds.
- You can receive login requests from clients, and users can add authentication logic to their needs.
/*' non-blocking socket server using Blocking apicreated on Dec, (Merry Christmas) @author: Scottgu<[email PR Otected], [email protected]>performance tested:environment:64bit win7, i7-4800mq, 8GB '*/#pragmaOnceBoost::recursive_mutex CS;//Thread-safe access to clients arraystructTalk_to_client:boost::enable_shared_from_this<talk_to_client>{talk_to_client (Boost::asio::io_service&io_service): Sock_ (io_service) {} std::stringUsername ()Const { returnusername_; } voidanswer_to_client () {Try{read_request (); Process_request (); } Catch(boost::system::system_error&) {stop (); } if(Timed_out ()) stop (); } voidread_request () {if(Sock_.available ()) {Already_read_+=Sock_.read_some (Boost::asio::buffer (buff_+ Already_read_, max_msg-already_read_)); } } voidprocess_request () {BOOLFound_enter = Std::find (buff_, buff_ + Already_read_,'\ n') < Buff_ +Already_read_; if(!found_enter)return;//message isn't full//Process the MSGLast_ping_ =boost::p osix_time::microsec_clock::local_time (); size_t POS= Std::find (buff_, buff_ + Already_read_,'\ n') -buff_; STD::stringmsg (buff_, POS); Std::copy (buff_+ POS, buff_ +Already_read_, buff_); //std::copy (buff_ + already_read_, buff_ + max_msg, buff_);Already_read_-= pos +1; if(Msg.find ("Login") ==0) On_login (msg); Else if(Msg.find ("Ping") ==0) on_ping (); Else if(Msg.find ("ask_clients") ==0) on_clients (); ElseStd::cerr <<"Invalid msg"<< msg <<Std::endl; } voidSet_clients_changed () {clients_changed_ =true; } Boost::asio::ip::tcp::socket& Sock () {returnSock_;} BOOLTimed_out ()Const{boost::p osix_time::p time now=boost::p osix_time::microsec_clock::local_time (); Long Longms = (NOW-last_ping_). Total_milliseconds (); returnMs > the; } voidStop () {Boost::system::error_code err; Sock_.close (err); } voidOn_login (ConstSTD::string&msg) {Std::istringstreaminch(msg); inch>> username_ >>username_; Write ("Login ok\n"); //update_clients_changed (); } voidon_ping () {Write (Clients_changed_?"Ping client_list_changed\n":"Ping ok\n"); Clients_changed_=false; } voidon_clients (); voidWriteConstSTD::string&msg) {Sock_.write_some (Boost::asio::buffer (msg));}Private: //In synchronous Client field is same BOOLClients_changed_; Boost::p osix_time::p time Last_ping_; Boost::asio::ip::tcp::socket Sock_; enum{max_msg =6*1024x768 }; intAlready_read_; CharBuff_[max_msg]; BOOLStarted_; STD::stringusername_;}; typedef BOOST::SHARED_PTR<talk_to_client>client_ptr;typedef std::vector<client_ptr>Array;array clients;voidtalk_to_client::on_clients () {std::stringmsg; Boost::recursive_mutex::scoped_lock LK (CS); for(Auto B = Clients.begin (), E = Clients.end (); b! = e; + +b) {msg+ = (*b)->username () +" "; } Write ("Clients"+ msg +"\ n");}voidAccept_thread () {Boost::asio::io_service io_service; Boost::asio::ip::tcp::acceptor acceptr (Io_service, Boost::asio::ip::tcp::endpoint (Boost::asio::ip::tcp::v4 (), 8001)); while(true) {client_ptr new_ (Newtalk_to_client (Io_service)); Acceptr.accept (New_-sock ()); Boost::recursive_mutex::scoped_lock LK (CS); Clients.push_back (New_); }}voidHandle_clients_thread () { while(true) {boost::this_thread::sleep (boost::p osix_time::millisec (1)); Boost::recursive_mutex::scoped_lock LK (CS); for(Array::iterator B = Clients.begin (), E = Clients.end (); b! = e; + +b) { (*B)answer_to_client (); } //Erase clients that timed outclients.erase (std::remove_if (Clients.begin (), Clients.end (), Boost::bind (&talk_to_client::timed_out, _1)), Clients.end ()); }}intRun_sync_talk_server () {Boost::thread_group threads; Threads.create_thread (Boost::bind (Accept_thread)); Threads.create_thread (Handle_clients_thread); Threads.join_all ();}
Create a non-blocking socket server using a synchronous socket