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