Comparison

net/server_epoll.lua @ 9310:2d2d4c293efa

net.server_epoll: Rename pattern to read_size
author Kim Alvefur <zash@zash.se>
date Thu, 13 Sep 2018 21:07:44 +0200
parent 9309:33d500c25d76
child 9311:9b0604fe01f1
comparison
equal deleted inserted replaced
9309:33d500c25d76 9310:2d2d4c293efa
201 function interface:socket() 201 function interface:socket()
202 return self.conn; 202 return self.conn;
203 end 203 end
204 204
205 function interface:set_mode(new_mode) 205 function interface:set_mode(new_mode)
206 self._pattern = new_mode; 206 self.read_size = new_mode;
207 end 207 end
208 208
209 function interface:setoption(k, v) 209 function interface:setoption(k, v)
210 -- LuaSec doesn't expose setoption :( 210 -- LuaSec doesn't expose setoption :(
211 if self.conn.setoption then 211 if self.conn.setoption then
304 return true; 304 return true;
305 end 305 end
306 306
307 -- Called when socket is readable 307 -- Called when socket is readable
308 function interface:onreadable() 308 function interface:onreadable()
309 local data, err, partial = self.conn:receive(self._pattern); 309 local data, err, partial = self.conn:receive(self.read_size);
310 if data then 310 if data then
311 self:onconnect(); 311 self:onconnect();
312 self:on("incoming", data); 312 self:on("incoming", data);
313 else 313 else
314 if partial and partial ~= "" then 314 if partial and partial ~= "" then
468 self:on("disconnect", err); 468 self:on("disconnect", err);
469 self:destroy(); 469 self:destroy();
470 end 470 end
471 end 471 end
472 472
473 local function wrapsocket(client, server, pattern, listeners, tls_ctx) -- luasocket object -> interface object 473 local function wrapsocket(client, server, read_size, listeners, tls_ctx) -- luasocket object -> interface object
474 client:settimeout(0); 474 client:settimeout(0);
475 local conn = setmetatable({ 475 local conn = setmetatable({
476 conn = client; 476 conn = client;
477 _server = server; 477 _server = server;
478 created = gettime(); 478 created = gettime();
479 listeners = listeners; 479 listeners = listeners;
480 _pattern = pattern or (server and server._pattern); 480 read_size = read_size or (server and server.read_size);
481 writebuffer = {}; 481 writebuffer = {};
482 tls_ctx = tls_ctx; 482 tls_ctx = tls_ctx;
483 }, interface_mt); 483 }, interface_mt);
484 484
485 if client.getpeername then 485 if client.getpeername then
546 end 546 end
547 self.onconnect = noop; 547 self.onconnect = noop;
548 self:on("connect"); 548 self:on("connect");
549 end 549 end
550 550
551 local function addserver(addr, port, listeners, pattern, tls_ctx) 551 local function addserver(addr, port, listeners, read_size, tls_ctx)
552 local conn, err = socket.bind(addr, port, cfg.tcp_backlog); 552 local conn, err = socket.bind(addr, port, cfg.tcp_backlog);
553 if not conn then return conn, err; end 553 if not conn then return conn, err; end
554 conn:settimeout(0); 554 conn:settimeout(0);
555 local server = setmetatable({ 555 local server = setmetatable({
556 conn = conn; 556 conn = conn;
557 created = gettime(); 557 created = gettime();
558 listeners = listeners; 558 listeners = listeners;
559 _pattern = pattern; 559 read_size = read_size;
560 onreadable = interface.onacceptable; 560 onreadable = interface.onacceptable;
561 tls_ctx = tls_ctx; 561 tls_ctx = tls_ctx;
562 tls_direct = tls_ctx and true or false; 562 tls_direct = tls_ctx and true or false;
563 sockname = addr; 563 sockname = addr;
564 sockport = port; 564 sockport = port;
566 server:setflags(true, false); 566 server:setflags(true, false);
567 return server; 567 return server;
568 end 568 end
569 569
570 -- COMPAT 570 -- COMPAT
571 local function wrapclient(conn, addr, port, listeners, pattern, tls_ctx) 571 local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx)
572 local client = wrapsocket(conn, nil, pattern, listeners, tls_ctx); 572 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx);
573 if not client.peername then 573 if not client.peername then
574 client.peername, client.peerport = addr, port; 574 client.peername, client.peerport = addr, port;
575 end 575 end
576 if tls_ctx then 576 if tls_ctx then
577 client:starttls(tls_ctx); 577 client:starttls(tls_ctx);
580 end 580 end
581 return client; 581 return client;
582 end 582 end
583 583
584 -- New outgoing TCP connection 584 -- New outgoing TCP connection
585 local function addclient(addr, port, listeners, pattern, tls_ctx) 585 local function addclient(addr, port, listeners, read_size, tls_ctx)
586 local conn, err = socket.tcp(); 586 local conn, err = socket.tcp();
587 if not conn then return conn, err; end 587 if not conn then return conn, err; end
588 conn:settimeout(0); 588 conn:settimeout(0);
589 conn:connect(addr, port); 589 conn:connect(addr, port);
590 local client = wrapsocket(conn, nil, pattern, listeners, tls_ctx) 590 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx)
591 if tls_ctx then 591 if tls_ctx then
592 client:starttls(tls_ctx); 592 client:starttls(tls_ctx);
593 else 593 else
594 client:init(); 594 client:init();
595 end 595 end