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