Advertisement
Guest User

Server.cpp

a guest
Jul 5th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include "Server.h"
  2.  
  3.  
  4. Server::Server(int port) : accept(io_service, tcp::endpoint(tcp::v4(), port)){
  5.  
  6.     waitForConnection();
  7. }
  8.  
  9. Server::Server(boost::asio::io_service& io_service, int port) : accept(io_service, tcp::endpoint(tcp::v4(), port)){
  10.     waitForConnection();
  11. }
  12.  
  13. void Server::waitForConnection(){
  14.  
  15.     Connection::pointer connect = Connection::create(accept.get_io_service());
  16.  
  17.     accept.async_accept(connect->getSocket(), boost::bind(&Server::acceptConnection, this, connect, boost::asio::placeholders::error));
  18. }
  19.  
  20. void Server::acceptConnection(Connection::pointer connection, const boost::system::error_code& error){
  21.     if (!error)
  22.     {
  23.         connections.push_back(connection);
  24.         connection->readMsg(boost::bind(&Server::msgHandler,this, boost::placeholders::_1));
  25.     }
  26.     else
  27.     {
  28.         std::cout << "Server::acceptConnection error" << std::endl;
  29.     }
  30.     waitForConnection();
  31.  
  32. }
  33.  
  34. void Server::msgHandler(std::string msg){
  35.     for (Connection::pointer con: connections){
  36.         con->writeMsg(msg);
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement