-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: udp connection #8
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6ebf83b
feat: udp "connection"
lchenut 3f41593
Creates a message/remote address tuple
lchenut ba75c76
Adds proc documentation & update debug log
lchenut a74a8d9
Add msys2 installation for the ci
lchenut 0dded9d
update msys2 installation
lchenut 9e61ef2
fix typo
lchenut db41cd6
fix windows ci
lchenut 3835056
Add proper error management
lchenut cf2d599
update deprecated newDatagramTransport call
lchenut 624e32d
fix uncatched error
lchenut a584e85
better exception management
lchenut 605171a
remove useless `except` & improve error message
lchenut c2df1b0
change constructor behavior
lchenut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Nim-WebRTC | ||
# Copyright (c) 2024 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. | ||
|
||
type | ||
# Base exception for nim-webrtc | ||
WebRtcError* = object of CatchableError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Nim-WebRTC | ||
# Copyright (c) 2024 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 errors | ||
import chronos, chronicles | ||
|
||
logScope: | ||
topics = "webrtc udp" | ||
|
||
# UdpConn is a small wrapper of the chronos DatagramTransport. | ||
# It's the simplest solution we found to store the message and | ||
# the remote address used by the underlying protocols (dtls/sctp etc...) | ||
|
||
type | ||
WebRtcUdpError = object of WebRtcError | ||
|
||
UdpPacketInfo* = tuple | ||
message: seq[byte] | ||
raddr: TransportAddress | ||
|
||
UdpConn* = ref object | ||
laddr*: TransportAddress | ||
udp: DatagramTransport | ||
dataRecv: AsyncQueue[UdpPacketInfo] | ||
closed: bool | ||
|
||
proc init*(T: type UdpConn, laddr: TransportAddress): T = | ||
## Initialize an Udp Connection | ||
## | ||
var self = T(laddr: laddr, closed: false) | ||
|
||
proc onReceive( | ||
udp: DatagramTransport, | ||
raddr: TransportAddress | ||
) {.async: (raises: []), gcsafe.} = | ||
# On receive Udp message callback, store the | ||
# message with the corresponding remote address | ||
try: | ||
trace "UDP onReceive" | ||
let msg = udp.getMessage() | ||
self.dataRecv.addLastNoWait((msg, raddr)) | ||
except CatchableError as exc: | ||
raiseAssert(exc.msg) | ||
|
||
self.dataRecv = newAsyncQueue[UdpPacketInfo]() | ||
self.udp = newDatagramTransport(onReceive, local = laddr) | ||
return self | ||
|
||
proc close*(self: UdpConn) = | ||
## Close an Udp Connection | ||
## | ||
if self.closed: | ||
debug "Trying to close an already closed UdpConn" | ||
return | ||
self.closed = true | ||
self.udp.close() | ||
|
||
proc write*( | ||
self: UdpConn, | ||
raddr: TransportAddress, | ||
msg: seq[byte] | ||
) {.async: (raises: [CancelledError, WebRtcUdpError]).} = | ||
## Write a message on Udp to a remote address `raddr` | ||
## | ||
if self.closed: | ||
debug "Try to write on an already closed UdpConn" | ||
return | ||
trace "UDP write", msg | ||
try: | ||
await self.udp.sendTo(raddr, msg) | ||
except TransportError as exc: | ||
raise newException(WebRtcUdpError, | ||
"Error when sending data on a DatagramTransport: " & exc.msg , exc) | ||
|
||
proc read*(self: UdpConn): Future[UdpPacketInfo] {.async: (raises: [CancelledError]).} = | ||
## Read the next received Udp message | ||
## | ||
if self.closed: | ||
debug "Try to read on an already closed UdpConn" | ||
return | ||
trace "UDP read" | ||
return await self.dataRecv.popFirst() | ||
lchenut marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arnetheduck In this case where we don't need to use the actual value that will be returned by the
Future
, should we do like it's done here or remove the await/async and return the Future fromsendTo
?