Mercurial > prosody-hg
comparison net/server_event.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 |
| parents | 469f78fe1c3e |
| children | 23f66f04e216 |
comparison
equal
deleted
inserted
replaced
| 8780:4cab4ee5dfcc | 8781:53178b6ba589 |
|---|---|
| 832 end | 832 end |
| 833 , delay); | 833 , delay); |
| 834 return event_handle; | 834 return event_handle; |
| 835 end | 835 end |
| 836 | 836 |
| 837 local function watchfd(fd, onreadable, onwriteable) | |
| 838 local handle = {}; | |
| 839 function handle:setflags(r,w) | |
| 840 if r ~= nil then | |
| 841 if r and not self.wantread then | |
| 842 self.wantread = base:addevent(fd, EV_READ, function () | |
| 843 onreadable(self); | |
| 844 end); | |
| 845 elseif not r and self.wantread then | |
| 846 self.wantread:close(); | |
| 847 self.wantread = nil; | |
| 848 end | |
| 849 end | |
| 850 if w ~= nil then | |
| 851 if w and not self.wantwrite then | |
| 852 self.wantwrite = base:addevent(fd, EV_WRITE, function () | |
| 853 onwriteable(self); | |
| 854 end); | |
| 855 elseif not r and self.wantread then | |
| 856 self.wantwrite:close(); | |
| 857 self.wantwrite = nil; | |
| 858 end | |
| 859 end | |
| 860 end | |
| 861 handle:setflags(onreadable, onwriteable); | |
| 862 return handle; | |
| 863 end | |
| 864 | |
| 837 return { | 865 return { |
| 838 cfg = cfg, | 866 cfg = cfg, |
| 839 base = base, | 867 base = base, |
| 840 loop = loop, | 868 loop = loop, |
| 841 link = link, | 869 link = link, |
| 848 setquitting = setquitting, | 876 setquitting = setquitting, |
| 849 closeall = closeallservers, | 877 closeall = closeallservers, |
| 850 get_backend = get_backend, | 878 get_backend = get_backend, |
| 851 hook_signal = hook_signal, | 879 hook_signal = hook_signal, |
| 852 add_task = add_task, | 880 add_task = add_task, |
| 881 watchfd = watchfd, | |
| 853 | 882 |
| 854 __NAME = SCRIPT_NAME, | 883 __NAME = SCRIPT_NAME, |
| 855 __DATE = LAST_MODIFIED, | 884 __DATE = LAST_MODIFIED, |
| 856 __AUTHOR = SCRIPT_AUTHOR, | 885 __AUTHOR = SCRIPT_AUTHOR, |
| 857 __VERSION = SCRIPT_VERSION, | 886 __VERSION = SCRIPT_VERSION, |
