1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Matthieu Lucas <lucasmatthieu@gmail.com>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
24 var host
: nullable String
26 private var socket
: FFSocket
27 private var addrin
: FFSocketAddrIn
30 # If the socket could not be created or if the socket was destroyed
31 # before a call needing the socket was made
32 # this flag will be set to false.
33 var still_alive
= true # Note : HUGE SUCCESS
35 init stream_with_host
(thost
: String, tport
: Int)
37 socket
= new FFSocket.socket
( new FFSocketAddressFamilies.af_inet
, new FFSocketTypes.sock_stream
, new FFSocketProtocolFamilies.pf_null
)
38 if socket
.address_is_null
then
42 socket
.setsockopt
(new FFSocketOptLevels.socket
, new FFSocketOptNames.reuseaddr
, 1)
43 var hostname
= socket
.gethostbyname
(thost
)
44 addrin
= new FFSocketAddrIn.with_hostent
(hostname
, tport
)
45 address
= addrin
.address
46 host
= hostname
.h_name
50 init stream_with_port
(tport
: Int)
52 socket
= new FFSocket.socket
( new FFSocketAddressFamilies.af_inet
, new FFSocketTypes.sock_stream
, new FFSocketProtocolFamilies.pf_null
)
53 if socket
.address_is_null
then
57 socket
.setsockopt
(new FFSocketOptLevels.socket
, new FFSocketOptNames.reuseaddr
, 1)
58 addrin
= new FFSocketAddrIn.with
(tport
, new FFSocketAddressFamilies.af_inet
)
59 address
= addrin
.address
64 init primitive_init
(h
: FFSocketAcceptResult)
68 address
= addrin
.address
73 # Returns an array containing an enum of the events ready to be read
75 # event_types : Combination of several event types to watch
77 # timeout : Time in milliseconds before stopping listening for events on this socket
79 private fun poll_in
(event_types
: Array[FFSocketPollValues], timeout
: Int): Array[FFSocketPollValues] do
80 if not still_alive
then return new Array[FFSocketPollValues]
81 return socket
.socket_poll
(new PollFD(socket
.descriptor
, event_types
), timeout
)
84 # Easier use of poll_in to check for something to read on all channels of any priority
86 # timeout : Time in milliseconds before stopping to wait for events
88 fun ready_to_read
(timeout
: Int): Bool
90 if not still_alive
then return false
91 var events
= new Array[FFSocketPollValues]
92 events
.push
(new FFSocketPollValues.pollin
)
93 events
.push
(new FFSocketPollValues.pollrdnorm
)
94 events
.push
(new FFSocketPollValues.pollpri
)
95 events
.push
(new FFSocketPollValues.pollrdband
)
96 return poll_in
(events
, timeout
).length
!= 0
99 # Checks if the socket still is connected
103 if not still_alive
then return false
104 var events
= new Array[FFSocketPollValues]
105 events
.push
(new FFSocketPollValues.pollhup
)
106 events
.push
(new FFSocketPollValues.pollerr
)
107 return poll_in
(events
, 0).length
== 0
112 return socket
.connect
(addrin
) >= 0
115 fun write
(msg
: String): Bool do
116 if not still_alive
then return false
117 return socket
.write
(msg
) >= 0
121 if not still_alive
then return ""
126 if not still_alive
then return true
127 if socket
.close
>= 0 then
135 if not still_alive
then return false
136 return socket
.bind
(addrin
) >= 0
139 fun listen
(size
: Int): Bool do
140 if not still_alive
then return false
141 return socket
.listen
(size
) >= 0
144 fun accept
: Socket do
146 return new Socket.primitive_init
(socket
.accept
)
152 var sset
: FFSocketSet
153 init do sset
= new FFSocketSet end
154 fun set
(s
: Socket) do sset
.set
(s
.socket
) end
155 fun is_set
(s
: Socket): Bool do return sset
.is_set
(s
.socket
) end
156 fun zero
do sset
.zero
end
157 fun clear
(s
: Socket) do sset
.clear
(s
.socket
) end
161 private var observer
: FFSocketObserver
162 var readset
: nullable SocketSet = null
163 var writeset
: nullable SocketSet = null
164 var exceptset
: nullable SocketSet = null
165 init(read
:Bool, write
:Bool, except
: Bool)
167 if read
then readset
= new SocketSet
168 if write
then writeset
= new SocketSet
169 if except
then exceptset
= new SocketSet
170 observer
= new FFSocketObserver
172 fun select
(max
: Socket,seconds
: Int, microseconds
: Int): Bool
174 var timeval
= new FFTimeval(seconds
, microseconds
)
175 return observer
.select
(max
.socket
, readset
.sset
, writeset
.sset
, readset
.sset
, timeval
) > 0