-
Notifications
You must be signed in to change notification settings - Fork 9
/
tcpserver.rb
185 lines (159 loc) · 4.23 KB
/
tcpserver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#
# tcpserver.rb -- TcpServer Class
#
# Author: Pengcheng Li <pli@cs.rochester.edu>
#
require 'socket'
#require 'config'
require 'logger'
module MicroServer
# under the assumptions of the following interfaces
=begin
config = {}
:Logger =>
:Ip
:Port
:MaxConns
:Timeout
=end
=begin
logger
info
debug
error
=end
=begin
HttpRequest
request = HttpRequest.parse(socket)
=end
=begin
HttpServer
process(request)
=end
class TcpServer
attr_reader :status, :config, :logger, :listeners
attr_accessor :http, :sock
def initialize(http, config=Config::General)
@config = config
@http = http
@status = :Stop
@config[:Logger] ||= Log::new
@logger = @config[:Logger]
#create IPv4 socket only
@sock = TCPServer.new(@config[:Ip], @config[:Port])
@config[:Port] = @sock.addr[1] if @config[:Port] == 0
end
def start
raise "#{self.class} already started." if @status != :Stop
@logger.info "#{self.class}#start : pid=#{$$} ip=#{@config[:Ip]} port=#{@config[:Port]}"
@status = :Running
while @status == :Running
begin
if clients = IO.select([@sock], nil, nil, @config[:Timeout])
clients.each{ |client|
if client_sock = accept_client(sock)
begin
begin
addr = client_sock.peeraddr
@logger.debug "accept: #{addr[3]}:#{addr[1]}"
rescue SocketError
@logger.debug "accept: <address unknown>"
# raise
end
request = HttpRequest.parse(client_sock)
@http.process(request)
rescue Errno::ENOTCONN
@logger.debug "Errno::ENOTCONN raised"
rescue Exception => ex
@logger.error ex
ensure
if addr
@logger.debug "close: #{addr[3]}:#{addr[1]}"
else
@logger.debug "close: <address unknown>"
end
client_sock.close
end
end
}
end
rescue Errno::EBADF, IOError => ex
# if the listening socket was closed in TcpServer#shutdown,
# IO::select raise it.
rescue Exception => ex
msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
@logger.error msg
end
end
@logger.info "going to shutdown ..."
@logger.info "#{self.class}#start done."
@status = :Stop
end
def stop
if @status == :Running
@status = :Shutdown
end
end
def close
stop
if @logger.debug?
addr = sock.addr
@logger.debug("close TCPSocket(#{addr[2]}, #{addr[1]})")
end
sock.close
end
def shutdown
stop
if @logger.debug?
addr = sock.addr
@logger.debug("shutdown TCPSocket(#{addr[2]}, #{addr[1]})")
end
begin
sock.shutdown
rescue Errno::ENOTCONN
# when `Errno::ENOTCONN: Socket is not connected' on some platforms,
# call #close instead of #shutdown.
sock.close
if @logger.debug?
addr = sock.addr
@logger.debug("actually close TCPSocket(#{addr[2]}, #{addr[1]})")
end
end
end
private
def accept_client(svr)
sock = nil
begin
sock = svr.accept
sock.sync = true
set_non_blocking(sock)
set_close_on_exec(sock)
rescue Errno::ECONNRESET, Errno::ECONNABORTED,
Errno::EPROTO, Errno::EINVAL => ex
rescue Exception => ex
msg = "#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}"
@logger.error msg
end
return sock
end
def set_non_blocking(io)
flag = File::NONBLOCK
if defined?(Fcntl::F_GETFL)
flag |= io.fcntl(Fcntl::F_GETFL)
end
io.fcntl(Fcntl::F_SETFL, flag)
end
def set_close_on_exec(io)
if defined?(Fcntl::FD_CLOEXEC)
io.fcntl(Fcntl::FD_CLOEXEC, 1)
end
end
end #end TcpServer
end #end module
config = {}
config[:Logger] = logger.new
config[:Ip] = "127.0.0.1"
config[:Port] = "8080"
config[:Timeout] = 2.0
tcp = TcpServer.new(nil, config)
tcp.start