From b39647b0d078b7dd09ceec61b168ebf7f6aee558 Mon Sep 17 00:00:00 2001 From: demotomohiro Date: Mon, 7 Dec 2020 22:47:55 +0900 Subject: [PATCH 1/2] Fix simple_example.nim so that it works on windows --- examples/gtk3/simple_example.nim | 43 ++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/examples/gtk3/simple_example.nim b/examples/gtk3/simple_example.nim index beb824b..6af1d3d 100644 --- a/examples/gtk3/simple_example.nim +++ b/examples/gtk3/simple_example.nim @@ -17,14 +17,11 @@ else: from strutils import `%`, split from os import paramCount, paramStr -from strutils import parseInt +from strutils import parseInt, removeSuffix +import nativesockets -from posix import INET6_ADDRSTRLEN - -when defined(windows): - import winlean -else: - discard # import posix +when not defined(windows): + import posix var gloop: glib.MainLoop @@ -34,11 +31,11 @@ var stateName = ["disconnected", "gathering", "connecting", "connected", "ready", "failed"] proc gMsg(s: string) = - stdout.write(s) + stdout.write(s & '\n') flushFile(stdout) proc gDebug(s: string) = - stdout.write(s) + stdout.write(s & '\n') flushFile(stdout) proc gError(s: string) = @@ -55,11 +52,13 @@ proc toIntVal(i: int): Value = discard init(result, gtype) setInt(result, i) +proc addrString(niceAddr: Address): string = + getAddrString(unsafeAddr niceAddr.`addr`) + proc printLocalData(agent: nice.Agent; streamId: int; componentId: int): int = var localUfrag: string localPassword: string - ipaddr = newString(INET6_ADDRSTRLEN).cstring # not really nice as that cstring is used as out parameter by toString() cands: seq[Candidate] result = QuitFailure block gotoEnd: @@ -70,13 +69,14 @@ proc printLocalData(agent: nice.Agent; streamId: int; componentId: int): int = break gotoEnd stdout.write(localUfrag, ' ', localPassword) for el in cands: - toString(el.impl.`addr`, ipaddr) + let ipaddr = el.impl.`addr`.addrString ## (foundation),(prio),(addr),(port),(type) stdout.write(' ', cast[cstring](addr el.impl.foundation), ',', el.impl.priority, ',', ipaddr, ',', getPort(el.impl.`addr`), ',', candidateTypeName[el.impl.`type`.ord]) echo "" - # end label - result = QuitSuccess + # end label + result = QuitSuccess + return result proc parseCandidate(scand: string; streamId: int): nice.Candidate = @@ -85,8 +85,11 @@ proc parseCandidate(scand: string; streamId: int): nice.Candidate = ntype: nice.CandidateType = CandidateType.host # that initialization is never used! tokens = scand.split(',', 5) block gotoEnd: + if tokens.len < 5: + gMsg("Invalid candidate text: " & scand) for i in 0 .. 4: if tokens[i] == "": + gMsg("Empty candidate field: " & scand) break gotoEnd #[ should work too for i, el in candidateTypeName: @@ -98,12 +101,15 @@ proc parseCandidate(scand: string; streamId: int): nice.Candidate = break gotoEnd ]# var i: int + var candTypeName = tokens[4] + removeSuffix(candTypeName) while i < candidateTypeName.len: - if tokens[4] == candidateTypeName[i]: + if candTypeName == candidateTypeName[i]: ntype = CandidateType(i) break # missing in initial release! inc(i) if i == candidateTypeName.len: + gMsg("Invalid candidate type name: " & candTypeName) break gotoEnd cand = newCandidate(ntype) #cand.impl.componentId = 1 @@ -154,8 +160,8 @@ proc parseRemoteData(agent: Agent; streamId: int; componentId: int; line: string if setRemoteCandidates(agent, streamId, componentId, remoteCandidates) < 1: gMsg("failed to set remote candidates") break gotoEnd - # end label - result = QuitSuccess + result = QuitSuccess + return result proc stdinRemoteInfoCb(source: glib.IOChannel; cond: glib.IOCondition; agent: nice.Agent): bool = @@ -214,10 +220,9 @@ proc cbComponentStateChanged(agent: nice.Agent; streamId: int; componentId: int; remote: nice.Candidate ## Get current selected candidate pair and print IP address used if getSelectedPair(agent, streamId, componentId, local, remote): - var ipaddr = newString(INET6_ADDRSTRLEN).cstring - toString(local.impl.`addr`, ipaddr) + var ipaddr = local.impl.`addr`.addrString echo("\nNegotiation complete: ([$1]:$2," % [$ipaddr, $getPort(local.impl.`addr`)]) - toString(remote.impl.`addr`, ipaddr) + ipaddr = remote.impl.`addr`.addrString echo(" [$1]:$2)" % [$ipaddr, $getPort(remote.impl.`addr`)]) ## Listen to stdin and send data written to it echo("\nSend lines to remote (Ctrl-D to quit):") From 532cd0a1227eec6f35a377cdf7470f75f3a545c3 Mon Sep 17 00:00:00 2001 From: demotomohiro Date: Tue, 8 Dec 2020 22:26:55 +0900 Subject: [PATCH 2/2] Remove unused module --- examples/gtk3/simple_example.nim | 3 --- 1 file changed, 3 deletions(-) diff --git a/examples/gtk3/simple_example.nim b/examples/gtk3/simple_example.nim index 6af1d3d..a16340b 100644 --- a/examples/gtk3/simple_example.nim +++ b/examples/gtk3/simple_example.nim @@ -20,9 +20,6 @@ from os import paramCount, paramStr from strutils import parseInt, removeSuffix import nativesockets -when not defined(windows): - import posix - var gloop: glib.MainLoop ioStdin: glib.IOChannel