Fix simple_example.nim so that it works on windows

This commit is contained in:
demotomohiro 2020-12-07 22:47:55 +09:00
commit b39647b0d0

View file

@ -17,14 +17,11 @@ else:
from strutils import `%`, split from strutils import `%`, split
from os import paramCount, paramStr from os import paramCount, paramStr
from strutils import parseInt from strutils import parseInt, removeSuffix
import nativesockets
from posix import INET6_ADDRSTRLEN when not defined(windows):
import posix
when defined(windows):
import winlean
else:
discard # import posix
var var
gloop: glib.MainLoop gloop: glib.MainLoop
@ -34,11 +31,11 @@ var
stateName = ["disconnected", "gathering", "connecting", "connected", "ready", "failed"] stateName = ["disconnected", "gathering", "connecting", "connected", "ready", "failed"]
proc gMsg(s: string) = proc gMsg(s: string) =
stdout.write(s) stdout.write(s & '\n')
flushFile(stdout) flushFile(stdout)
proc gDebug(s: string) = proc gDebug(s: string) =
stdout.write(s) stdout.write(s & '\n')
flushFile(stdout) flushFile(stdout)
proc gError(s: string) = proc gError(s: string) =
@ -55,11 +52,13 @@ proc toIntVal(i: int): Value =
discard init(result, gtype) discard init(result, gtype)
setInt(result, i) setInt(result, i)
proc addrString(niceAddr: Address): string =
getAddrString(unsafeAddr niceAddr.`addr`)
proc printLocalData(agent: nice.Agent; streamId: int; componentId: int): int = proc printLocalData(agent: nice.Agent; streamId: int; componentId: int): int =
var var
localUfrag: string localUfrag: string
localPassword: string localPassword: string
ipaddr = newString(INET6_ADDRSTRLEN).cstring # not really nice as that cstring is used as out parameter by toString()
cands: seq[Candidate] cands: seq[Candidate]
result = QuitFailure result = QuitFailure
block gotoEnd: block gotoEnd:
@ -70,13 +69,14 @@ proc printLocalData(agent: nice.Agent; streamId: int; componentId: int): int =
break gotoEnd break gotoEnd
stdout.write(localUfrag, ' ', localPassword) stdout.write(localUfrag, ' ', localPassword)
for el in cands: for el in cands:
toString(el.impl.`addr`, ipaddr) let ipaddr = el.impl.`addr`.addrString
## (foundation),(prio),(addr),(port),(type) ## (foundation),(prio),(addr),(port),(type)
stdout.write(' ', cast[cstring](addr el.impl.foundation), ',', el.impl.priority, ',', ipaddr, ',', getPort(el.impl.`addr`), stdout.write(' ', cast[cstring](addr el.impl.foundation), ',', el.impl.priority, ',', ipaddr, ',', getPort(el.impl.`addr`),
',', candidateTypeName[el.impl.`type`.ord]) ',', candidateTypeName[el.impl.`type`.ord])
echo "" echo ""
# end label # end label
result = QuitSuccess result = QuitSuccess
return result return result
proc parseCandidate(scand: string; streamId: int): nice.Candidate = 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! ntype: nice.CandidateType = CandidateType.host # that initialization is never used!
tokens = scand.split(',', 5) tokens = scand.split(',', 5)
block gotoEnd: block gotoEnd:
if tokens.len < 5:
gMsg("Invalid candidate text: " & scand)
for i in 0 .. 4: for i in 0 .. 4:
if tokens[i] == "": if tokens[i] == "":
gMsg("Empty candidate field: " & scand)
break gotoEnd break gotoEnd
#[ should work too #[ should work too
for i, el in candidateTypeName: for i, el in candidateTypeName:
@ -98,12 +101,15 @@ proc parseCandidate(scand: string; streamId: int): nice.Candidate =
break gotoEnd break gotoEnd
]# ]#
var i: int var i: int
var candTypeName = tokens[4]
removeSuffix(candTypeName)
while i < candidateTypeName.len: while i < candidateTypeName.len:
if tokens[4] == candidateTypeName[i]: if candTypeName == candidateTypeName[i]:
ntype = CandidateType(i) ntype = CandidateType(i)
break # missing in initial release! break # missing in initial release!
inc(i) inc(i)
if i == candidateTypeName.len: if i == candidateTypeName.len:
gMsg("Invalid candidate type name: " & candTypeName)
break gotoEnd break gotoEnd
cand = newCandidate(ntype) cand = newCandidate(ntype)
#cand.impl.componentId = 1 #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: if setRemoteCandidates(agent, streamId, componentId, remoteCandidates) < 1:
gMsg("failed to set remote candidates") gMsg("failed to set remote candidates")
break gotoEnd break gotoEnd
# end label
result = QuitSuccess result = QuitSuccess
return result return result
proc stdinRemoteInfoCb(source: glib.IOChannel; cond: glib.IOCondition; agent: nice.Agent): bool = 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 remote: nice.Candidate
## Get current selected candidate pair and print IP address used ## Get current selected candidate pair and print IP address used
if getSelectedPair(agent, streamId, componentId, local, remote): if getSelectedPair(agent, streamId, componentId, local, remote):
var ipaddr = newString(INET6_ADDRSTRLEN).cstring var ipaddr = local.impl.`addr`.addrString
toString(local.impl.`addr`, ipaddr)
echo("\nNegotiation complete: ([$1]:$2," % [$ipaddr, $getPort(local.impl.`addr`)]) 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`)]) echo(" [$1]:$2)" % [$ipaddr, $getPort(remote.impl.`addr`)])
## Listen to stdin and send data written to it ## Listen to stdin and send data written to it
echo("\nSend lines to remote (Ctrl-D to quit):") echo("\nSend lines to remote (Ctrl-D to quit):")