Software /
code /
prosody
Comparison
net/server_epoll.lua @ 10233:600eee3c4752
net.server: Accept and save an 'extra' field for client connections
This lets code attach some extra data to be attached to client
connections.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 01 Sep 2019 01:55:55 +0200 |
parent | 10228:e77bf4222fae |
child | 10234:d4390c427a66 |
comparison
equal
deleted
inserted
replaced
10232:58685d77c07c | 10233:600eee3c4752 |
---|---|
558 self:on("disconnect", err); | 558 self:on("disconnect", err); |
559 self:destroy(); | 559 self:destroy(); |
560 end | 560 end |
561 end | 561 end |
562 | 562 |
563 local function wrapsocket(client, server, read_size, listeners, tls_ctx) -- luasocket object -> interface object | 563 local function wrapsocket(client, server, read_size, listeners, tls_ctx, extra) -- luasocket object -> interface object |
564 client:settimeout(0); | 564 client:settimeout(0); |
565 local conn = setmetatable({ | 565 local conn = setmetatable({ |
566 conn = client; | 566 conn = client; |
567 _server = server; | 567 _server = server; |
568 created = gettime(); | 568 created = gettime(); |
570 read_size = read_size or (server and server.read_size); | 570 read_size = read_size or (server and server.read_size); |
571 writebuffer = {}; | 571 writebuffer = {}; |
572 tls_ctx = tls_ctx or (server and server.tls_ctx); | 572 tls_ctx = tls_ctx or (server and server.tls_ctx); |
573 tls_direct = server and server.tls_direct; | 573 tls_direct = server and server.tls_direct; |
574 log = logger.init(("conn%s"):format(new_id())); | 574 log = logger.init(("conn%s"):format(new_id())); |
575 extra = extra; | |
575 }, interface_mt); | 576 }, interface_mt); |
576 | 577 |
577 conn:updatenames(); | 578 conn:updatenames(); |
578 return conn; | 579 return conn; |
579 end | 580 end |
699 tls_direct = tls_ctx and true or false; | 700 tls_direct = tls_ctx and true or false; |
700 }); | 701 }); |
701 end | 702 end |
702 | 703 |
703 -- COMPAT | 704 -- COMPAT |
704 local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx) | 705 local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx, extra) |
705 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx); | 706 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx, extra); |
706 if not client.peername then | 707 if not client.peername then |
707 client.peername, client.peerport = addr, port; | 708 client.peername, client.peerport = addr, port; |
708 end | 709 end |
709 local ok, err = client:init(); | 710 local ok, err = client:init(); |
710 if not ok then return ok, err; end | 711 if not ok then return ok, err; end |
713 end | 714 end |
714 return client; | 715 return client; |
715 end | 716 end |
716 | 717 |
717 -- New outgoing TCP connection | 718 -- New outgoing TCP connection |
718 local function addclient(addr, port, listeners, read_size, tls_ctx, typ) | 719 local function addclient(addr, port, listeners, read_size, tls_ctx, typ, extra) |
719 local create; | 720 local create; |
720 if not typ then | 721 if not typ then |
721 local n = inet_pton(addr); | 722 local n = inet_pton(addr); |
722 if not n then return nil, "invalid-ip"; end | 723 if not n then return nil, "invalid-ip"; end |
723 if #n == 16 then | 724 if #n == 16 then |
736 if not conn then return conn, err; end | 737 if not conn then return conn, err; end |
737 local ok, err = conn:settimeout(0); | 738 local ok, err = conn:settimeout(0); |
738 if not ok then return ok, err; end | 739 if not ok then return ok, err; end |
739 local ok, err = conn:setpeername(addr, port); | 740 local ok, err = conn:setpeername(addr, port); |
740 if not ok and err ~= "timeout" then return ok, err; end | 741 if not ok and err ~= "timeout" then return ok, err; end |
741 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx) | 742 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx, extra) |
742 local ok, err = client:init(); | 743 local ok, err = client:init(); |
743 if not ok then return ok, err; end | 744 if not ok then return ok, err; end |
744 if tls_ctx then | 745 if tls_ctx then |
745 client:starttls(tls_ctx); | 746 client:starttls(tls_ctx); |
746 end | 747 end |