changeset 10578:b7ef021ecde3 octave-forge

refreshed the recv() function: * removed type conversion by constructing the output directly * assign output arguments also when the underlying recv() returns <=0, so empty data is returned. * updated the documentation string with clarification. * bug out with error if the user has to many output arguments Thanks to Sebastian Singer for reporting the error.
author pauldreik
date Mon, 23 Jul 2012 20:05:33 +0000
parents 72ebc7384647
children e55f263d0f5b
files main/sockets/src/Makefile main/sockets/src/sockets.cc
diffstat 2 files changed, 28 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/main/sockets/src/Makefile	Mon Jul 23 16:36:06 2012 +0000
+++ b/main/sockets/src/Makefile	Mon Jul 23 20:05:33 2012 +0000
@@ -5,6 +5,10 @@
 MKOCTFILE ?= mkoctfile
 OCTAVE ?= octave
 
+#windows users should link with -lws2_32 (on mingw, at least). If you
+#know how to detect windows and find the appropriate linker flag here,
+#please drop a mail to the octave-dev mailing list.
+
 #See which octave version we run by querying mkoctfile for its version
 #string. This is needed because there was an octave macro that changed
 #from 3.0 to 3.2. When 3.0 is considered long gone, this is not
--- a/main/sockets/src/sockets.cc	Mon Jul 23 16:36:06 2012 +0000
+++ b/main/sockets/src/sockets.cc	Mon Jul 23 20:05:33 2012 +0000
@@ -651,13 +651,19 @@
 	  "of recv.\n"
 	  "\n"
 	  "The read data is returned in an uint8 array data. The number of\n"
-	  "bytes read is returned in count\n"
+	  "bytes read is returned in count.\n"
 	  "\n"
 	  "You can get non-blocking operation by using the flag MSG_DONTWAIT\n"
 	  "which makes the recv() call return immediately. If there are no\n"
-	  "data, -1 is returned.\n"
+	  "data, -1 is returned in count.\n"
 	  "See the recv() man pages for further details.\n")
 {
+  if(nargout>2) 
+    {
+      error("recv: please use at most two output arguments.");
+      return octave_value(-1);
+    }
+
   int retval = 0;
   int flags = 0;
 
@@ -678,7 +684,7 @@
     s = &((octave_socket &)rep);
   }
   else if ( args(0).is_scalar_type() )
-  {
+    {//what happens if fd does not exist in socket_map?
     int fd = args(0).int_value();
     s = socket_map[fd];
   }
@@ -701,26 +707,32 @@
   retval = ::recv( s->get_sock_fd(), ( char* )buf, len, flags );
 #endif
 
+  octave_value_list return_list;
+  uint8NDArray data;
 
-  octave_value_list return_list;
   //always return the status in the second output parameter
   return_list(1) = retval; 
   if(retval<0) {
     //We get -1 if an error occurs,or if there is no data and the
     //socket is non-blocking. We should return in both cases.
+    return_list(0) = data;
   } else if (0==retval) {
     //The peer has shut down.
+    return_list(0) = data;
   } else {
-    //Normal behaviour.
-    Matrix return_buf(1,retval);
-    
+    //Normal behaviour. Copy the buffer to the output variable. For
+    //backward compatibility, a row vector is returned.
+    dim_vector d;
+    d(0)=1;
+    d(1)=retval;
+    data.resize(d);
+
+    //this could possibly be made more efficient with memcpy and
+    //fortran_vec() instead.
     for ( int i = 0 ; i < retval ; i++ )
-      return_buf(0,i) = buf[i];
+      data(i) = buf[i];
     
-    octave_value in_buf(return_buf);
-    octave_value out_buf;
-    OCTAVE_TYPE_CONV( in_buf, out_buf, uint8 );
-    return_list(0) = out_buf;
+    return_list(0) = data;
   }
 
   delete[] buf;