lib/socket: use a custom C call to detect error on sockets
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 31 Jul 2015 13:20:55 +0000 (09:20 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 4 Aug 2015 15:33:04 +0000 (11:33 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/socket/socket.nit
lib/socket/socket_c.nit

index 3c2a001..a75f7fb 100644 (file)
@@ -149,12 +149,11 @@ class TCPStream
                return pollin(events, timeout).length != 0
        end
 
-       # Checks if the socket still is connected
+       # Is this socket still connected?
        fun connected: Bool
        do
                if closed then return false
-               var events = [new NativeSocketPollValues.pollhup, new NativeSocketPollValues.pollerr]
-               if pollin(events, 0).length == 0 then
+               if native.poll_hup_err == 0 then
                        return true
                else
                        closed = true
index ea0b832..6842554 100644 (file)
@@ -164,6 +164,15 @@ extern class NativeSocket `{ int* `}
                return filedesc.check_response(result)
        end
 
+       # Poll this socket with `POLLHUP|POLLERR`
+       #
+       # A return value of 0 means there is no errors.
+       fun poll_hup_err: Int `{
+               struct pollfd fd = {*self, POLLHUP|POLLERR, 0};
+               int res = poll(&fd, 1, 0);
+               return res;
+       `}
+
        # Call to the poll function of the C socket
        #
        # Signature: