StunConn adds debug
This commit is contained in:
parent
1f98fae1af
commit
4c1eb13926
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue