lib/standard/string: Fixed substring, could produce strings with a negative length
[nit.git] / lib / socket / socket.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Matthieu Lucas <lucasmatthieu@gmail.com>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Socket services
18 module socket
19
20 private import socket_c
21 intrude import standard::stream
22
23 # A general TCP socket, either a `TCPStream` or a `TCPServer`
24 abstract class Socket
25
26 # Underlying C socket
27 private var socket: NativeSocket is noinit
28
29 # Port used by the socket
30 var port: Int
31
32 # IPv4 address to which `self` is connected
33 #
34 # Formatted as xxx.xxx.xxx.xxx.
35 var address: String is noinit
36
37 # Is this socket closed?
38 var closed = false
39 end
40
41 # Simple communication stream with a remote socket
42 class TCPStream
43 super Socket
44 super BufferedIStream
45 super OStream
46 super PollableIStream
47
48 # Real canonical name of the host to which `self` is connected
49 var host: String
50
51 private var addrin: NativeSocketAddrIn is noinit
52
53 redef var end_reached = false
54
55 # TODO make init private
56
57 # Creates a socket connection to host `host` on port `port`
58 init connect(host: String, port: Int)
59 do
60 _buffer = new FlatBuffer
61 _buffer_pos = 0
62 socket = new NativeSocket.socket(new NativeSocketAddressFamilies.af_inet,
63 new NativeSocketTypes.sock_stream, new NativeSocketProtocolFamilies.pf_null)
64 if socket.address_is_null then
65 end_reached = true
66 closed = true
67 return
68 end
69 socket.setsockopt(new NativeSocketOptLevels.socket, new NativeSocketOptNames.reuseaddr, 1)
70 var hostname = socket.gethostbyname(host)
71 addrin = new NativeSocketAddrIn.with_hostent(hostname, port)
72
73 address = addrin.address
74 init(addrin.port, hostname.h_name)
75
76 closed = not internal_connect
77 end_reached = closed
78 end
79
80 # Creates a client socket, this is meant to be used by accept only
81 private init server_side(h: SocketAcceptResult)
82 do
83 _buffer = new FlatBuffer
84 _buffer_pos = 0
85 socket = h.socket
86 addrin = h.addr_in
87 address = addrin.address
88
89 init(addrin.port, address)
90 end
91
92 redef fun poll_in do return ready_to_read(0)
93
94 # Returns an array containing an enum of the events ready to be read
95 #
96 # event_types : Combination of several event types to watch
97 #
98 # timeout : Time in milliseconds before stopping listening for events on this socket
99 private fun pollin(event_types: Array[NativeSocketPollValues], timeout: Int): Array[NativeSocketPollValues] do
100 if end_reached then return new Array[NativeSocketPollValues]
101 return socket.socket_poll(new PollFD(socket.descriptor, event_types), timeout)
102 end
103
104 # Easier use of pollin to check for something to read on all channels of any priority
105 #
106 # timeout : Time in milliseconds before stopping to wait for events
107 fun ready_to_read(timeout: Int): Bool
108 do
109 if _buffer_pos < _buffer.length then return true
110 if end_reached then return false
111 var events = [new NativeSocketPollValues.pollin]
112 return pollin(events, timeout).length != 0
113 end
114
115 # Checks if the socket still is connected
116 fun connected: Bool
117 do
118 if closed then return false
119 var events = [new NativeSocketPollValues.pollhup, new NativeSocketPollValues.pollerr]
120 if pollin(events, 0).length == 0 then
121 return true
122 else
123 closed = true
124 return false
125 end
126 end
127
128 redef fun is_writable do return not end_reached
129
130 # Establishes a connection to socket addrin
131 #
132 # REQUIRES : not self.end_reached
133 private fun internal_connect: Bool
134 do
135 assert not closed
136 return socket.connect(addrin) >= 0
137 end
138
139 # If socket.end_reached, nothing will happen
140 redef fun write(msg: Text)
141 do
142 if closed then return
143 socket.write(msg.to_s)
144 end
145
146 fun write_ln(msg: Text)
147 do
148 if end_reached then return
149 write(msg.to_s)
150 write("\n")
151 end
152
153 redef fun fill_buffer
154 do
155 _buffer.clear
156 _buffer_pos = 0
157 if not connected then return
158 var read = socket.read
159 if read.length == 0 then
160 close
161 end_reached = true
162 end
163 _buffer.append(read)
164 end
165
166 redef fun close
167 do
168 if closed then return
169 if socket.close >= 0 then
170 closed = true
171 end_reached = true
172 end
173 end
174
175 # Send the data present in the socket buffer
176 fun flush
177 do
178 socket.setsockopt(new NativeSocketOptLevels.tcp, new NativeSocketOptNames.tcp_nodelay, 1)
179 socket.setsockopt(new NativeSocketOptLevels.tcp, new NativeSocketOptNames.tcp_nodelay, 0)
180 end
181 end
182
183 # A socket listening on a given `port` for incomming connections
184 #
185 # Create streams to communicate with clients using `accept`.
186 class TCPServer
187 super Socket
188
189 private var addrin: NativeSocketAddrIn is noinit
190
191 # Create and bind a listening server socket on port `port`
192 init
193 do
194 socket = new NativeSocket.socket(new NativeSocketAddressFamilies.af_inet,
195 new NativeSocketTypes.sock_stream, new NativeSocketProtocolFamilies.pf_null)
196 assert not socket.address_is_null
197 socket.setsockopt(new NativeSocketOptLevels.socket, new NativeSocketOptNames.reuseaddr, 1)
198 addrin = new NativeSocketAddrIn.with(port, new NativeSocketAddressFamilies.af_inet)
199 address = addrin.address
200
201 # Bind it
202 closed = not bind
203 end
204
205 # Associates the socket to a local address and port
206 #
207 # Returns whether the socket has been be bound.
208 private fun bind: Bool do
209 return socket.bind(addrin) >= 0
210 end
211
212 # Sets the socket as ready to accept incoming connections, `size` is the maximum number of queued clients
213 #
214 # Returns `true` if the socket could be set, `false` otherwise
215 fun listen(size: Int): Bool do
216 return socket.listen(size) >= 0
217 end
218
219 # Accepts an incoming connection from a client
220 #
221 # Create and return a new socket to the client. May return null if not
222 # `blocking` and there's no waiting clients, or upon an interruption
223 # (whether `blocking` or not).
224 #
225 # Require: not closed
226 fun accept: nullable TCPStream
227 do
228 assert not closed
229 var native = socket.accept
230 if native == null then return null
231 return new TCPStream.server_side(native)
232 end
233
234 # Set whether calls to `accept` are blocking
235 fun blocking=(value: Bool)
236 do
237 # We use the opposite from the native version as the native API
238 # is closer to the C API. In the Nity API, we use a positive version
239 # of the name.
240 socket.non_blocking = not value
241 end
242
243 # Close this socket
244 fun close
245 do
246 # FIXME unify with `SocketStream::close` when we can use qualified names
247
248 if closed then return
249 if socket.close >= 0 then
250 closed = true
251 end
252 end
253 end
254
255 # A simple set of sockets used by `SocketObserver`
256 class SocketSet
257 private var native = new NativeSocketSet
258
259 init do clear
260
261 # Add `socket` to this set
262 fun add(socket: Socket) do native.set(socket.socket)
263
264 # Remove `socket` from this set
265 fun remove(socket: Socket) do native.clear(socket.socket)
266
267 # Does this set has `socket`?
268 fun has(socket: Socket): Bool do return native.is_set(socket.socket)
269
270 # Clear all sockets from this set
271 fun clear do native.zero
272 end
273
274 # Service class to manage calls to `select`
275 class SocketObserver
276 private var native = new NativeSocketObserver
277
278 var read_set: nullable SocketSet = null
279
280 var write_set: nullable SocketSet = null
281
282 var except_set: nullable SocketSet = null
283
284 init(read: Bool, write: Bool, except: Bool)
285 is old_style_init do
286 if read then read_set = new SocketSet
287 if write then write_set = new SocketSet
288 if except then except_set = new SocketSet
289 end
290
291 fun select(max: Socket, seconds: Int, microseconds: Int): Bool
292 do
293 # FIXME this implementation (see the call to nullable attributes below) and
294 # `NativeSockectObserver::select` is not stable.
295
296 var timeval = new NativeTimeval(seconds, microseconds)
297 return native.select(max.socket, read_set.native, write_set.native, except_set.native, timeval) > 0
298 end
299 end