root/lu/SocketServer.lua

Revision 814 (checked in by rsz, 3 years ago)

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               SocketServer.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 LULog = require( "LULog" )
13 local SocketRunLoop = require( "SocketRunLoop" )
14 local SocketTask = require( "SocketTask" )
15 local socket = require( "socket" )
16
17 -- define the class
18 local super = LUObject
19 local self = super()
20
21 -- initialization method
22 function self:init( anAddress, aPort, aDelegate )
23         self = super.init( self )
24
25         self._address = anAddress
26         self._port = aPort
27         self._delegate = aDelegate
28        
29         return self
30 end
31        
32 -- method to access this server address
33 function self:address()
34         if self._socket ~= nil then
35                 local aSocketAddress, aSocketPort = self._socket:getsockname()
36                
37                 if aSocketAddress ~= nil then
38                         return aSocketAddress
39                 end
40         end
41
42         if self._address == nil then
43                 self._address = "*"
44         end
45
46         return self._address
47 end
48
49  -- method to close this socket
50 function self:close()
51         self._status = "closed"
52
53         return self:socket():close()
54 end
55
56 -- method to access this server port
57 function self:port()
58         if self._socket ~= nil then
59                 local aSocketAddress, aSocketPort = self._socket:getsockname()
60                
61                 if aSocketPort ~= nil then
62                         return aSocketPort
63                 end
64         end
65
66         if self._port == nil then
67                 self._port = 0
68         end
69
70         return self._port
71 end
72
73 -- method to access this server delegate
74 function self:delegate()
75         return self._delegate
76 end
77
78 -- method to access this server socket
79 function self:socket()
80         if self._socket == nil then
81                 local anAddress = self:address()
82                 local aPort = self:port()
83                 local aSocket, aStatus = socket.bind( anAddress, aPort )
84                
85                 if aSocket ~= nil then
86                         aSocket:settimeout( 0 )
87                         aSocket:setoption( "linger", { on = false, timeout = 0 } )
88                         aSocket:setoption( "tcp-nodelay", true )
89                        
90                         self._socket = aSocket
91                 else
92                         LULog:warning( self .. " '" .. aStatus .. "'" )
93                
94                         return nil, aStatus
95                 end
96         end
97
98         return self._socket
99 end
100
101 -- method to start this server task
102 function self:start()
103         if self._task == nil then
104                 self:socket()
105                 self:task():run()
106         end
107        
108         return self
109 end
110
111 -- method to access this server task
112 function self:task()
113         if self._task == nil then
114                 self._task = LUTask( self, SocketRunLoop:default() )
115         end
116        
117         return self._task
118 end
119
120 -- method to run this server
121 function self:run()
122         local aServerSocket = self:socket()
123         local aDelegate = self:delegate()
124        
125         while true do
126                 local aSocket, aStatus = aServerSocket:accept()
127                
128                 if aSocket ~= nil then
129                         local aSocketTask = SocketTask( self, aSocket, aDelegate )
130
131                         aSocket:settimeout( 0 )
132                         aSocket:setoption( "linger", { on = false, timeout = 0 } )
133                         aSocket:setoption( "tcp-nodelay", true )
134                          
135                         aSocketTask:start()
136                 end
137                
138                 if aStatus ~= nil and aStatus ~= "timeout" then
139                         LULog:warning( self .. " '" .. aStatus .. "'" )
140                 end
141
142                 LUTask:yield( "reader" )
143        end
144 end
145
146 -- method for string representation
147 function self:toString()
148         return "SocketServer@" .. self:address() .. ":" .. self:port()
149 end
150
151 return self
Note: See TracBrowser for help on using the browser.