| 95 | | Called when data can be written. |
| 96 | | |
| 97 | | A result that is true (which will be a negative number or an |
| 98 | | exception instance) indicates that the connection was lost. A false |
| 99 | | result implies the connection is still there; a result of 0 |
| 100 | | indicates no write was done, and a result of None indicates that a |
| 101 | | write was done. |
| 102 | | """ |
| 103 | | if len(self.dataBuffer) - self.offset < self.SEND_LIMIT: |
| 104 | | # If there is currently less than SEND_LIMIT bytes left to send |
| 105 | | # in the string, extend it with the array data. |
| 106 | | self.dataBuffer = buffer(self.dataBuffer, self.offset) + "".join(self._tempDataBuffer) |
| 107 | | self.offset = 0 |
| 108 | | self._tempDataBuffer = [] |
| 109 | | self._tempDataLen = 0 |
| 110 | | |
| 111 | | # Send as much data as you can. |
| 112 | | if self.offset: |
| 113 | | l = self.writeSomeData(buffer(self.dataBuffer, self.offset)) |
| 114 | | else: |
| 115 | | l = self.writeSomeData(self.dataBuffer) |
| 116 | | |
| 117 | | # There is no writeSomeData implementation in Twisted which returns |
| 118 | | # 0, but the documentation for writeSomeData used to claim negative |
| 119 | | # integers meant connection lost. Keep supporting this here, |
| 120 | | # although it may be worth deprecating and removing at some point. |
| 121 | | if l < 0 or isinstance(l, Exception): |
| 122 | | return l |
| 123 | | if l == 0 and self.dataBuffer: |
| 124 | | result = 0 |
| 125 | | else: |
| 126 | | result = None |
| 127 | | self.offset += l |
| | 100 | while 1: |
| | 101 | # always try to do something |
| | 102 | # quit when dataBuffer size is acceptably low (see end) |
| | 103 | if len(self.dataBuffer) - self.offset < self.SEND_LIMIT: |
| | 104 | # If there is currently less than SEND_LIMIT bytes left to send |
| | 105 | # in the string, extend it with the array data. |
| | 106 | buf = [] |
| | 107 | accum = len(self.dataBuffer)-self.offset |
| | 108 | m = 2 * max(self.SEND_LIMIT, self.bufferSize) |
| | 109 | chunk = None |
| | 110 | while self._tempDataBuffer and accum < m: |
| | 111 | # don't want to join too much data at once |
| | 112 | chunk = self._tempDataBuffer.pop(0) |
| | 113 | buf.append(chunk) |
| | 114 | n = len(chunk) |
| | 115 | accum += n |
| | 116 | self._tempDataLen -= n |
| | 117 | del chunk, accum, m |
| | 118 | self.dataBuffer = (buffer(self.dataBuffer, self.offset) + "".join(buf)) |
| | 119 | del buf |
| | 120 | self.offset = 0 |
| | 121 | # Send as much data as you can. |
| | 122 | if self.offset: |
| | 123 | l = self.writeSomeData(buffer(self.dataBuffer, self.offset)) |
| | 124 | else: |
| | 125 | l = self.writeSomeData(self.dataBuffer) |
| | 126 | # There is no writeSomeData implementation in Twisted which returns |
| | 127 | # 0, but the documentation for writeSomeData used to claim negative |
| | 128 | # integers meant connection lost. Keep supporting this here, |
| | 129 | # although it may be worth deprecating and removing at some point. |
| | 130 | if l < 0 or isinstance(l, Exception): |
| | 131 | return l |
| | 132 | if l == 0 and self.dataBuffer: |
| | 133 | # Couldn't send data |
| | 134 | result = 0 |
| | 135 | break |
| | 136 | else: |
| | 137 | result = None |
| | 138 | self.offset += l |
| | 139 | if len(self.dataBuffer) - self.offset + self._tempDataLen < self.bufferSize: |
| | 140 | # quit, 'cause dataBuffer AND _tempDataBuffer is small enough |
| | 141 | break |
| | 142 | # |