qmsk/net/socket/socket.pyx
changeset 17 5f4077a530b0
parent 16 24ce1035b338
child 18 43a57943af9f
--- a/qmsk/net/socket/socket.pyx	Mon Aug 17 00:45:58 2009 +0300
+++ b/qmsk/net/socket/socket.pyx	Mon Aug 17 00:55:25 2009 +0300
@@ -444,13 +444,13 @@
             Recieve a message along with some extra data.
 
                 recv_addr   - ask for and return a sockaddr for the source address of the message?
-                lenv        - (optional) sequence of buffer sizes to use for the message data iov
+                iov_lens    - (optional) sequence of buffer sizes to use for the message data iov
                 control_len - (optional) amount of auxiliary data to recieve
                 flags       - (optional) flags to pass to recvmsg()
 
             Returns a (name, iovs, control, flags) tuple :
                 name        - the source address of the message, or None
-                iovs        - sequence of strings containing the recieved data, each at most lenv[x] bytes long
+                iovs        - sequence of strings containing the recieved data corresponding to the iov_lens
                 control     - string containing recieved control message, if any
                 flags       - recieved flags
         """
@@ -558,6 +558,50 @@
             for i in range(msg.msg_iovlen) :
                 sockbuf_deinit(&sb_list[i])
 
+    def read (self, size_t len) :
+        """
+            Read data from a socket, mostly equivalent to a recv() with flags=0.
+
+                len         - size of buffer to use for recv
+
+            Returns the recieved data as a newly allocated string of the correct length.
+        """
+
+        cdef sockbuf sb
+        cdef libc.ssize_t ret
+
+        # alloc the recv buffer
+        cdef char *buf = sockbuf_init(&sb, len)
+        
+        try :
+            # recv()
+            ret = libc.read(self.fd, buf, len)
+
+            if ret < 0 :
+                raise_errno('read')
+
+            # truncate to correct length
+            return sockbuf_truncate(&sb, ret)
+
+        finally :
+            sockbuf_deinit(&sb)
+    
+    def readv (self, object iov_lens) :
+        """
+            Read data from a socket into multiple buffers.
+
+                iov_lens    - sequence of buffer sizes to use as iovs
+
+            Returns a sequence of strings containing the recieved data corresponding to the iov_lens.
+
+            XXX: implement using real readv instead of faking it with recvmsg...
+        """
+        
+        # fake using recvmsg
+        _, iovs, _, _ = self.recvmsg(recv_addr=False, iov_lens=iov_lens)
+
+        return iovs
+
     def shutdown (self, how) :
         """
             Shutdown part of a full-duplex connection.