|
| 1 | +import hashes, chronos, libp2p/varint, stew/byteutils |
| 2 | +import libp2p/stream/connection |
| 3 | +from fragmentation import dataSize |
| 4 | + |
| 5 | +type MixExitConnection* = ref object of Connection |
| 6 | + message: seq[byte] |
| 7 | + response: seq[byte] |
| 8 | + |
| 9 | +method join*( |
| 10 | + self: MixExitConnection |
| 11 | +): Future[void] {.async: (raises: [CancelledError], raw: true), public.} = |
| 12 | + discard |
| 13 | + |
| 14 | +method readExactly*( |
| 15 | + self: MixExitConnection, pbytes: pointer, nbytes: int |
| 16 | +): Future[void] {.async: (raises: [CancelledError, LPStreamError]), public.} = |
| 17 | + if nbytes == 0: |
| 18 | + return |
| 19 | + |
| 20 | + if self.message.len < nbytes: |
| 21 | + raise newException( |
| 22 | + LPStreamError, "Not enough data in to read exactly " & $nbytes & " bytes." |
| 23 | + ) |
| 24 | + |
| 25 | + var pbuffer = cast[ptr UncheckedArray[byte]](pbytes) |
| 26 | + for i in 0 ..< nbytes: |
| 27 | + pbuffer[i] = self.message[i] |
| 28 | + |
| 29 | + if nbytes < self.message.len: |
| 30 | + self.message = self.message[nbytes .. ^1] |
| 31 | + else: |
| 32 | + self.isEof = true |
| 33 | + self.message = @[] |
| 34 | + |
| 35 | +# ToDo: Check readLine, readVarint implementations |
| 36 | +method readLine*( |
| 37 | + self: MixExitConnection, limit = 0, sep = "\r\n" |
| 38 | +): Future[string] {.async: (raises: [CancelledError, LPStreamError]), public.} = |
| 39 | + var |
| 40 | + lim = if limit <= 0: -1 else: limit |
| 41 | + result: seq[byte] = @[] |
| 42 | + state = 0 |
| 43 | + |
| 44 | + while true: |
| 45 | + if state < len(sep): |
| 46 | + if self.message.len == 0: |
| 47 | + raise newException(LPStreamError, "Not enough data to read line.") |
| 48 | + |
| 49 | + let ch = self.message[0] |
| 50 | + self.message.delete(0) |
| 51 | + |
| 52 | + if byte(sep[state]) == ch: |
| 53 | + inc(state) |
| 54 | + if state == len(sep): |
| 55 | + break |
| 56 | + else: |
| 57 | + result.add(ch) |
| 58 | + state = 0 |
| 59 | + |
| 60 | + if lim > 0 and len(result) == lim: |
| 61 | + break |
| 62 | + else: |
| 63 | + break |
| 64 | + |
| 65 | + return cast[string](result) |
| 66 | + |
| 67 | +method readVarint*( |
| 68 | + self: MixExitConnection |
| 69 | +): Future[uint64] {.async: (raises: [CancelledError, LPStreamError]), public.} = |
| 70 | + var |
| 71 | + buffer: array[10, byte] |
| 72 | + bytesRead = 0 |
| 73 | + |
| 74 | + while bytesRead < buffer.len: |
| 75 | + if self.message.len == 0: |
| 76 | + raise newException(LPStreamError, "Not enough data to read varint") |
| 77 | + |
| 78 | + buffer[bytesRead] = self.message[0] |
| 79 | + self.message.delete(0) |
| 80 | + bytesRead += 1 |
| 81 | + |
| 82 | + var |
| 83 | + varint: uint64 |
| 84 | + length: int |
| 85 | + let res = PB.getUVarint(buffer.toOpenArray(0, bytesRead - 1), length, varint) |
| 86 | + if res.isOk(): |
| 87 | + return varint |
| 88 | + if res.error() != VarintError.Incomplete: |
| 89 | + break |
| 90 | + |
| 91 | + raise newException(LPStreamError, "Cannot parse varint") |
| 92 | + |
| 93 | +method readLp*( |
| 94 | + self: MixExitConnection, maxSize: int |
| 95 | +): Future[seq[byte]] {.async: (raises: [CancelledError, LPStreamError]), public.} = |
| 96 | + let |
| 97 | + length = await self.readVarint() |
| 98 | + maxLen = uint64(if maxSize < 0: int.high else: maxSize) |
| 99 | + |
| 100 | + if length > maxLen: |
| 101 | + raise (ref MaxSizeError)(msg: "Message exceeds maximum length") |
| 102 | + |
| 103 | + if length == 0: |
| 104 | + self.isEof = true |
| 105 | + return @[] |
| 106 | + |
| 107 | + if self.message.len < int(length): |
| 108 | + raise newException(LPStreamError, "Not enough data to read " & $length & " bytes.") |
| 109 | + |
| 110 | + result = self.message[0 ..< int(length)] |
| 111 | + if int(length) == self.message.len: |
| 112 | + self.isEof = true |
| 113 | + self.message = @[] |
| 114 | + else: |
| 115 | + self.message = self.message[int(length) .. ^1] |
| 116 | + return result |
| 117 | + |
| 118 | +method write*( |
| 119 | + self: MixExitConnection, msg: seq[byte] |
| 120 | +): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true), public.} = |
| 121 | + self.response.add(msg) |
| 122 | + let fut = newFuture[void]() |
| 123 | + fut.complete() |
| 124 | + return fut |
| 125 | + |
| 126 | +proc write*( |
| 127 | + self: MixExitConnection, msg: string |
| 128 | +): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true), public.} = |
| 129 | + self.write(msg.toBytes()) |
| 130 | + |
| 131 | +method writeLp*( |
| 132 | + self: MixExitConnection, msg: openArray[byte] |
| 133 | +): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true), public.} = |
| 134 | + if msg.len() > dataSize: |
| 135 | + let fut = newFuture[void]() |
| 136 | + fut.fail( |
| 137 | + newException(LPStreamError, "exceeds max msg size of " & $dataSize & " bytes") |
| 138 | + ) |
| 139 | + return fut |
| 140 | + |
| 141 | + var |
| 142 | + vbytes: seq[byte] = @[] |
| 143 | + value = msg.len().uint64 |
| 144 | + |
| 145 | + while value >= 128: |
| 146 | + vbytes.add(byte((value and 127) or 128)) |
| 147 | + value = value shr 7 |
| 148 | + vbytes.add(byte(value)) |
| 149 | + |
| 150 | + var buf = newSeqUninitialized[byte](msg.len() + vbytes.len) |
| 151 | + buf[0 ..< vbytes.len] = vbytes.toOpenArray(0, vbytes.len - 1) |
| 152 | + buf[vbytes.len ..< buf.len] = msg |
| 153 | + |
| 154 | + self.write(buf) |
| 155 | + |
| 156 | +method writeLp*( |
| 157 | + self: MixExitConnection, msg: string |
| 158 | +): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true), public.} = |
| 159 | + self.writeLp(msg.toOpenArrayByte(0, msg.high)) |
| 160 | + |
| 161 | +func shortLog*(self: MixExitConnection): string {.raises: [].} = |
| 162 | + "MixExitConnection" |
| 163 | + |
| 164 | +method initStream*(self: MixExitConnection) = |
| 165 | + discard |
| 166 | + |
| 167 | +method closeImpl*( |
| 168 | + self: MixExitConnection |
| 169 | +): Future[void] {.async: (raises: [], raw: true).} = |
| 170 | + let fut = newFuture[void]() |
| 171 | + fut.complete() |
| 172 | + return fut |
| 173 | + |
| 174 | +func hash*(self: MixExitConnection): Hash = |
| 175 | + discard |
| 176 | + |
| 177 | +proc getResponse*(self: MixExitConnection): seq[byte] = |
| 178 | + let r = self.response |
| 179 | + self.response = @[] |
| 180 | + return r |
| 181 | + |
| 182 | +proc new*(T: typedesc[MixExitConnection], message: seq[byte]): T = |
| 183 | + let instance = T(message: message) |
| 184 | + |
| 185 | + when defined(libp2p_agents_metrics): |
| 186 | + instance.shortAgent = connection.shortAgent |
| 187 | + |
| 188 | + instance |
| 189 | + |
| 190 | +when defined(libp2p_agents_metrics): |
| 191 | + proc setShortAgent*(self: MixExitConnection, shortAgent: string) = |
| 192 | + discard |
0 commit comments