| 1 |
-------------------------------------------------------------------------------- |
|---|
| 2 |
-- Title: SocketWriter.lua |
|---|
| 3 |
-- Description: Like a square peg in a round hole |
|---|
| 4 |
-- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ |
|---|
| 5 |
-- Creation Date: February 1, 2005 |
|---|
| 6 |
-- Legal: Copyright (C) 2005 Raphaël Szwarc |
|---|
| 7 |
-------------------------------------------------------------------------------- |
|---|
| 8 |
|
|---|
| 9 |
-- import dependencies |
|---|
| 10 |
local LUObject = require( "LUObject" ) |
|---|
| 11 |
local LUTask = require( "LUTask" ) |
|---|
| 12 |
local os = require( "os" ) |
|---|
| 13 |
|
|---|
| 14 |
-- define the class |
|---|
| 15 |
local super = LUObject |
|---|
| 16 |
local self = super() |
|---|
| 17 |
|
|---|
| 18 |
-- constant(s) |
|---|
| 19 |
local Timeout = 60 |
|---|
| 20 |
|
|---|
| 21 |
-- initialization method |
|---|
| 22 |
function self:init( aSocket ) |
|---|
| 23 |
self = super.init( self ) |
|---|
| 24 |
|
|---|
| 25 |
self._socket = aSocket |
|---|
| 26 |
self._status = "timeout" |
|---|
| 27 |
|
|---|
| 28 |
return self |
|---|
| 29 |
end |
|---|
| 30 |
|
|---|
| 31 |
-- method to access this reader socket |
|---|
| 32 |
function self:socket() |
|---|
| 33 |
return self._socket |
|---|
| 34 |
end |
|---|
| 35 |
|
|---|
| 36 |
-- method to close this socket |
|---|
| 37 |
function self:close() |
|---|
| 38 |
self._status = "closed" |
|---|
| 39 |
|
|---|
| 40 |
return self:socket():close() |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
-- method to access this writer status |
|---|
| 44 |
function self:status() |
|---|
| 45 |
return self._status |
|---|
| 46 |
end |
|---|
| 47 |
|
|---|
| 48 |
-- method to write a value |
|---|
| 49 |
function self:write( aValue, aTimeout ) |
|---|
| 50 |
local aTime = os.time() |
|---|
| 51 |
local aStatus = nil |
|---|
| 52 |
local anIndex = 0 |
|---|
| 53 |
local aSize = 0 |
|---|
| 54 |
|
|---|
| 55 |
aTimeout = aTimeout or Timeout |
|---|
| 56 |
|
|---|
| 57 |
while true do |
|---|
| 58 |
aSize, aStatus, anIndex = self:socket():send( aValue, anIndex + 1 ) |
|---|
| 59 |
|
|---|
| 60 |
self._status = aStatus |
|---|
| 61 |
|
|---|
| 62 |
if aSize ~= nil or aStatus ~= "timeout" then |
|---|
| 63 |
return aSize, aStatus |
|---|
| 64 |
end |
|---|
| 65 |
|
|---|
| 66 |
if aStatus == "timeout" then |
|---|
| 67 |
if os.difftime( os.time(), aTime ) >= aTimeout then |
|---|
| 68 |
return aSize, aStatus |
|---|
| 69 |
end |
|---|
| 70 |
else |
|---|
| 71 |
aTime = os.time() |
|---|
| 72 |
end |
|---|
| 73 |
|
|---|
| 74 |
LUTask:yield( "writer" ) |
|---|
| 75 |
end |
|---|
| 76 |
|
|---|
| 77 |
return aSize, aStatus |
|---|
| 78 |
end |
|---|
| 79 |
|
|---|
| 80 |
-- method to write a value, followed by a new line |
|---|
| 81 |
function self:writeln( aValue ) |
|---|
| 82 |
if aValue ~= nil then |
|---|
| 83 |
aValue = aValue .. self:eol() |
|---|
| 84 |
else |
|---|
| 85 |
aValue = self:eol() |
|---|
| 86 |
end |
|---|
| 87 |
|
|---|
| 88 |
return self:write( aValue ) |
|---|
| 89 |
end |
|---|
| 90 |
|
|---|
| 91 |
-- method to access this writer task |
|---|
| 92 |
function self:task() |
|---|
| 93 |
return self._task |
|---|
| 94 |
end |
|---|
| 95 |
|
|---|
| 96 |
-- class method to access the EOF marker |
|---|
| 97 |
function self:eol() |
|---|
| 98 |
return "\r\n" |
|---|
| 99 |
end |
|---|
| 100 |
|
|---|
| 101 |
return self |
|---|