Search code examples
c++socketsactorc++-actor-framework

libcppa actor that spawns actors on socket connections


I'm looking for example code for a libcppa actor that listens on a port for new connection and then spawn new actors to handle the new connection.

Any help would be appreciated.

Thanks


Solution

  • Do you want the actors to read/write directly from/to the socket? A proper actor-based IO abstraction is not implemented yet, but it is a planned feature for the next version of libcppa (stay tuned). If you only want to distribute your actors via the network, take a look at the publish/remote_actor function pair.

    /edit:

    For now, you can use some of libcppa's utility to get a blocking version up and running:

    using namespace cppa;
    spawn<detached>([] {
        auto ack = network::ipv4_acceptor::create(4242);
        for (;;) {
            auto spair = ack->accept_connection();
            spawn<detached>([spair] {
                // spair.first is the input stream
                // spair.second is the output stream
                // please see http://neverlord.github.io/libcppa/namespacecppa_1_1network.html
            });
        }
    );
    

    This does not scale well, because you will create a thread per connection, and it's not very elegant. Plus, it's hard to multiplex between messages received via the socket and messages received from other actors. A more elegant solution is on its way. ;)