fix socket send for string types (#21155)
* fix socket send for string types * include windows version Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
This commit is contained in:
parent
18c115c8d0
commit
3bba2b34fd
1 changed files with 29 additions and 8 deletions
|
|
@ -1735,15 +1735,36 @@ proc send*(socket: Socket, data: pointer, size: int): int {.
|
||||||
result = send(socket.fd, data, size, int32(MSG_NOSIGNAL))
|
result = send(socket.fd, data, size, int32(MSG_NOSIGNAL))
|
||||||
|
|
||||||
proc send*(socket: Socket, data: string,
|
proc send*(socket: Socket, data: string,
|
||||||
flags = {SocketFlag.SafeDisconn}) {.tags: [WriteIOEffect].} =
|
flags = {SocketFlag.SafeDisconn}, maxRetries = 100) {.tags: [WriteIOEffect].} =
|
||||||
## sends data to a socket.
|
## Sends data to a socket. Will try to send all the data by handling interrupts
|
||||||
|
## and incomplete writes up to `maxRetries`.
|
||||||
|
var written = 0
|
||||||
|
var attempts = 0
|
||||||
|
while data.len - written > 0:
|
||||||
let sent = send(socket, cstring(data), data.len)
|
let sent = send(socket, cstring(data), data.len)
|
||||||
|
|
||||||
if sent < 0:
|
if sent < 0:
|
||||||
let lastError = osLastError()
|
let lastError = osLastError()
|
||||||
socketError(socket, lastError = lastError, flags = flags)
|
let isBlockingErr =
|
||||||
|
when defined(nimdoc):
|
||||||
|
false
|
||||||
|
elif useWinVersion:
|
||||||
|
lastError.int32 == WSAEINTR or
|
||||||
|
lastError.int32 == WSAEWOULDBLOCK
|
||||||
|
else:
|
||||||
|
lastError.int32 == EINTR or
|
||||||
|
lastError.int32 == EWOULDBLOCK or
|
||||||
|
lastError.int32 == EAGAIN
|
||||||
|
|
||||||
if sent != data.len:
|
if not isBlockingErr:
|
||||||
|
let lastError = osLastError()
|
||||||
|
socketError(socket, lastError = lastError, flags = flags)
|
||||||
|
else:
|
||||||
|
attempts.inc()
|
||||||
|
if attempts > maxRetries:
|
||||||
raiseOSError(osLastError(), "Could not send all data.")
|
raiseOSError(osLastError(), "Could not send all data.")
|
||||||
|
else:
|
||||||
|
written.inc(sent)
|
||||||
|
|
||||||
template `&=`*(socket: Socket; data: typed) =
|
template `&=`*(socket: Socket; data: typed) =
|
||||||
## an alias for 'send'.
|
## an alias for 'send'.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue