Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a various ways of handling this issue; which one you will select depends on how much work you want to do.<sup>*</sup></p> <p>But first, you should clarify (to yourself) whether you are dealing with UDP or TCP; there is no "underlying TCP stack" for UDP sockets. Also, UDP is the wrong protocol to use for sending whole data such as a text, or a photo; it is an unreliable protocol so you aren't guaranteed to receive every packet, unless you're using a managed socket library (such as <a href="http://enet.bespin.org/" rel="noreferrer">ENet</a>).</p> <h1>Lua51/LuaJIT + LuaSocket</h1> <p>Polling is the only method.</p> <ul> <li>Blocking: call <code>socket.select</code> with no time argument and wait for the socket to be readable.</li> <li>Non-blocking: call <code>socket.select</code> with a timeout argument of <code>0</code>, and use <code>sock:settimeout(0)</code> on the socket you're reading from.</li> </ul> <p>Then simply call these repeatedly. I would suggest using a <a href="https://gist.github.com/1818054" rel="noreferrer">coroutine scheduler</a> for the non-blocking version, to allow other parts of the program to continue executing without causing too much delay.</p> <h1>Lua51/LuaJIT + LuaSocket + <a href="http://kotisivu.dnainternet.net/askok/bin/lanes/" rel="noreferrer">Lua Lanes</a> (Recommended)</h1> <p>Same as the above method, but the socket exists in another lane (a lightweight Lua state in another thread) made using <a href="http://kotisivu.dnainternet.net/askok/bin/lanes/" rel="noreferrer">Lua Lanes</a> (<a href="https://github.com/LuaLanes/lanes" rel="noreferrer">latest source</a>). This allows you to instantly read the data from the socket and into a buffer. Then, you use a <a href="http://kotisivu.dnainternet.net/askok/bin/lanes/#lindas" rel="noreferrer">linda</a> to send the data to the main thread for processing.</p> <p>This is probably the best solution to your problem.</p> <p>I've made a simple example of this, available <a href="https://gist.github.com/4026258" rel="noreferrer">here</a>. It relies on Lua Lanes 3.4.0 (<a href="https://github.com/LuaLanes/lanes" rel="noreferrer">GitHub repo</a>) and a patched LuaSocket 2.0.2 (<a href="http://luarocks.org/repositories/rocks/#luasocket" rel="noreferrer">source</a>, <a href="http://www.net-core.org/dl/luasocket-2.0.2-acceptfd.patch" rel="noreferrer">patch</a>, <a href="http://www.net-core.org/39/lua/patching-luasocket-to-make-it-compatible" rel="noreferrer">blog post re' patch</a>)</p> <p>The results are promising, though you should definitely refactor my example code if you derive from it.</p> <h1>LuaJIT + OS-specific sockets</h1> <p>If you're a little masochistic, you can try implementing a socket library from scratch. <a href="http://luajit.org/luajit.html" rel="noreferrer">LuaJIT</a>'s <a href="http://luajit.org/ext_ffi.html" rel="noreferrer">FFI library</a> makes this possible from pure Lua. Lua Lanes would be useful for this as well.</p> <p>For Windows, I suggest taking a look at <a href="http://williamaadams.wordpress.com/2012/07/01/netnet/" rel="noreferrer">William Adam's blog</a>. He's had some very interesting adventures with LuaJIT and Windows development. As for Linux and the rest, look at tutorials for C or the source of LuaSocket and translate them to LuaJIT FFI operations.</p> <p>(LuaJIT supports <a href="http://luajit.org/ext_ffi_semantics.html#callback" rel="noreferrer">callbacks</a> if the API requires it; however, there is a signficant performance cost compared to polling from Lua to C.)</p> <h1>LuaJIT + ENet</h1> <p><a href="http://enet.bespin.org/" rel="noreferrer">ENet</a> is a great library. It provides the perfect mix between TCP and UDP: reliable when desired, unreliable otherwise. It also abstracts operating system specific details, much like LuaSocket does. You can use the Lua API to bind it, or directly access it via LuaJIT's FFI (recommended).</p> <p><sub><sup>*</sup> Pun unintentional.</sub></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload