Diff

net/server_select.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 9993:02a41315d275
child 10234:d4390c427a66
line wrap: on
line diff
--- a/net/server_select.lua	Sat Aug 31 16:15:09 2019 +0200
+++ b/net/server_select.lua	Sun Sep 01 01:55:55 2019 +0200
@@ -266,7 +266,7 @@
 	return handler
 end
 
-wrapconnection = function( server, listeners, socket, ip, serverport, clientport, pattern, sslctx, ssldirect ) -- this function wraps a client to a handler object
+wrapconnection = function( server, listeners, socket, ip, serverport, clientport, pattern, sslctx, ssldirect, extra ) -- this function wraps a client to a handler object
 
 	if socket:getfd() >= _maxfd then
 		out_error("server.lua: Disallowed FD number: "..socket:getfd()) -- PROTIP: Switch to libevent
@@ -316,6 +316,8 @@
 
 	local handler = bufferqueue -- saves a table ^_^
 
+	handler.extra = extra
+
 	handler.dispatch = function( )
 		return dispatch
 	end
@@ -1005,8 +1007,8 @@
 
 --// EXPERIMENTAL //--
 
-local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx )
-	local handler, socket, err = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx, sslctx)
+local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx, extra )
+	local handler, socket, err = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx, sslctx, extra)
 	if not handler then return nil, err end
 	_socketlist[ socket ] = handler
 	if not sslctx then
@@ -1025,7 +1027,7 @@
 	return handler, socket
 end
 
-local addclient = function( address, port, listeners, pattern, sslctx, typ )
+local addclient = function( address, port, listeners, pattern, sslctx, typ, extra )
 	local err
 	if type( listeners ) ~= "table" then
 		err = "invalid listener table"
@@ -1062,7 +1064,7 @@
 	client:settimeout( 0 )
 	local ok, err = client:setpeername( address, port )
 	if ok or err == "timeout" or err == "Operation already in progress" then
-		return wrapclient( client, address, port, listeners, pattern, sslctx )
+		return wrapclient( client, address, port, listeners, pattern, sslctx, extra )
 	else
 		return nil, err
 	end