<div dir="ltr"><br><br><div class="gmail_quote">On Sat, Nov 3, 2012 at 7:03 AM, Ahmed Samy <span dir="ltr"><<a href="mailto:f.fallen45@gmail.com" target="_blank">f.fallen45@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div><div><br><br><div class="gmail_quote">On Sat, Nov 3, 2012 at 5:44 AM, Ahmed Samy <span dir="ltr"><<a href="mailto:f.fallen45@gmail.com" target="_blank">f.fallen45@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<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 style="color:rgb(51,51,51);margin:0px;padding:0px 0px 0px 10px;border:0px">
<span style="margin:0px;padding:0px;border:0px;font-weight:bold">struct</span> <span style="margin:0px;padding:0px;border:0px">socket_t</span> <span style="margin:0px;padding:0px;border:0px;font-weight:bold">*</span><span style="margin:0px;padding:0px;border:0px">sock</span> <span style="margin:0px;padding:0px;border:0px;font-weight:bold">=</span> <span style="margin:0px;padding:0px;border:0px">socket_create</span><span style="margin:0px;padding:0px;border:0px">();</span></div>
<div style="margin:0px;padding:0px 0px 0px 10px;border:0px"><font color="#333333"> </font><span style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px;font-weight:bold">if</span><font color="#333333"> </font><span style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">(</span><span style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px;font-weight:bold">!</span><span style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">socket_listen</span><span style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">(</span><span style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">sock</span><span style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">,</span><font color="#333333"> </font><span style="color:rgb(0,134,179);margin:0px;padding:0px;border:0px">NULL</span><span style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">,</span><font color="#333333"> </font><font color="#009999">1337</font><span style="color:rgb(51,51,51);margin:0px;padding:0px;border:0px">))</span></div>
<div style="color:rgb(51,51,51);margin:0px;padding:0px 0px 0px 10px;border:0px"> fatal("failed to listen on port 1337.");</div><div style="color:rgb(51,51,51);margin:0px;padding:0px 0px 0px 10px;border:0px">
/*</div><div 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 style="color:rgb(51,51,51);margin:0px;padding:0px 0px 0px 10px;border:0px">
*/</div><div style="color:rgb(51,51,51);margin:0px;padding:0px 0px 0px 10px;border:0px"> <span style="margin:0px;padding:0px;border:0px">socket_poll</span><span style="margin:0px;padding:0px;border:0px">(</span><span style="margin:0px;padding:0px;border:0px">sock</span><span 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" target="_blank">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>
</blockquote></div></div></div>Btw, the tests I used can be found at: <a href="https://github.com/otfallen/csnippets/blob/master/src/tests.c" target="_blank">https://github.com/otfallen/csnippets/blob/master/src/tests.c</a><div>
Also, I sent this message for a discussion, i.e what code can be improved etc etc. So, after the code is as-wanted, I'll upload patch(es) with ccan coding style and so on.</div>
</div>
</blockquote></div>Hi,<div><br></div><div>The code is now portable to other interfaces, I've added an optional select interface instead of epoll.</div><div>See socket_select.c and socket_epoll.c</div></div>