mirror of https://github.com/vacp2p/nim-webrtc.git
rework udp and stun connection after removing webrtc connection
This commit is contained in:
parent
9394c065ac
commit
d4b5b420a2
|
@ -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
|
||||
|
|
|
@ -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<UDP>\e[0;1m onReceive\e[0m: ", udp.getMessage().len()
|
||||
self.remote = address
|
||||
self.recvData.add(msg)
|
||||
echo "\e[33m<UDP>\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<UDP>\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<UDP>\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
|
||||
|
|
|
@ -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")
|
Loading…
Reference in New Issue