lib/socket: intro `flush` and `blocking=`
authorAlexis Laferrière <alexis.laf@xymus.net>
Mon, 22 Dec 2014 02:42:32 +0000 (21:42 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 23 Dec 2014 15:57:33 +0000 (10:57 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/socket/socket.nit

index f0aaa85..c28a354 100644 (file)
@@ -171,6 +171,14 @@ class TCPStream
                end
        end
 
+       # Send the data present in the socket buffer
+       fun flush
+       do
+               socket.setsockopt(new NativeSocketOptLevels.tcp, new NativeSocketOptNames.tcp_nodelay, 1)
+               socket.setsockopt(new NativeSocketOptLevels.tcp, new NativeSocketOptNames.tcp_nodelay, 0)
+       end
+end
+
 # A socket listening on a given `port` for incomming connections
 #
 # Create streams to communicate with clients using `accept`.
@@ -222,6 +230,14 @@ class TCPServer
                return new TCPStream.server_side(native)
        end
 
+       # Set whether calls to `accept` are blocking
+       fun blocking=(value: Bool)
+       do
+               # We use the opposite from the native version as the native API
+               # is closer to the C API. In the Nity API, we use a positive version
+               # of the name.
+               socket.non_blocking = not value
+       end
 
        # Close this socket
        fun close