nim-libp2p-experimental/libp2p/muxers/mplex/lpchannel.nim

194 lines
6.6 KiB
Nim
Raw Normal View History

2019-09-04 03:08:51 +00:00
## Nim-LibP2P
2019-09-24 17:48:23 +00:00
## Copyright (c) 2019 Status Research & Development GmbH
2019-09-04 03:08:51 +00:00
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import oids, deques
2019-09-09 17:33:32 +00:00
import chronos, chronicles
2019-09-12 17:07:34 +00:00
import types,
2019-09-08 07:59:14 +00:00
coder,
nimcrypto/utils,
2019-09-12 17:07:34 +00:00
../../stream/bufferstream,
../../stream/lpstream,
../../connection,
../../utility,
../../errors
2019-09-04 03:08:51 +00:00
export lpstream
2019-09-10 02:15:52 +00:00
logScope:
2019-09-28 19:54:52 +00:00
topic = "MplexChannel"
2019-09-10 02:15:52 +00:00
## Channel half-closed states
##
## | State | Closed local | Closed remote
## |=============================================
## | Read | Yes (until EOF) | No
## | Write | No | Yes
##
2019-09-04 03:08:51 +00:00
type
2019-09-12 17:07:34 +00:00
LPChannel* = ref object of BufferStream
id*: uint64 # channel id
name*: string # name of the channel (for debugging)
conn*: Connection # wrapped connection used to for writing
initiator*: bool # initiated remotely or locally flag
isLazy*: bool # is channel lazy
isOpen*: bool # has channel been oppened (only used with isLazy)
closedLocal*: bool # has channel been closed locally
msgCode*: MessageType # cached in/out message code
closeCode*: MessageType # cached in/out close code
resetCode*: MessageType # cached in/out reset code
proc open*(s: LPChannel) {.async, gcsafe.}
template withWriteLock(lock: AsyncLock, body: untyped): untyped =
try:
await lock.acquire()
body
finally:
lock.release()
template withEOFExceptions(body: untyped): untyped =
try:
body
except LPStreamEOFError as exc:
trace "muxed connection EOF", exc = exc.msg
except LPStreamClosedError as exc:
trace "muxed connection closed", exc = exc.msg
except LPStreamIncompleteError as exc:
trace "incomplete message", exc = exc.msg
2019-09-04 03:08:51 +00:00
2020-03-23 17:14:06 +00:00
proc newChannel*(id: uint64,
2019-09-04 06:40:11 +00:00
conn: Connection,
2019-09-04 03:08:51 +00:00
initiator: bool,
2019-09-06 21:27:55 +00:00
name: string = "",
2020-05-19 22:15:36 +00:00
size: int = DefaultBufferSize,
lazy: bool = false): LPChannel =
2019-09-04 03:08:51 +00:00
new result
result.id = id
2019-09-06 21:27:55 +00:00
result.name = name
2019-09-04 06:40:11 +00:00
result.conn = conn
2019-09-04 03:08:51 +00:00
result.initiator = initiator
2019-09-04 06:40:11 +00:00
result.msgCode = if initiator: MessageType.MsgOut else: MessageType.MsgIn
result.closeCode = if initiator: MessageType.CloseOut else: MessageType.CloseIn
result.resetCode = if initiator: MessageType.ResetOut else: MessageType.ResetIn
result.isLazy = lazy
2019-09-04 03:08:51 +00:00
2019-09-04 06:40:11 +00:00
let chan = result
proc writeHandler(data: seq[byte]): Future[void] {.async, gcsafe.} =
if chan.isLazy and not(chan.isOpen):
await chan.open()
2019-09-07 23:32:32 +00:00
# writes should happen in sequence
trace "sending data", data = data.shortLog,
id = chan.id,
initiator = chan.initiator,
name = chan.name,
oid = chan.oid
await conn.writeMsg(chan.id, chan.msgCode, data) # write header
2019-09-04 06:40:11 +00:00
result.initBufferStream(writeHandler, size)
when chronicles.enabledLogLevel == LogLevel.TRACE:
result.name = if result.name.len > 0: result.name else: $result.oid
2019-09-04 06:40:11 +00:00
trace "created new lpchannel", id = result.id,
oid = result.oid,
initiator = result.initiator,
name = result.name
2019-09-04 06:40:11 +00:00
proc closeMessage(s: LPChannel) {.async.} =
withEOFExceptions:
withWriteLock(s.writeLock):
trace "sending close message", id = s.id,
initiator = s.initiator,
name = s.name,
oid = s.oid
2019-09-07 23:32:32 +00:00
await s.conn.writeMsg(s.id, s.closeCode) # write close
proc resetMessage(s: LPChannel) {.async.} =
withEOFExceptions:
withWriteLock(s.writeLock):
trace "sending reset message", id = s.id,
initiator = s.initiator,
name = s.name,
oid = s.oid
await s.conn.writeMsg(s.id, s.resetCode) # write reset
proc open*(s: LPChannel) {.async, gcsafe.} =
## NOTE: Don't call withExcAndLock or withWriteLock,
## because this already gets called from writeHandler
## which is locked
withEOFExceptions:
await s.conn.writeMsg(s.id, MessageType.New, s.name)
trace "oppened channel", oid = s.oid,
name = s.name,
initiator = s.initiator
s.isOpen = true
proc closeRemote*(s: LPChannel) {.async.} =
trace "got EOF, closing channel", id = s.id,
initiator = s.initiator,
name = s.name,
oid = s.oid
# wait for all data in the buffer to be consumed
while s.len > 0:
await s.dataReadEvent.wait()
s.dataReadEvent.clear()
# TODO: Not sure if this needs to be set here or bfore consuming
# the buffer
s.isEof = true # set EOF immediately to prevent further reads
await procCall BufferStream(s).close() # close parent bufferstream
trace "channel closed on EOF", id = s.id,
initiator = s.initiator,
oid = s.oid,
name = s.name
method closed*(s: LPChannel): bool =
## this emulates half-closed behavior
## when closed locally writing is
## dissabled - see the table in the
## header of the file
s.closedLocal
2019-09-12 17:07:34 +00:00
method close*(s: LPChannel) {.async, gcsafe.} =
if s.closedLocal:
return
trace "closing local lpchannel", id = s.id,
initiator = s.initiator,
name = s.name,
oid = s.oid
# TODO: we should install a timer that on expire
# will make sure the channel did close by the remote
# so the hald-closed flow completed, if it didn't
# we should send a `reset` and move on.
await s.closeMessage()
s.closedLocal = true
if s.atEof: # already closed by remote close parent buffer imediately
await procCall BufferStream(s).close()
trace "lpchannel closed local", id = s.id,
initiator = s.initiator,
name = s.name,
oid = s.oid
method reset*(s: LPChannel) {.base, async.} =
# we asyncCheck here because the other end
# might be dead already - reset is always
# optimistic
asyncCheck s.resetMessage()
await procCall BufferStream(s).close()
s.isEof = true
s.closedLocal = true