rework udp and stun connection after removing webrtc connection

This commit is contained in:
Ludovic Chenut 2023-10-06 17:33:41 +02:00
parent 9394c065ac
commit d4b5b420a2
No known key found for this signature in database
GPG Key ID: D9A59B1907F1D50C
3 changed files with 24 additions and 60 deletions

View File

@ -8,27 +8,30 @@
# those terms. # those terms.
import chronos import chronos
import ../webrtc_connection, stun import ../udp_connection, stun
type type
StunConn* = ref object of WebRTCConn StunConn* = ref object
recvData: seq[seq[byte]] conn: UdpConn
address: TransportAddress
recvData: seq[(seq[byte], TransportAddress)]
recvEvent: AsyncEvent recvEvent: AsyncEvent
handlesFut: Future[void] handlesFut: Future[void]
proc handles(self: StunConn) {.async.} = proc handles(self: StunConn) {.async.} =
while true: # TODO: while not self.conn.atEof() while true: # TODO: while not self.conn.atEof()
let msg = await self.conn.read() let (msg, address) = await self.conn.read()
if Stun.isMessage(msg): if Stun.isMessage(msg):
let res = Stun.getResponse(msg, self.address) let res = Stun.getResponse(msg, self.address)
if res.isSome(): if res.isSome():
await self.conn.write(res.get()) await self.conn.write(res.get())
else: else:
self.recvData.add(msg) self.recvData.add((msg, address))
self.recvEvent.fire() self.recvEvent.fire()
method init(self: StunConn, conn: WebRTCConn, address: TransportAddress) {.async.} = method init(self: StunConn, conn: UdpConn, address: TransportAddress) {.async.} =
await procCall(WebRTCConn(self).init(conn, address)) self.conn = conn
self.address = address
self.recvEvent = newAsyncEvent() self.recvEvent = newAsyncEvent()
self.handlesFut = handles() self.handlesFut = handles()
@ -40,12 +43,10 @@ method close(self: StunConn) {.async.} =
method write(self: StunConn, msg: seq[byte]) {.async.} = method write(self: StunConn, msg: seq[byte]) {.async.} =
await self.conn.write(msg) await self.conn.write(msg)
method read(self: StunConn): Future[seq[byte]] {.async.} = method read(self: StunConn): Future[(seq[byte], TransportAddress)] {.async.} =
while self.recvData.len() <= 0: while self.recvData.len() <= 0:
self.recvEvent.clear() self.recvEvent.clear()
await self.recvEvent.wait() await self.recvEvent.wait()
result = self.recvData[0] let res = self.recvData[0]
self.recvData.delete(0..0) self.recvData.delete(0..0)
return res
method getRemoteAddress*(self: StunConn): TransportAddress =
self.conn.getRemoteAddress()

View File

@ -15,41 +15,37 @@ logScope:
topics = "webrtc udp" topics = "webrtc udp"
type type
UdpConn* = ref object of WebRTCConn UdpConn* = ref object
localAddress: TransportAddress
udp: DatagramTransport udp: DatagramTransport
remote: TransportAddress recvData: seq[(seq[byte], TransportAddress)]
recvData: seq[seq[byte]]
recvEvent: AsyncEvent recvEvent: AsyncEvent
method init(self: UdpConn, conn: WebRTCConn, addrss: TransportAddress) {.async.} = proc init(self: UdpConn, laddr: TransportAddress) {.async.} =
await procCall(WebRTCConn(self).init(conn, addrss)) self.localAddress = laddr
proc onReceive(udp: DatagramTransport, address: TransportAddress) {.async, gcsafe.} = proc onReceive(udp: DatagramTransport, address: TransportAddress) {.async, gcsafe.} =
let msg = udp.getMessage() let msg = udp.getMessage()
echo "\e[33m<UDP>\e[0;1m onReceive\e[0m: ", udp.getMessage().len() echo "\e[33m<UDP>\e[0;1m onReceive\e[0m: ", msg.len()
self.remote = address self.recvData.add((msg, address))
self.recvData.add(msg)
self.recvEvent.fire() self.recvEvent.fire()
self.recvEvent = newAsyncEvent() self.recvEvent = newAsyncEvent()
self.udp = newDatagramTransport(onReceive, local = addrss) self.udp = newDatagramTransport(onReceive, local = laddr)
method close(self: UdpConn) {.async.} = proc close(self: UdpConn) {.async.} =
self.udp.close() self.udp.close()
if not self.conn.isNil(): if not self.conn.isNil():
await self.conn.close() await self.conn.close()
method write(self: UdpConn, msg: seq[byte]) {.async.} = proc write(self: UdpConn, msg: seq[byte]) {.async.} =
echo "\e[33m<UDP>\e[0;1m write\e[0m" echo "\e[33m<UDP>\e[0;1m write\e[0m"
await self.udp.sendTo(self.remote, msg) await self.udp.sendTo(self.remote, msg)
method read(self: UdpConn): Future[seq[byte]] {.async.} = proc read(self: UdpConn): Future[(seq[byte], TransportAddress)] {.async.} =
echo "\e[33m<UDP>\e[0;1m read\e[0m" echo "\e[33m<UDP>\e[0;1m read\e[0m"
while self.recvData.len() <= 0: while self.recvData.len() <= 0:
self.recvEvent.clear() self.recvEvent.clear()
await self.recvEvent.wait() await self.recvEvent.wait()
result = self.recvData[0] result = self.recvData[0]
self.recvData.delete(0..0) self.recvData.delete(0..0)
method getRemoteAddress*(self: UdpConn): TransportAddress =
self.remote

View File

@ -1,33 +0,0 @@
# Nim-WebRTC
# Copyright (c) 2023 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.
import chronos
type
WebRTCConn* = ref object of RootObj
conn*: WebRTCConn
address*: TransportAddress
# isClosed: bool
# isEof: bool
method init*(self: WebRTCConn, conn: WebRTCConn, address: TransportAddress) {.async, base.} =
self.conn = conn
self.address = address
method close*(self: WebRTCConn) {.async, base.} =
doAssert(false, "not implemented!")
method write*(self: WebRTCConn, msg: seq[byte]) {.async, base.} =
doAssert(false, "not implemented!")
method read*(self: WebRTCConn): Future[seq[byte]] {.async, base.} =
doAssert(false, "not implemented!")
method getRemoteAddress*(self: WebRTCConn): TransportAddress {.base.} =
doAssert(false, "not implemented")