Commits

James Lan committed 281b842

Implement connect_ex

  • Participants
  • Parent commits 2bd9cc0

Comments (0)

Files changed (2)

pypesocketTest.py

         self.socket.bind(_PipePath)
         self.socket.listen(5)
 
-        self.thread = Thread(target=run)
-        self.thread.start()
+        self.serverthread = Thread(target=run)
+        self.serverthread.start()
         #print "server thread started"
 
     def stop(self):
             sock = socket.socket(socket.AF_UNIX)
             sock.connect(_PipePath)
             sock.close()
-        except socket.error, err:
+        except socket.error:
             pass
-            # wait for server thread
-        self.thread.join()
+        self.serverthread.join()
 
 
 class PipesocketTest(unittest.TestCase):
     def tearDown(self):
         try:
             os.unlink(_PipePath)
-        except OSError, err:
+        except OSError:
             pass
 
 
     def test_connect_ex(self):
         for i in range(10):
             sock = socket.socket(socket.AF_UNIX)
-            sock.connect_ex(_PipePath)
+            self.assertEqual(0, sock.connect_ex(_PipePath))
             sock.close()
 
     @expect_socketerror(errno.ENOENT)
             self.listener.join()
 
     def connect(self, address):
+        err = self.connect_ex(address)
+        if err:
+            raise socket.error(err)
+
+    def connect_ex(self, address):
         self.calc_pipename(address)
         timeout = 0.01
         for retry in xrange(10):
                 # connected, set message mode
                 mode = _DWORD(_PIPE_READMODE_MESSAGE)
                 if not _kernel32.SetNamedPipeHandleState(
-                        self._handle,
-                        _byref(mode),
-                        None,
-                        None):
-                    raise socket.error(str(_kernel32.GetLastError())) # todo: should be meaningful text
-                return
-                # failed to connect, see if server isn't ready
+                    self._handle,
+                    _byref(mode),
+                    None,
+                    None):
+                    return str(_kernel32.GetLastError()) # todo: should be meaningful text
+                return 0
+            # failed to connect, see if server isn't ready
             err = _kernel32.GetLastError()
             if err == _ERROR_FILE_NOT_FOUND:
-                raise socket.error(errno.ENOENT)
+                return errno.ENOENT
             if err != _ERROR_PIPE_BUSY:
-                raise socket.error(str(err)) # todo: should be meaningful text
-            # retry
+                return str(err) # todo: should be meaningful text
+                # retry
             time.sleep(timeout)
             timeout *= 2
         else:
-            raise socket.error(str(_ERROR_PIPE_BUSY))
-
-    def connect_ex(self, address):
-        pass
+            return str(_ERROR_PIPE_BUSY)
 
     def fileno(self):
         return self.event
         pass
 
     def sendall(self, string, flag=0):
-        _kernel32.WriteFile(self._handle, string, len(string), None, None)
+        written = _DWORD(0)
+        _kernel32.WriteFile(self._handle, string, len(string), _byref(written), None)
+        print repr(string), written
 
     def sendto(self, string, flag=0, address=None):
         pass