From 4c1eb13926f94eab4514c735ef315842ff4e664f Mon Sep 17 00:00:00 2001 From: Ludovic Chenut Date: Fri, 1 Mar 2024 12:11:04 +0100 Subject: [PATCH] StunConn adds debug --- webrtc/stun/stun_connection.nim | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/webrtc/stun/stun_connection.nim b/webrtc/stun/stun_connection.nim index 6a60141..68d1105 100644 --- a/webrtc/stun/stun_connection.nim +++ b/webrtc/stun/stun_connection.nim @@ -7,15 +7,21 @@ # This file may not be copied, modified, or distributed except according to # those terms. -import chronos +import chronos, chronicles import ../udp_connection, stun +logScope: + topics = "webrtc stun" + +# TODO: Work fine when behaves like a server, need to implement the client side + type StunConn* = ref object conn: UdpConn laddr: TransportAddress dataRecv: AsyncQueue[(seq[byte], TransportAddress)] handlesFut: Future[void] + closed: bool proc handles(self: StunConn) {.async.} = while true: @@ -30,16 +36,26 @@ proc handles(self: StunConn) {.async.} = proc init*(self: StunConn, conn: UdpConn, laddr: TransportAddress) = self.conn = conn self.laddr = laddr + self.closed = false self.dataRecv = newAsyncQueue[(seq[byte], TransportAddress)]() self.handlesFut = self.handles() proc close*(self: StunConn) {.async.} = + if self.closed: + debug "Try to close StunConn twice" + return self.handlesFut.cancel() # check before? await self.conn.close() proc write*(self: StunConn, raddr: TransportAddress, msg: seq[byte]) {.async.} = + if self.closed: + debug "Try to write on an already closed StunConn" + return await self.conn.write(raddr, msg) proc read*(self: StunConn): Future[(seq[byte], TransportAddress)] {.async.} = + if self.closed: + debug "Try to read on an already closed StunConn" + return return await self.dataRecv.popFirst()