From d4b5b420a2e2921fc7b9cd3b7020da4bcc65a675 Mon Sep 17 00:00:00 2001 From: Ludovic Chenut Date: Fri, 6 Oct 2023 17:33:41 +0200 Subject: [PATCH] rework udp and stun connection after removing webrtc connection --- webrtc/stun/stun_connection.nim | 25 +++++++++++++------------ webrtc/udp_connection.nim | 26 +++++++++++--------------- webrtc/webrtc_connection.nim | 33 --------------------------------- 3 files changed, 24 insertions(+), 60 deletions(-) delete mode 100644 webrtc/webrtc_connection.nim diff --git a/webrtc/stun/stun_connection.nim b/webrtc/stun/stun_connection.nim index 5edd9d4..823d410 100644 --- a/webrtc/stun/stun_connection.nim +++ b/webrtc/stun/stun_connection.nim @@ -8,27 +8,30 @@ # those terms. import chronos -import ../webrtc_connection, stun +import ../udp_connection, stun type - StunConn* = ref object of WebRTCConn - recvData: seq[seq[byte]] + StunConn* = ref object + conn: UdpConn + address: TransportAddress + recvData: seq[(seq[byte], TransportAddress)] recvEvent: AsyncEvent handlesFut: Future[void] proc handles(self: StunConn) {.async.} = 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): let res = Stun.getResponse(msg, self.address) if res.isSome(): await self.conn.write(res.get()) else: - self.recvData.add(msg) + self.recvData.add((msg, address)) self.recvEvent.fire() -method init(self: StunConn, conn: WebRTCConn, address: TransportAddress) {.async.} = - await procCall(WebRTCConn(self).init(conn, address)) +method init(self: StunConn, conn: UdpConn, address: TransportAddress) {.async.} = + self.conn = conn + self.address = address self.recvEvent = newAsyncEvent() self.handlesFut = handles() @@ -40,12 +43,10 @@ method close(self: StunConn) {.async.} = method write(self: StunConn, msg: seq[byte]) {.async.} = 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: self.recvEvent.clear() await self.recvEvent.wait() - result = self.recvData[0] + let res = self.recvData[0] self.recvData.delete(0..0) - -method getRemoteAddress*(self: StunConn): TransportAddress = - self.conn.getRemoteAddress() + return res diff --git a/webrtc/udp_connection.nim b/webrtc/udp_connection.nim index 6bd4952..53146dd 100644 --- a/webrtc/udp_connection.nim +++ b/webrtc/udp_connection.nim @@ -15,41 +15,37 @@ logScope: topics = "webrtc udp" type - UdpConn* = ref object of WebRTCConn + UdpConn* = ref object + localAddress: TransportAddress udp: DatagramTransport - remote: TransportAddress - recvData: seq[seq[byte]] + recvData: seq[(seq[byte], TransportAddress)] recvEvent: AsyncEvent -method init(self: UdpConn, conn: WebRTCConn, addrss: TransportAddress) {.async.} = - await procCall(WebRTCConn(self).init(conn, addrss)) +proc init(self: UdpConn, laddr: TransportAddress) {.async.} = + self.localAddress = laddr proc onReceive(udp: DatagramTransport, address: TransportAddress) {.async, gcsafe.} = let msg = udp.getMessage() - echo "\e[33m\e[0;1m onReceive\e[0m: ", udp.getMessage().len() - self.remote = address - self.recvData.add(msg) + echo "\e[33m\e[0;1m onReceive\e[0m: ", msg.len() + self.recvData.add((msg, address)) self.recvEvent.fire() 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() if not self.conn.isNil(): await self.conn.close() -method write(self: UdpConn, msg: seq[byte]) {.async.} = +proc write(self: UdpConn, msg: seq[byte]) {.async.} = echo "\e[33m\e[0;1m write\e[0m" 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\e[0;1m read\e[0m" while self.recvData.len() <= 0: self.recvEvent.clear() await self.recvEvent.wait() result = self.recvData[0] self.recvData.delete(0..0) - -method getRemoteAddress*(self: UdpConn): TransportAddress = - self.remote diff --git a/webrtc/webrtc_connection.nim b/webrtc/webrtc_connection.nim deleted file mode 100644 index 103d48c..0000000 --- a/webrtc/webrtc_connection.nim +++ /dev/null @@ -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")