StunConn adds debug

This commit is contained in:
Ludovic Chenut 2024-03-01 12:11:04 +01:00
parent 1f98fae1af
commit 4c1eb13926
No known key found for this signature in database
GPG Key ID: D9A59B1907F1D50C
1 changed files with 17 additions and 1 deletions

View File

@ -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()