abstract.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. # -*- test-case-name: twisted.test.test_abstract -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Support for generic select()able objects.
  6. """
  7. from __future__ import annotations
  8. from socket import AF_INET, AF_INET6, inet_pton
  9. from typing import Iterable, List, Optional, Union
  10. from zope.interface import implementer
  11. from twisted.internet import interfaces, main
  12. from twisted.python import failure, reflect
  13. # Twisted Imports
  14. from twisted.python.compat import lazyByteSlice
  15. def _dataMustBeBytes(obj):
  16. if not isinstance(obj, bytes): # no, really, I mean it
  17. raise TypeError("Data must be bytes")
  18. # Python 3.4+ can join bytes and memoryviews; using a
  19. # memoryview prevents the slice from copying
  20. def _concatenate(bObj, offset, bArray):
  21. return b"".join([memoryview(bObj)[offset:]] + bArray)
  22. class _ConsumerMixin:
  23. """
  24. L{IConsumer} implementations can mix this in to get C{registerProducer} and
  25. C{unregisterProducer} methods which take care of keeping track of a
  26. producer's state.
  27. Subclasses must provide three attributes which L{_ConsumerMixin} will read
  28. but not write:
  29. - connected: A C{bool} which is C{True} as long as the consumer has
  30. someplace to send bytes (for example, a TCP connection), and then
  31. C{False} when it no longer does.
  32. - disconnecting: A C{bool} which is C{False} until something like
  33. L{ITransport.loseConnection} is called, indicating that the send buffer
  34. should be flushed and the connection lost afterwards. Afterwards,
  35. C{True}.
  36. - disconnected: A C{bool} which is C{False} until the consumer no longer
  37. has a place to send bytes, then C{True}.
  38. Subclasses must also override the C{startWriting} method.
  39. @ivar producer: L{None} if no producer is registered, otherwise the
  40. registered producer.
  41. @ivar producerPaused: A flag indicating whether the producer is currently
  42. paused.
  43. @type producerPaused: L{bool}
  44. @ivar streamingProducer: A flag indicating whether the producer was
  45. registered as a streaming (ie push) producer or not (ie a pull
  46. producer). This will determine whether the consumer may ever need to
  47. pause and resume it, or if it can merely call C{resumeProducing} on it
  48. when buffer space is available.
  49. @ivar streamingProducer: C{bool} or C{int}
  50. """
  51. producer = None
  52. producerPaused = False
  53. streamingProducer = False
  54. def startWriting(self):
  55. """
  56. Override in a subclass to cause the reactor to monitor this selectable
  57. for write events. This will be called once in C{unregisterProducer} if
  58. C{loseConnection} has previously been called, so that the connection can
  59. actually close.
  60. """
  61. raise NotImplementedError("%r did not implement startWriting")
  62. def registerProducer(self, producer, streaming):
  63. """
  64. Register to receive data from a producer.
  65. This sets this selectable to be a consumer for a producer. When this
  66. selectable runs out of data on a write() call, it will ask the producer
  67. to resumeProducing(). When the FileDescriptor's internal data buffer is
  68. filled, it will ask the producer to pauseProducing(). If the connection
  69. is lost, FileDescriptor calls producer's stopProducing() method.
  70. If streaming is true, the producer should provide the IPushProducer
  71. interface. Otherwise, it is assumed that producer provides the
  72. IPullProducer interface. In this case, the producer won't be asked to
  73. pauseProducing(), but it has to be careful to write() data only when its
  74. resumeProducing() method is called.
  75. """
  76. if self.producer is not None:
  77. raise RuntimeError(
  78. "Cannot register producer %s, because producer %s was never "
  79. "unregistered." % (producer, self.producer)
  80. )
  81. if self.disconnected:
  82. producer.stopProducing()
  83. else:
  84. self.producer = producer
  85. self.streamingProducer = streaming
  86. if not streaming:
  87. producer.resumeProducing()
  88. def unregisterProducer(self):
  89. """
  90. Stop consuming data from a producer, without disconnecting.
  91. """
  92. self.producer = None
  93. if self.connected and self.disconnecting:
  94. self.startWriting()
  95. @implementer(interfaces.ILoggingContext)
  96. class _LogOwner:
  97. """
  98. Mixin to help implement L{interfaces.ILoggingContext} for transports which
  99. have a protocol, the log prefix of which should also appear in the
  100. transport's log prefix.
  101. """
  102. def _getLogPrefix(self, applicationObject: object) -> str:
  103. """
  104. Determine the log prefix to use for messages related to
  105. C{applicationObject}, which may or may not be an
  106. L{interfaces.ILoggingContext} provider.
  107. @return: A C{str} giving the log prefix to use.
  108. """
  109. if interfaces.ILoggingContext.providedBy(applicationObject):
  110. return applicationObject.logPrefix()
  111. return applicationObject.__class__.__name__
  112. def logPrefix(self):
  113. """
  114. Override this method to insert custom logging behavior. Its
  115. return value will be inserted in front of every line. It may
  116. be called more times than the number of output lines.
  117. """
  118. return "-"
  119. @implementer(
  120. interfaces.IPushProducer,
  121. interfaces.IReadWriteDescriptor,
  122. interfaces.IConsumer,
  123. interfaces.ITransport,
  124. interfaces.IHalfCloseableDescriptor,
  125. )
  126. class FileDescriptor(_ConsumerMixin, _LogOwner):
  127. """
  128. An object which can be operated on by select().
  129. This is an abstract superclass of all objects which may be notified when
  130. they are readable or writable; e.g. they have a file-descriptor that is
  131. valid to be passed to select(2).
  132. """
  133. # We have two buffers: a list (_tempDataBuffer), and a byte string
  134. # (self.dataBuffer). A given write may not be able to write everything, and
  135. # we also limit to sending at most SEND_LIMIT bytes at a time. Thus, we
  136. # also have self.offset tracks where in self.dataBuffer we are in the
  137. # writing process, to reduce unnecessary copying if we failed to write all
  138. # the data.
  139. connected = 0
  140. disconnected = 0
  141. disconnecting = 0
  142. _writeDisconnecting = False
  143. _writeDisconnected = False
  144. dataBuffer = b""
  145. offset = 0
  146. SEND_LIMIT = 128 * 1024
  147. def __init__(self, reactor: Optional[interfaces.IReactorFDSet] = None):
  148. """
  149. @param reactor: An L{IReactorFDSet} provider which this descriptor will
  150. use to get readable and writeable event notifications. If no value
  151. is given, the global reactor will be used.
  152. """
  153. if not reactor:
  154. from twisted.internet import reactor as _reactor
  155. reactor = _reactor # type: ignore[assignment]
  156. self.reactor = reactor
  157. # will be added to dataBuffer in doWrite
  158. self._tempDataBuffer: List[bytes] = []
  159. self._tempDataLen = 0
  160. def connectionLost(self, reason):
  161. """The connection was lost.
  162. This is called when the connection on a selectable object has been
  163. lost. It will be called whether the connection was closed explicitly,
  164. an exception occurred in an event handler, or the other end of the
  165. connection closed it first.
  166. Clean up state here, but make sure to call back up to FileDescriptor.
  167. """
  168. self.disconnected = 1
  169. self.connected = 0
  170. if self.producer is not None:
  171. self.producer.stopProducing()
  172. self.producer = None
  173. self.stopReading()
  174. self.stopWriting()
  175. def writeSomeData(self, data: bytes) -> Union[int, BaseException]:
  176. """
  177. Write as much as possible of the given data, immediately.
  178. This is called to invoke the lower-level writing functionality, such
  179. as a socket's send() method, or a file's write(); this method
  180. returns an integer or an exception. If an integer, it is the number
  181. of bytes written (possibly zero); if an exception, it indicates the
  182. connection was lost.
  183. """
  184. raise NotImplementedError(
  185. "%s does not implement writeSomeData" % reflect.qual(self.__class__)
  186. )
  187. def doRead(self):
  188. """
  189. Called when data is available for reading.
  190. Subclasses must override this method. The result will be interpreted
  191. in the same way as a result of doWrite().
  192. """
  193. raise NotImplementedError(
  194. "%s does not implement doRead" % reflect.qual(self.__class__)
  195. )
  196. def doWrite(self):
  197. """
  198. Called when data can be written.
  199. @return: L{None} on success, an exception or a negative integer on
  200. failure.
  201. @see: L{twisted.internet.interfaces.IWriteDescriptor.doWrite}.
  202. """
  203. # We only send at most SEND_LIMIT bytes at a time. If the amount of
  204. # bytes in our send-immediately buffer is smaller than that limit,
  205. # probably a good time to add the bytes from our secondary, list-based
  206. # buffer (self._tempDataBuffer.)
  207. remaining = len(self.dataBuffer) - self.offset
  208. if remaining < self.SEND_LIMIT:
  209. if remaining > 0:
  210. # There is currently some data to write, extend it with the
  211. # list data.
  212. self.dataBuffer = _concatenate(
  213. self.dataBuffer, self.offset, self._tempDataBuffer
  214. )
  215. else:
  216. # self.dataBuffer has nothing left to write, so just convert
  217. # the list buffer to bytes buffer in a cheaper way:
  218. self.dataBuffer = b"".join(self._tempDataBuffer)
  219. self.offset = 0
  220. self._tempDataBuffer = []
  221. self._tempDataLen = 0
  222. # Send as much data as you can.
  223. if self.offset:
  224. l = self.writeSomeData(lazyByteSlice(self.dataBuffer, self.offset))
  225. else:
  226. # Optimization: skip lazyByteSlice() when it's unnecessary.
  227. l = self.writeSomeData(self.dataBuffer)
  228. # There is no writeSomeData implementation in Twisted which returns
  229. # < 0, but the documentation for writeSomeData used to claim negative
  230. # integers meant connection lost. Keep supporting this here,
  231. # although it may be worth deprecating and removing at some point.
  232. if isinstance(l, Exception) or l < 0:
  233. return l
  234. self.offset += l
  235. # If there is nothing left to send,
  236. if self.offset == len(self.dataBuffer) and not self._tempDataLen:
  237. self.dataBuffer = b""
  238. self.offset = 0
  239. # stop writing.
  240. self.stopWriting()
  241. # If I've got a producer who is supposed to supply me with data,
  242. if self.producer is not None and (
  243. (not self.streamingProducer) or self.producerPaused
  244. ):
  245. # tell them to supply some more.
  246. self.producerPaused = False
  247. self.producer.resumeProducing()
  248. elif self.disconnecting:
  249. # But if I was previously asked to let the connection die, do
  250. # so.
  251. return self._postLoseConnection()
  252. elif self._writeDisconnecting:
  253. # I was previously asked to half-close the connection. We
  254. # set _writeDisconnected before calling handler, in case the
  255. # handler calls loseConnection(), which will want to check for
  256. # this attribute.
  257. self._writeDisconnected = True
  258. result = self._closeWriteConnection()
  259. return result
  260. return None
  261. def _postLoseConnection(self):
  262. """Called after a loseConnection(), when all data has been written.
  263. Whatever this returns is then returned by doWrite.
  264. """
  265. # default implementation, telling reactor we're finished
  266. return main.CONNECTION_DONE
  267. def _closeWriteConnection(self):
  268. # override in subclasses
  269. pass
  270. def writeConnectionLost(self, reason):
  271. # in current code should never be called
  272. self.connectionLost(reason)
  273. def readConnectionLost(self, reason: failure.Failure) -> None:
  274. # override in subclasses
  275. self.connectionLost(reason)
  276. def getHost(self):
  277. # ITransport.getHost
  278. raise NotImplementedError()
  279. def getPeer(self):
  280. # ITransport.getPeer
  281. raise NotImplementedError()
  282. def _isSendBufferFull(self):
  283. """
  284. Determine whether the user-space send buffer for this transport is full
  285. or not.
  286. When the buffer contains more than C{self.bufferSize} bytes, it is
  287. considered full. This might be improved by considering the size of the
  288. kernel send buffer and how much of it is free.
  289. @return: C{True} if it is full, C{False} otherwise.
  290. """
  291. return len(self.dataBuffer) + self._tempDataLen > self.bufferSize
  292. def _maybePauseProducer(self):
  293. """
  294. Possibly pause a producer, if there is one and the send buffer is full.
  295. """
  296. # If we are responsible for pausing our producer,
  297. if self.producer is not None and self.streamingProducer:
  298. # and our buffer is full,
  299. if self._isSendBufferFull():
  300. # pause it.
  301. self.producerPaused = True
  302. self.producer.pauseProducing()
  303. def write(self, data: bytes) -> None:
  304. """Reliably write some data.
  305. The data is buffered until the underlying file descriptor is ready
  306. for writing. If there is more than C{self.bufferSize} data in the
  307. buffer and this descriptor has a registered streaming producer, its
  308. C{pauseProducing()} method will be called.
  309. """
  310. _dataMustBeBytes(data)
  311. if not self.connected or self._writeDisconnected:
  312. return
  313. if data:
  314. self._tempDataBuffer.append(data)
  315. self._tempDataLen += len(data)
  316. self._maybePauseProducer()
  317. self.startWriting()
  318. def writeSequence(self, iovec: Iterable[bytes]) -> None:
  319. """
  320. Reliably write a sequence of data.
  321. Currently, this is a convenience method roughly equivalent to::
  322. for chunk in iovec:
  323. fd.write(chunk)
  324. It may have a more efficient implementation at a later time or in a
  325. different reactor.
  326. As with the C{write()} method, if a buffer size limit is reached and a
  327. streaming producer is registered, it will be paused until the buffered
  328. data is written to the underlying file descriptor.
  329. """
  330. for i in iovec:
  331. _dataMustBeBytes(i)
  332. if not self.connected or not iovec or self._writeDisconnected:
  333. return
  334. self._tempDataBuffer.extend(iovec)
  335. for i in iovec:
  336. self._tempDataLen += len(i)
  337. self._maybePauseProducer()
  338. self.startWriting()
  339. def loseConnection(self):
  340. """Close the connection at the next available opportunity.
  341. Call this to cause this FileDescriptor to lose its connection. It will
  342. first write any data that it has buffered.
  343. If there is data buffered yet to be written, this method will cause the
  344. transport to lose its connection as soon as it's done flushing its
  345. write buffer. If you have a producer registered, the connection won't
  346. be closed until the producer is finished. Therefore, make sure you
  347. unregister your producer when it's finished, or the connection will
  348. never close.
  349. """
  350. if self.connected and not self.disconnecting:
  351. if self._writeDisconnected:
  352. # doWrite won't trigger the connection close anymore
  353. self.stopReading()
  354. self.stopWriting()
  355. self.connectionLost(failure.Failure(main.CONNECTION_DONE))
  356. else:
  357. self.stopReading()
  358. self.startWriting()
  359. self.disconnecting = 1
  360. def loseWriteConnection(self):
  361. self._writeDisconnecting = True
  362. self.startWriting()
  363. def stopReading(self):
  364. """Stop waiting for read availability.
  365. Call this to remove this selectable from being notified when it is
  366. ready for reading.
  367. """
  368. self.reactor.removeReader(self)
  369. def stopWriting(self):
  370. """Stop waiting for write availability.
  371. Call this to remove this selectable from being notified when it is ready
  372. for writing.
  373. """
  374. self.reactor.removeWriter(self)
  375. def startReading(self):
  376. """Start waiting for read availability."""
  377. self.reactor.addReader(self)
  378. def startWriting(self):
  379. """Start waiting for write availability.
  380. Call this to have this FileDescriptor be notified whenever it is ready for
  381. writing.
  382. """
  383. self.reactor.addWriter(self)
  384. # Producer/consumer implementation
  385. # first, the consumer stuff. This requires no additional work, as
  386. # any object you can write to can be a consumer, really.
  387. producer = None
  388. bufferSize = 2**2**2**2
  389. def stopConsuming(self):
  390. """Stop consuming data.
  391. This is called when a producer has lost its connection, to tell the
  392. consumer to go lose its connection (and break potential circular
  393. references).
  394. """
  395. self.unregisterProducer()
  396. self.loseConnection()
  397. # producer interface implementation
  398. def resumeProducing(self):
  399. if self.connected and not self.disconnecting:
  400. self.startReading()
  401. def pauseProducing(self):
  402. self.stopReading()
  403. def stopProducing(self):
  404. self.loseConnection()
  405. def fileno(self):
  406. """File Descriptor number for select().
  407. This method must be overridden or assigned in subclasses to
  408. indicate a valid file descriptor for the operating system.
  409. """
  410. return -1
  411. def isIPAddress(addr: str, family: int = AF_INET) -> bool:
  412. """
  413. Determine whether the given string represents an IP address of the given
  414. family; by default, an IPv4 address.
  415. @param addr: A string which may or may not be the decimal dotted
  416. representation of an IPv4 address.
  417. @param family: The address family to test for; one of the C{AF_*} constants
  418. from the L{socket} module. (This parameter has only been available
  419. since Twisted 17.1.0; previously L{isIPAddress} could only test for IPv4
  420. addresses.)
  421. @return: C{True} if C{addr} represents an IPv4 address, C{False} otherwise.
  422. """
  423. if isinstance(addr, bytes): # type: ignore[unreachable]
  424. try: # type: ignore[unreachable]
  425. addr = addr.decode("ascii")
  426. except UnicodeDecodeError:
  427. return False
  428. if family == AF_INET6:
  429. # On some platforms, inet_ntop fails unless the scope ID is valid; this
  430. # is a test for whether the given string *is* an IP address, so strip
  431. # any potential scope ID before checking.
  432. addr = addr.split("%", 1)[0]
  433. elif family == AF_INET:
  434. # On Windows, where 3.5+ implement inet_pton, "0" is considered a valid
  435. # IPv4 address, but we want to ensure we have all 4 segments.
  436. if addr.count(".") != 3:
  437. return False
  438. else:
  439. raise ValueError(f"unknown address family {family!r}")
  440. try:
  441. # This might be a native implementation or the one from
  442. # twisted.python.compat.
  443. inet_pton(family, addr)
  444. except (ValueError, OSError):
  445. return False
  446. return True
  447. def isIPv6Address(addr: str) -> bool:
  448. """
  449. Determine whether the given string represents an IPv6 address.
  450. @param addr: A string which may or may not be the hex
  451. representation of an IPv6 address.
  452. @type addr: C{str}
  453. @return: C{True} if C{addr} represents an IPv6 address, C{False}
  454. otherwise.
  455. @rtype: C{bool}
  456. """
  457. return isIPAddress(addr, AF_INET6)
  458. __all__ = ["FileDescriptor", "isIPAddress", "isIPv6Address"]