Software /
code /
prosody
Annotate
net/server_event.lua @ 4192:c1e4d8326ab0
net.httpserver: Catch errors thrown in HTTP handlers.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Wed, 23 Feb 2011 02:03:55 +0500 |
parent | 4111:4716c7645ad2 |
child | 4377:705a8f14427b |
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 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
|
17 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
|
18 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
|
19 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
|
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 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
|
22 MAX_CONNECTIONS = 100000, -- max per server connections (use "ulimit -n" on *nix) |
3237
721a83a3beb9
net.server_event: attemp to fix more spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
3149
diff
changeset
|
23 MAX_HANDSHAKE_ATTEMPTS= 1000, -- attempts to finish ssl handshake |
721a83a3beb9
net.server_event: attemp to fix more spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
3149
diff
changeset
|
24 HANDSHAKE_TIMEOUT = 60, -- timeout in seconds per handshake attempt |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
25 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
|
26 MAX_SEND_LENGTH = 1024 * 1024 * 1024 * 1024, -- max bytes size of write buffer (for writing on sockets) |
2952
5069534f7f36
net.server_event: Fixed spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
2941
diff
changeset
|
27 ACCEPT_DELAY = 10, -- seconds to wait until the next attempt of a full server to accept |
2939
dc73cbc69bd5
net.server_event: Bump timeout values up somewhat
Matthew Wild <mwild1@gmail.com>
parents:
2731
diff
changeset
|
28 READ_TIMEOUT = 60 * 60 * 6, -- timeout in seconds for read data from socket |
dc73cbc69bd5
net.server_event: Bump timeout values up somewhat
Matthew Wild <mwild1@gmail.com>
parents:
2731
diff
changeset
|
29 WRITE_TIMEOUT = 180, -- timeout in seconds for write data on socket |
2952
5069534f7f36
net.server_event: Fixed spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
2941
diff
changeset
|
30 CONNECT_TIMEOUT = 20, -- timeout in seconds for connection attempts |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
31 CLEAR_DELAY = 5, -- seconds to wait for clearing interface list (and calling ondisconnect listeners) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
32 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
|
33 } |
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 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 |
2599
cc6164d85024
net.server_event: Fixes for traceback with no LuaSec
Matthew Wild <mwild1@gmail.com>
parents:
2561
diff
changeset
|
46 local ssl = use "ssl" |
2367
22dc9005360f
net.server_event: Load ssl/socket libraries if not already available
Matthew Wild <mwild1@gmail.com>
parents:
2340
diff
changeset
|
47 local socket = use "socket" or require "socket" |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
48 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
49 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
|
50 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
51 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
|
52 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
|
53 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
54 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
|
55 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
56 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
|
57 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
|
58 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
|
59 end |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
60 return function( x, y ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
61 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
|
62 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
|
63 local limit = x > y and x or y |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
64 while p <= limit do |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
65 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
|
66 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
|
67 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
68 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
|
69 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
70 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
|
71 end |
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 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 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
|
78 local EV_TIMEOUT = event.EV_TIMEOUT |
2434
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
79 local EV_SIGNAL = event.EV_SIGNAL |
2318
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 |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
106 return len |
2318
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 ) |
3237
721a83a3beb9
net.server_event: attemp to fix more spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
3149
diff
changeset
|
139 if EV_TIMEOUT == event then -- timeout during connection |
2318
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 |
2599
cc6164d85024
net.server_event: Fixes for traceback with no LuaSec
Matthew Wild <mwild1@gmail.com>
parents:
2561
diff
changeset
|
145 if plainssl and ssl then -- start ssl session |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
146 self:starttls(nil, true) |
2318
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 |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
148 self:_start_session(true) |
2318
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 |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
158 function interface_mt:_start_session(call_onconnect) -- new session, for example after startssl |
2318
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 ) |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
162 --vdebug( "start listening on client socket with id:", self.id ) |
2972
f9901175d535
net.server_event: Call listener.onconnect if it exists, otherwise stick to listener.onincoming
Matthew Wild <mwild1@gmail.com>
parents:
2952
diff
changeset
|
163 self.eventread = addevent( base, self.conn, EV_READ, self.readcallback, cfg.READ_TIMEOUT ); -- register callback |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
164 if call_onconnect then |
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
165 self:onconnect() |
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
166 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
167 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
|
168 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
|
169 end |
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.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
|
171 else |
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:_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
|
173 --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
|
174 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
|
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 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
|
177 end |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
178 function interface_mt:_start_ssl(call_onconnect) -- old socket will be destroyed, therefore we have to close read/write events first |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
179 --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
|
180 local _ |
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 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
|
182 _ = 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
|
183 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
|
184 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
|
185 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
|
186 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
|
187 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
|
188 self.conn = nil -- cannot be used anymore |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
189 if call_onconnect then |
2318
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.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
|
191 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
192 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
|
193 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
|
194 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
|
195 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
196 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
|
197 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
|
198 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
|
199 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
|
200 local attempt = 0 |
3237
721a83a3beb9
net.server_event: attemp to fix more spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
3149
diff
changeset
|
201 local maxattempt = cfg.MAX_HANDSHAKE_ATTEMPTS |
2656
9fab59009397
net.server_event: Increase SSL handshake timeout to 30s, make handshake round-trip count configurable, and bump connect timeout to 20s (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
2641
diff
changeset
|
202 while attempt < maxattempt do -- no endless loop |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
203 attempt = attempt + 1 |
3237
721a83a3beb9
net.server_event: attemp to fix more spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
3149
diff
changeset
|
204 debug( "ssl handshake of client with id:"..tostring(self)..", attempt:"..attempt ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
205 if attempt > maxattempt then |
3237
721a83a3beb9
net.server_event: attemp to fix more spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
3149
diff
changeset
|
206 self.fatalerror = "max handshake attempts exceeded" |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
207 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
|
208 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
|
209 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
210 _, 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 local onsomething |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
216 if not call_onconnect then -- trigger listener |
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
217 self:onstatus("ssl-handshake-complete"); |
2318
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 |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
219 self:_start_session( call_onconnect ) |
2318
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 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
|
225 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
|
226 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
|
227 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
|
228 else |
4111
4716c7645ad2
net.server_select, net.server_event: Silence 'error during ssl handshake' for wantread and wantwrite
Matthew Wild <mwild1@gmail.com>
parents:
4074
diff
changeset
|
229 debug( "ssl handshake error:", err ) |
2318
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 |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
231 end |
2318
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 |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
234 if call_onconnect then |
2318
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..." |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
247 self:_lock( false, true, true ) -- unlock read/write events, but keep interface locked |
2318
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( ) |
2660
9e5b21b7b2f9
net.server_event: Pass false as connection error to disconnect handler if close was initiated by us
Matthew Wild <mwild1@gmail.com>
parents:
2656
diff
changeset
|
264 _ = self.ondisconnect and self:ondisconnect( self.fatalerror ~= "client to close" and self.fatalerror) -- call ondisconnect listener (wont be the case if handshake failed on connect) |
2318
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 |
2708
d9c4ade80ae9
net.server_event: Add lock_read() method to connections
Matthew Wild <mwild1@gmail.com>
parents:
2660
diff
changeset
|
283 |
3000
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
284 --TODO: Deprecate |
2708
d9c4ade80ae9
net.server_event: Add lock_read() method to connections
Matthew Wild <mwild1@gmail.com>
parents:
2660
diff
changeset
|
285 function interface_mt:lock_read(switch) |
3000
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
286 if switch then |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
287 return self:pause(); |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
288 else |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
289 return self:resume(); |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
290 end |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
291 end |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
292 |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
293 function interface_mt:pause() |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
294 return self:_lock(self.nointerface, true, self.nowriting); |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
295 end |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
296 |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
297 function interface_mt:resume() |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
298 return self:_lock(self.nointerface, false, self.nowriting); |
2708
d9c4ade80ae9
net.server_event: Add lock_read() method to connections
Matthew Wild <mwild1@gmail.com>
parents:
2660
diff
changeset
|
299 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
300 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
301 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
|
302 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
|
303 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
|
304 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
305 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
|
306 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
307 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
308 -- 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
|
309 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
|
310 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
|
311 --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
|
312 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
|
313 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
|
314 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
|
315 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
|
316 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
|
317 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
|
318 return nil, err |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
319 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
320 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
|
321 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
|
322 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
|
323 --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
|
324 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
|
325 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
326 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
|
327 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
328 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 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
|
338 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
|
339 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
|
340 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
|
341 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
342 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
343 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
|
344 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
|
345 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
|
346 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
|
347 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
|
348 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
|
349 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
|
350 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
|
351 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
352 end |
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 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
|
355 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
|
356 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
|
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 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
|
359 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
|
360 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
|
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 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
363 |
3649
ae99396ee261
net.server_event: API parity with net.server_socket
Paul Aurich <paul@darkrain42.org>
parents:
3458
diff
changeset
|
364 function interface_mt:socket() |
ae99396ee261
net.server_event: API parity with net.server_socket
Paul Aurich <paul@darkrain42.org>
parents:
3458
diff
changeset
|
365 return self.conn |
ae99396ee261
net.server_event: API parity with net.server_socket
Paul Aurich <paul@darkrain42.org>
parents:
3458
diff
changeset
|
366 end |
ae99396ee261
net.server_event: API parity with net.server_socket
Paul Aurich <paul@darkrain42.org>
parents:
3458
diff
changeset
|
367 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
368 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
|
369 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
|
370 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
371 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
372 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
|
373 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
|
374 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
375 |
2374
9b6a9d521620
net.server_event: Add :serverport() method to be compatible with old API
Matthew Wild <mwild1@gmail.com>
parents:
2367
diff
changeset
|
376 function interface_mt:serverport() |
9b6a9d521620
net.server_event: Add :serverport() method to be compatible with old API
Matthew Wild <mwild1@gmail.com>
parents:
2367
diff
changeset
|
377 return self._serverport |
9b6a9d521620
net.server_event: Add :serverport() method to be compatible with old API
Matthew Wild <mwild1@gmail.com>
parents:
2367
diff
changeset
|
378 end |
9b6a9d521620
net.server_event: Add :serverport() method to be compatible with old API
Matthew Wild <mwild1@gmail.com>
parents:
2367
diff
changeset
|
379 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
380 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
|
381 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
|
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:ssl() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
385 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
|
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:type() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
389 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
|
390 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
391 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
392 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
|
393 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
|
394 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
395 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
396 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
|
397 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
|
398 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
399 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
400 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
|
401 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
|
402 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
|
403 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
|
404 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
405 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
|
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 end |
3001
fa00c6f563b4
net.server_select, net.server_event: Rename :pattern() method to :set_mode() for select backend, and add :set_mode() to event backend
Matthew Wild <mwild1@gmail.com>
parents:
3000
diff
changeset
|
408 |
fa00c6f563b4
net.server_select, net.server_event: Rename :pattern() method to :set_mode() for select backend, and add :set_mode() to event backend
Matthew Wild <mwild1@gmail.com>
parents:
3000
diff
changeset
|
409 function interface_mt:set_mode(pattern) |
fa00c6f563b4
net.server_select, net.server_event: Rename :pattern() method to :set_mode() for select backend, and add :set_mode() to event backend
Matthew Wild <mwild1@gmail.com>
parents:
3000
diff
changeset
|
410 if pattern then |
fa00c6f563b4
net.server_select, net.server_event: Rename :pattern() method to :set_mode() for select backend, and add :set_mode() to event backend
Matthew Wild <mwild1@gmail.com>
parents:
3000
diff
changeset
|
411 self._pattern = pattern; |
fa00c6f563b4
net.server_select, net.server_event: Rename :pattern() method to :set_mode() for select backend, and add :set_mode() to event backend
Matthew Wild <mwild1@gmail.com>
parents:
3000
diff
changeset
|
412 end |
fa00c6f563b4
net.server_select, net.server_event: Rename :pattern() method to :set_mode() for select backend, and add :set_mode() to event backend
Matthew Wild <mwild1@gmail.com>
parents:
3000
diff
changeset
|
413 return self._pattern; |
fa00c6f563b4
net.server_select, net.server_event: Rename :pattern() method to :set_mode() for select backend, and add :set_mode() to event backend
Matthew Wild <mwild1@gmail.com>
parents:
3000
diff
changeset
|
414 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
415 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
416 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
|
417 -- 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
|
418 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
419 |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
420 function interface_mt:starttls(sslctx, call_onconnect) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
421 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
|
422 local err |
2561
e7bec4072c61
net.server_event: Update for compatibility with new server API
Matthew Wild <mwild1@gmail.com>
parents:
2535
diff
changeset
|
423 self._sslctx = sslctx; |
e7bec4072c61
net.server_event: Update for compatibility with new server API
Matthew Wild <mwild1@gmail.com>
parents:
2535
diff
changeset
|
424 if self._usingssl then -- startssl was already called |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
425 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
|
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 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
|
428 debug( "error:", err ) |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
429 return nil, err |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
430 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
431 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
|
432 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
|
433 self.startsslcallback = nil |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
434 self:_start_ssl(call_onconnect); |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
435 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
|
436 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
|
437 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
438 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
|
439 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
|
440 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
|
441 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
|
442 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
|
443 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
|
444 end |
2462
2d81cc25b5a6
net.server_event: Remove access to starttls method when starttls is negotiated - fixes offering starttls to client who are already using it (thanks darkrain and Adium)
Matthew Wild <mwild1@gmail.com>
parents:
2453
diff
changeset
|
445 self.starttls = false; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
446 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
|
447 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
448 |
2453
44d4c69155e0
net.server_event: Add conn:setoption() to set options on the socket
Matthew Wild <mwild1@gmail.com>
parents:
2434
diff
changeset
|
449 function interface_mt:setoption(option, value) |
2478
7be72eca5666
net.server_{select,event}: Fail gracefully if socket doesn't have a setoption method (LuaSec issue I think)
Matthew Wild <mwild1@gmail.com>
parents:
2477
diff
changeset
|
450 if self.conn.setoption then |
7be72eca5666
net.server_{select,event}: Fail gracefully if socket doesn't have a setoption method (LuaSec issue I think)
Matthew Wild <mwild1@gmail.com>
parents:
2477
diff
changeset
|
451 return self.conn:setoption(option, value); |
7be72eca5666
net.server_{select,event}: Fail gracefully if socket doesn't have a setoption method (LuaSec issue I think)
Matthew Wild <mwild1@gmail.com>
parents:
2477
diff
changeset
|
452 end |
7be72eca5666
net.server_{select,event}: Fail gracefully if socket doesn't have a setoption method (LuaSec issue I think)
Matthew Wild <mwild1@gmail.com>
parents:
2477
diff
changeset
|
453 return false, "setoption not implemented"; |
2453
44d4c69155e0
net.server_event: Add conn:setoption() to set options on the socket
Matthew Wild <mwild1@gmail.com>
parents:
2434
diff
changeset
|
454 end |
44d4c69155e0
net.server_event: Add conn:setoption() to set options on the socket
Matthew Wild <mwild1@gmail.com>
parents:
2434
diff
changeset
|
455 |
2534
1f46a9d9b16a
net.server_event: Support :setlistener() method on connections to work with port multiplexing
Matthew Wild <mwild1@gmail.com>
parents:
2478
diff
changeset
|
456 function interface_mt:setlistener(listener) |
2561
e7bec4072c61
net.server_event: Update for compatibility with new server API
Matthew Wild <mwild1@gmail.com>
parents:
2535
diff
changeset
|
457 self.onconnect, self.ondisconnect, self.onincoming, self.ontimeout, self.onstatus |
e7bec4072c61
net.server_event: Update for compatibility with new server API
Matthew Wild <mwild1@gmail.com>
parents:
2535
diff
changeset
|
458 = listener.onconnect, listener.ondisconnect, listener.onincoming, listener.ontimeout, listener.onstatus; |
2534
1f46a9d9b16a
net.server_event: Support :setlistener() method on connections to work with port multiplexing
Matthew Wild <mwild1@gmail.com>
parents:
2478
diff
changeset
|
459 end |
1f46a9d9b16a
net.server_event: Support :setlistener() method on connections to work with port multiplexing
Matthew Wild <mwild1@gmail.com>
parents:
2478
diff
changeset
|
460 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
461 -- 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
|
462 function interface_mt:onconnect() |
2988
0c61f73ac0fb
net.server_event: Be sure to fire onincoming for backwards-compatibility with pre-onconnect listeners (thanks darkrain/mod_console)
Matthew Wild <mwild1@gmail.com>
parents:
2972
diff
changeset
|
463 return self:onincoming(nil); |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
464 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
465 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
|
466 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
467 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
|
468 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
469 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
|
470 end |
3002
d1f864ed44c0
net.server_select, net.server_event: Support for ondrain listener callback for when send buffer is empty
Matthew Wild <mwild1@gmail.com>
parents:
3001
diff
changeset
|
471 function interface_mt:ondrain() |
d1f864ed44c0
net.server_select, net.server_event: Support for ondrain listener callback for when send buffer is empty
Matthew Wild <mwild1@gmail.com>
parents:
3001
diff
changeset
|
472 end |
2561
e7bec4072c61
net.server_event: Update for compatibility with new server API
Matthew Wild <mwild1@gmail.com>
parents:
2535
diff
changeset
|
473 function interface_mt:onstatus() |
e7bec4072c61
net.server_event: Update for compatibility with new server API
Matthew Wild <mwild1@gmail.com>
parents:
2535
diff
changeset
|
474 end |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
475 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
476 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
477 -- 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
|
478 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
479 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
|
480 do |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
481 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
|
482 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
|
483 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
|
484 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
|
485 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
|
486 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
|
487 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
|
488 --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
|
489 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
|
490 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
|
491 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
|
492 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
|
493 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
|
494 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
|
495 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
|
496 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
|
497 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
|
498 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
|
499 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
|
500 ontimeout = listener.ontimeout; -- called when fatal socket timeout occurs |
2561
e7bec4072c61
net.server_event: Update for compatibility with new server API
Matthew Wild <mwild1@gmail.com>
parents:
2535
diff
changeset
|
501 onstatus = listener.onstatus; -- called for status changes (e.g. of SSL/TLS) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
502 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
|
503 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
|
504 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
|
505 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
|
506 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
|
507 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
|
508 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
|
509 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
|
510 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
|
511 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
|
512 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
|
513 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
|
514 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
515 -- Properties |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
516 _ip = ip, _port = port, _server = server, _pattern = pattern, |
2374
9b6a9d521620
net.server_event: Add :serverport() method to be compatible with old API
Matthew Wild <mwild1@gmail.com>
parents:
2367
diff
changeset
|
517 _serverport = (server and server:port() or nil), |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
518 _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
|
519 _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
|
520 } |
2599
cc6164d85024
net.server_event: Fixes for traceback with no LuaSec
Matthew Wild <mwild1@gmail.com>
parents:
2561
diff
changeset
|
521 if not ssl then interface.starttls = false; end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
522 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
|
523 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
|
524 --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
|
525 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
|
526 --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
|
527 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
|
528 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
|
529 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
530 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
|
531 interface.fatalerror = "timeout during writing" |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
532 debug( "writing failed:", interface.fatalerror ) |
2318
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 = false |
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 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
|
537 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
|
538 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
|
539 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
|
540 --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
|
541 end |
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.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
|
543 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
|
544 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
|
545 end |
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 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
|
548 --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
|
549 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
|
550 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
|
551 interface.writebufferlen = 0 |
3002
d1f864ed44c0
net.server_select, net.server_event: Support for ondrain listener callback for when send buffer is empty
Matthew Wild <mwild1@gmail.com>
parents:
3001
diff
changeset
|
552 interface:ondrain(); |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
553 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
|
554 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
|
555 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
|
556 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
|
557 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
|
558 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
|
559 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
|
560 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
|
561 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
562 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
|
563 return -1 |
2941
a2b01936f8f9
net.server_event: Don't re-try a write if the connection closed (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
2939
diff
changeset
|
564 elseif byte and (err == "timeout" or err == "wantwrite") then -- want write again |
2318
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( "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
|
566 interface.writebuffer = string_sub( interface.writebuffer, byte + 1, interface.writebufferlen ) -- new buffer |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
567 interface.writebufferlen = interface.writebufferlen - byte |
2318
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 "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
|
569 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
|
570 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
|
571 interface.eventwritetimeout = nil |
2731
373a7c960409
net.server_event: Fix some global accesses
Matthew Wild <mwild1@gmail.com>
parents:
2726
diff
changeset
|
572 return -1; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
573 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
574 interface.eventwritetimeout = addevent( base, nil, EV_TIMEOUT, callback, cfg.WRITE_TIMEOUT ) -- reg a new timeout event |
3237
721a83a3beb9
net.server_event: attemp to fix more spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
3149
diff
changeset
|
575 debug( "wantread during write attempt, reg it in readcallback but dont know what really happens next..." ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
576 -- 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
|
577 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
|
578 end |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
579 return EV_WRITE, cfg.WRITE_TIMEOUT |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
580 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
|
581 interface.fatalerror = err or "fatal error" |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
582 debug( "connection failed in write event:", interface.fatalerror ) |
2318
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 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
|
585 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
|
586 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
587 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
588 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
589 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
590 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
|
591 --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
|
592 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
|
593 --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
|
594 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
|
595 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
|
596 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
597 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
|
598 interface.fatalerror = "timeout during receiving" |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
599 debug( "connection failed:", interface.fatalerror ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
600 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
|
601 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
|
602 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
|
603 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
|
604 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
|
605 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
|
606 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
|
607 --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
|
608 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
609 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
|
610 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
|
611 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
|
612 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
613 end |
3001
fa00c6f563b4
net.server_select, net.server_event: Rename :pattern() method to :set_mode() for select backend, and add :set_mode() to event backend
Matthew Wild <mwild1@gmail.com>
parents:
3000
diff
changeset
|
614 local buffer, err, part = interface.conn:receive( interface._pattern ) -- receive buffer with "pattern" |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
615 --vdebug( "read data:", tostring(buffer), "error:", tostring(err), "part:", tostring(part) ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
616 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
|
617 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
|
618 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
|
619 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
|
620 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
|
621 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
|
622 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
|
623 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
|
624 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
625 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
|
626 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
|
627 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
|
628 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
|
629 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
|
630 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
631 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
|
632 function( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
633 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
|
634 end, cfg.READ_TIMEOUT |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
635 ) |
3237
721a83a3beb9
net.server_event: attemp to fix more spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
3149
diff
changeset
|
636 debug( "wantwrite during read attempt, reg it in writecallback but dont know what really happens next..." ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
637 -- to be honest i dont know what happens next, if it is allowed to first read, the write etc... |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
638 else -- connection was closed or fatal error |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
639 interface.fatalerror = err |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
640 debug( "connection failed in read event:", interface.fatalerror ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
641 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
|
642 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
|
643 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
|
644 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
645 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
646 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
|
647 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
648 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
649 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
650 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
|
651 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
|
652 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
|
653 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
|
654 end |
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 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
657 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
|
658 do |
2561
e7bec4072c61
net.server_event: Update for compatibility with new server API
Matthew Wild <mwild1@gmail.com>
parents:
2535
diff
changeset
|
659 function handleserver( server, addr, port, pattern, listener, sslctx ) -- creates an server interface |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
660 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
|
661 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
|
662 _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
|
663 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
664 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
|
665 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
|
666 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
|
667 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
|
668 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
|
669 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
|
670 nointerface = true; -- lock/unlock parameter |
2375
31297a5641e3
net.server_event: Add properties like _ip and _port to server objects too
Matthew Wild <mwild1@gmail.com>
parents:
2374
diff
changeset
|
671 |
31297a5641e3
net.server_event: Add properties like _ip and _port to server objects too
Matthew Wild <mwild1@gmail.com>
parents:
2374
diff
changeset
|
672 _ip = addr, _port = port, _pattern = pattern, |
31297a5641e3
net.server_event: Add properties like _ip and _port to server objects too
Matthew Wild <mwild1@gmail.com>
parents:
2374
diff
changeset
|
673 _sslctx = sslctx; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
674 } |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
675 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
|
676 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
|
677 --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
|
678 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
|
679 --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
|
680 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
|
681 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
|
682 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
683 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
|
684 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
|
685 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
|
686 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
|
687 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
|
688 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
689 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
|
690 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
691 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
692 --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
|
693 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
|
694 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
|
695 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
|
696 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
|
697 debug( "maximal connections reached, refuse client connection; accept delay:", delay ) |
3237
721a83a3beb9
net.server_event: attemp to fix more spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
3149
diff
changeset
|
698 return EV_TIMEOUT, delay -- delay for next accept attempt |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
699 end |
2726
1b9179e04233
net.server_event: Improve and fix potential traceback in logging of new incoming connections
Matthew Wild <mwild1@gmail.com>
parents:
2708
diff
changeset
|
700 local client_ip, client_port = client:getpeername( ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
701 interface._connections = interface._connections + 1 -- increase connection count |
2726
1b9179e04233
net.server_event: Improve and fix potential traceback in logging of new incoming connections
Matthew Wild <mwild1@gmail.com>
parents:
2708
diff
changeset
|
702 local clientinterface = handleclient( client, client_ip, client_port, interface, pattern, listener, nil, sslctx ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
703 --vdebug( "client id:", clientinterface, "startssl:", startssl ) |
2599
cc6164d85024
net.server_event: Fixes for traceback with no LuaSec
Matthew Wild <mwild1@gmail.com>
parents:
2561
diff
changeset
|
704 if ssl and sslctx then |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
705 clientinterface:starttls(sslctx, true) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
706 else |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
707 clientinterface:_start_session( true ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
708 end |
2726
1b9179e04233
net.server_event: Improve and fix potential traceback in logging of new incoming connections
Matthew Wild <mwild1@gmail.com>
parents:
2708
diff
changeset
|
709 debug( "accepted incoming client connection from:", client_ip or "<unknown IP>", client_port or "<unknown port>", "to", port or "<unknown port>"); |
2641
b083a667e3be
net.server_event: Fire onincoming() callback for new connections as opposed to the new onconnect() - let's change this for a future version though
Matthew Wild <mwild1@gmail.com>
parents:
2639
diff
changeset
|
710 |
2318
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, 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
|
712 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
713 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
|
714 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
715 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
716 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
|
717 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
|
718 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
|
719 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
|
720 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
|
721 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
722 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
723 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
724 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
|
725 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
|
726 --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
|
727 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
|
728 if not server then |
4074
d2c9d9f569f9
net.server_event: Log interface and port number when binding fails
Matthew Wild <mwild1@gmail.com>
parents:
3649
diff
changeset
|
729 debug( "creating server socket on "..addr.." port "..port.." failed:", err ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
730 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
|
731 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
732 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
|
733 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
|
734 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
|
735 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
|
736 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
|
737 end |
2638
115104acdd7b
net.server_event: Fix traceback in creating SSL context
Matthew Wild <mwild1@gmail.com>
parents:
2599
diff
changeset
|
738 sslctx, err = sslcfg |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
739 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
|
740 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
|
741 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
|
742 end |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
743 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
744 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
|
745 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
|
746 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
|
747 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
748 end )( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
749 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
750 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
|
751 do |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
752 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
|
753 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
|
754 interface:_start_session() |
3149
5aca2f01c0f9
net.server_event: Also return the socket from wrapclient, to maintain compatibility with server_select (thanks uhoreg)
Matthew Wild <mwild1@gmail.com>
parents:
3003
diff
changeset
|
755 return interface, client |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
756 --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
|
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 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
759 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
|
760 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
|
761 if not client then |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
762 debug( "cannot create socket:", err ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
763 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
|
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 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
|
766 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
|
767 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
|
768 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
|
769 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
|
770 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
|
771 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
772 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
773 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
|
774 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
|
775 if not ssl then |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
776 debug "need luasec, but not available" |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
777 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
|
778 end |
2639
a7f683622b35
net.server_event: Fix another instance of creating an SSL context - certmanager now does this
Matthew Wild <mwild1@gmail.com>
parents:
2638
diff
changeset
|
779 sslctx, err = sslcfg |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
780 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
|
781 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
|
782 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
|
783 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
784 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
785 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
|
786 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
|
787 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
|
788 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
|
789 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
|
790 end |
2731
373a7c960409
net.server_event: Fix some global accesses
Matthew Wild <mwild1@gmail.com>
parents:
2726
diff
changeset
|
791 local interface = wrapclient( client, ip, serverport, listener, pattern, sslctx, startssl ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
792 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
|
793 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
|
794 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
|
795 else |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
796 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
|
797 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
|
798 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
799 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
800 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
801 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
802 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
803 local loop = function( ) -- starts the event loop |
2333
ed824115becf
net.server_event: Return "quitting" on loop exit to be compatible with net.server_select, and give Prosody a way to know why the loop broke
Matthew Wild <mwild1@gmail.com>
parents:
2318
diff
changeset
|
804 base:loop( ) |
ed824115becf
net.server_event: Return "quitting" on loop exit to be compatible with net.server_select, and give Prosody a way to know why the loop broke
Matthew Wild <mwild1@gmail.com>
parents:
2318
diff
changeset
|
805 return "quitting"; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
806 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
807 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
808 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
|
809 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
|
810 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
|
811 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
|
812 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
813 end )( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
814 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
815 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
|
816 for _, item in ipairs( interfacelist( ) ) do |
2334
4cedfeeaaed4
net.server_event: Fix closeallservers() to work correctly after the giant refactorizification
Matthew Wild <mwild1@gmail.com>
parents:
2333
diff
changeset
|
817 if item.type == "server" then |
4cedfeeaaed4
net.server_event: Fix closeallservers() to work correctly after the giant refactorizification
Matthew Wild <mwild1@gmail.com>
parents:
2333
diff
changeset
|
818 item:close( arg ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
819 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
820 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
821 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
822 |
2335
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
823 local function setquitting(yes) |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
824 if yes then |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
825 -- Quit now |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
826 closeallservers(); |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
827 base:loopexit(); |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
828 end |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
829 end |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
830 |
2340
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
831 function get_backend() |
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
832 return base:method(); |
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
833 end |
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
834 |
2434
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
835 -- We need to hold onto the events to stop them |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
836 -- being garbage-collected |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
837 local signal_events = {}; -- [signal_num] -> event object |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
838 function hook_signal(signal_num, handler) |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
839 local function _handler(event) |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
840 local ret = handler(); |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
841 if ret ~= false then -- Continue handling this signal? |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
842 return EV_SIGNAL; -- Yes |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
843 end |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
844 return -1; -- Close this event |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
845 end |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
846 signal_events[signal_num] = base:addevent(signal_num, EV_SIGNAL, _handler); |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
847 return signal_events[signal_num]; |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
848 end |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
849 |
3003
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
850 local function link(sender, receiver, buffersize) |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
851 local sender_locked; |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
852 |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
853 function receiver:ondrain() |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
854 if sender_locked then |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
855 sender:resume(); |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
856 sender_locked = nil; |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
857 end |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
858 end |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
859 |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
860 function sender:onincoming(data) |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
861 receiver:write(data); |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
862 if receiver.writebufferlen >= buffersize then |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
863 sender_locked = true; |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
864 sender:pause(); |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
865 end |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
866 end |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
867 end |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
868 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
869 return { |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
870 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
871 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
|
872 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
|
873 loop = loop, |
3003
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
874 link = link, |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
875 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
|
876 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
|
877 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
|
878 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
|
879 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
|
880 wrapclient = wrapclient, |
2336
fb1d9d2444df
Rename closeallservers to closeall in the public API (same as net.server_select) and expose setquitting
Matthew Wild <mwild1@gmail.com>
parents:
2335
diff
changeset
|
881 setquitting = setquitting, |
fb1d9d2444df
Rename closeallservers to closeall in the public API (same as net.server_select) and expose setquitting
Matthew Wild <mwild1@gmail.com>
parents:
2335
diff
changeset
|
882 closeall = closeallservers, |
2340
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
883 get_backend = get_backend, |
2434
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
884 hook_signal = hook_signal, |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
885 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
886 __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
|
887 __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
|
888 __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
|
889 __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
|
890 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
891 } |