PHP 8.5.2
Preview: stateful.py Size: 1.64 KB
//lib/python3/dist-packages/twisted/protocols/stateful.py

# -*- test-case-name: twisted.test.test_stateful -*-

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.


from io import BytesIO

from twisted.internet import protocol


class StatefulProtocol(protocol.Protocol):
    """A Protocol that stores state for you.

    state is a pair (function, num_bytes). When num_bytes bytes of data arrives
    from the network, function is called. It is expected to return the next
    state or None to keep same state. Initial state is returned by
    getInitialState (override it).
    """

    _sful_data = None, None, 0

    def makeConnection(self, transport):
        protocol.Protocol.makeConnection(self, transport)
        self._sful_data = self.getInitialState(), BytesIO(), 0

    def getInitialState(self):
        raise NotImplementedError

    def dataReceived(self, data):
        state, buffer, offset = self._sful_data
        buffer.seek(0, 2)
        buffer.write(data)
        blen = buffer.tell()  # how many bytes total is in the buffer
        buffer.seek(offset)
        while blen - offset >= state[1]:
            d = buffer.read(state[1])
            offset += state[1]
            next = state[0](d)
            if (
                self.transport.disconnecting
            ):  # XXX: argh stupid hack borrowed right from LineReceiver
                return  # dataReceived won't be called again, so who cares about consistent state
            if next:
                state = next
        if offset != 0:
            b = buffer.read()
            buffer.seek(0)
            buffer.truncate()
            buffer.write(b)
            offset = 0
        self._sful_data = state, buffer, offset

Directory Contents

Dirs: 3 × Files: 19

Name Size Perms Modified Actions
haproxy DIR
- drwxr-xr-x 2026-01-08 12:56:22
Edit Download
test DIR
- drwxr-xr-x 2026-01-08 12:56:22
Edit Download
- drwxr-xr-x 2026-01-08 12:56:23
Edit Download
94.75 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
30.90 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.19 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
101.97 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
9.20 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
7.76 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
11.65 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
23.10 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
7.01 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
20.87 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.31 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.78 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.48 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
37.07 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
7.74 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.64 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
36.02 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.44 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
151 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).