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