mirror of https://github.com/vacp2p/nim-webrtc.git
add logs & fix some bugs in sctp
This commit is contained in:
parent
525fb37882
commit
9a6657922a
|
@ -91,6 +91,7 @@ proc write*(stream: DataChannelStream, buf: seq[byte]) {.async.} =
|
|||
#TODO add reliability params
|
||||
|
||||
if buf.len == 0:
|
||||
trace "Datachannel write empty"
|
||||
sendInfo.protocolId = uint32(WebRtcBinaryEmpty)
|
||||
await stream.conn.write(@[0'u8], sendInfo)
|
||||
else:
|
||||
|
|
|
@ -58,11 +58,11 @@ type
|
|||
remoteCert: seq[byte]
|
||||
|
||||
proc dtlsSend*(ctx: pointer, buf: ptr byte, len: uint): cint {.cdecl.} =
|
||||
trace "dtls send", len
|
||||
var self = cast[DtlsConn](ctx)
|
||||
var toWrite = newSeq[byte](len)
|
||||
if len > 0:
|
||||
copyMem(addr toWrite[0], buf, len)
|
||||
trace "dtls send", len
|
||||
self.sendFuture = self.conn.write(self.raddr, toWrite)
|
||||
result = len.cint
|
||||
|
||||
|
@ -82,6 +82,7 @@ proc init*(self: DtlsConn, conn: StunConn, laddr: TransportAddress) {.async.} =
|
|||
self.dataRecv = newAsyncQueue[seq[byte]]()
|
||||
|
||||
proc write*(self: DtlsConn, msg: seq[byte]) {.async.} =
|
||||
trace "Dtls write", length = msg.len()
|
||||
var buf = msg
|
||||
discard mbedtls_ssl_write(addr self.ssl, cast[ptr byte](addr buf[0]), buf.len().uint)
|
||||
|
||||
|
@ -93,6 +94,8 @@ proc read*(self: DtlsConn): Future[seq[byte]] {.async.} =
|
|||
let length = mbedtls_ssl_read(addr self.ssl, cast[ptr byte](addr res[0]), res.len().uint)
|
||||
if length == MBEDTLS_ERR_SSL_WANT_READ:
|
||||
continue
|
||||
if length < 0:
|
||||
trace "dtls read", error = $(length.mbedtls_high_level_strerr())
|
||||
res.setLen(length)
|
||||
return res
|
||||
|
||||
|
|
|
@ -133,7 +133,7 @@ proc write*(
|
|||
buf: seq[byte],
|
||||
sendParams = default(SctpMessageParameters),
|
||||
) {.async.} =
|
||||
trace "Write", buf
|
||||
trace "Write", buf, sctp = cast[uint64](self), sock = cast[uint64](self.sctpSocket)
|
||||
self.sctp.sentConnection = self
|
||||
self.sctp.sentAddress = self.address
|
||||
|
||||
|
@ -142,7 +142,6 @@ proc write*(
|
|||
if sendParams != default(SctpMessageParameters):
|
||||
(sctp_sndinfo(
|
||||
snd_sid: sendParams.streamId,
|
||||
#TODO endianness?
|
||||
snd_ppid: sendParams.protocolId,
|
||||
snd_flags: sendParams.toFlags
|
||||
), cuint(SCTP_SENDV_SNDINFO))
|
||||
|
@ -152,6 +151,9 @@ proc write*(
|
|||
self.sctpSocket.usrsctp_sendv(unsafeAddr buf[0], buf.len.uint,
|
||||
nil, 0, unsafeAddr sendInfo, sizeof(sendInfo).SockLen,
|
||||
infoType, 0)
|
||||
if sendvErr < 0:
|
||||
perror("usrsctp_sendv")
|
||||
trace "write sendv error?", sendvErr, sendParams
|
||||
|
||||
proc write*(self: SctpConn, s: string) {.async.} =
|
||||
await self.write(s.toBytes())
|
||||
|
@ -161,8 +163,9 @@ proc close*(self: SctpConn) {.async.} =
|
|||
|
||||
proc handleUpcall(sock: ptr socket, data: pointer, flags: cint) {.cdecl.} =
|
||||
let
|
||||
events = usrsctp_get_events(sock)
|
||||
conn = cast[SctpConn](data)
|
||||
events = usrsctp_get_events(sock)
|
||||
|
||||
trace "Handle Upcall", events
|
||||
if conn.state == Connecting:
|
||||
if bitand(events, SCTP_EVENT_ERROR) != 0:
|
||||
|
@ -182,6 +185,7 @@ proc handleUpcall(sock: ptr socket, data: pointer, flags: cint) {.cdecl.} =
|
|||
rnLen = sizeof(message.info).SockLen
|
||||
infotype: uint
|
||||
flags: int
|
||||
trace "recv from", sockuint64=cast[uint64](sock)
|
||||
let n = sock.usrsctp_recvv(cast[pointer](addr message.data[0]), message.data.len.uint,
|
||||
cast[ptr SockAddr](addr address),
|
||||
cast[ptr SockLen](addr addressLen),
|
||||
|
@ -206,6 +210,8 @@ proc handleUpcall(sock: ptr socket, data: pointer, flags: cint) {.cdecl.} =
|
|||
conn.dataRecv.addLastNoWait(message)
|
||||
except AsyncQueueFullError:
|
||||
trace "Queue full, dropping packet"
|
||||
elif bitand(events, SCTP_EVENT_WRITE) != 0:
|
||||
trace "sctp event write in the upcall"
|
||||
else:
|
||||
warn "Handle Upcall unexpected event", events
|
||||
|
||||
|
@ -220,7 +226,14 @@ proc handleAccept(sock: ptr socket, data: pointer, flags: cint) {.cdecl.} =
|
|||
|
||||
doAssert 0 == sctpSocket.usrsctp_set_non_blocking(1)
|
||||
let conn = cast[SctpConn](sconn.sconn_addr)
|
||||
conn.sctpSocket = sctpSocket
|
||||
conn.state = Connected
|
||||
var nodelay: uint32 = 1
|
||||
doAssert 0 == conn.sctpSocket.usrsctp_set_upcall(handleUpcall, cast[pointer](conn))
|
||||
doAssert 0 == conn.sctpSocket.usrsctp_setsockopt(IPPROTO_SCTP,
|
||||
SCTP_NODELAY,
|
||||
addr nodelay,
|
||||
sizeof(nodelay).SockLen)
|
||||
conn.acceptEvent.fire()
|
||||
|
||||
# proc getOrCreateConnection(self: Sctp,
|
||||
|
@ -347,8 +360,8 @@ proc readLoopProc(res: SctpConn) {.async.} =
|
|||
msg = await res.conn.read()
|
||||
data = usrsctp_dumppacket(unsafeAddr msg[0], uint(msg.len), SCTP_DUMP_INBOUND)
|
||||
trace "Read Loop Proc Before", isnil=data.isNil()
|
||||
if data != nil:
|
||||
trace "Receive connection", remoteAddress = res.conn.raddr, data = data.packetPretty()
|
||||
if not data.isNil():
|
||||
trace "Receive data", remoteAddress = res.conn.raddr, data = data.packetPretty()
|
||||
usrsctp_freedumpbuffer(data)
|
||||
res.sctp.sentConnection = res
|
||||
usrsctp_conninput(cast[pointer](res), unsafeAddr msg[0], uint(msg.len), 0)
|
||||
|
|
Loading…
Reference in New Issue