{"title":"funcptr - cpp","subtitle":"An engineer's technical notebook","link":[{"@attributes":{"rel":"self","type":"application\/atom+xml","href":"https:\/\/funcptr.net\/language\/cpp\/atom.xml"}},{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/"}}],"generator":"Zola","updated":"2014-06-10T17:32:03+00:00","id":"https:\/\/funcptr.net\/language\/cpp\/atom.xml","entry":[{"title":"Build numbers in binaries using waf","published":"2014-06-10T17:32:03+00:00","updated":"2014-06-10T17:32:03+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2014\/build-numbers-in-binaries-using-waf\/"}},"id":"https:\/\/funcptr.net\/2014\/build-numbers-in-binaries-using-waf\/","content":"<p>My build system of choice these days for any C++ project is <a rel=\"external\" href=\"https:\/\/waf.io\">waf<\/a>. One of\nthe things I always like havig is the build number included in the final\nbinary, so that with a simple <code>.\/binary --version<\/code> or even <code>.\/binary<\/code> the\nversion is printed that it was built from. This can make it much simpler to\ndebug any potential issues, especially if fixes may have already been made but\na bad binary was deployed.<\/p>\n<h2 id=\"setup-the-wscript\">Setup the wscript<\/h2>\n<p>Make sure that your wscript somewhere near the top contains the following:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #82AAFF;\">APPNAME<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">myapp<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;\">VERSION<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">0.0.0<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><\/span><\/code><\/pre>\n<p>Then in your <code>configure(cfg)<\/code> add the following:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span>cfg.env.<\/span><span style=\"color: #82AAFF;\">VERSION<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;\"> VERSION<\/span><\/span>\n<span class=\"giallo-l\"><span>cfg.env.<\/span><span style=\"color: #82AAFF;\">APPNAME<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;\"> APPNAME<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>git_version<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #B2CCD6;\"> try_git_version<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> git_version:<\/span><\/span>\n<span class=\"giallo-l\"><span>    cfg.env.<\/span><span style=\"color: #82AAFF;\">VERSION<\/span><span style=\"color: #C792EA;\"> +=<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">-<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #C792EA;\"> +<\/span><span> git_version<\/span><\/span><\/code><\/pre>\n<p>The <code>try_git_version()<\/code> function is fairly simple and looks like this:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> try_git_version<\/span><span style=\"color: #D9F5DD;\">()<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    import<\/span><span> os<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    import<\/span><span> sys<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>    version<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #FF5874;\"> None<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    try<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span>        version<\/span><span style=\"color: #C792EA;\"> =<\/span><span> os.<\/span><span style=\"color: #B2CCD6;\">popen<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">git describe --always --dirty --long<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>).<\/span><span style=\"color: #B2CCD6;\">read<\/span><span>().<\/span><span style=\"color: #B2CCD6;\">strip<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    except<\/span><span style=\"color: #C5E478;\"> Exception<\/span><span style=\"color: #C792EA;font-style: italic;\"> as<\/span><span> e:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">        print<\/span><span> e<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    return<\/span><span> version<\/span><\/span><\/code><\/pre>\n<p>It runs <code>git describe --always --dirty --long<\/code> which will return something\nalong these lines: <code>401b85f-dirty<\/code>. If you have any annoted tags, it will\nreturn the tag name as well.<\/p>\n<p>If <code>git<\/code> is not installed, or it is not a valid <code>git<\/code> directory, then it will\nsimply return <code>None<\/code>. At that point all we have to go on is the <code>VERSION<\/code>\nvariable set at the top of the wscript.<\/p>\n<p>Now that we have our configuration environment set up with the <code>VERSION<\/code> we\nwant to get that into a file that we can then include in our C++ source code.<\/p>\n<h2 id=\"create-a-build-version-h-in-file\">Create a <code>build_version.h.in<\/code> file<\/h2>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">ifndef<\/span><span style=\"color: #7FDBCA;font-style: italic;\"> BUILD_VERSION_H_IN_941AD1F24D0A9D<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">define<\/span><span style=\"color: #7FDBCA;font-style: italic;\"> BUILD_VERSION_H_IN_941AD1F24D0A9D<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">char<\/span><span style=\"color: #C5E478;\"> VERSION<\/span><span>[]<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">@VERSION@<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">endif<\/span><span style=\"color: #637777;font-style: italic;\"> \/* BUILD_VERSION_H_IN_941AD1F24D0A9D *\/<\/span><\/span><\/code><\/pre><h2 id=\"add-the-following-to-build-ctx\">Add the following to <code>build(ctx)<\/code><\/h2>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #B2CCD6;\">ctx<\/span><span>(<\/span><span style=\"color: #D7DBE0;\">features<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">subst<\/span><span style=\"color: #D9F5DD;\">&#39;,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        source<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">build_version.h.in<\/span><span style=\"color: #D9F5DD;\">&#39;,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        target<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">build_version.h<\/span><span style=\"color: #D9F5DD;\">&#39;,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        VERSION<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;\"> ctx.env<\/span><span>[<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">VERSION<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>]<\/span><span style=\"color: #D9F5DD;\">,<\/span><\/span>\n<span class=\"giallo-l\"><span>        )<\/span><\/span><\/code><\/pre>\n<p>This uses the substitution feature to transform <code>build_version.h.in<\/code> into\n<code>build_version.h<\/code>, while inserting the version into the file.<\/p>\n<h2 id=\"include-build-version-h-in-your-source-code\">Include <code>build_version.h<\/code> in your source code<\/h2>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">build_version.h<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><\/span><\/code><\/pre>\n<p>And add something along these lines to your <code>main()<\/code>:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span>std::<\/span><span style=\"color: #C5E478;\">cerr<\/span><span style=\"color: #C792EA;\"> &lt;&lt;<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">Version: <\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #C792EA;\"> &lt;&lt;<\/span><span style=\"color: #C5E478;\"> VERSION<\/span><span style=\"color: #C792EA;\"> &lt;&lt;<\/span><span> std::<\/span><span style=\"color: #C5E478;\">endl<\/span><span>;<\/span><\/span><\/code><\/pre>\n<p>This will print out the <code>VERSION<\/code> that has been stored in <code>build_version.h<\/code>.<\/p>\n<h2 id=\"full-example\">Full example<\/h2>\n<p>Check out my <a rel=\"external\" href=\"https:\/\/github.com\/digitalresistor\/mdns-announce\">mdns-announce<\/a> project on Github for an example of how this is\nimplemented.<\/p>\n"},{"title":"Embedding ZeroMQ in the libev Event Loop","published":"2013-04-20T18:22:00+00:00","updated":"2013-04-20T18:22:00+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2013\/embedding-zeromq-in-the-libev-event-loop\/"}},"id":"https:\/\/funcptr.net\/2013\/embedding-zeromq-in-the-libev-event-loop\/","content":"<p>In a <a href=\"\/2012\/zeromq-edge-triggered\/\">previous article on ZeroMQ<\/a> we went over how ZeroMQ is triggered when\nyou use the socket that ZeroMQ returns, in that article there was some\ndiscussion of embedding ZeroMQ into another event loop. Let's do that.<\/p>\n<p><a rel=\"external\" href=\"http:\/\/software.schmorp.de\/pkg\/libev.html\">libev<\/a> is an absolutely fantastic library that helps make it easy to write\nevented programs. Evented programs work by getting notified that an action has\nhappened, and acting upon it. Unlike threaded where multiple pieces of work are\nbeing executed at the same time, in an evented system you move every item that\ncould block to an event loop, that then calls back into user code with a\nnotification to continue. If one event uses up more than its fair share of CPU\ntime because it is busy doing a long calculation, every single other event that\nis waiting will never get notified.<\/p>\n<p>Now, as previously discussed ZeroMQ is edge triggered, so embedding it into an\nevent loop that is level triggered doesn't do us much good, because we will miss\ncertain ZeroMQ notifications.<\/p>\n<p>One way to solve this problem is by looping over ZeroMQ's event system until we\nget back a notification that it no longer has anything else for us to process,\nthat would look something like this<sup class=\"footnote-reference\" id=\"fr-20130420zeromqedge-1\"><a href=\"https:\/\/funcptr.net\/2013\/embedding-zeromq-in-the-libev-event-loop\/#fn-20130420zeromqedge\">[1]<\/a><\/sup>:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #C5E478;\"> zevents<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">size_t<\/span><span style=\"color: #C5E478;\"> zevents_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">do<\/span><span> {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ We can read from the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><span style=\"color: #C792EA;font-style: italic;\"> else<\/span><span> {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        break<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Check to see if there is more to read ...<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><span style=\"color: #C792EA;font-style: italic;\"> while<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLOUT<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ We can write to the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ If neither of the above is true, then it was a false positive<\/span><\/span><\/code><\/pre>\n<p>However if we are receiving information from ZeroMQ remote endpoints faster\nthan we can process them, we end up being stuck in that <code>do<\/code> ... <code>while<\/code> loop\nforever. If we have other events we want to process, that isn't entirely fair\nsince they will never ever get called again. Especially in a server application\nwhere it may be servicing thousands of clients this is simply not acceptable.<\/p>\n<h2 id=\"libev\">libev<\/h2>\n<p>libev provides various different event notifications, to be able to get around\nedge triggered notifications, and still provide fair round-robin for all events\nwe are going to have to build on top of multiple different events.<\/p>\n<p>The events used will be:<\/p>\n<ul>\n<li><a rel=\"external\" href=\"http:\/\/pod.tst.eu\/http:\/\/cvs.schmorp.de\/libev\/ev.pod#code_ev_io_code_is_this_file_descrip\"><code>ev::io<\/code><\/a>: This one is pretty self explanatory, this is for getting notified\nabout input output changes. This is the one we are going to use on the\n<code>ZMQ_FD<\/code>.<\/li>\n<li><a rel=\"external\" href=\"http:\/\/pod.tst.eu\/http:\/\/cvs.schmorp.de\/libev\/ev.pod#code_ev_prepare_code_and_code_ev_che\"><code>ev::prepare<\/code><\/a> and <a rel=\"external\" href=\"http:\/\/pod.tst.eu\/http:\/\/cvs.schmorp.de\/libev\/ev.pod#code_ev_prepare_code_and_code_ev_che\"><code>ev::check<\/code><\/a>: These two are generally used together, they\ncan be used to change the event loop and or make modifications on the fly to\nevents that have been registered with the event loop.<\/li>\n<li><a rel=\"external\" href=\"http:\/\/pod.tst.eu\/http:\/\/cvs.schmorp.de\/libev\/ev.pod#code_ev_idle_code_when_you_ve_got_no\"><code>ev::idle<\/code><\/a>: This is an event that gets fired whenever the event loop has\nnothing else to do, so no other events fired, this will fire.<\/li>\n<\/ul>\n<h2 id=\"plan-of-attack\">Plan of attack<\/h2>\n<p>Since the <code>prepare<\/code> and <code>check<\/code> run before and after the loop, we are going to\nbe using those to do most of the work. We use an <code>io<\/code> so that we can turn off\nthe <code>idle<\/code> when we can actually wait for a result from ZeroMQ's file\ndescriptor, otherwise we use <code>idle<\/code> so that we will always get called once\nevery loop.<\/p>\n<p>In the <code>prepare<\/code> watcher callback we do the following:<\/p>\n<ol>\n<li>Check to see what events ZeroMQ has for us, and check what events the user\nhas requested.<\/li>\n<li>If the ZeroMQ has an event for us that we want, and the user has requested\nthat event, we start the idle watcher.<\/li>\n<li>If ZeroMQ has no events, we start the io watcher.<\/li>\n<\/ol>\n<p>In the <code>check<\/code> watcher callback we do the following:<\/p>\n<ol>\n<li>Stop both the <code>io<\/code> and <code>idle<\/code> watchers, they were only there to make sure\nthat our <code>check<\/code> watcher was called.<\/li>\n<li>See what event ZeroMQ has for us, and check that against what the user\nwants. Depending on the event, call user defined function write() or user\ndefined function read().<\/li>\n<li>If this was a spurious wake-up on the part of ZeroMQ we simply ignore it\nand let libev go on to other events.<\/li>\n<\/ol>\n<p>We could make all of this work by simply using the <code>prepare<\/code>, <code>check<\/code> and\n<code>idle<\/code> watchers, but that would mean libev would be busy-waiting for something\nto happen on the ZeroMQ socket. The <code>io<\/code> watcher is required simply so in times\nof nothing happening libev in its library can call into the kernels event\nhandling mechanism and go to sleep. We can't use just the <code>io<\/code> watcher due to\nthe edge-triggered notification, because we'd miss all kinds of ZeroMQ\nmessages. So all four watchers are required, and play crucial parts in making\nthis work.<\/p>\n<h2 id=\"let-s-get-down-to-code\">Let's get down to code<\/h2>\n<p>Below you will find example code, it is not complete. Do note that I am using\nsome C++11isms, error checking code may not be complete\/correct and in general\nI don't suggest you copy and paste this without reading and understanding what\nit does.<\/p>\n<p>The <code>zmq_event<\/code> class is meant to be used as a base class, inherit from it, and\ncreate the <code>write()<\/code> and <code>read()<\/code> functions. These functions will be called\nwhen you are able to read from the ZeroMQ socket, or when you are able to write\nto the ZeroMQ socket. You are guaranteed to be able to read one whole ZeroMQ\nmessage, so if it is a multi-part message, do make sure to loop on\n<code>ZMQ_SNDMORE<\/code> as required.<\/p>\n<p>Upon instantiation it will automatically start being notified about events, we\nstart off with <code>ev::READ<\/code>. When your sub-class wants to write to ZeroMQ it\nshould put the messages to be written into a list somewhere, and set\n<code>ev::READ | ev::WRITE<\/code> on <code>watcher_io<\/code>, by calling\n<code>watcher_io.set(socket_fd, ev::READ | ev::WRITE)<\/code>. <code>write()<\/code> will then be\ncalled, write a single message to ZeroMQ, and if necessary when finished\nwriting, unset <code>ev::WRITE<\/code> using <code>watcher_io.set(socket_fd, ev::READ)<\/code>. If you\nare not finished writing, after writing that singular message you may return\nand <code>write()<\/code> will be called again the next loop iteration. This way if you\nhave a lot of data to write you don't starve the other events from receiving\ntheir notifications.<\/p>\n<h4 id=\"zmq-event-h\"><code>zmq_event.h<\/code><\/h4>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">string<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">ev++.h<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">zmq.hpp<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">class<\/span><span> zmq_event {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    public:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">        zmq_event<\/span><span>(zmq::<\/span><span style=\"color: #C5E478;\">context_t<\/span><span style=\"color: #C792EA;font-style: italic;\">&amp;<\/span><span style=\"color: #D7DBE0;\"> context<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #D7DBE0;\"> type<\/span><span>,<\/span><span style=\"color: #C792EA;font-style: italic;\"> const<\/span><span> std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">&amp;<\/span><span style=\"color: #D7DBE0;\"> connect<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        virtual<\/span><span style=\"color: #82AAFF;font-style: italic;\"> ~zmq_event<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    protected:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ This gets fired before the event loop, to prepare<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> before<\/span><span>(ev::prepare<\/span><span style=\"color: #C792EA;font-style: italic;\">&amp;<\/span><span style=\"color: #D7DBE0;\"> prep<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #D7DBE0;\"> revents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ This is fired after the event loop, but before any other type of events<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> after<\/span><span>(ev::check<\/span><span style=\"color: #C792EA;font-style: italic;\">&amp;<\/span><span style=\"color: #D7DBE0;\"> check<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #D7DBE0;\"> revents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ We need to have a no-op function available for those events that we<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ want to add to the list, but should never fire an actual event<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        template<\/span><span> &lt;<\/span><span style=\"color: #C792EA;\">typename<\/span><span> T&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">            inline<\/span><span style=\"color: #C792EA;\"> void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> noop<\/span><span>(T<\/span><span style=\"color: #C792EA;font-style: italic;\">&amp;<\/span><span style=\"color: #D7DBE0;\"> w<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #D7DBE0;\"> revents<\/span><span>) {};<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ Function we are going to call to write to the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        virtual<\/span><span style=\"color: #C792EA;\"> void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> write<\/span><span>()<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ Function we are going to call to read from the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        virtual<\/span><span style=\"color: #C792EA;\"> void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> read<\/span><span>()<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ Some helper function, one to start notifications<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> start_notify<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ And one to stop notifications.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> stop_notify<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ Our event types<\/span><\/span>\n<span class=\"giallo-l\"><span>        ev::io<\/span><span style=\"color: #C5E478;\">      watcher_io<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        ev::prepare<\/span><span style=\"color: #C5E478;\"> watcher_prepare<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        ev::check<\/span><span style=\"color: #C5E478;\">   watcher_check<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        ev::idle<\/span><span style=\"color: #C5E478;\">    watcher_idle<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ Our ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>        zmq::<\/span><span style=\"color: #C5E478;\">socket_t socket<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        int<\/span><span style=\"color: #C5E478;\">           socket_fd<\/span><span style=\"color: #C792EA;\"> = -<\/span><span style=\"color: #F78C6C;\">1<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>};<\/span><\/span><\/code><\/pre><h4 id=\"zmq-event-cc\"><code>zmq_event.cc<\/code><\/h4>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">stdexcept<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">zmq_event.h<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #82AAFF;font-style: italic;\">zmq_event<\/span><span>(zmq::<\/span><span style=\"color: #C5E478;\">context_t<\/span><span style=\"color: #C792EA;\">&amp;<\/span><span style=\"color: #C5E478;\"> context<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #C5E478;\"> type<\/span><span>,<\/span><span style=\"color: #C792EA;font-style: italic;\"> const<\/span><span> std::<\/span><span style=\"color: #C5E478;\">string<\/span><span style=\"color: #C792EA;\">&amp;<\/span><span style=\"color: #C5E478;\"> connect<\/span><span>) :<\/span><span style=\"color: #82AAFF;font-style: italic;\"> socket<\/span><span>(<\/span><span style=\"color: #C5E478;\">context<\/span><span>,<\/span><span style=\"color: #C5E478;\"> type<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Get the file descriptor for the socket<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    size_t<\/span><span style=\"color: #C5E478;\"> fd_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">_socket_fd<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_FD<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">socket_fd<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">fd_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Actually connect to the ZeroMQ endpoint, could replace this with a bind as well ...<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">bind<\/span><span>(<\/span><span style=\"color: #C5E478;\">connect<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">c_str<\/span><span>());<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Set up all of our watchers<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Have our IO watcher check for READ on the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_io<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">set<\/span><span>(<\/span><span style=\"color: #C5E478;\">socket_fd<\/span><span>, ev::<\/span><span style=\"color: #C5E478;\">READ<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ This watcher has a no-op callback<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_io<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">set<\/span><span style=\"color: #C792EA;\">&lt;<\/span><span style=\"color: #C5E478;\">zmq_event<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span>zmq_event::<\/span><span style=\"color: #C5E478;\">noop<\/span><span style=\"color: #C792EA;\">&gt;<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">this<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Set up our prepare watcher to call the before() function<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_prepare<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">set<\/span><span style=\"color: #C792EA;\">&lt;<\/span><span style=\"color: #C5E478;\">zmq_event<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span>zmq_event::<\/span><span style=\"color: #C5E478;\">before<\/span><span style=\"color: #C792EA;\">&gt;<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">this<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Set up the check watcher to call the after() function<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_check<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">set<\/span><span style=\"color: #C792EA;\">&lt;<\/span><span style=\"color: #C5E478;\">zmq_event<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span>zmq_event::<\/span><span style=\"color: #C5E478;\">after<\/span><span style=\"color: #C792EA;\">&gt;<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">this<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Set up our idle watcher, once again a no-op<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_idle<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">set<\/span><span style=\"color: #C792EA;\">&lt;<\/span><span style=\"color: #C5E478;\">zmq_event<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span>zmq_event::<\/span><span style=\"color: #C5E478;\">noop<\/span><span style=\"color: #C792EA;\">&gt;<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">this<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Tell libev to start notifying us!<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">    start_notify<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #C792EA;\">~<\/span><span style=\"color: #82AAFF;font-style: italic;\">zmq_event<\/span><span>() {}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #82AAFF;font-style: italic;\">before<\/span><span>(ev::<\/span><span style=\"color: #C5E478;\">prepare<\/span><span style=\"color: #C792EA;\">&amp;<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #C5E478;\"> revents<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">EV_ERROR<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> revents<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        throw<\/span><span> std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">runtime_error<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">libev error<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Get any events that may be waiting<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    uint32_t<\/span><span style=\"color: #C5E478;\"> zevents<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    size_t<\/span><span style=\"color: #C5E478;\"> zevents_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Lucky for us, getting the events available doesn&#39;t invalidate the<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ events, so that calling this in `before()` and in `after()` will<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ give us the same results.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Check what events exists, and check it against what event we want. We<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ &quot;abuse&quot; our watcher_io.events for this information.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> ((<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLOUT<\/span><span>)<\/span><span style=\"color: #C792EA;\"> &amp;&amp;<\/span><span> (<\/span><span style=\"color: #C5E478;\">watcher_io<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">events<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span> ev::<\/span><span style=\"color: #C5E478;\">WRITE<\/span><span>)) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">        watcher_idle<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> ((<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>)<\/span><span style=\"color: #C792EA;\"> &amp;&amp;<\/span><span> (<\/span><span style=\"color: #C5E478;\">watcher_io<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">events<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span> ev::<\/span><span style=\"color: #C5E478;\">READ<\/span><span>)) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">        watcher_idle<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ No events ready to be processed, we&#39;ll just go watch some io<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_io<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #82AAFF;font-style: italic;\">after<\/span><span>(ev::<\/span><span style=\"color: #C5E478;\">check<\/span><span style=\"color: #C792EA;\">&amp;<\/span><span>,<\/span><span style=\"color: #C792EA;\"> int<\/span><span style=\"color: #C5E478;\"> revents<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">EV_ERROR<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> revents<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        throw<\/span><span> std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">runtime_error<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">libev error<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Stop both the idle and the io watcher, no point in calling the no-op callback<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ One of them will be reactived by before() on the next loop<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_idle<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">stop<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_io<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">stop<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Get the events<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    uint32_t<\/span><span style=\"color: #C5E478;\"> zevents<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    size_t<\/span><span style=\"color: #C5E478;\"> zevents_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Check the events and call the users read\/write function<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> ((<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>)<\/span><span style=\"color: #C792EA;\"> &amp;&amp;<\/span><span> (<\/span><span style=\"color: #C5E478;\">watcher_io<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">events<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span> ev::<\/span><span style=\"color: #C5E478;\">READ<\/span><span>)) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">        this<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">read<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> ((<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLOUT<\/span><span>)<\/span><span style=\"color: #C792EA;\"> &amp;&amp;<\/span><span> (<\/span><span style=\"color: #C5E478;\">watcher_io<\/span><span>.<\/span><span style=\"color: #BAEBE2;\">events<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span> ev::<\/span><span style=\"color: #C5E478;\">WRITE<\/span><span>)) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #7FDBCA;\">        this<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">write<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #82AAFF;font-style: italic;\">start_notify<\/span><span>() {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_check<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_prepare<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">start<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>zmq_event::<\/span><span style=\"color: #82AAFF;font-style: italic;\">stop_notify<\/span><span>() {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_check<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">stop<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    watcher_prepare<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">stop<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span><\/code><\/pre><h2 id=\"other-event-loops\">Other event loops<\/h2>\n<p>libev is but just one of many event loops that exist out there, hopefully\nthis shows how it is possible to embed ZeroMQ into an event loop, thereby\nmaking it easier to embed ZeroMQ into any other event loops.<\/p>\n<section class=\"footnotes\">\n<ol class=\"footnotes-list\">\n<li id=\"fn-20130420zeromqedge\">\n<p>This snippet was from my older article regarding\n<a href=\"\/2012\/zeromq-edge-triggered\/\">ZeroMQ edge triggered notifications<\/a>. I would highly suggest reading that\narticle for more information and even more background on what is going on. <a href=\"https:\/\/funcptr.net\/2013\/embedding-zeromq-in-the-libev-event-loop\/#fr-20130420zeromqedge-1\">\u21a9<\/a><\/p>\n<\/li>\n<\/ol>\n<\/section>\n"},{"title":"ZeroMQ - Edge Triggered Notification","published":"2012-09-10T19:01:50+00:00","updated":"2012-09-10T19:01:50+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2012\/zeromq-edge-triggered\/"}},"id":"https:\/\/funcptr.net\/2012\/zeromq-edge-triggered\/","content":"<p><a rel=\"external\" href=\"http:\/\/www.zeromq.org\">ZeroMQ<\/a> is an absolutely fantastic communications library that has allowed\nme to very easily create networks\/connections between multiple different\napplications to communicate in an extremely fast yet directed manner and allow\nscaling of the connected entities with almost no second thought.<\/p>\n<p>I have used ZeroMQ within Python and C++, and it has been fantastic leaving\nthat part of the communication layer to a library rather than writing something\nsimilar myself. However, embedding ZeroMQ into an another event loop has proven\nto have some quirks. They are documented in the API, but not so much by other\nusers\/examples found around the web, and caught me by surprise.<\/p>\n<p>This post mainly goes over the issue, the difference between edge and level\ntriggered and how ZeroMQ works. I am hoping to spend some time documenting how\nto embed ZeroMQ correctly inside <a rel=\"external\" href=\"http:\/\/software.schmorp.de\/pkg\/libev.html\">libev<\/a> at a later date.<\/p>\n<h2 id=\"the-quirk\">The quirk<\/h2>\n<p>ZeroMQ allows you to get an underlying file descriptor (<code>ZMQ_FD<\/code>) for a ZeroMQ\nsocket using <a rel=\"external\" href=\"http:\/\/api.zeromq.org\/2-2:zmq-getsockopt#toc21\"><code>getsockopt()<\/code><\/a>, this is not the actual ZeroMQ file descriptor\nbut a stand-in:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #C5E478;\"> fd<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">size_t<\/span><span style=\"color: #C5E478;\"> fd_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">fd<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_FD<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">fd<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">fd_len<\/span><span>);<\/span><\/span><\/code><\/pre>\n<p>This file descriptor can then be passed on to <code>poll()<\/code>, <code>select()<\/code>, <code>kqueue()<\/code>\nor whatever your event notification library of choice may be, the idea being\nthat if the file descriptor is notified to be available for reading then you\ncheck the sockets <a rel=\"external\" href=\"http:\/\/api.zeromq.org\/2-2:zmq-getsockopt#toc22\"><code>ZMQ_EVENTS<\/code><\/a> and find out if you are allowed to read or\nwrite to the ZeroMQ socket, or even possibly neither (false positive). The same\nfile descriptor is also used internally within the ZeroMQ library for\nnotification and thus we may receive an event notification that we can't do\nanything with.<\/p>\n<p>As stated in the <a rel=\"external\" href=\"http:\/\/api.zeromq.org\/2-2:zmq-getsockopt#toc21\">ZeroMQ API guide for <code>getsockopt()<\/code> under <code>ZMQ_FD<\/code><\/a>:<\/p>\n<blockquote>\n<p>the \u00d8MQ library shall signal any pending events on the socket in an\n<em>edge-triggered<\/em> fashion by making the file descriptor become ready for\nreading.<\/p>\n<\/blockquote>\n<p>and further it states:<\/p>\n<blockquote>\n<p>The ability to read from the returned file descriptor does not necessarily\nindicate that messages are available to be read from, or can be written to,\nthe underlying socket; applications must retrieve the actual event state with\na subsequent retrieval of the <code>ZMQ_EVENTS<\/code> option.<\/p>\n<\/blockquote>\n<p>So far so good, so after we return from our event notification on the file\ndescriptor we simply ask ZeroMQ whether or not we can read or write (or\npossibly both):<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #C5E478;\"> zevents<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">size_t<\/span><span style=\"color: #C5E478;\"> zevents_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ We can read from the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLOUT<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ We can write to the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ If neither of the above is true, then it was a false positive<\/span><\/span><\/code><\/pre>\n<p>So now you are happily reading from ZeroMQ and using the data as you wish,\nexcept you start to notice that sometimes it can take a little while for data\nto show up, or that if the end-point you are connected to on ZeroMQ sends a lot\nof data it is not delivered in a timely fashion. This is where the\ndocumentation for edge triggered comes into play, and that is something that is\ndifferent about ZeroMQ compared to for example BSD sockets.<\/p>\n<h2 id=\"edge-triggered-versus-level-triggered\">Edge triggered versus level triggered<\/h2>\n<p>Edge and level triggered refer to what kind of signal a device will output in\nan attempt to gain the attention of another device. Edge and level triggered\nhave their history in electronics and hardware, where they play a very crucial\nrole.<\/p>\n<p>For both level and edge triggered assume the data is represented by this simple string:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>00000000000111111111111111111111100000000011111111111000000000<\/span><\/span><\/code><\/pre>\n<p>The zeroes (0) are where no data is available, and the ones (1) are where data\nis available for processing.<\/p>\n<h3 id=\"level-triggered\">Level triggered<\/h3>\n<p>Level triggered devices when they require attention, and until they no longer\nrequire attention will signal with a high voltage (going back to early\nelectronics), and when they no longer require attention signal with a low\nvoltage.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>Data:  00000000001111111111111111111111000000000011111111111000000000<\/span><\/span>\n<span class=\"giallo-l\"><span>Block:      A               B              C         D<\/span><\/span>\n<span class=\"giallo-l\"><span>+ 5v              ____________________           _________<\/span><\/span>\n<span class=\"giallo-l\"><span>  -    __________|                    |_________|         |__________<\/span><\/span>\n<span class=\"giallo-l\"><span>  0v<\/span><\/span>\n<span class=\"giallo-l\"><span>State: 0          1                    0          1        0<\/span><\/span><\/code><\/pre>\n<p>At this point, once the signal turns to 1 in block B we can start processing\ndata, and until we are done processing data the signal will stay 1. Once we are\ndone processing the device will signal that we are done by setting the state to\n0 in block C, and the process can repeat itself with block D.<\/p>\n<h3 id=\"edge-triggered\">Edge triggered<\/h3>\n<p>Edge triggered devices when they require attention will simply turn on the\nsignal for a short period of time and then turn it back off until all of the\nrequireding processing is complete at which point they will once again signal\nthat they need processing.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>Data:  00000000001111111111111111111111100000000011111111111000000000<\/span><\/span>\n<span class=\"giallo-l\"><span>Block:      A     B              C                 D<\/span><\/span>\n<span class=\"giallo-l\"><span>+ 5v              __                              __<\/span><\/span>\n<span class=\"giallo-l\"><span>  -    __________|  |____________________________|  |________________<\/span><\/span>\n<span class=\"giallo-l\"><span>  0v<\/span><\/span>\n<span class=\"giallo-l\"><span>State: 0          1              0                1        0<\/span><\/span><\/code><\/pre>\n<p>In this case when the trigger arrives you have to process all of the data\navailable. When block B arrives and we get 1 as our signal, we have to process\nall of the data, we won't know when we are done other than some other\nnotification scheme by the device. (Such as a special message that says we are\ndone). Then when we are done we can go back to listening to waiting for a\ntrigger.<\/p>\n<p>If however in the time that we are processing the data from the trigger at B\nreceive more data to process we won't get notified because we weren't\nofficially done processing data from the device, and thus the device will not\nnotify us of the new data again until we have processed all of the data.<\/p>\n<p>There are some devices that as soon as you start processing their data, you\nre-enable the trigger, so if you receive new data while still processing the\nold you will get a new notification.<\/p>\n<h2 id=\"zeromq-uses-edge-triggered\">ZeroMQ uses edge triggered<\/h2>\n<p>When multiple message arrive for the ZeroMQ socket, one notification is sent.\nYou check to see if the <code>ZMQ_EVENTS<\/code> actually contains <code>ZMQ_POLLIN<\/code> and then\nread a single message from ZeroMQ. Content that you have processed the data\nZeroMQ has for you, you add the file descriptor back to the event notification\nlibrary and wait. However, you know you sent two messages, but all you got was\none. The next time you send data to the socket, it returns the second message,\nand you have now send three but the third doesn't seem to arrive.<\/p>\n<p>This is where the edge triggered comes in. ZeroMQ only notifies you once, and\nafter it has notified you it won't notify you again until it receives new\nmessages, <em>EVEN<\/em> if you have messages waiting for you. In BSD sockets when you\n<code>recv()<\/code> data from the socket, if it is not all of the data it will simply tell\nyou, and until all of the data has been read that the socket has to offer it is\ngoing to tell you (level triggered).<\/p>\n<p>This means that when you get a notification from ZeroMQ you have to use a loop\nto process all of the messages that are coming your way:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #C5E478;\"> zevents<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">size_t<\/span><span style=\"color: #C5E478;\"> zevents_len<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> sizeof<\/span><span>(<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">do<\/span><span> {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        \/\/ We can read from the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><span style=\"color: #C792EA;font-style: italic;\"> else<\/span><span> {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        break<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ Check to see if there is more to read ...<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">    zmq_socket<\/span><span>.<\/span><span style=\"color: #82AAFF;font-style: italic;\">getsockopt<\/span><span>(<\/span><span style=\"color: #C5E478;\">ZMQ_EVENTS<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents<\/span><span>,<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\">zevents_len<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><span style=\"color: #C792EA;font-style: italic;\"> while<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLIN<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> (<\/span><span style=\"color: #C5E478;\">zevents<\/span><span style=\"color: #C792EA;\"> &amp;<\/span><span style=\"color: #C5E478;\"> ZMQ_POLLOUT<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">    \/\/ We can write to the ZeroMQ socket<\/span><\/span>\n<span class=\"giallo-l\"><span>}<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ If neither of the above is true, then it was a false positive<\/span><\/span><\/code><\/pre>\n<p>The same is true if you need to send multiple messages, you will need to check\n<code>ZMQ_EVENTS<\/code> for <code>ZMQ_POLLOUT<\/code> and send messages so long as that holds true, as\nsoon as it is no longer true you will need to wait until you get triggered\nagain by using the file descriptor in your event notification library.<\/p>\n<h2 id=\"embedding-or-co-existing-with-another-event-loop\">Embedding or co-existing with another event loop<\/h2>\n<p>Embedding ZeroMQ into a different event loop becomes more difficult when you\nrealise that it is edge triggered versus level triggered. If you want to allow\nfair processing of data between events it becomes harder to accomplish with\nZeroMQ because you are required to process all of the data before going back to\nyour event loop or requires the setting up of different types of events\ndepending on the state that the ZeroMQ socket is in to make sure you process\nall of the data and don't forget any.<\/p>\n<p>Edge triggered for the ZeroMQ file descriptor was a design decision which\nunfortunately made the use of ZeroMQ sockets more difficult and different from\nBSD sockets which makes it harder for application programmers to do the right\nthing. There are plenty of questions on the internet regarding the proper usage\nof <code>ZMQ_FD<\/code> with various different event notification schemes\/implementations.<\/p>\n"},{"title":"OpenSSL as a Filter (or non-blocking OpenSSL)","published":"2012-04-08T16:50:14+00:00","updated":"2012-04-08T16:50:14+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2012\/openssl-as-a-filter\/"}},"id":"https:\/\/funcptr.net\/2012\/openssl-as-a-filter\/","content":"<p><a rel=\"external\" href=\"http:\/\/openssl.org\/\">OpenSSL<\/a> as a library at first glance is complicated, and then you realise\nthat a lot of the <a rel=\"external\" title=\"ssl(3) documentation\" href=\"http:\/\/openssl.org\/docs\/ssl\/ssl.html\">documentation<\/a> seems to be incomplete or missing.\nGenerally the individual man pages for the various functions are not bad, and\nthey will give you relevant information to help you along, but it can be hard\nto find.<\/p>\n<p>The idea is to write a filter in such a way that OpenSSL can easily be disabled\nor removed. We don't want to rely on OpenSSL specific functionality in our\ncode, for instance in the future we may want to change to using <a rel=\"external\" href=\"http:\/\/botan.randombit.net\/\">Botan<\/a> or\nanother SSL\/TLS library without having to change a lot of core functionality.<\/p>\n<h2 id=\"possible-methods\">Possible methods<\/h2>\n<p>There are various ways of doing non-blocking OpenSSL, the main one is to simply\nset the underlying socket to non-blocking and pass it into OpenSSL, at that\npoint the functions <code>SSL_write()<\/code> and <code>SSL_read()<\/code> will enter various error\nstates that can be read by <a rel=\"external\" href=\"http:\/\/www.openssl.org\/docs\/ssl\/SSL_get_error.html\"><code>SSL_get_error()<\/code><\/a>, more specifically the\nfunction will return either <code>SSL_ERROR_WANT_READ<\/code> or <code>SSL_ERROR_WANT_WRITE<\/code>.<\/p>\n<p>The other method, the one used below, was described by <a rel=\"external\" href=\"http:\/\/lists.schmorp.de\/pipermail\/libev\/2008q2\/000325.html\">Marc Lehmann in a post\nto the libev mailling list<\/a> and uses <a rel=\"external\" href=\"http:\/\/www.openssl.org\/docs\/crypto\/bio.html\">memory BIO<\/a> objects. Specifically\nhe linked to some Perl sample code; the OpenSSL <a rel=\"external\" href=\"http:\/\/ue.tst.eu\/f7383715351b4427b6b9db660b6289f8.txt\">setup<\/a>, and the function\ncalled for new incoming data\/data to be written: <a rel=\"external\" href=\"http:\/\/ue.tst.eu\/2f4cfee5166db411bd85a40e06de6dbf.txt\">dotls function<\/a>.<\/p>\n<blockquote>\n<p>The code is very new and might still be buggy, but it outlines the\nprinciples: use a memory stream, which will avoid all issues with blocking in\nopenssl<\/p>\n<\/blockquote>\n<p>It shows the idea pretty clearly but unless you know Perl it can be really\nconfusing as to what is going on.<\/p>\n<h2 id=\"the-ssl-filter-implementation\">The SSL Filter implementation<\/h2>\n<p>The <a rel=\"external\" href=\"http:\/\/www.openssl.org\/docs\/ssl\/SSL_CTX_new.html\"><code>SSL_CTX<\/code><\/a> that is passed in contains all of the initialisation that is\ndone for OpenSSL in general, which is not shown here. The <code>SSLFilter<\/code> is\ncreated for each time you want to have a socket start doing SSL.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">\/\/ openssl_filter.h<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">class<\/span><span> SSLFilter {<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    public:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">        SSLFilter<\/span><span>(SSL_CTX<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #D7DBE0;\"> ctxt<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span>                  std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #D7DBE0;\"> nread<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span>                  std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #D7DBE0;\"> nwrite<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span>                  std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #D7DBE0;\"> aread<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span>                  std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #D7DBE0;\"> awrite<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        virtual<\/span><span style=\"color: #82AAFF;font-style: italic;\"> ~SSLFilter<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        void<\/span><span style=\"color: #82AAFF;font-style: italic;\"> update<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    private:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        bool<\/span><span style=\"color: #82AAFF;font-style: italic;\"> continue_ssl_<\/span><span>(<\/span><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #D7DBE0;\"> function_return<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>        SSL<\/span><span style=\"color: #C792EA;font-style: italic;\"> *<\/span><span style=\"color: #C5E478;\"> ssl<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        BIO<\/span><span style=\"color: #C792EA;font-style: italic;\"> *<\/span><span style=\"color: #C5E478;\"> rbio<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        BIO<\/span><span style=\"color: #C792EA;font-style: italic;\"> *<\/span><span style=\"color: #C5E478;\"> wbio<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>        std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #C5E478;\"> nread<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #C5E478;\"> nwrite<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #C5E478;\"> aread<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>        std::string<\/span><span style=\"color: #C792EA;font-style: italic;\">*<\/span><span style=\"color: #C5E478;\"> awrite<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span>};<\/span><\/span><\/code><\/pre>\n<p>The class contains mainly various different bits of state, when you create the\nfilter you pass in a pointer to four different strings, they are used as\nfollows:<\/p>\n<ul>\n<li><code>nread<\/code>: This contains data to be processed by the filter, from the network<\/li>\n<li><code>nwrite<\/code>: This contains data that has been processed by the filter, and has\nto be sent to the network<\/li>\n<li><code>aread<\/code>: This contains data that has been processed by the filter, and is\nready to be processed by the application<\/li>\n<li><code>awrite<\/code>: This contains data that the application wants to send out to the\nnetwork and is ready for processing by the filter.<\/li>\n<\/ul>\n<p>The other private variables contain various different pieces of state and are\ncreated by the filters constructors.<\/p>\n<ul>\n<li><code>ssl<\/code>: Contains the state for the current SSL connection.<\/li>\n<li><code>rbio<\/code>: Contains the data that the SSL functions will read from, data put in\nhere is copied from <code>nread<\/code>.<\/li>\n<li><code>wbio<\/code>: Contains the data that the SSL functions will write to, data from\nthis BIO is copied to <code>nwrite<\/code>.<\/li>\n<\/ul>\n<p>The implementation is the really interesting part.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  1<\/span><span style=\"color: #637777;font-style: italic;\">\/\/ openssl_filter.cc<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  2<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  3<\/span><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &lt;<\/span><span style=\"color: #ECC48D;\">stdexcept<\/span><span style=\"color: #D9F5DD;\">&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  4<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  5<\/span><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">openssl_filter.h<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  6<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  7<\/span><span>SSLFilter::<\/span><span style=\"color: #82AAFF;font-style: italic;\">SSLFilter<\/span><span>(<\/span><span style=\"color: #C5E478;\">SSL_CTX<\/span><span style=\"color: #C792EA;\">*<\/span><span style=\"color: #C5E478;\"> ctxt<\/span><span>, <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  8<\/span><span>                     std::<\/span><span style=\"color: #C5E478;\">string<\/span><span style=\"color: #C792EA;\">*<\/span><span style=\"color: #C5E478;\"> nread<\/span><span>, <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  9<\/span><span>                     std::<\/span><span style=\"color: #C5E478;\">string<\/span><span style=\"color: #C792EA;\">*<\/span><span style=\"color: #C5E478;\"> nwrite<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 10<\/span><span>                     std::<\/span><span style=\"color: #C5E478;\">string<\/span><span style=\"color: #C792EA;\">*<\/span><span style=\"color: #C5E478;\"> aread<\/span><span>,<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 11<\/span><span>                     std::<\/span><span style=\"color: #C5E478;\">string<\/span><span style=\"color: #C792EA;\">*<\/span><span style=\"color: #C5E478;\"> awrite<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 12<\/span><span>                      :<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 13<\/span><span style=\"color: #82AAFF;font-style: italic;\">                     nread<\/span><span>(<\/span><span style=\"color: #C5E478;\">nread<\/span><span>), <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 14<\/span><span style=\"color: #82AAFF;font-style: italic;\">                     nwrite<\/span><span>(<\/span><span style=\"color: #C5E478;\">nwrite<\/span><span>), <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 15<\/span><span style=\"color: #82AAFF;font-style: italic;\">                     aread<\/span><span>(<\/span><span style=\"color: #C5E478;\">aread<\/span><span>), <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 16<\/span><span style=\"color: #82AAFF;font-style: italic;\">                     awrite<\/span><span>(<\/span><span style=\"color: #C5E478;\">awrite<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 17<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 18<\/span><span style=\"color: #C5E478;\">    rbio<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> BIO_new<\/span><span>(<\/span><span style=\"color: #82AAFF;font-style: italic;\">BIO_s_mem<\/span><span>());<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 19<\/span><span style=\"color: #C5E478;\">    wbio<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> BIO_new<\/span><span>(<\/span><span style=\"color: #82AAFF;font-style: italic;\">BIO_s_mem<\/span><span>());<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 20<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 21<\/span><span style=\"color: #C5E478;\">    ssl<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> SSL_new<\/span><span>(<\/span><span style=\"color: #C5E478;\">ctxt<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 22<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 23<\/span><span style=\"color: #82AAFF;font-style: italic;\">    SSL_set_accept_state<\/span><span>(<\/span><span style=\"color: #C5E478;\">ssl<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 24<\/span><span style=\"color: #82AAFF;font-style: italic;\">    SSL_set_bio<\/span><span>(<\/span><span style=\"color: #C5E478;\">ssl<\/span><span>,<\/span><span style=\"color: #C5E478;\"> rbio<\/span><span>,<\/span><span style=\"color: #C5E478;\"> wbio<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 25<\/span><span>}<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 26<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 27<\/span><span>SSLFilter::<\/span><span style=\"color: #C792EA;\">~<\/span><span style=\"color: #82AAFF;font-style: italic;\">SSLFilter<\/span><span>() {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 28<\/span><span style=\"color: #82AAFF;font-style: italic;\">    SSL_free<\/span><span>(<\/span><span style=\"color: #C5E478;\">_ssl<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 29<\/span><span>}<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 30<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 31<\/span><span style=\"color: #C792EA;\">void<\/span><span> SSLFilter::<\/span><span style=\"color: #82AAFF;font-style: italic;\">update<\/span><span>(Filter::FilterDirection) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 32<\/span><span style=\"color: #637777;font-style: italic;\">    \/\/ If we have data from the network to process, put it the memory BIO for OpenSSL<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 33<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C792EA;\">!<\/span><span style=\"color: #C5E478;\">nread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">empty<\/span><span>()) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 34<\/span><span style=\"color: #C792EA;\">        int<\/span><span style=\"color: #C5E478;\"> written<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> BIO_write<\/span><span>(<\/span><span style=\"color: #C5E478;\">rbio<\/span><span>,<\/span><span style=\"color: #C5E478;\"> nread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">c_str<\/span><span>(),<\/span><span style=\"color: #C5E478;\"> nread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">length<\/span><span>());<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 35<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C5E478;\">written<\/span><span style=\"color: #C792EA;\"> &gt;<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>)<\/span><span style=\"color: #C5E478;\"> nread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">erase<\/span><span>(<\/span><span style=\"color: #F78C6C;\">0<\/span><span>,<\/span><span style=\"color: #C5E478;\"> written<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 36<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 37<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 38<\/span><span style=\"color: #637777;font-style: italic;\">    \/\/ If the application wants to write data out to the network, process it with SSL_write<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 39<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C792EA;\">!<\/span><span style=\"color: #C5E478;\">awrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">empty<\/span><span>()) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 40<\/span><span style=\"color: #C792EA;\">        int<\/span><span style=\"color: #C5E478;\"> written<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> SSL_write<\/span><span>(<\/span><span style=\"color: #C5E478;\">ssl<\/span><span>,<\/span><span style=\"color: #C5E478;\"> awrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">c_str<\/span><span>(),<\/span><span style=\"color: #C5E478;\"> awrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">length<\/span><span>());<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 41<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 42<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C792EA;\">!<\/span><span style=\"color: #82AAFF;font-style: italic;\">continue_ssl_<\/span><span>()) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 43<\/span><span style=\"color: #C792EA;font-style: italic;\">            throw<\/span><span> std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">runtime_error<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">An SSL error occured.<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 44<\/span><span>        }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 45<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 46<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C5E478;\">written<\/span><span style=\"color: #C792EA;\"> &gt;<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>)<\/span><span style=\"color: #C5E478;\"> awrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">erase<\/span><span>(<\/span><span style=\"color: #F78C6C;\">0<\/span><span>,<\/span><span style=\"color: #C5E478;\"> written<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 47<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 48<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 49<\/span><span style=\"color: #637777;font-style: italic;\">    \/\/ Read data for the application from the encrypted connection and place it in the string for the app to read<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 50<\/span><span style=\"color: #C792EA;font-style: italic;\">    while<\/span><span> (<\/span><span style=\"color: #F78C6C;\">1<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 51<\/span><span style=\"color: #C792EA;\">        char<\/span><span style=\"color: #C792EA;font-style: italic;\"> *<\/span><span style=\"color: #C5E478;\">readto<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> new<\/span><span style=\"color: #C792EA;\"> char<\/span><span>[<\/span><span style=\"color: #F78C6C;\">1024<\/span><span>];<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 52<\/span><span style=\"color: #C792EA;\">        int<\/span><span style=\"color: #C5E478;\"> read<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> SSL_read<\/span><span>(<\/span><span style=\"color: #C5E478;\">ssl<\/span><span>,<\/span><span style=\"color: #C5E478;\"> readto<\/span><span>,<\/span><span style=\"color: #F78C6C;\"> 1024<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 53<\/span><span>       <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 54<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C792EA;\">!<\/span><span style=\"color: #82AAFF;font-style: italic;\">continue_ssl_<\/span><span>()) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 55<\/span><span style=\"color: #7FDBCA;\">            delete<\/span><span style=\"color: #C5E478;\"> readto<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 56<\/span><span style=\"color: #C792EA;font-style: italic;\">            throw<\/span><span> std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">runtime_error<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">An SSL error occured.<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 57<\/span><span>        }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 58<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 59<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C5E478;\">read<\/span><span style=\"color: #C792EA;\"> &gt;<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 60<\/span><span style=\"color: #C792EA;\">            size_t<\/span><span style=\"color: #C5E478;\"> cur_size<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #C5E478;\"> aread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">length<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 61<\/span><span style=\"color: #C5E478;\">            aread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">resize<\/span><span>(<\/span><span style=\"color: #C5E478;\">cur_size<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> read<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 62<\/span><span>            std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">copy<\/span><span>(<\/span><span style=\"color: #C5E478;\">readto<\/span><span>,<\/span><span style=\"color: #C5E478;\"> readto<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> read<\/span><span>,<\/span><span style=\"color: #C5E478;\"> aread<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">begin<\/span><span>()<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> cur_size<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 63<\/span><span>        }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 64<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 65<\/span><span style=\"color: #7FDBCA;\">        delete<\/span><span style=\"color: #C5E478;\"> readto<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 66<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 67<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #7FDBCA;\">static_cast<\/span><span style=\"color: #C792EA;\">&lt;size_t&gt;<\/span><span>(<\/span><span style=\"color: #C5E478;\">read<\/span><span>)<\/span><span style=\"color: #C792EA;\"> !=<\/span><span style=\"color: #F78C6C;\"> 1024<\/span><span style=\"color: #C792EA;\"> ||<\/span><span style=\"color: #C5E478;\"> written<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>)<\/span><span style=\"color: #C792EA;font-style: italic;\"> break<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 68<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 69<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 70<\/span><span style=\"color: #637777;font-style: italic;\">    \/\/ Read any data to be written to the network from the memory BIO and copy it to nwrite<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 71<\/span><span style=\"color: #C792EA;font-style: italic;\">    while<\/span><span> (<\/span><span style=\"color: #F78C6C;\">1<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 72<\/span><span style=\"color: #C792EA;\">        char<\/span><span style=\"color: #C792EA;font-style: italic;\"> *<\/span><span style=\"color: #C5E478;\">readto<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #7FDBCA;\"> new<\/span><span style=\"color: #C792EA;\"> char<\/span><span>[<\/span><span style=\"color: #F78C6C;\">1024<\/span><span>];<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 73<\/span><span style=\"color: #C792EA;\">        int<\/span><span style=\"color: #C5E478;\"> read<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> BIO_read<\/span><span>(<\/span><span style=\"color: #C5E478;\">wbio<\/span><span>,<\/span><span style=\"color: #C5E478;\"> readto<\/span><span>,<\/span><span style=\"color: #F78C6C;\"> 1024<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 74<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 75<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #C5E478;\">read<\/span><span style=\"color: #C792EA;\"> &gt;<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 76<\/span><span style=\"color: #C792EA;\">            size_t<\/span><span style=\"color: #C5E478;\"> cur_size<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #C5E478;\"> nwrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">length<\/span><span>();<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 77<\/span><span style=\"color: #C5E478;\">            nwrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">resize<\/span><span>(<\/span><span style=\"color: #C5E478;\">cur_size<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> read<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 78<\/span><span>            std::<\/span><span style=\"color: #82AAFF;font-style: italic;\">copy<\/span><span>(<\/span><span style=\"color: #C5E478;\">readto<\/span><span>,<\/span><span style=\"color: #C5E478;\"> readto<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> read<\/span><span>,<\/span><span style=\"color: #C5E478;\"> nwrite<\/span><span>-&gt;<\/span><span style=\"color: #82AAFF;font-style: italic;\">begin<\/span><span>()<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #C5E478;\"> cur_size<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 79<\/span><span>        }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 80<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 81<\/span><span style=\"color: #7FDBCA;\">        delete<\/span><span style=\"color: #C5E478;\"> readto<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 82<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 83<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> (<\/span><span style=\"color: #7FDBCA;\">static_cast<\/span><span style=\"color: #C792EA;\">&lt;size_t&gt;<\/span><span>(<\/span><span style=\"color: #C5E478;\">read<\/span><span>)<\/span><span style=\"color: #C792EA;\"> !=<\/span><span style=\"color: #F78C6C;\"> 1024<\/span><span style=\"color: #C792EA;\"> ||<\/span><span style=\"color: #C5E478;\"> read<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #F78C6C;\"> 0<\/span><span>)<\/span><span style=\"color: #C792EA;font-style: italic;\"> break<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 84<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 85<\/span><span>}<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 86<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 87<\/span><span style=\"color: #C792EA;\">bool<\/span><span> SSLFilter::<\/span><span style=\"color: #82AAFF;font-style: italic;\">continue_ssl_<\/span><span>(<\/span><span style=\"color: #C792EA;\">int<\/span><span style=\"color: #D7DBE0;\"> function_return<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 88<\/span><span style=\"color: #C792EA;\">    int<\/span><span style=\"color: #C5E478;\"> err<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;font-style: italic;\"> SSL_get_error<\/span><span>(<\/span><span style=\"color: #C5E478;\">ssl<\/span><span>,<\/span><span style=\"color: #C5E478;\"> function_return<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 89<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 90<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">err<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #C5E478;\"> SSL_ERROR_NONE<\/span><span style=\"color: #C792EA;\"> ||<\/span><span style=\"color: #C5E478;\"> err<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #C5E478;\"> SSL_ERROR_WANT_READ<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 91<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #82AAFF;\"> true<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 92<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 93<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 94<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">err<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #C5E478;\"> SSL_ERROR_SYSCALL<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 95<\/span><span style=\"color: #82AAFF;font-style: italic;\">        ERR_print_errors_fp<\/span><span>(<\/span><span style=\"color: #C5E478;\">stderr<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 96<\/span><span style=\"color: #82AAFF;font-style: italic;\">        perror<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">syscall error: <\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 97<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #82AAFF;\"> false<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 98<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 99<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">100<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> (<\/span><span style=\"color: #C5E478;\">err<\/span><span style=\"color: #C792EA;\"> ==<\/span><span style=\"color: #C5E478;\"> SSL_ERROR_SSL<\/span><span>) {<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">101<\/span><span style=\"color: #82AAFF;font-style: italic;\">        ERR_print_errors_fp<\/span><span>(<\/span><span style=\"color: #C5E478;\">stderr<\/span><span>);<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">102<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #82AAFF;\"> false<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">103<\/span><span>    }<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">104<\/span><span style=\"color: #C792EA;font-style: italic;\">    return<\/span><span style=\"color: #82AAFF;\"> true<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">105<\/span><span>}<\/span><\/span><\/code><\/pre><h3 id=\"explanation-of-the-ssl-filter-class\">Explanation of the SSL Filter class<\/h3>\n<p><strong>Line 7 - 25<\/strong>, we create the two memory BIO's and then call <code>SSL_new()<\/code> using\nthe passed in <code>SSL_CTX<\/code>, set the accept set for SSL and then we hook up the two\nBIO objects to the <code>ssl<\/code> object.<\/p>\n<p><strong>Line 27 - 29<\/strong>, All we do in the destructor is <code>SSL_free()<\/code> the SSL state,\nwhich automatically takes care of freeing the two memory BIOs that were given\nto it.<\/p>\n<p><strong>Line 33 - 36<\/strong>, if <code>nread<\/code> is not empty we copy the data into <code>rbio<\/code>. <code>rbio<\/code>\nis used by the SSL functions to read from (as if it were reading from a\nsocket). We read into the memory BIO as much as possible, technically this\nshould be everything, but it is possible it won't read everything due to memory\nconstraints, next time <code>SSLFilter::update()<\/code> is called it will get emptied as\nmuch as possible again.<\/p>\n<p><strong>Line 39 - 47<\/strong>, we see if there is anything that the app wants to write out\nto the remote client, if there is we call <code>SSL_write()<\/code>. After the call to\n<code>SSL_write()<\/code> we call <code>continue_ssl_()<\/code> to see if we can safely continue using\nSSL.<\/p>\n<p><strong>Line 50 - 68<\/strong>, this is where try to read as much data from <code>SSL_read()<\/code> as\nwe can and place it in <code>aread<\/code>. This is data that has been decrypted by OpenSSL\nand can be used by the application. After the call to <code>SSL_read()<\/code> we call\n<code>continue_ssl_()<\/code> to see if we can safely continue using SSL.<\/p>\n<p><strong>Line 71 - 84<\/strong>, if OpenSSL has to write something to the network, either\ndirectly due to something the app did (by filling <code>awrite<\/code>) or because of\nsomething the remote client sent, it will place it in its memory buffer\n(<code>wbio<\/code>), what we do here is drain that memory buffer and fill <code>nwrite<\/code>.<\/p>\n<p><strong>Line 87 - 105<\/strong>, the <code>continue_ssl_()<\/code> function gets the SSL errors using\n<a rel=\"external\" href=\"http:\/\/www.openssl.org\/docs\/ssl\/SSL_get_error.html\"><code>SSL_get_error<\/code><\/a> and checks that either it is <code>SSL_ERROR_NONE<\/code> or\n<code>SSL_ERROR_WANT_READ<\/code> both of which are acceptable errors, any other errors and\nwe check to see if it is an error due to a system call or due to an SSL error,\nwe print out the errors to standard error and return false. At that point the\ncalling code is free to go about its business as it pleases (most likely\nthrowing an error).<\/p>\n<h4 id=\"notes-regarding-this-sample-code\">Notes regarding this sample code ...<\/h4>\n<p>The only thing missing in this example code is checking to see if the SSL\nconnection has gone into an <code>SSL_shutdown()<\/code> mode. This should be added so that\nyou cleanly shut down an SSL connection, but it is not absolutely required,\nespecially since it seems to be shaky as to whether or not it will work,\nespecially if you are using SSLv3 or SSLv2.<\/p>\n<h3 id=\"how-to-use-the-ssl-filter\">How to use the SSL Filter<\/h3>\n<p>Here are the steps to using this filter:<\/p>\n<ol>\n<li>At program startup set up the <code>SSL_CTX<\/code> structure as required, so that it\nbe used in the <code>SSLFilter<\/code><\/li>\n<li>Set up socket\/event loop and accept() new socket.<\/li>\n<li>Create four new <code>std::string<\/code>'s and create a new SSLFilter, pass in the\n<code>SSL_CTX<\/code> you created, and the four buffers.<\/li>\n<li>Add the new socket to the event loop to wait for reading.<\/li>\n<\/ol>\n<p>Now upon receiving a read ready status from the event loop, the following should be done:<\/p>\n<ol>\n<li>Read data into <code>nread<\/code> buffer.<\/li>\n<li>call <code>SSLFilter::update()<\/code><\/li>\n<li>See if <code>aread<\/code> is not empty, process the data as you please<\/li>\n<li>Write data to <code>awrite<\/code> as needed, if data is written to <code>awrite<\/code>, call\n<code>SSLFilter::update(WRITE)<\/code> to process the data<\/li>\n<li>See if <code>nwrite<\/code> is not empty, if so add socket to event loop for write\nreadiness.<\/li>\n<\/ol>\n<p>Once you receive a write ready status from the event loop, you should do the following:<\/p>\n<ol>\n<li>Write data in <code>nwrite<\/code> to the socket.<\/li>\n<li>If <code>nwrite<\/code> is empty, remove the socket from the event loop for writing (so\nthat you don't have the event loop notifying you of the ability to write,\neven-though you have nothing to write).<\/li>\n<\/ol>\n<p>Now it becomes simple to add OpenSSL to any open socket, in a way that is easy\nto refactor later and or disable depending on parameters passed in as arguments\nto the program. If you set <code>nread<\/code> to the same string as <code>aread<\/code> and <code>nwrite<\/code>\nto <code>awrite<\/code> and don't create an SSLFilter you have bypassed the filter...<\/p>\n"}]}