| 12 | pass |
| 13 | |
| 14 | class ql_socket: |
| 15 | def __init__(self, socket: socket): |
| 16 | self.__fd = socket.fileno() |
| 17 | self.__socket = socket |
| 18 | |
| 19 | def __getstate__(self, *args, **kwargs): |
| 20 | _state = self.__dict__.copy() |
| 21 | |
| 22 | fname = f'_{self.__class__.__name__}__socket' |
| 23 | sock = self.__dict__[fname] |
| 24 | |
| 25 | _state[fname] = { |
| 26 | "family" : sock.family, |
| 27 | "type" : sock.type, |
| 28 | "proto" : sock.proto, |
| 29 | "laddr" : sock.getsockname(), |
| 30 | } |
| 31 | |
| 32 | return _state |
| 33 | |
| 34 | def __setstate__(self, state): |
| 35 | self.__dict__ = state |
| 36 | |
| 37 | @classmethod |
| 38 | def open(cls, domain: Union[AddressFamily, int], socktype: Union[SocketKind, int], protocol: int): |
| 39 | s = socket(domain, socktype, protocol) |
| 40 | |
| 41 | return cls(s) |
| 42 | |
| 43 | @classmethod |
| 44 | def socketpair(cls, domain: Union[AddressFamily, int], socktype: Union[SocketKind, int], protocol: int): |
| 45 | a, b = socketpair(domain, socktype, protocol) |
| 46 | |
| 47 | return cls(a), cls(b) |
| 48 | |
| 49 | def read(self, length: int) -> bytes: |
| 50 | return os.read(self.__fd, length) |
| 51 | |
| 52 | def write(self, data: bytes) -> int: |
| 53 | return os.write(self.__fd, data) |
| 54 | |
| 55 | def fileno(self) -> int: |
| 56 | return self.__fd |
| 57 | |
| 58 | def close(self) -> None: |
| 59 | os.close(self.__fd) |
| 60 | |
| 61 | def fcntl(self, cmd, arg): |
| 62 | try: |
| 63 | return fcntl.fcntl(self.__fd, cmd, arg) |
| 64 | except Exception: |
| 65 | pass |
| 66 | |
| 67 | def ioctl(self, cmd, arg): |
| 68 | # might throw an OSError |
| 69 | return fcntl.ioctl(self.__fd, cmd, arg) |
| 70 | |
| 71 | def dup(self) -> 'ql_socket': |