Software /
code /
prosody
Comparison
net/server_event.lua @ 5286:0cbe57fbe2cd
net.server_event: Optimize socket write buffers (don't do buf=buf..newdata).
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sun, 06 Jan 2013 04:55:53 +0500 |
parent | 5277:8585f3cc4976 |
child | 5339:b5cae12411af |
comparison
equal
deleted
inserted
replaced
5284:5d2282a29387 | 5286:0cbe57fbe2cd |
---|---|
37 local select = use "select" | 37 local select = use "select" |
38 local require = use "require" | 38 local require = use "require" |
39 local tostring = use "tostring" | 39 local tostring = use "tostring" |
40 local coroutine = use "coroutine" | 40 local coroutine = use "coroutine" |
41 local setmetatable = use "setmetatable" | 41 local setmetatable = use "setmetatable" |
42 | |
43 local t_insert = table.insert | |
44 local t_concat = table.concat | |
42 | 45 |
43 local ssl = use "ssl" | 46 local ssl = use "ssl" |
44 local socket = use "socket" or require "socket" | 47 local socket = use "socket" or require "socket" |
45 | 48 |
46 local log = require ("util.logger").init("socket") | 49 local log = require ("util.logger").init("socket") |
307 if total > cfg.MAX_SEND_LENGTH then -- check buffer length | 310 if total > cfg.MAX_SEND_LENGTH then -- check buffer length |
308 local err = "send buffer exceeded" | 311 local err = "send buffer exceeded" |
309 debug( "error:", err ) -- to much, check your app | 312 debug( "error:", err ) -- to much, check your app |
310 return nil, err | 313 return nil, err |
311 end | 314 end |
312 self.writebuffer = self.writebuffer .. data -- new buffer | 315 t_insert(self.writebuffer, data) -- new buffer |
313 self.writebufferlen = total | 316 self.writebufferlen = total |
314 if not self.eventwrite then -- register new write event | 317 if not self.eventwrite then -- register new write event |
315 --vdebug( "register new write event" ) | 318 --vdebug( "register new write event" ) |
316 self.eventwrite = addevent( base, self.conn, EV_WRITE, self.writecallback, cfg.WRITE_TIMEOUT ) | 319 self.eventwrite = addevent( base, self.conn, EV_WRITE, self.writecallback, cfg.WRITE_TIMEOUT ) |
317 end | 320 end |
464 --vdebug("creating client interfacce...") | 467 --vdebug("creating client interfacce...") |
465 local interface = { | 468 local interface = { |
466 type = "client"; | 469 type = "client"; |
467 conn = client; | 470 conn = client; |
468 currenttime = socket_gettime( ); -- safe the origin | 471 currenttime = socket_gettime( ); -- safe the origin |
469 writebuffer = ""; -- writebuffer | 472 writebuffer = {}; -- writebuffer |
470 writebufferlen = 0; -- length of writebuffer | 473 writebufferlen = 0; -- length of writebuffer |
471 send = client.send; -- caching table lookups | 474 send = client.send; -- caching table lookups |
472 receive = client.receive; | 475 receive = client.receive; |
473 onconnect = listener.onconnect; -- will be called when client disconnects | 476 onconnect = listener.onconnect; -- will be called when client disconnects |
474 ondisconnect = listener.ondisconnect; -- will be called when client disconnects | 477 ondisconnect = listener.ondisconnect; -- will be called when client disconnects |
518 if interface.eventwritetimeout then -- luasec only | 521 if interface.eventwritetimeout then -- luasec only |
519 interface.eventwritetimeout:close( ) -- first we have to close timeout event which where regged after a wantread error | 522 interface.eventwritetimeout:close( ) -- first we have to close timeout event which where regged after a wantread error |
520 interface.eventwritetimeout = false | 523 interface.eventwritetimeout = false |
521 end | 524 end |
522 end | 525 end |
523 local succ, err, byte = interface.conn:send( interface.writebuffer, 1, interface.writebufferlen ) | 526 interface.writebuffer = { t_concat(interface.writebuffer) } |
527 local succ, err, byte = interface.conn:send( interface.writebuffer[1], 1, interface.writebufferlen ) | |
524 --vdebug( "write data:", interface.writebuffer, "error:", err, "part:", byte ) | 528 --vdebug( "write data:", interface.writebuffer, "error:", err, "part:", byte ) |
525 if succ then -- writing succesful | 529 if succ then -- writing succesful |
526 interface.writebuffer = "" | 530 interface.writebuffer[1] = nil |
527 interface.writebufferlen = 0 | 531 interface.writebufferlen = 0 |
528 interface:ondrain(); | 532 interface:ondrain(); |
529 if interface.fatalerror then | 533 if interface.fatalerror then |
530 debug "closing client after writing" | 534 debug "closing client after writing" |
531 interface:_close() -- close interface if needed | 535 interface:_close() -- close interface if needed |
537 end | 541 end |
538 interface.eventwrite = nil | 542 interface.eventwrite = nil |
539 return -1 | 543 return -1 |
540 elseif byte and (err == "timeout" or err == "wantwrite") then -- want write again | 544 elseif byte and (err == "timeout" or err == "wantwrite") then -- want write again |
541 --vdebug( "writebuffer is not empty:", err ) | 545 --vdebug( "writebuffer is not empty:", err ) |
542 interface.writebuffer = string_sub( interface.writebuffer, byte + 1, interface.writebufferlen ) -- new buffer | 546 interface.writebuffer[1] = string_sub( interface.writebuffer[1], byte + 1, interface.writebufferlen ) -- new buffer |
543 interface.writebufferlen = interface.writebufferlen - byte | 547 interface.writebufferlen = interface.writebufferlen - byte |
544 if "wantread" == err then -- happens only with luasec | 548 if "wantread" == err then -- happens only with luasec |
545 local callback = function( ) | 549 local callback = function( ) |
546 interface:_close() | 550 interface:_close() |
547 interface.eventwritetimeout = nil | 551 interface.eventwritetimeout = nil |