2023-05-10 13:50:04 +00:00
|
|
|
# Copyright (c) 2020-2023 Status Research & Development GmbH
|
2021-09-13 12:54:06 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-05-10 13:50:04 +00:00
|
|
|
{.push raises: [].}
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
import
|
2021-11-18 09:05:56 +00:00
|
|
|
chronos, stew/[results, byteutils],
|
2021-11-04 06:38:46 +00:00
|
|
|
./utp_router,
|
2021-10-28 09:41:43 +00:00
|
|
|
./utp_socket,
|
2021-09-13 12:54:06 +00:00
|
|
|
./utp_protocol
|
|
|
|
|
2023-05-10 13:50:04 +00:00
|
|
|
# Example application to interact with the reference implementation server
|
|
|
|
# to be able to test against the reference implementation.
|
|
|
|
# To run libutp server:
|
2021-09-13 12:54:06 +00:00
|
|
|
# 1. git clone https://github.com/bittorrent/libutp.git
|
|
|
|
# 2. cd libutp
|
|
|
|
# 3. make
|
|
|
|
# 4. ./ucat -ddddd -l -p 9078 - it will run utp server on port 9078
|
|
|
|
when isMainModule:
|
2021-11-04 06:38:46 +00:00
|
|
|
proc echoIncomingSocketCallBack(): AcceptConnectionCallback[TransportAddress] =
|
2021-10-19 11:36:57 +00:00
|
|
|
return (
|
2023-05-10 13:50:04 +00:00
|
|
|
proc (server: UtpRouter[TransportAddress],
|
|
|
|
client: UtpSocket[TransportAddress]):
|
|
|
|
Future[void] {.gcsafe, raises: [].} =
|
2021-10-19 11:36:57 +00:00
|
|
|
echo "received incoming connection"
|
|
|
|
let fakeFuture = newFuture[void]()
|
|
|
|
fakeFuture.complete()
|
|
|
|
return fakeFuture
|
|
|
|
)
|
2021-09-13 12:54:06 +00:00
|
|
|
# TODO read client/server ports and address from cmd line or config file
|
|
|
|
let localAddress = initTAddress("0.0.0.0", 9077)
|
2021-10-19 11:36:57 +00:00
|
|
|
let utpProt = UtpProtocol.new(echoIncomingSocketCallBack(), localAddress)
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2021-10-11 12:16:06 +00:00
|
|
|
let remoteServer = initTAddress("127.0.0.1", 9078)
|
2021-11-18 09:05:56 +00:00
|
|
|
let socResult = waitFor utpProt.connectTo(remoteServer)
|
|
|
|
let soc = socResult.get()
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2021-10-15 11:38:51 +00:00
|
|
|
doAssert(soc.numPacketsInOutGoingBuffer() == 0)
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
let helloUtp = "Hello from nim implementation"
|
2021-10-19 11:36:57 +00:00
|
|
|
let bytes = helloUtp.toBytes()
|
|
|
|
|
2021-10-25 07:58:13 +00:00
|
|
|
discard waitFor soc.write(bytes)
|
2021-10-19 11:36:57 +00:00
|
|
|
|
2021-11-04 06:38:46 +00:00
|
|
|
waitFor(sleepAsync(milliseconds(1000)))
|
|
|
|
|
2021-12-09 09:52:21 +00:00
|
|
|
# discard waitFor soc.write(bytes)
|
|
|
|
|
|
|
|
# waitFor(sleepAsync(milliseconds(1000)))
|
|
|
|
|
|
|
|
# discard waitFor soc.write(bytes)
|
2021-11-04 06:38:46 +00:00
|
|
|
|
2021-10-19 11:36:57 +00:00
|
|
|
runForever()
|
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
waitFor utpProt.shutdownWait()
|