-
Notifications
You must be signed in to change notification settings - Fork 51
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
Breaking Datagram API change. #25
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ import net, nativesockets, os, deques | |
import ../asyncloop, ../handles | ||
import common | ||
|
||
export OSErrorCode | ||
|
||
when defined(windows): | ||
import winlean | ||
else: | ||
|
@@ -28,7 +30,9 @@ type | |
writer: Future[void] # Writer vector completion Future | ||
|
||
DatagramCallback* = proc(transp: DatagramTransport, | ||
remote: TransportAddress): Future[void] {.gcsafe.} | ||
message: seq[byte], | ||
remote: TransportAddress, | ||
error: OSErrorCode): Future[void] {.gcsafe.} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, but |
||
|
||
DatagramTransport* = ref object of RootRef | ||
fd*: AsyncFD # File descriptor | ||
|
@@ -138,17 +142,18 @@ when defined(windows): | |
if bytesCount == 0: | ||
transp.state.incl({ReadEof, ReadPaused}) | ||
fromSAddr(addr transp.raddr, transp.ralen, raddr) | ||
transp.buflen = bytesCount | ||
asyncCheck transp.function(transp, raddr) | ||
var msg = transp.buffer | ||
msg.setLen(bytesCount) | ||
asyncCheck transp.function(transp, msg, raddr, OSErrorCode(0)) | ||
elif int(err) == ERROR_OPERATION_ABORTED: | ||
# CancelIO() interrupt | ||
transp.state.incl(ReadPaused) | ||
break | ||
else: | ||
transp.setReadError(err) | ||
transp.state.incl(ReadPaused) | ||
transp.buflen = 0 | ||
asyncCheck transp.function(transp, raddr) | ||
var msg = newSeq[byte]() | ||
asyncCheck transp.function(transp, msg, raddr, err) | ||
else: | ||
## Initiation | ||
if transp.state * {ReadEof, ReadClosed, ReadError} == {}: | ||
|
@@ -177,8 +182,8 @@ when defined(windows): | |
transp.state.excl(ReadPending) | ||
transp.state.incl(ReadPaused) | ||
transp.setReadError(err) | ||
transp.buflen = 0 | ||
asyncCheck transp.function(transp, raddr) | ||
var msg = newSeq[byte]() | ||
asyncCheck transp.function(transp, msg, raddr, err) | ||
break | ||
|
||
proc resumeRead(transp: DatagramTransport) {.inline.} = | ||
|
@@ -311,16 +316,17 @@ else: | |
addr transp.ralen) | ||
if res >= 0: | ||
fromSAddr(addr transp.raddr, transp.ralen, raddr) | ||
transp.buflen = res | ||
asyncCheck transp.function(transp, raddr) | ||
var msg = transp.buffer | ||
msg.setLen(res) | ||
asyncCheck transp.function(transp, msg, raddr, OSErrorCode(0)) | ||
else: | ||
let err = osLastError() | ||
if int(err) == EINTR: | ||
continue | ||
else: | ||
transp.buflen = 0 | ||
transp.setReadError(err) | ||
asyncCheck transp.function(transp, raddr) | ||
var msg = newSeq[byte]() | ||
asyncCheck transp.function(transp, msg, raddr, err) | ||
break | ||
|
||
proc writeDatagramLoop(udata: pointer) = | ||
|
@@ -658,22 +664,6 @@ proc sendTo*[T](transp: DatagramTransport, remote: TransportAddress, | |
transp.resumeWrite() | ||
return retFuture | ||
|
||
proc peekMessage*(transp: DatagramTransport, msg: var seq[byte], | ||
msglen: var int) = | ||
## Get access to internal message buffer and length of incoming datagram. | ||
if ReadError in transp.state: | ||
raise transp.getError() | ||
shallowCopy(msg, transp.buffer) | ||
msglen = transp.buflen | ||
|
||
proc getMessage*(transp: DatagramTransport): seq[byte] = | ||
## Copy data from internal message buffer and return result. | ||
if ReadError in transp.state: | ||
raise transp.getError() | ||
if transp.buflen > 0: | ||
result = newSeq[byte](transp.buflen) | ||
copyMem(addr result[0], addr transp.buffer[0], transp.buflen) | ||
|
||
proc getUserData*[T](transp: DatagramTransport): T {.inline.} = | ||
## Obtain user data stored in ``transp`` object. | ||
result = cast[T](transp.udata) | ||
|
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.
openArray[byte]
tends to give more flexibility for the implementation.. a common option later on is to allow the original caller to set up a byte buffer of their choice..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.
this is also the kind of place where
Result
is appropriate - giving either an error or a buffer but never both - makes for cleaner code everywhereThere 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 we can't use
openarray[T]
because it can't be captured inside of iterator, so it can't work in async procedures.