3

I am new to boost asio. I've read this comparison: How does libuv compare to Boost/ASIO? (Dead link)

It turned out that there is an opportunity to use asio's event loop for other reasons but socket programming. I am planning to use this async event loop for processing messages coming from the UI.
So far I have a concurrent queue and I would like to implement my own boost::asio IO object. The goal is to use this object as you can see here:

#include <iostream>
#include <boost/asio.hpp>
#include "MProcessor.h"
#include "concurrent_queue.h"

void foo(const boost::system::error_code& /*e*/)
{
  std::cout << "got message" << std::endl;
}

int main()
{
  concurrent_queue<int> messages;
  /* ... */
  boost::asio::io_service io;

  MProcessor t(io, &messages);
  t.async_wait(&foo);

  io.run();

  return 0;
}

So the aim is to keep busy the io_service by an IO object which is able to call a function asynchronously (foo), when the queue (messages) has a message can be popped. The queue has a method called wait_and_pop. It blocks the caller thread until the queue has something to pop, than it returns with the popped item value.
I am stucked at this point, and I couldn't find any boost asio tutorial online without sockects. There are some similar questions, but those all use sockets. Any idea how to implement my own IO object? Or am I missing any important obstacles? Thank you very much!

Geier
  • 894
  • 7
  • 16
  • 1
    Sadly [the online book I was going to link you to](http://theboostcpplibraries.com/boost.asio) seems to have removed its section about making an Asio Extension. :( The [_old TOC_](http://en.highscore.de/cpp/boost/toc.html) still refers the chapter. – sehe Jul 07 '15 at 23:35
  • 1
    See here too though: [How to design a custom IO object for Boost Asio](http://stackoverflow.com/questions/19633085/how-to-design-a-custom-io-object-for-boost-asio) – sehe Jul 07 '15 at 23:35
  • 1
    http://stackoverflow.com/q/23887056 and http://stackoverflow.com/q/26562965 may provide some insight into the separation of responsibilities between the I/O object, its implementation, and its I/O service (not the `io_service`). For example, the I/O object implementation would often own the `concurrent_queue`. There are quite a few subtle details that need to be considered. Could you please expand on your `concurrent_queue`? For instance, is there a non-blocking pop (e.g. `try_pop()`) or a way to cancel the `wait_and_pop()` operation? Any way to query the queue's size? – Tanner Sansbury Jul 08 '15 at 04:26
  • @sehe Thank you for your answer. I found the stackoverflow link too, but when implementing the ssl socket, It still uses `boost::asio::ssl::stream ssl_socket_;`. Or does the Connection class have everything necessary to use as an IO object? Thanks. – Lőrincz Tamás Jul 08 '15 at 07:25
  • @TannerSansbury Thanks! I am checking these links. The concurrent_queue is just a wrapper around an std::queue as you can see here: [concurrent_queue](http://pastebin.com/wqik9A6k) – Lőrincz Tamás Jul 08 '15 at 07:35
  • Does the `concurrent_queue` need to be exposed to the user, or would an object with a `push()` and `async_pop()` functionality suffice? This distinction could affect the I/O service implementation. – Tanner Sansbury Jul 09 '15 at 03:07
  • @TannerSansbury I should've give you the context of this, sorry. We are currently in the proof of concept stage in the development. I don't know if you are familiar with cordova or not, but here is a quick overview of our concept. The ui is going to be implemented in js (ts), html, css, and the backend will be written in c++. When the device is ready, it loads the backend in a new c++ thread. Cordova gives us the ability to call java functions from the ui, than through JNI it is possible to call a c++ code. – Lőrincz Tamás Jul 09 '15 at 07:04
  • @TannerSansbury So when, the device is ready, I use an async cordova call, to call a java function, which calls the c++ code, which creates a new thread for the backend. This thread is alive during the app is running. I use another cordova async call to push messages (from the ui) to the backend's queue. Yet the queue is a singleton. I would like to use the boost asio to process these messages async. (The backend to ui communication is working, I implemented that with long polling.) – Lőrincz Tamás Jul 09 '15 at 07:10
  • @TannerSansbury This link looks interesting (found it in your linked page): [link](http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/examples/cpp03_examples.html#boost_asio.examples.cpp03_examples.services) Thank you so much for your help. – Lőrincz Tamás Jul 09 '15 at 07:11
  • Glad the links are helpful. Given the lack of material on creating custom I/O objects, I will try to find some time over the next few days to answer this question. However, if I understand the problem correctly (needing a threadpool to asynchronously process messages outside of the UI thread), much simpler solutions exists. For example, one could post a functor with the bound message to the `io_service` itself (e.g. the `io_service` now functions as-if it was the `concurrent_queue`), or posting a handler to process the `concurrent_queue` after pushing to `concurrent_queue`. – Tanner Sansbury Jul 09 '15 at 14:42
  • Is it possible to post a function with a bound message to a `boost::asio::io_service` "directly"? That could work, because I can do that every time I push something to the queue. I didn't think of this, but sounds easier than dealing with my own io services. Thanks @TannerSansbury – Lőrincz Tamás Jul 10 '15 at 06:27

0 Answers0