2024-12-18 14:53:06 +01:00
|
|
|
import ../basics
|
|
|
|
|
import ./address
|
2024-12-19 17:16:17 +01:00
|
|
|
import ./error
|
2024-12-18 14:53:06 +01:00
|
|
|
|
|
|
|
|
type NetworkConnection* = distinct StreamTransport
|
|
|
|
|
|
|
|
|
|
proc connect*(
|
|
|
|
|
_: type NetworkConnection,
|
|
|
|
|
address: NetworkAddress
|
2024-12-19 17:16:17 +01:00
|
|
|
): Future[NetworkConnection] {.async:(raises:[NetworkError, CancelledError]).} =
|
|
|
|
|
convertNetworkErrors:
|
|
|
|
|
NetworkConnection(await TransportAddress(address).connect())
|
|
|
|
|
|
|
|
|
|
proc sendPacket*(connection: NetworkConnection, packet: seq[byte]) {.
|
|
|
|
|
async:(raises:[NetworkError, CancelledError])
|
|
|
|
|
.} =
|
|
|
|
|
convertNetworkErrors:
|
|
|
|
|
let transport = StreamTransport(connection)
|
|
|
|
|
let header = @[packet.len.uint32]
|
|
|
|
|
discard await transport.write(header)
|
|
|
|
|
if packet.len > 0:
|
|
|
|
|
discard await transport.write(packet)
|
|
|
|
|
|
|
|
|
|
proc receivePacket*(connection: NetworkConnection): Future[?seq[byte]] {.
|
|
|
|
|
async:(raises:[NetworkError, CancelledError])
|
|
|
|
|
.} =
|
|
|
|
|
convertNetworkErrors:
|
|
|
|
|
let transport = StreamTransport(connection)
|
|
|
|
|
let header = await transport.read(sizeof(uint32))
|
|
|
|
|
if header.len != sizeof(uint32):
|
|
|
|
|
return none seq[byte]
|
|
|
|
|
let length = (cast[ptr uint32](addr header[0]))[]
|
|
|
|
|
if length == 0:
|
|
|
|
|
return some seq[byte].default
|
|
|
|
|
some await transport.read(length.int)
|
2024-12-18 14:53:06 +01:00
|
|
|
|
|
|
|
|
proc close*(connection: NetworkConnection) {.async:(raises:[]).} =
|
2024-12-19 17:16:17 +01:00
|
|
|
await StreamTransport(connection).closeWait()
|