views:

135

answers:

2

I'm trying to implement the irc protocol in a very basic manner. My first attempt is to use boost::asio and connect to a server and read the motd. AFAIK the motd is sent to every client when they connect. I have made the following test program and it seems to not do anything. It's not a very good piece of code but this is all I have after several frustrating hours.

#define _WIN32_WINNT 0x0501
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <queue>



class IrcConnection
{
public:
    IrcConnection(const std::string& server, int port, boost::function<void (const std::string&)> onMessage, boost::asio::io_service& io_service): s(server), p(port), onM(onMessage), ios(io_service), socket(io_service)
    {
    }
    void connect() 
    {
        somethingHappened = false;
        //DNS stuff
        boost::asio::ip::tcp::resolver resolver(ios);
        boost::asio::ip::tcp::resolver::query query(s , "0");
        boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);
        boost::asio::ip::tcp::resolver::iterator end;

        boost::system::error_code error;

        while(iter != end)
        {
            boost::asio::ip::tcp::endpoint endpoint = iter->endpoint();
            endpoint.port(p);
            socket.close();
            socket.connect(endpoint, error);
            iter++;
        }
        if(error)
        {
            std::cout << "ERROR" << std::endl;
            std::cout << error << std::endl;
        }

        std::cout << "Connected to: " << socket.remote_endpoint().address().to_string() << std::endl; 

        //read from the socket until a space is found
        boost::asio::async_read_until(socket, currentData, ' ', boost::bind(&IrcConnection::onReceiveFinished, this, boost::asio::placeholders::error));
    }
    void disconnect() 
    {
        socket.close();
    }
    void send(int priority, const std::string& message) 
    {
    }
bool somethingHappened;

private:
    std::string s;
    int p;
    boost::function<void (const std::string&)> onM;
    boost::asio::io_service& ios;
    boost::asio::ip::tcp::socket socket;
    std::priority_queue<std::string> outQueue;
    boost::asio::streambuf currentData;


    void onReceiveFinished(const boost::system::error_code& error)
    {
        if(error)
        {
            disconnect();
            std::cout << "ERRORRRR" << std::endl;
            std::cout << error << std::endl;
        }

        std::cout << "STUFF IS OCCURING" << std::endl;
        somethingHappened = true;

         std::istream stream(&currentData);
         std::string data;
         std::getline(stream, data);

         boost::asio::async_read_until(socket, currentData, ' ', boost::bind(&IrcConnection::onReceiveFinished, this, boost::asio::placeholders::error));
         ios.dispatch(boost::bind(onM, data));
    }


};


void onMe(const std::string& x)
{
    std::cout << "MESSAGE" << std::endl;
    std::cout << x << std::endl;
}

int main()
{
    boost::asio::io_service ios;
    std::string server = "irc.efnet.org";
    int port = 6667;

    IrcConnection x(server, port, onMe, ios);

    x.connect();

    while(!x.somethingHappened)
    {
        //
    }

    return 0;

}

The program attempts to connect to irc.efnet.org and read until a space is sent and then spits that data out to the terminal. However I run this and all that is printed is the ip address of what I connected to and then the program does not terminate.

What do I need to do to get my indented behavior?

+3  A: 

If you're making asychronous calls, you have to post requests to the io_service. In your case after the "x.connect();" you should call "ios.run();"

Just remember if you don't make any further posts after the async callback returns, run will exit/return.

btw in "ios.dispatch(boost::bind(onM, data));" is onM meant to be onMe ?

Hippicoder
connect is not asynchronous, async_connect is.
Sam Miller
OnM is the method that is local to the object. I pass in a function pointer in the initialization. This also fixed my problem, thanks a lot!
zoke
@Hippocoder: Do you have any suggestions or resources for me to look at while I'm developing with boost::asio? I rather not bother SO every time I run into a small problem.
zoke
@Zoke: Look at the examples found here: http://think-async.com/Asio/ Also signup to the asio mailing list CK answers questions on that list.
Hippicoder
A: 

You are mixing asynchronous (io_service::post, async_read_until) and synchronous (ip::tcp::socket::connect, ip::tcp::endpoint::resolve) operations. This can work but I really do not suggest this approach. Stick to asynchronous operations, use ip::tcp::socket::async_connect instead of ip::tcp::socket::connect and ip::tcp::socket::async_resolve instead of ip::tcp::socket::resolve. You'll need to invoke io_service::run or equivalent, this is why your code does nothing after the connect. The Boost.Asio documentation has numerous examples showing how to achieve this.

Sam Miller