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 var hostname
= socket
.gethostbyname
(thost
)
43 addrin
= new FFSocketAddrIn.with_hostent
(hostname
, tport
)
44 address
= addrin
.address
45 host
= hostname
.h_name
49 init stream_with_port
(tport
: Int)
51 socket
= new FFSocket.socket
( new FFSocketAddressFamilies.af_inet
, new FFSocketTypes.sock_stream
, new FFSocketProtocolFamilies.pf_null
)
52 if socket
.address_is_null
then
56 addrin
= new FFSocketAddrIn.with
(tport
, new FFSocketAddressFamilies.af_inet
)
57 address
= addrin
.address
62 init primitive_init
(h
: FFSocketAcceptResult)
66 address
= addrin
.address
71 # Returns an array containing an enum of the events ready to be read
73 # event_types : Combination of several event types to watch
75 # timeout : Time in milliseconds before stopping listening for events on this socket
77 private fun poll_in
(event_types
: Array[FFSocketPollValues], timeout
: Int): Array[FFSocketPollValues] do
78 if not still_alive
then return new Array[FFSocketPollValues]
79 return socket
.socket_poll
(new PollFD(socket
.descriptor
, event_types
), timeout
)
82 # Easier use of poll_in to check for something to read on all channels of any priority
84 # timeout : Time in milliseconds before stopping to wait for events
86 fun ready_to_read
(timeout
: Int): Bool
88 if not still_alive
then return false
89 var events
= new Array[FFSocketPollValues]
90 events
.push
(new FFSocketPollValues.pollin
)
91 events
.push
(new FFSocketPollValues.pollrdnorm
)
92 events
.push
(new FFSocketPollValues.pollpri
)
93 events
.push
(new FFSocketPollValues.pollrdband
)
94 return poll_in
(events
, timeout
).length
!= 0
97 # Checks if the socket still is connected
101 if not still_alive
then return false
102 var events
= new Array[FFSocketPollValues]
103 events
.push
(new FFSocketPollValues.pollhup
)
104 events
.push
(new FFSocketPollValues.pollerr
)
105 return poll_in
(events
, 0).length
== 0
110 return socket
.connect
(addrin
) >= 0
113 fun write
(msg
: String): Bool do
114 if not still_alive
then return false
115 return socket
.write
(msg
) >= 0
119 if not still_alive
then return ""
124 if not still_alive
then return true
125 if socket
.close
>= 0 then
133 if not still_alive
then return false
134 return socket
.bind
(addrin
) >= 0
137 fun listen
(size
: Int): Bool do
138 if not still_alive
then return false
139 return socket
.listen
(size
) >= 0
142 fun accept
: Socket do
144 return new Socket.primitive_init
(socket
.accept
)
147 fun errno
: Int do return socket
.errno
151 var sset
: FFSocketSet
152 init do sset
= new FFSocketSet end
153 fun set
(s
: Socket) do sset
.set
(s
.socket
) end
154 fun is_set
(s
: Socket): Bool do return sset
.is_set
(s
.socket
) end
155 fun zero
do sset
.zero
end
156 fun clear
(s
: Socket) do sset
.clear
(s
.socket
) end
160 private var observer
: FFSocketObserver
161 var readset
: nullable SocketSet = null
162 var writeset
: nullable SocketSet = null
163 var exceptset
: nullable SocketSet = null
164 init(read
:Bool, write
:Bool, except
: Bool)
166 if read
then readset
= new SocketSet
167 if write
then writeset
= new SocketSet
168 if except
then exceptset
= new SocketSet
169 observer
= new FFSocketObserver
171 fun select
(max
: Socket,seconds
: Int, microseconds
: Int): Bool
173 var timeval
= new FFTimeval(seconds
, microseconds
)
174 return observer
.select
(max
.socket
, readset
.sset
, writeset
.sset
, readset
.sset
, timeval
) > 0