peer_server()
		: m_peer_requests(0)
		, m_acceptor(m_ios)
		, m_port(0)
	{
		error_code ec;
		m_acceptor.open(tcp::v4(), ec);
		if (ec)
		{
			fprintf(stderr, "Error opening peer listen socket: %s\n", ec.message().c_str());
			return;
		}

		m_acceptor.bind(tcp::endpoint(address_v4::any(), 0), ec);
		if (ec)
		{
			fprintf(stderr, "Error binding peer socket to port 0: %s\n", ec.message().c_str());
			return;
		}
		m_port = m_acceptor.local_endpoint(ec).port();
		if (ec)
		{
			fprintf(stderr, "Error getting local endpoint of peer socket: %s\n", ec.message().c_str());
			return;
		}
		m_acceptor.listen(10, ec);
		if (ec)
		{
			fprintf(stderr, "Error listening on peer socket: %s\n", ec.message().c_str());
			return;
		}

		fprintf(stderr, "%s: peer initialized on port %d\n", time_now_string(), m_port);

		m_thread.reset(new thread(boost::bind(&peer_server::thread_fun, this)));
	}
	void getClientConnection (tcp::acceptor &acceptor) {
		std::cout << "In GUIMessageSenderThread getClientConnection()" << std::endl;
		try
		{
			std::cout << "waiting for gui client to connect..." << std::endl;
			acceptor.accept(*stream_.rdbuf());
			std::cout << "...gui client connected" << std::endl;
			connected_ = true;
		}
		catch (std::exception& e)
		{
			std::cerr << "Error in gui client connection: " << e.what() << std::endl;
		}
		std::cout << "done GUIMessageSenderThread getClientConnection()" << std::endl;
	}
Esempio n. 3
0
/*****************************************************************************
*   fiber function per client
*****************************************************************************/
void client( boost::asio::io_service & io_svc, tcp::acceptor & a,
             boost::fibers::barrier& barrier, unsigned iterations) {
    print( tag(), ": echo-client started");
    for (unsigned count = 0; count < iterations; ++count) {
        tcp::resolver resolver( io_svc);
        tcp::resolver::query query( tcp::v4(), "127.0.0.1", "9999");
        tcp::resolver::iterator iterator = resolver.resolve( query);
        tcp::socket s( io_svc);
        boost::asio::connect( s, iterator);
        for (unsigned msg = 0; msg < 1; ++msg) {
            std::ostringstream msgbuf;
            msgbuf << "from " << fiber_names.lookup() << " " << count << "." << msg;
            std::string message(msgbuf.str());
            print( tag(), ": Sending: ", message);
            boost::system::error_code ec;
            boost::asio::async_write(
                    s,
                    boost::asio::buffer( message),
                    boost::fibers::asio::yield[ec]);
            if ( ec == boost::asio::error::eof) {
                return; //connection closed cleanly by peer
            } else if ( ec) {
                throw boost::system::system_error( ec); //some other error
            }
            char reply[max_length];
            size_t reply_length = s.async_read_some(
                    boost::asio::buffer( reply, max_length),
                    boost::fibers::asio::yield[ec]);
            if ( ec == boost::asio::error::eof) {
                return; //connection closed cleanly by peer
            } else if ( ec) {
                throw boost::system::system_error( ec); //some other error
            }
            print( tag(), ": Reply  : ", std::string( reply, reply_length));
        }
    }
    // done with all iterations, wait for rest of client fibers
    if ( barrier.wait()) {
        // exactly one barrier.wait() call returns true
        // we're the lucky one
        a.close();
        print( tag(), ": acceptor stopped");
    }
    print( tag(), ": echo-client stopped");
}
Esempio n. 4
0
void acceptorLoop()
{
	cout << "Waiting for clients..." << endl;

	for(;;)
	{
		socket_ptr clientSock(new tcp::socket(service));

		acceptor.accept(*clientSock);

		cout << "New client joined! ";

		mtx.lock();
		clientList->emplace_back(clientSock);
		mtx.unlock();

		cout << clientList->size() << " total clients" << endl;
	}
}
Esempio n. 5
0
/*****************************************************************************
*   listening server
*****************************************************************************/
void server( boost::asio::io_service & io_svc, tcp::acceptor & a) {
    print( tag(), ": echo-server started");
    try {
        for (;;) {
            socket_ptr socket( new tcp::socket( io_svc) );
            boost::system::error_code ec;
            a.async_accept(
                    * socket,
                    boost::fibers::asio::yield[ec]);
            if ( ec) {
                throw boost::system::system_error( ec); //some other error
            } else {
                boost::fibers::fiber( session, socket).detach();
            }
        }
    } catch ( std::exception const& ex) {
        print( tag(), ": caught exception : ", ex.what());
    }
    io_svc.stop();
    print( tag(), ": echo-server stopped");
}
    void run(unsigned sleepMillis)
    {
		std::cout << "in GUIMessageSenderThread: run "
                  << sleepMillis << "ms"
                  << std::endl;

		while (running_) {
			if (!connected_) {
				std::cout << "In GUIMessageSenderThread trying to connect to: " << host_ << " " << port_ << std::endl;
				//tcp::endpoint endpoint_(boost::asio::ip::address::from_string(host_), port_);
				//tcp::endpoint endpoint_(boost::asio::ip::address::from_string(host_), port_);
				endpoint_.address(boost::asio::ip::address::from_string(host_));
				endpoint_.port(port_);
				tcp::acceptor acceptor(*io_service_, endpoint_);

				getClientConnection(acceptor);
				try {
				
					sendMessages(sleepMillis);

					acceptor.close();
					stream_.clear();
					stream_.close();
				}
				catch (std::exception& e)
				{
					std::cerr << "GUIMessageSenderThread Error in connection: " << e.what() << std::endl;
				}
				connected_ = false;
				//boost::asio::deadline_timer timer(*io_service_,boost::posix_time::milliseconds(1000));
				//timer.expires_from_now(boost::posix_time::milliseconds(1000));
				//timer.wait();
			}
		}
		std::cout << "done GUIMessageSenderThread: run "
                  << sleepMillis << "ms"
                  << std::endl;
	}
Esempio n. 7
0
	void accept()
	{
		auto self = shared_from_this();
		awaitingConnection = std::make_shared<Connection>(io);
		acceptor.async_accept(awaitingConnection->stream1(),
			[this, self] (const error_code &error) {
				if (error) {
					std::cerr << "accept: " << error << std::endl;
				} else {
					Connection::Ptr thisConnecion;
					std::swap(thisConnecion, awaitingConnection);
					accept();

					error_code localSocketError;
					thisConnecion->stream2().connect(socketPath, localSocketError);
					if (localSocketError) {
						std::cerr << "connecting local socket: " << localSocketError << std::endl;
						thisConnecion->stream1().close(localSocketError);
						return;
					}
					thisConnecion->start();
				}
			});
	}
	~peer_server()
	{
		m_acceptor.cancel();
		m_acceptor.close();
		if (m_thread) m_thread->join();
	}
Esempio n. 9
0
void TcpServer::start()
{
	TcpConnection::SP_TCPCONNECTION spConnection = TcpConnection::create(m_acceptor.get_io_service());
	m_acceptor.async_accept(spConnection->socket(),
		boost::bind(&TcpServer::OnAccept, this, spConnection, boost::asio::placeholders::error));
}
Esempio n. 10
0
File: main.cpp Progetto: CCJY/coliru
	~CServer()
	{
		DEBUG_MSG("Listener Destroyed");
		acceptor_.close();
	}
Esempio n. 11
0
	void start_accept()
	{
		shared_ptr<session> s(new session(acceptor_.io_service(),cache,sessions));
		acceptor_.async_accept(s->socket_,boost::bind(&tcp_cache_server::on_accept,this,aio::placeholders::error,s));
	}