Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Server.h"
- Server::Server(int port) : accept(io_service, tcp::endpoint(tcp::v4(), port)){
- waitForConnection();
- }
- Server::Server(boost::asio::io_service& io_service, int port) : accept(io_service, tcp::endpoint(tcp::v4(), port)){
- waitForConnection();
- }
- void Server::waitForConnection(){
- Connection::pointer connect = Connection::create(accept.get_io_service());
- accept.async_accept(connect->getSocket(), boost::bind(&Server::acceptConnection, this, connect, boost::asio::placeholders::error));
- }
- void Server::acceptConnection(Connection::pointer connection, const boost::system::error_code& error){
- if (!error)
- {
- connections.push_back(connection);
- connection->readMsg(boost::bind(&Server::msgHandler,this, boost::placeholders::_1));
- }
- else
- {
- std::cout << "Server::acceptConnection error" << std::endl;
- }
- waitForConnection();
- }
- void Server::msgHandler(std::string msg){
- for (Connection::pointer con: connections){
- con->writeMsg(msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement