GithubHelp home page GithubHelp logo

xpol / luv Goto Github PK

View Code? Open in Web Editor NEW

This project forked from luvit/luv

0.0 2.0 0.0 698 KB

Bare libuv bindings for lua

License: Apache License 2.0

Makefile 0.58% Lua 25.33% C 71.88% C++ 0.61% CMake 1.54% Batchfile 0.05%

luv's Introduction

luv

Linux Build Status

Windows Build status

libuv bindings for luajit and lua 5.1/5.2.

This library makes libuv available to lua scripts. It was made for the luvit project but should usable from nearly any lua project.

The library can be used by multiple threads at once. Each thread is assumed to load the library from a different lua_State. Luv will create a unique uv_loop_t for each state. You can't share uv handles between states/loops.

The best docs currently are the libuv docs themselves. Hopfully soon we'll have a copy locally tailored for lua.

local uv = require('luv')

-- Create a handle to a uv_timer_t
local timer = uv.new_timer()

-- This will wait 1000ms and then continue inside the callback
timer:start(1000, 0, function ()
  -- timer here is the value we passed in before from new_timer.

  print ("Awake!")

  -- You must always close your uv handles or you'll leak memory
  -- We can't depend on the GC since it doesn't know enough about libuv.
  timer:close()
end)

print("Sleeping");

-- uv.run will block and wait for all events to run.
-- When there are no longer any active handles, it will return
uv.run()

Here is an example of an TCP echo server

local uv = require('luv')

local function create_server(host, port, on_connection)

  local server = uv.new_tcp()
  server:bind(host, port)

  server:listen(128, function(self)
    -- self is the same as server
    assert(self == server)

    -- Accept the client
    local client = uv.new_tcp()
    server:accept(client)

    on_connection(client)
  end)

  return server
end

local server = create_server("0.0.0.0", 0, function (client)

  client:read_start(function (err, chunk)

    -- Crash on errors
    assert(not err, err)

    if chunk then
      -- Echo anything heard
      client:write(chunk)
    else
      -- When the stream ends, close the socket
      client:close()
    end
  end)
end)

print("TCP Echo serverr listening on port " .. server:getsockname().port)

uv.run()

More examples can be found in the examples and tests folders.

Building From Source

To build, first install your compiler tools.

Get a Compiler

On linux this probably means gcc and make. On Ubuntu, the build-essential package is good for this.

On OSX, you probably want XCode which comes with clang and make and friends.

For windows the free Visual Studio Express works. If you get the 2013 edition, make sure to get the Windows Deskop edition. The Windows version doesn't include a working C compiler. Make sure to run all of setup including getting a free license.

Install CMake

Now install Cmake. The version in brew on OSX or most Linux package managers is good. The version on Travis CI is too old and so I use a PPA there. On windows use the installer and make sure to add cmake to your command prompt path.

Install Git

If you haven't already, install git and make sure it's in your path. This comes with XCode on OSX. On Linux it's in your package manager. For windows, use the installer at http://git-scm.com. Make sure it's available to your windows command prompt.

Clone the Code

Now open a terminal and clone the code. For windows I recommend the special developer command prompt that came with Visual Studio.

git clone https://github.com/luvit/luv.git --recursive
cd luv

Build the Code and Test

On windows I wrote a small batch file that runs the correct cmake commands and copies the output files for easy access.

C:\Code\luv> msvcbuild.bat
C:\Code\luv> luajit tests\run.lua

On unix systems, use the Makefile.

~/Code/luv> make test

luv's People

Contributors

andrewtsao avatar bjorn avatar clementfarabet avatar creationix avatar daurnimator avatar devurandom avatar dvv avatar hnakamur avatar joerg-krause avatar mei-rune avatar rjemanuele avatar rphillips avatar songgao avatar soumith avatar starwing avatar xpol avatar zhaozg avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.