~ubuntu-branches/ubuntu/intrepid/luatex/intrepid

« back to all changes in this revision

Viewing changes to src/libs/luasocket/etc/forward.lua

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2008-07-07 11:01:13 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080707110113-1y7lam37zbbb7bbt
Tags: 0.28.0-1
* two new upstream releases, see the respective ANNOUCE files
* add luasocket license statement to debian/copyright
* activate the pdfluatex format
* prepare for latex based formats
  - add the ini files from TeX Live
  - add debian/formats file
  - adjust dh_installtex incantation
  the problem is that luatex dies when loading ukrhypmp.tex from 
  texlive-lang-cyrillic, but we don't want to conflict with it by now.
* policy 3.8.0, rename README.Debian-source to README.source, and add
  notes about quilt usage
* disable patch fix-pwd-inclusion (it was from svn)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-- load our favourite library
 
2
local dispatch = require("dispatch")
 
3
local handler = dispatch.newhandler()
 
4
 
 
5
-- make sure the user knows how to invoke us
 
6
if table.getn(arg) < 1 then
 
7
    print("Usage")
 
8
    print("    lua forward.lua <iport:ohost:oport> ...")
 
9
    os.exit(1)
 
10
end
 
11
 
 
12
-- function to move data from one socket to the other
 
13
local function move(foo, bar)
 
14
    local live
 
15
    while 1 do
 
16
        local data, error, partial = foo:receive(2048)
 
17
        live = data or error == "timeout"
 
18
        data = data or partial
 
19
        local result, error = bar:send(data)
 
20
        if not live or not result then
 
21
            foo:close()
 
22
            bar:close()
 
23
            break
 
24
        end
 
25
    end
 
26
end
 
27
 
 
28
-- for each tunnel, start a new server
 
29
for i, v in ipairs(arg) do
 
30
    -- capture forwarding parameters
 
31
    local _, _, iport, ohost, oport = string.find(v, "([^:]+):([^:]+):([^:]+)")
 
32
    assert(iport, "invalid arguments")
 
33
    -- create our server socket
 
34
    local server = assert(handler.tcp())
 
35
    assert(server:setoption("reuseaddr", true))
 
36
    assert(server:bind("*", iport))
 
37
    assert(server:listen(32))
 
38
    -- handler for the server object loops accepting new connections
 
39
    handler:start(function()
 
40
        while 1 do
 
41
            local client = assert(server:accept())
 
42
            assert(client:settimeout(0))
 
43
            -- for each new connection, start a new client handler
 
44
            handler:start(function()
 
45
                -- handler tries to connect to peer
 
46
                local peer = assert(handler.tcp())
 
47
                assert(peer:settimeout(0))
 
48
                assert(peer:connect(ohost, oport))
 
49
                -- if sucessful, starts a new handler to send data from
 
50
                -- client to peer
 
51
                handler:start(function()
 
52
                    move(client, peer)
 
53
                end)
 
54
                -- afte starting new handler, enter in loop sending data from
 
55
                -- peer to client
 
56
                move(peer, client)
 
57
            end)
 
58
        end
 
59
    end)
 
60
end
 
61
 
 
62
-- simply loop stepping the server
 
63
while 1 do
 
64
    handler:step()
 
65
end