mirror of
https://github.com/logos-storage/logos-storage-nim-dht.git
synced 2026-01-05 23:13:10 +00:00
add simple send functionality
Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
parent
6109cbae80
commit
fd317a55cb
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
# Everything below the handling of ordinary messages
|
# Everything below the handling of ordinary messages
|
||||||
import
|
import
|
||||||
std/[tables, options],
|
std/[tables, options, deques],
|
||||||
bearssl/rand,
|
bearssl/rand,
|
||||||
chronos,
|
chronos,
|
||||||
chronicles,
|
chronicles,
|
||||||
@ -28,16 +28,27 @@ type
|
|||||||
DatagramTransport = ref object
|
DatagramTransport = ref object
|
||||||
udata*: pointer # User-driven pointer
|
udata*: pointer # User-driven pointer
|
||||||
local: TransportAddress # Local address
|
local: TransportAddress # Local address
|
||||||
|
function: DatagramCallback # Receive data callback
|
||||||
|
ingress: Deque[seq[byte]]
|
||||||
|
|
||||||
var network = initTable[Port, DatagramTransport]()
|
var network = initTable[Port, DatagramTransport]()
|
||||||
|
|
||||||
|
proc `$`*(transp: DatagramTransport): string =
|
||||||
|
$transp.local
|
||||||
|
|
||||||
proc sendTo*[T](transp: DatagramTransport, remote: TransportAddress,
|
proc sendTo*[T](transp: DatagramTransport, remote: TransportAddress,
|
||||||
msg: sink seq[T], msglen = -1) {.async.} =
|
msg: sink seq[T], msglen = -1) {.async.} =
|
||||||
echo "sending to ", remote
|
echo "sending to ", remote
|
||||||
|
{.gcsafe.}:
|
||||||
|
network[remote.port].ingress.addLast(msg)
|
||||||
|
# call the callback on remote
|
||||||
|
asyncCheck network[remote.port].function(network[remote.port], transp.local)
|
||||||
|
|
||||||
|
|
||||||
proc getMessage*(t: DatagramTransport,): seq[byte] {.
|
proc getMessage*(t: DatagramTransport,): seq[byte] {.
|
||||||
raises: [Defect, CatchableError].} =
|
raises: [Defect, CatchableError].} =
|
||||||
echo "getMessage "
|
echo "getMessage "
|
||||||
|
t.ingress.popFirst()
|
||||||
|
|
||||||
proc close*(transp: DatagramTransport) =
|
proc close*(transp: DatagramTransport) =
|
||||||
echo "close"
|
echo "close"
|
||||||
@ -62,6 +73,7 @@ proc newDatagramTransport*[T](cbproc: DatagramCallback,
|
|||||||
GC_ref(udata)
|
GC_ref(udata)
|
||||||
result.udata = cast[pointer](udata)
|
result.udata = cast[pointer](udata)
|
||||||
result.local = local
|
result.local = local
|
||||||
|
result.function = cbproc
|
||||||
{.gcsafe.}:
|
{.gcsafe.}:
|
||||||
network[local.port] = result
|
network[local.port] = result
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user