Comparison

net/server_epoll.lua @ 7584:98ee3ed105cf

net.server_epoll: Collect and expose local and remote address and ports
author Kim Alvefur <zash@zash.se>
date Thu, 18 Aug 2016 15:55:50 +0200
parent 7583:0498daffa6f0
child 7585:b64218c816de
comparison
equal deleted inserted replaced
7583:0498daffa6f0 7584:98ee3ed105cf
112 112
113 local interface = {}; 113 local interface = {};
114 local interface_mt = { __index = interface }; 114 local interface_mt = { __index = interface };
115 115
116 function interface_mt:__tostring() 116 function interface_mt:__tostring()
117 if self.peer then 117 if self.peername then
118 if self.conn then 118 if self.conn then
119 return ("%d %s [%s]:%d"):format(self:getfd(), tostring(self.conn), self.peer[1], self.peer[2]); 119 return ("%d %s [%s]:%d"):format(self:getfd(), tostring(self.conn), self.peer[1], self.peer[2]);
120 else 120 else
121 return ("%d [%s]:%d"):format(self:getfd(), self.peer[1], self.peer[2]); 121 return ("%d [%s]:%d"):format(self:getfd(), self.peername, self.peerport);
122 end 122 end
123 end 123 end
124 return tostring(self:getfd()); 124 return tostring(self:getfd());
125 end 125 end
126 126
145 function interface:getfd() 145 function interface:getfd()
146 return self.conn:getfd(); 146 return self.conn:getfd();
147 end 147 end
148 148
149 function interface:ip() 149 function interface:ip()
150 return self.peer[1]; 150 return self.peername or self.sockname;
151 end
152
153 function interface:port()
154 return self.sockport or self.peerport;
155 end
156
157 function interface:clientport()
158 return self.sockport;
159 end
160
161 function interface:serverport()
162 if self.sockport then
163 return self.sockport;
164 elseif self.server then
165 self.server:port();
166 end
151 end 167 end
152 168
153 function interface:socket() 169 function interface:socket()
154 return self.conn; 170 return self.conn;
155 end 171 end
394 onwriteable = interface.onconnect; 410 onwriteable = interface.onconnect;
395 writebuffer = {}; 411 writebuffer = {};
396 tls = tls; 412 tls = tls;
397 }, interface_mt); 413 }, interface_mt);
398 if client.getpeername then 414 if client.getpeername then
399 conn.peer = {client:getpeername()} 415 conn.peername, conn.peerport = client:getpeername();
416 end
417 if client.getsockname then
418 conn.sockname, conn.sockport = client:getsockname();
400 end 419 end
401 420
402 fds[conn:getfd()] = conn; 421 fds[conn:getfd()] = conn;
403 return conn; 422 return conn;
404 end 423 end
459 created = gettime(); 478 created = gettime();
460 listeners = listeners; 479 listeners = listeners;
461 _pattern = pattern; 480 _pattern = pattern;
462 onreadable = interface.onacceptable; 481 onreadable = interface.onacceptable;
463 tls = tls; 482 tls = tls;
464 peer = { addr, port }; 483 sockname = addr;
484 sockport = port;
465 }, interface_mt); 485 }, interface_mt);
466 server:setflags(true, false); 486 server:setflags(true, false);
467 fds[server:getfd()] = server; 487 fds[server:getfd()] = server;
468 return server; 488 return server;
469 end 489 end