Tanguy Cizain af3be7966b
Websocket Transport (#593)
* start of websocket transport

* more ws tests

* switch to common test

* add close to wsstream

* update ws & chronicles version

* cleanup

* removed multicodec

* clean ws outgoing connections

* renamed to websock

* removed stream from logs

* renamed ws to websock

* add connection closing test to common transport

* close incoming connection on ws stop

* renamed testwebsocket.nim -> testwstransport.nim

* removed raise todo

* split out/in connections

* add wss to tests

* Fix tls (#608)

* change log level

* fixed issue related to stopping

some cosmetic cleanup

* use `allFutures` to stop/close things

Prevent potential race conditions when stopping two or more transports

* misc

* point websock to server-case-object branch

* interop test with go

* removed websock version specification

* add daemon -> native ws test

* fix & test closed read/write

* update readOnce, thanks jangko

Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
2021-08-03 15:48:03 +02:00

101 lines
2.5 KiB
Nim

## Nim-LibP2P
## Copyright (c) 2019 Status Research & Development GmbH
## 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.
##
{.push raises: [Defect].}
import sequtils
import chronos, chronicles
import ../stream/connection,
../multiaddress,
../multicodec,
../upgrademngrs/upgrade
logScope:
topics = "libp2p transport"
type
TransportError* = object of LPError
TransportClosedError* = object of TransportError
Transport* = ref object of RootObj
ma*: Multiaddress
running*: bool
upgrader*: Upgrade
proc newTransportClosedError*(parent: ref Exception = nil): ref LPError =
newException(TransportClosedError,
"Transport closed, no more connections!", parent)
method start*(
self: Transport,
ma: MultiAddress): Future[void] {.base, async.} =
## start the transport
##
trace "starting transport", address = $ma
self.ma = ma
self.running = true
method stop*(self: Transport): Future[void] {.base, async.} =
## stop and cleanup the transport
## including all outstanding connections
##
trace "stopping transport", address = $self.ma
self.running = false
method accept*(self: Transport): Future[Connection]
{.base, gcsafe.} =
## accept incoming connections
##
doAssert(false, "Not implemented!")
method dial*(
self: Transport,
address: MultiAddress): Future[Connection] {.base, gcsafe.} =
## dial a peer
##
doAssert(false, "Not implemented!")
method upgradeIncoming*(
self: Transport,
conn: Connection): Future[void] {.base, gcsafe.} =
## base upgrade method that the transport uses to perform
## transport specific upgrades
##
self.upgrader.upgradeIncoming(conn)
method upgradeOutgoing*(
self: Transport,
conn: Connection): Future[Connection] {.base, gcsafe.} =
## base upgrade method that the transport uses to perform
## transport specific upgrades
##
self.upgrader.upgradeOutgoing(conn)
method handles*(
self: Transport,
address: MultiAddress): bool {.base, gcsafe.} =
## check if transport supports the multiaddress
##
# by default we skip circuit addresses to avoid
# having to repeat the check in every transport
if address.protocols.isOk:
return address.protocols
.get()
.filterIt(
it == multiCodec("p2p-circuit")
).len == 0