Ticket #5562: abstract.diff2

File abstract.diff2, 4.2 KB (added by BrianMatthews, 22 months ago)
Line 
1--- C:/Users/BRMA/AppData/Local/Temp/abstract.py-revBASE.svn006.tmp.py  Thu Mar 15 13:18:28 2012
2+++ E:/Workspace/svn/Twisted-5562/twisted/internet/iocpreactor/abstract.py      Wed Jan 16 10:16:40 2013
3@@ -146,12 +146,12 @@
4 
5     # write stuff
6     dataBuffer = ''
7-    offset = 0
8     writing = False
9     _writeScheduled = None
10     _writeDisconnecting = False
11     _writeDisconnected = False
12     writeBufferSize = 2**2**2**2
13+    writePending = False
14 
15 
16     def loseWriteConnection(self):
17@@ -168,28 +168,26 @@
18         # in current code should never be called
19         self.connectionLost(reason)
20 
21-
22     def startWriting(self):
23         self.reactor.addActiveHandle(self)
24         self.writing = True
25-        if not self._writeScheduled:
26+        if not self.writePending and not self._writeScheduled:
27             self._writeScheduled = self.reactor.callLater(0,
28                                                           self._resumeWriting)
29 
30-
31     def stopWriting(self):
32         if self._writeScheduled:
33             self._writeScheduled.cancel()
34             self._writeScheduled = None
35         self.writing = False
36 
37-
38     def _resumeWriting(self):
39         self._writeScheduled = None
40         self.doWrite()
41 
42 
43     def _cbWrite(self, rc, bytes, evt):
44+        self.writePending = False
45         if self._handleWrite(rc, bytes, evt):
46             self.doWrite()
47 
48@@ -208,11 +206,9 @@
49                                     (errno.errorcode.get(rc, 'unknown'), rc))))
50             return False
51         else:
52-            self.offset += bytes
53             # If there is nothing left to send,
54-            if self.offset == len(self.dataBuffer) and not self._tempDataLen:
55+            if bytes == len(self.dataBuffer) and not self._tempDataLen:
56                 self.dataBuffer = ""
57-                self.offset = 0
58                 # stop writing
59                 self.stopWriting()
60                 # If I've got a producer who is supposed to supply me with data
61@@ -227,35 +223,42 @@
62                     self.connectionLost(failure.Failure(main.CONNECTION_DONE))
63                 elif self._writeDisconnecting:
64                     # I was previously asked to to half-close the connection.
65+                    self._closeWriteConnection()
66                     self._writeDisconnected = True
67-                    self._closeWriteConnection()
68                 return False
69             else:
70+                if bytes > 0:
71+                      if bytes < len(self.dataBuffer):
72+                        # remove processed bytes
73+                        self.dataBuffer = buffer(self.dataBuffer, bytes)
74+                    else:
75+                        self.dataBuffer = ""
76                 return True
77 
78 
79     def doWrite(self):
80-        if len(self.dataBuffer) - self.offset < self.SEND_LIMIT:
81+        ''' should only called when no current write is pending,
82+            we dont know where to start the write from until we know how many were written
83+        '''
84+        if self.writePending:
85+            return
86+        if len(self.dataBuffer) < self.SEND_LIMIT:
87             # If there is currently less than SEND_LIMIT bytes left to send
88             # in the string, extend it with the array data.
89-            self.dataBuffer = (buffer(self.dataBuffer, self.offset) +
90-                               "".join(self._tempDataBuffer))
91-            self.offset = 0
92+            self.dataBuffer += "".join(self._tempDataBuffer)
93             self._tempDataBuffer = []
94             self._tempDataLen = 0
95 
96         evt = _iocp.Event(self._cbWrite, self)
97 
98         # Send as much data as you can.
99-        if self.offset:
100-            evt.buff = buff = buffer(self.dataBuffer, self.offset)
101+        evt.buff = buff = self.dataBuffer
102+        rc, _bytes = self.writeToHandle(buff, evt)
103+        if rc and rc != ERROR_IO_PENDING:
104+            self._handleWrite(rc, 0, evt)
105         else:
106-            evt.buff = buff = self.dataBuffer
107-        rc, bytes = self.writeToHandle(buff, evt)
108-        if rc and rc != ERROR_IO_PENDING:
109-            self._handleWrite(rc, bytes, evt)
110+            self.writePending = True
111 
112-
113     def writeToHandle(self, buff, evt):
114         raise NotImplementedError() # TODO: this should default to WriteFile
115