misc: cleanup mplex

This commit is contained in:
Dmitriy Ryajov 2019-09-04 10:40:05 -06:00
parent 6ce2782e5c
commit ec351cc2b0
5 changed files with 23 additions and 13 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ build/
*.la *.la
*.exe *.exe
*.dll *.dll
.vscode/

View File

@ -50,7 +50,7 @@ proc closeMessage(s: Channel) {.async, gcsafe.} =
proc closed*(s: Channel): bool = proc closed*(s: Channel): bool =
s.closedLocal s.closedLocal
proc closeRemote*(s: Channel) {.async.} = proc closedByRemote*(s: Channel) {.async.} =
s.closedRemote = true s.closedRemote = true
method close*(s: Channel) {.async, gcsafe.} = method close*(s: Channel) {.async, gcsafe.} =
@ -58,14 +58,14 @@ method close*(s: Channel) {.async, gcsafe.} =
await s.closeMessage() await s.closeMessage()
proc resetMessage(s: Channel) {.async, gcsafe.} = proc resetMessage(s: Channel) {.async, gcsafe.} =
await s.conn.writeHeader(s.id, s.resetCode, 0) # write header await s.conn.writeHeader(s.id, s.resetCode, 0)
proc remoteReset*(s: Channel) {.async, gcsafe.} = proc resetByRemote*(s: Channel) {.async, gcsafe.} =
await allFutures(s.close(), s.closeRemote()) await allFutures(s.close(), s.closedByRemote())
s.isReset = true s.isReset = true
proc reset*(s: Channel) {.async.} = proc reset*(s: Channel) {.async.} =
await allFutures(s.resetMessage(), s.remoteReset()) await allFutures(s.resetMessage(), s.resetByRemote())
proc isReadEof(s: Channel): bool = proc isReadEof(s: Channel): bool =
bool((s.closedRemote or s.closedLocal) and s.len() < 1) bool((s.closedRemote or s.closedLocal) and s.len() < 1)

View File

@ -7,6 +7,13 @@
## This file may not be copied, modified, or distributed except according to ## This file may not be copied, modified, or distributed except according to
## those terms. ## those terms.
## TODO: I have to be carefull to clean up channels correctly
## both by removing them from the internal tables as well as
## cleaning up when the channel is completelly finished, this
## is complicated because half closed makes it non-deterministic.
## This still needs to be implemented properly - I'm leaving it here
## to not forget that this needs to be fixed ASAP.
import tables, sequtils import tables, sequtils
import chronos import chronos
import ../../varint, ../../connection, import ../../varint, ../../connection,
@ -44,7 +51,7 @@ proc newStreamInternal*(m: Mplex,
proc newStreamInternal*(m: Mplex): Future[Channel] {.gcsafe.} = proc newStreamInternal*(m: Mplex): Future[Channel] {.gcsafe.} =
result = m.newStreamInternal(true, 0) result = m.newStreamInternal(true, 0)
proc handle*(m: Mplex): Future[void] {.async, gcsafe.} = method handle*(m: Mplex): Future[void] {.async, gcsafe.} =
try: try:
while not m.connection.closed: while not m.connection.closed:
let (id, msgType) = await m.connection.readHeader() let (id, msgType) = await m.connection.readHeader()
@ -59,10 +66,11 @@ proc handle*(m: Mplex): Future[void] {.async, gcsafe.} =
await channel.pushTo(msg) await channel.pushTo(msg)
of MessageType.CloseIn, MessageType.CloseOut: of MessageType.CloseIn, MessageType.CloseOut:
let channel = m.getChannelList(initiator)[id.int] let channel = m.getChannelList(initiator)[id.int]
await channel.closeRemote() await channel.closedByRemote()
m.getChannelList(initiator).del(id.int)
of MessageType.ResetIn, MessageType.ResetOut: of MessageType.ResetIn, MessageType.ResetOut:
let channel = m.getChannelList(initiator)[id.int] let channel = m.getChannelList(initiator)[id.int]
await channel.reset() await channel.resetByRemote()
else: raise newMplexUnknownMsgError() else: raise newMplexUnknownMsgError()
except Exception as exc: except Exception as exc:
#TODO: add proper loging #TODO: add proper loging
@ -88,4 +96,4 @@ method newStream*(m: Mplex): Future[Connection] {.async, gcsafe.} =
method close*(m: Mplex) {.async, gcsafe.} = method close*(m: Mplex) {.async, gcsafe.} =
await allFutures(@[allFutures(toSeq(m.remote.values).mapIt(it.close())), await allFutures(@[allFutures(toSeq(m.remote.values).mapIt(it.close())),
allFutures(toSeq(m.local.values).mapIt(it.close()))]) allFutures(toSeq(m.local.values).mapIt(it.close()))])
await m.connection.close() m.connection.reset()

View File

@ -32,3 +32,4 @@ method init(c: MuxerProvider) =
method newStream*(m: Muxer): Future[Connection] {.base, async, gcsafe.} = discard method newStream*(m: Muxer): Future[Connection] {.base, async, gcsafe.} = discard
method close*(m: Muxer) {.base, async, gcsafe.} = discard method close*(m: Muxer) {.base, async, gcsafe.} = discard
method handle*(m: Muxer): Future[void] {.base, async, gcsafe.} = discard

View File

@ -47,10 +47,10 @@ suite "Mplex":
test "encode header": test "encode header":
proc testEncodeHeader(): Future[bool] {.async.} = proc testEncodeHeader(): Future[bool] {.async.} =
proc encHandler(msg: seq[byte]) = proc encHandler(msg: seq[byte]) =
check msg == fromHex("880102") check msg == fromHex("886f04")
let conn = newConnection(newTestEncodeStream(encHandler)) let conn = newConnection(newTestEncodeStream(encHandler))
await conn.writeHeader(17, MessageType.New, 2) await conn.writeHeader(1777, MessageType.New, 4)
result = true result = true
check: check:
@ -143,7 +143,7 @@ suite "Mplex":
test "half closed - channel should close for read": test "half closed - channel should close for read":
proc testClosedForRead(): Future[void] {.async.} = proc testClosedForRead(): Future[void] {.async.} =
let chann = newChannel(1, newConnection(new LPStream), true) let chann = newChannel(1, newConnection(new LPStream), true)
await chann.closeRemote() await chann.closedByRemote()
asyncDiscard chann.read() asyncDiscard chann.read()
expect LPStreamClosedError: expect LPStreamClosedError:
@ -182,7 +182,7 @@ suite "Mplex":
test "should not allow pushing data to channel when remote end closed": test "should not allow pushing data to channel when remote end closed":
proc testResetWrite(): Future[void] {.async.} = proc testResetWrite(): Future[void] {.async.} =
let chann = newChannel(1, newConnection(new LPStream), true) let chann = newChannel(1, newConnection(new LPStream), true)
await chann.closeRemote() await chann.closedByRemote()
await chann.pushTo(@[byte(1)]) await chann.pushTo(@[byte(1)])
expect LPStreamClosedError: expect LPStreamClosedError: