<div dir="ltr">Hi,<div><br></div><div>This is a lightweight networking code that I'd like to add in The ccan tree.</div><div>Reading the code should be straight forward, it uses epoll etc, and has support for callbacks, it can be used a server or a client with 3 simple calls:</div>
<div><pre style="margin-top:0px;margin-bottom:0px;padding:0px;border:0px;font-size:12px;font-family:Consolas,'Liberation Mono',Courier,monospace;line-height:16px"><div class="line" id="LC64" style="color:rgb(51,51,51);margin:0px;padding:0px 0px 0px 10px;border:0px">
<span class="k" style="margin:0px;padding:0px;border:0px;font-weight:bold">struct</span> <span class="n" style="margin:0px;padding:0px;border:0px">socket_t</span> <span class="o" style="margin:0px;padding:0px;border:0px;font-weight:bold">*</span><span class="n" style="margin:0px;padding:0px;border:0px">sock</span> <span class="o" style="margin:0px;padding:0px;border:0px;font-weight:bold">=</span> <span class="n" style="margin:0px;padding:0px;border:0px">socket_create</span><span class="p" style="margin:0px;padding:0px;border:0px">();</span></div>
<div class="line" id="LC65" style="margin:0px;padding:0px 0px 0px 10px;border:0px"><font color="#333333"> </font><span class="k" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px;font-weight:bold">if</span><font color="#333333"> </font><span class="p" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">(</span><span class="o" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px;font-weight:bold">!</span><span class="n" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">socket_listen</span><span class="p" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">(</span><span class="n" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">sock</span><span class="p" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">,</span><font color="#333333"> </font><span class="nb" style="color:rgb(0,134,179);margin:0px;padding:0px;border:0px">NULL</span><span class="p" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">,</span><font color="#333333"> </font><font color="#009999">1337</font><span class="p" style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">))</span></div>
<div class="line" id="LC66" style="color:rgb(51,51,51);margin:0px;padding:0px 0px 0px 10px;border:0px"> fatal("failed to listen on port 1337.");</div><div class="line" id="LC66" style="color:rgb(51,51,51);margin:0px;padding:0px 0px 0px 10px;border:0px">
/*</div><div class="line" id="LC66" style="color:rgb(51,51,51);margin:0px;padding:0px 0px 0px 10px;border:0px"> * callbacks should be setup before polling on the socket</div><div class="line" id="LC66" style="color:rgb(51,51,51);margin:0px;padding:0px 0px 0px 10px;border:0px">
*/</div><div class="line" id="LC67" style="color:rgb(51,51,51);margin:0px;padding:0px 0px 0px 10px;border:0px"> <span class="n" style="margin:0px;padding:0px;border:0px">socket_poll</span><span class="p" style="margin:0px;padding:0px;border:0px">(</span><span class="n" style="margin:0px;padding:0px;border:0px">sock</span><span class="p" style="margin:0px;padding:0px;border:0px">);</span></div>
</pre></div><div><br></div><div>For client side, replace socket_listen with socket_connect.</div><div>The code can be viewed at: at:<a href="https://github.com/otfallen/csnippets/blob/master/src/socket.c">https://github.com/otfallen/csnippets/blob/master/src/socket.c</a> (to view the header change '.c' to '.h')</div>
<div><br></div><div>Please leave any kind of feedback.</div><div><br></div><div>Thanks</div><div> asamy</div></div>