lib/standard/ropes: Introducing RopeBuffer, a mutable `Rope` optimized for concatenat...
[nit.git] / lib / socket / socket.nit
index 9824111..aad6449 100644 (file)
 module socket
 
 import socket_c
+intrude import standard::stream
 
 # Portal for communication between two machines
 class Socket
        super BufferedIStream
        super OStream
+       super PollableIStream
 
        # IPv4 address the socket is connected to
        # Formatted as xxx.xxx.xxx.xxx
-       var address: String
+       var address: String is noinit
 
        # Hostname of the socket connected to self
        # In C : The real canonical host name (e.g. example.org)
-       var host: nullable String
+       var host: nullable String = null
 
        # Port open for the socket
-       var port: Int
+       var port: Int is noinit
 
        # Underlying C socket
-       private var socket: FFSocket
+       private var socket: FFSocket is noinit
 
        # Underlying C socket
-       private var addrin: FFSocketAddrIn
+       private var addrin: FFSocketAddrIn is noinit
 
        redef var end_reached = false
 
@@ -93,18 +95,20 @@ class Socket
                host = null
        end
 
+       redef fun poll_in do return ready_to_read(0)
+
        # Returns an array containing an enum of the events ready to be read
        #
        # event_types : Combination of several event types to watch
        #
        # timeout : Time in milliseconds before stopping listening for events on this socket
        #
-       private fun poll_in(event_types: Array[FFSocketPollValues], timeout: Int): Array[FFSocketPollValues] do
-               if not still_alive then return new Array[FFSocketPollValues]
+       private fun pollin(event_types: Array[FFSocketPollValues], timeout: Int): Array[FFSocketPollValues] do
+               if end_reached then return new Array[FFSocketPollValues]
                return socket.socket_poll(new PollFD(socket.descriptor, event_types), timeout)
        end
 
-       # Easier use of poll_in to check for something to read on all channels of any priority
+       # Easier use of pollin to check for something to read on all channels of any priority
        #
        # timeout : Time in milliseconds before stopping to wait for events
        #
@@ -114,7 +118,7 @@ class Socket
                if eof then return false
                var events = new Array[FFSocketPollValues]
                events.push(new FFSocketPollValues.pollin)
-               return poll_in(events, timeout).length != 0
+               return pollin(events, timeout).length != 0
        end
 
        # Checks if the socket still is connected
@@ -125,7 +129,12 @@ class Socket
                var events = new Array[FFSocketPollValues]
                events.push(new FFSocketPollValues.pollhup)
                events.push(new FFSocketPollValues.pollerr)
-               return poll_in(events, 0).length == 0
+               if pollin(events, 0).length == 0 then
+                       return true
+               else
+                       end_reached = true
+                       return false
+               end
        end
 
        redef fun is_writable do return not end_reached
@@ -203,8 +212,7 @@ class Socket
 end
 
 class SocketSet
-       var sset: FFSocketSet
-       init do sset = new FFSocketSet end
+       var sset = new FFSocketSet
        fun set(s: Socket) do sset.set(s.socket) end
        fun is_set(s: Socket): Bool do return sset.is_set(s.socket) end
        fun zero do sset.zero end