Software /
code /
prosody
Annotate
net/server_event.lua @ 2318:a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 04 Dec 2009 03:07:17 +0000 |
parent | 2303:ecbd68fc46db |
child | 2333:ed824115becf |
rev | line source |
---|---|
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
1 --[[ |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
2 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
3 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
4 server.lua based on lua/libevent by blastbeat |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
5 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
6 notes: |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
7 -- when using luaevent, never register 2 or more EV_READ at one socket, same for EV_WRITE |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
8 -- you cant even register a new EV_READ/EV_WRITE callback inside another one |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
9 -- never call eventcallback:close( ) from inside eventcallback |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
10 -- to do some of the above, use timeout events or something what will called from outside |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
11 -- dont let garbagecollect eventcallbacks, as long they are running |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
12 -- when using luasec, there are 4 cases of timeout errors: wantread or wantwrite during reading or writing |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
13 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
14 --]] |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
15 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
16 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
17 local SCRIPT_NAME = "server_event.lua" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
18 local SCRIPT_VERSION = "0.05" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
19 local SCRIPT_AUTHOR = "blastbeat" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
20 local LAST_MODIFIED = "2009/11/20" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
21 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
22 local cfg = { |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
23 MAX_CONNECTIONS = 100000, -- max per server connections (use "ulimit -n" on *nix) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
24 MAX_HANDSHAKE_ATTEMPS = 10, -- attemps to finish ssl handshake |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
25 HANDSHAKE_TIMEOUT = 1, -- timout in seconds per handshake attemp |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
26 MAX_READ_LENGTH = 1024 * 1024 * 1024 * 1024, -- max bytes allowed to read from sockets |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
27 MAX_SEND_LENGTH = 1024 * 1024 * 1024 * 1024, -- max bytes size of write buffer (for writing on sockets) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
28 ACCEPT_DELAY = 10, -- seconds to wait until the next attemp of a full server to accept |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
29 READ_TIMEOUT = 60 * 30, -- timeout in seconds for read data from socket |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
30 WRITE_TIMEOUT = 30, -- timeout in seconds for write data on socket |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
31 CONNECT_TIMEOUT = 10, -- timeout in seconds for connection attemps |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
32 CLEAR_DELAY = 5, -- seconds to wait for clearing interface list (and calling ondisconnect listeners) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
33 DEBUG = true, -- show debug messages |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
34 } |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
35 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
36 local function use(x) return rawget(_G, x); end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
37 local print = use "print" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
38 local pcall = use "pcall" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
39 local ipairs = use "ipairs" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
40 local string = use "string" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
41 local select = use "select" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
42 local require = use "require" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
43 local tostring = use "tostring" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
44 local coroutine = use "coroutine" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
45 local setmetatable = use "setmetatable" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
46 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
47 local ssl = use "ssl" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
48 local socket = use "socket" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
49 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
50 local log = require ("util.logger").init("socket") |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
51 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
52 local function debug(...) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
53 return log("debug", ("%s "):rep(select('#', ...)), ...) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
54 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
55 local vdebug = debug; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
56 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
57 local bitor = ( function( ) -- thx Rici Lake |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
58 local hasbit = function( x, p ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
59 return x % ( p + p ) >= p |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
60 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
61 return function( x, y ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
62 local p = 1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
63 local z = 0 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
64 local limit = x > y and x or y |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
65 while p <= limit do |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
66 if hasbit( x, p ) or hasbit( y, p ) then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
67 z = z + p |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
68 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
69 p = p + p |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
70 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
71 return z |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
72 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
73 end )( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
74 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
75 local event = require "luaevent.core" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
76 local base = event.new( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
77 local EV_READ = event.EV_READ |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
78 local EV_WRITE = event.EV_WRITE |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
79 local EV_TIMEOUT = event.EV_TIMEOUT |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
80 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
81 local EV_READWRITE = bitor( EV_READ, EV_WRITE ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
82 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
83 local interfacelist = ( function( ) -- holds the interfaces for sockets |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
84 local array = { } |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
85 local len = 0 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
86 return function( method, arg ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
87 if "add" == method then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
88 len = len + 1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
89 array[ len ] = arg |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
90 arg:_position( len ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
91 return len |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
92 elseif "delete" == method then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
93 if len <= 0 then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
94 return nil, "array is already empty" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
95 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
96 local position = arg:_position() -- get position in array |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
97 if position ~= len then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
98 local interface = array[ len ] -- get last interface |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
99 array[ position ] = interface -- copy it into free position |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
100 array[ len ] = nil -- free last position |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
101 interface:_position( position ) -- set new position in array |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
102 else -- free last position |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
103 array[ len ] = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
104 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
105 len = len - 1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
106 return len |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
107 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
108 return array |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
109 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
110 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
111 end )( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
112 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
113 -- Client interface methods |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
114 local interface_mt |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
115 do |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
116 interface_mt = {}; interface_mt.__index = interface_mt; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
117 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
118 local addevent = base.addevent |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
119 local coroutine_wrap, coroutine_yield = coroutine.wrap,coroutine.yield |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
120 local string_len = string.len |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
121 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
122 -- Private methods |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
123 function interface_mt:_position(new_position) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
124 self.position = new_position or self.position |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
125 return self.position; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
126 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
127 function interface_mt:_close() -- regs event to start self:_destroy() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
128 local callback = function( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
129 self:_destroy(); |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
130 self.eventclose = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
131 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
132 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
133 self.eventclose = addevent( base, nil, EV_TIMEOUT, callback, 0 ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
134 return true |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
135 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
136 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
137 function interface_mt:_start_connection(plainssl) -- should be called from addclient |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
138 local callback = function( event ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
139 if EV_TIMEOUT == event then -- timout during connection |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
140 self.fatalerror = "connection timeout" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
141 self:ontimeout() -- call timeout listener |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
142 self:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
143 debug( "new connection failed. id:", self.id, "error:", self.fatalerror ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
144 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
145 if plainssl then -- start ssl session |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
146 self:_start_ssl( self.listener.onconnect ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
147 else -- normal connection |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
148 self:_start_session( self.listener.onconnect ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
149 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
150 debug( "new connection established. id:", self.id ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
151 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
152 self.eventconnect = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
153 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
154 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
155 self.eventconnect = addevent( base, self.conn, EV_WRITE, callback, cfg.CONNECT_TIMEOUT ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
156 return true |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
157 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
158 function interface_mt:_start_session(onconnect) -- new session, for example after startssl |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
159 if self.type == "client" then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
160 local callback = function( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
161 self:_lock( false, false, false ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
162 --vdebug( "start listening on client socket with id:", self.id ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
163 self.eventread = addevent( base, self.conn, EV_READ, self.readcallback, cfg.READ_TIMEOUT ) -- register callback |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
164 self:onconnect() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
165 self.eventsession = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
166 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
167 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
168 self.eventsession = addevent( base, nil, EV_TIMEOUT, callback, 0 ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
169 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
170 self:_lock( false ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
171 --vdebug( "start listening on server socket with id:", self.id ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
172 self.eventread = addevent( base, self.conn, EV_READ, self.readcallback ) -- register callback |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
173 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
174 return true |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
175 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
176 function interface_mt:_start_ssl(arg) -- old socket will be destroyed, therefore we have to close read/write events first |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
177 --vdebug( "starting ssl session with client id:", self.id ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
178 local _ |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
179 _ = self.eventread and self.eventread:close( ) -- close events; this must be called outside of the event callbacks! |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
180 _ = self.eventwrite and self.eventwrite:close( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
181 self.eventread, self.eventwrite = nil, nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
182 local err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
183 self.conn, err = ssl.wrap( self.conn, self._sslctx ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
184 if err then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
185 self.fatalerror = err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
186 self.conn = nil -- cannot be used anymore |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
187 if "onconnect" == arg then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
188 self.ondisconnect = nil -- dont call this when client isnt really connected |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
189 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
190 self:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
191 debug( "fatal error while ssl wrapping:", err ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
192 return false |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
193 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
194 self.conn:settimeout( 0 ) -- set non blocking |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
195 local handshakecallback = coroutine_wrap( |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
196 function( event ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
197 local _, err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
198 local attempt = 0 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
199 local maxattempt = cfg.MAX_HANDSHAKE_ATTEMPS |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
200 while attempt < 1000 do -- no endless loop |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
201 attempt = attempt + 1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
202 debug( "ssl handshake of client with id:"..tostring(self).."attemp:"..attempt ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
203 if attempt > maxattempt then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
204 self.fatalerror = "max handshake attemps exceeded" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
205 elseif EV_TIMEOUT == event then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
206 self.fatalerror = "timeout during handshake" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
207 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
208 _, err = self.conn:dohandshake( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
209 if not err then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
210 self:_lock( false, false, false ) -- unlock the interface; sending, closing etc allowed |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
211 self.send = self.conn.send -- caching table lookups with new client object |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
212 self.receive = self.conn.receive |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
213 local onsomething |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
214 if "onconnect" == arg then -- trigger listener |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
215 onsomething = self.onconnect |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
216 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
217 onsomething = self.onsslconnection |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
218 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
219 self:_start_session( onsomething ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
220 debug( "ssl handshake done" ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
221 self.eventhandshake = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
222 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
223 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
224 debug( "error during ssl handshake:", err ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
225 if err == "wantwrite" then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
226 event = EV_WRITE |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
227 elseif err == "wantread" then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
228 event = EV_READ |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
229 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
230 self.fatalerror = err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
231 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
232 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
233 if self.fatalerror then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
234 if "onconnect" == arg then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
235 self.ondisconnect = nil -- dont call this when client isnt really connected |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
236 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
237 self:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
238 debug( "handshake failed because:", self.fatalerror ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
239 self.eventhandshake = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
240 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
241 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
242 event = coroutine_yield( event, cfg.HANDSHAKE_TIMEOUT ) -- yield this monster... |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
243 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
244 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
245 ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
246 debug "starting handshake..." |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
247 self:_lock( false, true, true ) -- unlock read/write events, but keep interface locked |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
248 self.eventhandshake = addevent( base, self.conn, EV_READWRITE, handshakecallback, cfg.HANDSHAKE_TIMEOUT ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
249 return true |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
250 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
251 function interface_mt:_destroy() -- close this interface + events and call last listener |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
252 debug( "closing client with id:", self.id ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
253 self:_lock( true, true, true ) -- first of all, lock the interface to avoid further actions |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
254 local _ |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
255 _ = self.eventread and self.eventread:close( ) -- close events; this must be called outside of the event callbacks! |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
256 if self.type == "client" then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
257 _ = self.eventwrite and self.eventwrite:close( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
258 _ = self.eventhandshake and self.eventhandshake:close( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
259 _ = self.eventstarthandshake and self.eventstarthandshake:close( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
260 _ = self.eventconnect and self.eventconnect:close( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
261 _ = self.eventsession and self.eventsession:close( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
262 _ = self.eventwritetimeout and self.eventwritetimeout:close( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
263 _ = self.eventreadtimeout and self.eventreadtimeout:close( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
264 _ = self.ondisconnect and self:ondisconnect( self.fatalerror ) -- call ondisconnect listener (wont be the case if handshake failed on connect) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
265 _ = self.conn and self.conn:close( ) -- close connection, must also be called outside of any socket registered events! |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
266 _ = self._server and self._server:counter(-1); |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
267 self.eventread, self.eventwrite = nil, nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
268 self.eventstarthandshake, self.eventhandshake, self.eventclose = nil, nil, nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
269 self.readcallback, self.writecallback = nil, nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
270 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
271 self.conn:close( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
272 self.eventread, self.eventclose = nil, nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
273 self.interface, self.readcallback = nil, nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
274 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
275 interfacelist( "delete", self ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
276 return true |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
277 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
278 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
279 function interface_mt:_lock(nointerface, noreading, nowriting) -- lock or unlock this interface or events |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
280 self.nointerface, self.noreading, self.nowriting = nointerface, noreading, nowriting |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
281 return nointerface, noreading, nowriting |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
282 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
283 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
284 function interface_mt:counter(c) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
285 if c then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
286 self._connections = self._connections + c |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
287 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
288 return self._connections |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
289 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
290 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
291 -- Public methods |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
292 function interface_mt:write(data) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
293 if self.nowriting then return nil, "locked" end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
294 --vdebug( "try to send data to client, id/data:", self.id, data ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
295 data = tostring( data ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
296 local len = string_len( data ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
297 local total = len + self.writebufferlen |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
298 if total > cfg.MAX_SEND_LENGTH then -- check buffer length |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
299 local err = "send buffer exceeded" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
300 debug( "error:", err ) -- to much, check your app |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
301 return nil, err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
302 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
303 self.writebuffer = self.writebuffer .. data -- new buffer |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
304 self.writebufferlen = total |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
305 if not self.eventwrite then -- register new write event |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
306 --vdebug( "register new write event" ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
307 self.eventwrite = addevent( base, self.conn, EV_WRITE, self.writecallback, cfg.WRITE_TIMEOUT ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
308 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
309 return true |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
310 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
311 function interface_mt:close(now) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
312 if self.nointerface then return nil, "locked"; end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
313 debug( "try to close client connection with id:", self.id ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
314 if self.type == "client" then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
315 self.fatalerror = "client to close" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
316 if ( not self.eventwrite ) or now then -- try to close immediately |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
317 self:_lock( true, true, true ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
318 self:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
319 return true |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
320 else -- wait for incomplete write request |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
321 self:_lock( true, true, false ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
322 debug "closing delayed until writebuffer is empty" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
323 return nil, "writebuffer not empty, waiting" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
324 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
325 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
326 debug( "try to close server with id:", self.id, "args:", now ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
327 self.fatalerror = "server to close" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
328 self:_lock( true ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
329 local count = 0 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
330 for _, item in ipairs( interfacelist( ) ) do |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
331 if ( item.type ~= "server" ) and ( item._server == self ) then -- client/server match |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
332 if item:close( now ) then -- writebuffer was empty |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
333 count = count + 1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
334 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
335 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
336 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
337 local timeout = 0 -- dont wait for unfinished writebuffers of clients... |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
338 if not now then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
339 timeout = cfg.WRITE_TIMEOUT -- ...or wait for it |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
340 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
341 self:_close( timeout ) -- add new event to remove the server interface |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
342 debug( "seconds remained until server is closed:", timeout ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
343 return count -- returns finished clients with empty writebuffer |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
344 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
345 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
346 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
347 function interface_mt:server() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
348 return self._server or self; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
349 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
350 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
351 function interface_mt:port() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
352 return self._port |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
353 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
354 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
355 function interface_mt:ip() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
356 return self._ip |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
357 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
358 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
359 function interface_mt:ssl() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
360 return self._usingssl |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
361 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
362 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
363 function interface_mt:type() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
364 return self._type or "client" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
365 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
366 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
367 function interface_mt:connections() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
368 return self._connections |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
369 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
370 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
371 function interface_mt:address() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
372 return self.addr |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
373 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
374 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
375 function interface_mt:set_sslctx(sslctx) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
376 self._sslctx = sslctx; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
377 if sslctx then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
378 self.starttls = nil; -- use starttls() of interface_mt |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
379 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
380 self.starttls = false; -- prevent starttls() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
381 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
382 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
383 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
384 function interface_mt:set_send(new_send) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
385 -- No-op, we always use the underlying connection's send |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
386 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
387 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
388 function interface_mt:starttls() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
389 debug( "try to start ssl at client id:", self.id ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
390 local err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
391 if not self._sslctx then -- no ssl available |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
392 err = "no ssl context available" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
393 elseif self._usingssl then -- startssl was already called |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
394 err = "ssl already active" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
395 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
396 if err then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
397 debug( "error:", err ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
398 return nil, err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
399 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
400 self._usingssl = true |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
401 self.startsslcallback = function( ) -- we have to start the handshake outside of a read/write event |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
402 self.startsslcallback = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
403 self:_start_ssl(); |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
404 self.eventstarthandshake = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
405 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
406 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
407 if not self.eventwrite then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
408 self:_lock( true, true, true ) -- lock the interface, to not disturb the handshake |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
409 self.eventstarthandshake = addevent( base, nil, EV_TIMEOUT, self.startsslcallback, 0 ) -- add event to start handshake |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
410 else -- wait until writebuffer is empty |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
411 self:_lock( true, true, false ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
412 debug "ssl session delayed until writebuffer is empty..." |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
413 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
414 return true |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
415 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
416 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
417 -- Stub handlers |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
418 function interface_mt:onconnect() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
419 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
420 function interface_mt:onincoming() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
421 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
422 function interface_mt:ondisconnect() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
423 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
424 function interface_mt:ontimeout() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
425 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
426 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
427 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
428 -- End of client interface methods |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
429 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
430 local handleclient; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
431 do |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
432 local string_sub = string.sub -- caching table lookups |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
433 local string_len = string.len |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
434 local addevent = base.addevent |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
435 local coroutine_wrap = coroutine.wrap |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
436 local socket_gettime = socket.gettime |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
437 local coroutine_yield = coroutine.yield |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
438 function handleclient( client, ip, port, server, pattern, listener, _, sslctx ) -- creates an client interface |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
439 --vdebug("creating client interfacce...") |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
440 local interface = { |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
441 type = "client"; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
442 conn = client; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
443 currenttime = socket_gettime( ); -- safe the origin |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
444 writebuffer = ""; -- writebuffer |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
445 writebufferlen = 0; -- length of writebuffer |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
446 send = client.send; -- caching table lookups |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
447 receive = client.receive; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
448 onconnect = listener.onconnect; -- will be called when client disconnects |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
449 ondisconnect = listener.ondisconnect; -- will be called when client disconnects |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
450 onincoming = listener.onincoming; -- will be called when client sends data |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
451 ontimeout = listener.ontimeout; -- called when fatal socket timeout occurs |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
452 eventread = false, eventwrite = false, eventclose = false, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
453 eventhandshake = false, eventstarthandshake = false; -- event handler |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
454 eventconnect = false, eventsession = false; -- more event handler... |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
455 eventwritetimeout = false; -- even more event handler... |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
456 eventreadtimeout = false; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
457 fatalerror = false; -- error message |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
458 writecallback = false; -- will be called on write events |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
459 readcallback = false; -- will be called on read events |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
460 nointerface = true; -- lock/unlock parameter of this interface |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
461 noreading = false, nowriting = false; -- locks of the read/writecallback |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
462 startsslcallback = false; -- starting handshake callback |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
463 position = false; -- position of client in interfacelist |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
464 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
465 -- Properties |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
466 _ip = ip, _port = port, _server = server, _pattern = pattern, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
467 _sslctx = sslctx; -- parameters |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
468 _usingssl = false; -- client is using ssl; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
469 } |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
470 if not sslctx then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
471 interface.starttls = false -- don't allow TLS |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
472 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
473 interface.id = tostring(interface):match("%x+$"); |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
474 interface.writecallback = function( event ) -- called on write events |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
475 --vdebug( "new client write event, id/ip/port:", interface, ip, port ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
476 if interface.nowriting or ( interface.fatalerror and ( "client to close" ~= interface.fatalerror ) ) then -- leave this event |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
477 --vdebug( "leaving this event because:", interface.nowriting or interface.fatalerror ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
478 interface.eventwrite = false |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
479 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
480 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
481 if EV_TIMEOUT == event then -- took too long to write some data to socket -> disconnect |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
482 interface.fatalerror = "timeout during writing" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
483 debug( "writing failed:", interface.fatalerror ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
484 interface:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
485 interface.eventwrite = false |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
486 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
487 else -- can write :) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
488 if interface._usingssl then -- handle luasec |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
489 if interface.eventreadtimeout then -- we have to read first |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
490 local ret = interface.readcallback( ) -- call readcallback |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
491 --vdebug( "tried to read in writecallback, result:", ret ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
492 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
493 if interface.eventwritetimeout then -- luasec only |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
494 interface.eventwritetimeout:close( ) -- first we have to close timeout event which where regged after a wantread error |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
495 interface.eventwritetimeout = false |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
496 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
497 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
498 local succ, err, byte = interface.conn:send( interface.writebuffer, 1, interface.writebufferlen ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
499 --vdebug( "write data:", interface.writebuffer, "error:", err, "part:", byte ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
500 if succ then -- writing succesful |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
501 interface.writebuffer = "" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
502 interface.writebufferlen = 0 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
503 if interface.fatalerror then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
504 debug "closing client after writing" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
505 interface:_close() -- close interface if needed |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
506 elseif interface.startsslcallback then -- start ssl connection if needed |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
507 debug "starting ssl handshake after writing" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
508 interface.eventstarthandshake = addevent( base, nil, EV_TIMEOUT, interface.startsslcallback, 0 ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
509 elseif interface.eventreadtimeout then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
510 return EV_WRITE, EV_TIMEOUT |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
511 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
512 interface.eventwrite = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
513 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
514 elseif byte then -- want write again |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
515 --vdebug( "writebuffer is not empty:", err ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
516 interface.writebuffer = string_sub( interface.writebuffer, byte + 1, interface.writebufferlen ) -- new buffer |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
517 interface.writebufferlen = interface.writebufferlen - byte |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
518 if "wantread" == err then -- happens only with luasec |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
519 local callback = function( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
520 interface:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
521 interface.eventwritetimeout = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
522 return evreturn, evtimeout |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
523 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
524 interface.eventwritetimeout = addevent( base, nil, EV_TIMEOUT, callback, cfg.WRITE_TIMEOUT ) -- reg a new timeout event |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
525 debug( "wantread during write attemp, reg it in readcallback but dont know what really happens next..." ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
526 -- hopefully this works with luasec; its simply not possible to use 2 different write events on a socket in luaevent |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
527 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
528 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
529 return EV_WRITE, cfg.WRITE_TIMEOUT |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
530 else -- connection was closed during writing or fatal error |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
531 interface.fatalerror = err or "fatal error" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
532 debug( "connection failed in write event:", interface.fatalerror ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
533 interface:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
534 interface.eventwrite = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
535 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
536 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
537 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
538 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
539 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
540 interface.readcallback = function( event ) -- called on read events |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
541 --vdebug( "new client read event, id/ip/port:", tostring(interface.id), tostring(ip), tostring(port) ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
542 if interface.noreading or interface.fatalerror then -- leave this event |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
543 --vdebug( "leaving this event because:", tostring(interface.noreading or interface.fatalerror) ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
544 interface.eventread = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
545 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
546 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
547 if EV_TIMEOUT == event then -- took too long to get some data from client -> disconnect |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
548 interface.fatalerror = "timeout during receiving" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
549 debug( "connection failed:", interface.fatalerror ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
550 interface:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
551 interface.eventread = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
552 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
553 else -- can read |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
554 if interface._usingssl then -- handle luasec |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
555 if interface.eventwritetimeout then -- ok, in the past writecallback was regged |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
556 local ret = interface.writecallback( ) -- call it |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
557 --vdebug( "tried to write in readcallback, result:", tostring(ret) ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
558 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
559 if interface.eventreadtimeout then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
560 interface.eventreadtimeout:close( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
561 interface.eventreadtimeout = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
562 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
563 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
564 local buffer, err, part = interface.conn:receive( pattern ) -- receive buffer with "pattern" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
565 --vdebug( "read data:", tostring(buffer), "error:", tostring(err), "part:", tostring(part) ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
566 buffer = buffer or part or "" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
567 local len = string_len( buffer ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
568 if len > cfg.MAX_READ_LENGTH then -- check buffer length |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
569 interface.fatalerror = "receive buffer exceeded" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
570 debug( "fatal error:", interface.fatalerror ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
571 interface:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
572 interface.eventread = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
573 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
574 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
575 interface.onincoming( interface, buffer, err ) -- send new data to listener |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
576 if err and ( err ~= "timeout" and err ~= "wantread" ) then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
577 if "wantwrite" == err then -- need to read on write event |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
578 if not interface.eventwrite then -- register new write event if needed |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
579 interface.eventwrite = addevent( base, interface.conn, EV_WRITE, interface.writecallback, cfg.WRITE_TIMEOUT ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
580 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
581 interface.eventreadtimeout = addevent( base, nil, EV_TIMEOUT, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
582 function( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
583 interface:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
584 end, cfg.READ_TIMEOUT |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
585 ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
586 debug( "wantwrite during read attemp, reg it in writecallback but dont know what really happens next..." ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
587 -- to be honest i dont know what happens next, if it is allowed to first read, the write etc... |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
588 else -- connection was closed or fatal error |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
589 interface.fatalerror = err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
590 debug( "connection failed in read event:", interface.fatalerror ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
591 interface:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
592 interface.eventread = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
593 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
594 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
595 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
596 return EV_READ, cfg.READ_TIMEOUT |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
597 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
598 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
599 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
600 client:settimeout( 0 ) -- set non blocking |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
601 setmetatable(interface, interface_mt) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
602 interfacelist( "add", interface ) -- add to interfacelist |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
603 return interface |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
604 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
605 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
606 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
607 local handleserver |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
608 do |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
609 function handleserver( server, addr, port, pattern, listener, sslctx, startssl ) -- creates an server interface |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
610 debug "creating server interface..." |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
611 local interface = { |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
612 _connections = 0; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
613 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
614 conn = server; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
615 onconnect = listener.onconnect; -- will be called when new client connected |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
616 eventread = false; -- read event handler |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
617 eventclose = false; -- close event handler |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
618 readcallback = false; -- read event callback |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
619 fatalerror = false; -- error message |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
620 nointerface = true; -- lock/unlock parameter |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
621 } |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
622 interface.id = tostring(interface):match("%x+$"); |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
623 interface.readcallback = function( event ) -- server handler, called on incoming connections |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
624 --vdebug( "server can accept, id/addr/port:", interface, addr, port ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
625 if interface.fatalerror then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
626 --vdebug( "leaving this event because:", self.fatalerror ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
627 interface.eventread = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
628 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
629 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
630 local delay = cfg.ACCEPT_DELAY |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
631 if EV_TIMEOUT == event then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
632 if interface._connections >= cfg.MAX_CONNECTIONS then -- check connection count |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
633 debug( "to many connections, seconds to wait for next accept:", delay ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
634 return EV_TIMEOUT, delay -- timeout... |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
635 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
636 return EV_READ -- accept again |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
637 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
638 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
639 --vdebug("max connection check ok, accepting...") |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
640 local client, err = server:accept() -- try to accept; TODO: check err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
641 while client do |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
642 if interface._connections >= cfg.MAX_CONNECTIONS then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
643 client:close( ) -- refuse connection |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
644 debug( "maximal connections reached, refuse client connection; accept delay:", delay ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
645 return EV_TIMEOUT, delay -- delay for next accept attemp |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
646 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
647 local ip, port = client:getpeername( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
648 interface._connections = interface._connections + 1 -- increase connection count |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
649 local clientinterface = handleclient( client, ip, port, interface, pattern, listener, nil, sslctx ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
650 --vdebug( "client id:", clientinterface, "startssl:", startssl ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
651 if startssl then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
652 clientinterface:_start_ssl( clientinterface.onconnect ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
653 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
654 clientinterface:_start_session( clientinterface.onconnect ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
655 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
656 debug( "accepted incoming client connection from:", ip, port ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
657 client, err = server:accept() -- try to accept again |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
658 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
659 return EV_READ |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
660 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
661 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
662 server:settimeout( 0 ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
663 setmetatable(interface, interface_mt) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
664 interfacelist( "add", interface ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
665 interface:_start_session() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
666 return interface |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
667 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
668 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
669 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
670 local addserver = ( function( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
671 return function( addr, port, listener, pattern, sslcfg, startssl ) -- TODO: check arguments |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
672 --vdebug( "creating new tcp server with following parameters:", addr or "nil", port or "nil", sslcfg or "nil", startssl or "nil") |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
673 local server, err = socket.bind( addr, port, cfg.ACCEPT_QUEUE ) -- create server socket |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
674 if not server then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
675 debug( "creating server socket failed because:", err ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
676 return nil, err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
677 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
678 local sslctx |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
679 if sslcfg then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
680 if not ssl then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
681 debug "fatal error: luasec not found" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
682 return nil, "luasec not found" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
683 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
684 sslctx, err = ssl.newcontext( sslcfg ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
685 if err then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
686 debug( "error while creating new ssl context for server socket:", err ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
687 return nil, err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
688 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
689 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
690 local interface = handleserver( server, addr, port, pattern, listener, sslctx, startssl ) -- new server handler |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
691 debug( "new server created with id:", tostring(interface)) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
692 return interface |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
693 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
694 end )( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
695 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
696 local addclient, wrapclient |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
697 do |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
698 function wrapclient( client, ip, port, listeners, pattern, sslctx, startssl ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
699 local interface = handleclient( client, ip, port, nil, pattern, listeners, sslctx ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
700 interface:_start_session() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
701 return interface |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
702 --function handleclient( client, ip, port, server, pattern, listener, _, sslctx ) -- creates an client interface |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
703 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
704 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
705 function addclient( addr, serverport, listener, pattern, localaddr, localport, sslcfg, startssl ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
706 local client, err = socket.tcp() -- creating new socket |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
707 if not client then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
708 debug( "cannot create socket:", err ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
709 return nil, err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
710 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
711 client:settimeout( 0 ) -- set nonblocking |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
712 if localaddr then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
713 local res, err = client:bind( localaddr, localport, -1 ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
714 if not res then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
715 debug( "cannot bind client:", err ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
716 return nil, err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
717 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
718 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
719 local sslctx |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
720 if sslcfg then -- handle ssl/new context |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
721 if not ssl then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
722 debug "need luasec, but not available" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
723 return nil, "luasec not found" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
724 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
725 sslctx, err = ssl.newcontext( sslcfg ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
726 if err then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
727 debug( "cannot create new ssl context:", err ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
728 return nil, err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
729 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
730 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
731 local res, err = client:connect( addr, serverport ) -- connect |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
732 if res or ( err == "timeout" ) then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
733 local ip, port = client:getsockname( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
734 local server = function( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
735 return nil, "this is a dummy server interface" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
736 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
737 local interface = wrapclient( client, ip, serverport, listeners, pattern, sslctx, startssl ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
738 interface:_start_connection( startssl ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
739 debug( "new connection id:", interface.id ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
740 return interface, err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
741 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
742 debug( "new connection failed:", err ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
743 return nil, err |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
744 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
745 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
746 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
747 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
748 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
749 local loop = function( ) -- starts the event loop |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
750 return base:loop( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
751 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
752 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
753 local newevent = ( function( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
754 local add = base.addevent |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
755 return function( ... ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
756 return add( base, ... ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
757 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
758 end )( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
759 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
760 local closeallservers = function( arg ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
761 for _, item in ipairs( interfacelist( ) ) do |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
762 if item "type" == "server" then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
763 item( "close", arg ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
764 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
765 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
766 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
767 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
768 return { |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
769 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
770 cfg = cfg, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
771 base = base, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
772 loop = loop, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
773 event = event, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
774 event_base = base, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
775 addevent = newevent, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
776 addserver = addserver, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
777 addclient = addclient, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
778 wrapclient = wrapclient, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
779 closeallservers = closeallservers, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
780 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
781 __NAME = SCRIPT_NAME, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
782 __DATE = LAST_MODIFIED, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
783 __AUTHOR = SCRIPT_AUTHOR, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
784 __VERSION = SCRIPT_VERSION, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
785 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
786 } |