diff --git a/chronos/streams/hexdump.nim b/chronos/streams/hexdump.nim deleted file mode 100644 index 38498d9..0000000 --- a/chronos/streams/hexdump.nim +++ /dev/null @@ -1,92 +0,0 @@ -# -# Copyright (c) 2016 Eugene Kabanov -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. - -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -# - -from strutils import toHex, repeat - -proc dumpHex*(pbytes: pointer, nbytes: int, items = 1, ascii = true): string = - ## Return hexadecimal memory dump representation pointed by ``p``. - ## ``nbytes`` - number of bytes to show - ## ``items`` - number of bytes in group (supported ``items`` count is - ## 1, 2, 4, 8) - ## ``ascii`` - if ``true`` show ASCII representation of memory dump. - result = "" - let hexSize = items * 2 - var i = 0 - var slider = pbytes - var asciiText = "" - while i < nbytes: - if i %% 16 == 0: - result = result & toHex(cast[BiggestInt](slider), - sizeof(BiggestInt) * 2) & ": " - var k = 0 - while k < items: - var ch = cast[ptr char](cast[uint](slider) + k.uint)[] - if ord(ch) > 31 and ord(ch) < 127: asciiText &= ch else: asciiText &= "." - inc(k) - case items: - of 1: - result = result & toHex(cast[BiggestInt](cast[ptr uint8](slider)[]), - hexSize) - of 2: - result = result & toHex(cast[BiggestInt](cast[ptr uint16](slider)[]), - hexSize) - of 4: - result = result & toHex(cast[BiggestInt](cast[ptr uint32](slider)[]), - hexSize) - of 8: - result = result & toHex(cast[BiggestInt](cast[ptr uint64](slider)[]), - hexSize) - else: - raise newException(ValueError, "Wrong items size!") - result = result & " " - slider = cast[pointer](cast[uint](slider) + items.uint) - i = i + items - if i %% 16 == 0: - result = result & " " & asciiText - asciiText.setLen(0) - result = result & "\n" - - if i %% 16 != 0: - var spacesCount = ((16 - (i %% 16)) div items) * (hexSize + 1) + 1 - result = result & repeat(' ', spacesCount) - result = result & asciiText - result = result & "\n" - -proc dumpHex*[T](v: openarray[T], items: int = 0, ascii = true): string = - ## Return hexadecimal memory dump representation of openarray[T] ``v``. - ## ``items`` - number of bytes in group (supported ``items`` count is - ## 0, 1, 2, 4, 8). If ``items`` is ``0`` group size will depend on - ## ``sizeof(T)``. - ## ``ascii`` - if ``true`` show ASCII representation of memory dump. - var i = 0 - if items == 0: - when sizeof(T) == 2: - i = 2 - elif sizeof(T) == 4: - i = 4 - elif sizeof(T) == 8: - i = 8 - else: - i = 1 - else: - i = items - result = dumpHex(unsafeAddr v[0], sizeof(T) * len(v), i, ascii) diff --git a/chronos/streams/tlsstream.nim b/chronos/streams/tlsstream.nim index c25cbc2..18f7c5d 100644 --- a/chronos/streams/tlsstream.nim +++ b/chronos/streams/tlsstream.nim @@ -11,7 +11,7 @@ import bearssl, bearssl/cacert import ../asyncloop, ../timer, ../asyncsync import asyncstream, ../transports/stream, ../transports/common -import strutils, hexdump +import strutils type TLSStreamKind {.pure.} = enum @@ -110,18 +110,16 @@ proc tlsWriteLoop(stream: AsyncStreamWriter) {.async.} = var length: uint while true: var state = engine.sslEngineCurrentState() - echo "tlsWriteLoop() state = ", getStringState(state) if (state and SSL_CLOSED) == SSL_CLOSED: wstream.state = AsyncStreamState.Finished break if (state and (SSL_RECVREC or SSL_RECVAPP)) != 0: - echo "tlsWriteLoop() signal to tlsReadLoop()" - wstream.switchToReader.fire() + if not(wstream.switchToReader.isSet()): + wstream.switchToReader.fire() if (state and (SSL_SENDREC or SSL_SENDAPP)) == 0: - echo "tlsWriteLoop() waiting" await wstream.switchToWriter.wait() wstream.switchToWriter.clear() # We need to refresh `state` because we just returned from readerLoop. @@ -141,9 +139,7 @@ proc tlsWriteLoop(stream: AsyncStreamWriter) {.async.} = if (state and SSL_SENDAPP) == SSL_SENDAPP: # Application data can be sent over stream. - echo "tlsWriteLoop() waiting for an item" var item = await wstream.queue.get() - echo "tlsWriteLoop() obtained an item" if item.size > 0: length = 0'u var buf = sslEngineSendappBuf(engine, length) @@ -209,7 +205,6 @@ proc tlsReadLoop(stream: AsyncStreamReader) {.async.} = var length: uint while true: var state = engine.sslEngineCurrentState() - echo "tlsReadLoop() state = ", getStringState(state) if (state and SSL_CLOSED) == SSL_CLOSED: let err = engine.sslEngineLastError() @@ -222,11 +217,10 @@ proc tlsReadLoop(stream: AsyncStreamReader) {.async.} = break if (state and (SSL_SENDREC or SSL_SENDAPP)) != 0: - echo "tlsReadLoop() signal to tlsWriteLoop()" - rstream.switchToWriter.fire() + if not(rstream.switchToWriter.isSet()): + rstream.switchToWriter.fire() if (state and (SSL_RECVREC or SSL_RECVAPP)) == 0: - echo "tlsReadLoop() waiting" await rstream.switchToReader.wait() rstream.switchToReader.clear() # We need to refresh `state` because we just returned from writerLoop. @@ -236,9 +230,7 @@ proc tlsReadLoop(stream: AsyncStreamReader) {.async.} = # TLS records required for further processing length = 0'u var buf = sslEngineRecvrecBuf(engine, length) - echo "tlsReadLoop() reading" var resFut = awaitne rstream.rsource.readOnce(buf, int(length)) - echo "tlsReadLoop() read completed" if resFut.failed(): rstream.error = resFut.error rstream.state = AsyncStreamState.Error @@ -255,10 +247,7 @@ proc tlsReadLoop(stream: AsyncStreamReader) {.async.} = # Application data can be recovered. length = 0'u var buf = sslEngineRecvappBuf(engine, length) - echo "tlsReadLoop(SSL_RECVAPP) received ", length, " bytes" await upload(addr rstream.buffer, buf, int(length)) - echo dumpHex(buf, int(length)) - echo "tlsReadLoop(SSL_RECVAPP) uploaded ", length, " bytes to buffer" sslEngineRecvappAck(engine, length) continue