Comparison

net/server_select.lua @ 8781:53178b6ba589

net.server: Add watchfd, a simple API for watching file descriptors
author Kim Alvefur <zash@zash.se>
date Wed, 09 May 2018 16:15:40 +0200
parent 8749:fb3cd6b2263b
child 8983:23f66f04e216
comparison
equal deleted inserted replaced
8780:4cab4ee5dfcc 8781:53178b6ba589
1032 else 1032 else
1033 return nil, err 1033 return nil, err
1034 end 1034 end
1035 end 1035 end
1036 1036
1037 local closewatcher = function (handler)
1038 local socket = handler.conn;
1039 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
1040 _readlistlen = removesocket( _readlist, socket, _readlistlen )
1041 _socketlist[ socket ] = nil
1042 end;
1043
1044 local addremove = function (handler, read, send)
1045 local socket = handler.conn
1046 _socketlist[ socket ] = handler
1047 if read ~= nil then
1048 if read then
1049 _readlistlen = addsocket( _readlist, socket, _readlistlen )
1050 else
1051 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
1052 end
1053 end
1054 if send ~= nil then
1055 if send then
1056 _sendlistlen = addsocket( _sendlist, socket, _sendlistlen )
1057 else
1058 _readlistlen = removesocket( _readlist, socket, _readlistlen )
1059 end
1060 end
1061 end
1062
1063 local watchfd = function ( fd, onreadable, onwriteable )
1064 local socket = fd
1065 if type(fd) == "number" then
1066 socket = { getfd = function () return fd; end }
1067 end
1068 local handler = {
1069 conn = socket;
1070 readbuffer = onreadable or id;
1071 sendbuffer = onwriteable or id;
1072 close = closewatcher;
1073 setflags = addremove;
1074 };
1075 addremove( handler, onreadable, onwriteable )
1076 return handler
1077 end
1078
1037 ----------------------------------// BEGIN //-- 1079 ----------------------------------// BEGIN //--
1038 1080
1039 use "setmetatable" ( _socketlist, { __mode = "k" } ) 1081 use "setmetatable" ( _socketlist, { __mode = "k" } )
1040 use "setmetatable" ( _readtimes, { __mode = "k" } ) 1082 use "setmetatable" ( _readtimes, { __mode = "k" } )
1041 use "setmetatable" ( _writetimes, { __mode = "k" } ) 1083 use "setmetatable" ( _writetimes, { __mode = "k" } )
1056 _addtimer = addtimer, 1098 _addtimer = addtimer,
1057 add_task = add_task; 1099 add_task = add_task;
1058 1100
1059 addclient = addclient, 1101 addclient = addclient,
1060 wrapclient = wrapclient, 1102 wrapclient = wrapclient,
1103 watchfd = watchfd,
1061 1104
1062 loop = loop, 1105 loop = loop,
1063 link = link, 1106 link = link,
1064 step = step, 1107 step = step,
1065 stats = stats, 1108 stats = stats,