2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import std/options
|
|
|
|
import ../common/protobuf, ../waku_core, ./rpc
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
const MaxRpcSize* = MaxWakuMessageSize + 64 * 1024
|
|
|
|
# We add a 64kB safety buffer for protocol overhead
|
2022-10-25 12:55:31 +00:00
|
|
|
|
2022-08-02 23:47:42 +00:00
|
|
|
proc encode*(rpc: PushRequest): ProtoBuffer =
|
2022-11-07 15:24:16 +00:00
|
|
|
var pb = initProtoBuffer()
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb.write3(1, rpc.pubSubTopic)
|
|
|
|
pb.write3(2, rpc.message.encode())
|
|
|
|
pb.finish3()
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2023-02-20 14:03:32 +00:00
|
|
|
proc decode*(T: type PushRequest, buffer: seq[byte]): ProtobufResult[T] =
|
2022-11-07 15:24:16 +00:00
|
|
|
let pb = initProtoBuffer(buffer)
|
2022-08-02 23:47:42 +00:00
|
|
|
var rpc = PushRequest()
|
|
|
|
|
2022-11-09 08:55:47 +00:00
|
|
|
var pubSubTopic: PubsubTopic
|
2022-11-18 19:01:01 +00:00
|
|
|
if not ?pb.getField(1, pubSubTopic):
|
2023-02-20 14:03:32 +00:00
|
|
|
return err(ProtobufError.missingRequiredField("pubsub_topic"))
|
2022-11-18 19:01:01 +00:00
|
|
|
else:
|
|
|
|
rpc.pubSubTopic = pubSubTopic
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-11-18 19:01:01 +00:00
|
|
|
var messageBuf: seq[byte]
|
|
|
|
if not ?pb.getField(2, messageBuf):
|
2023-02-20 14:03:32 +00:00
|
|
|
return err(ProtobufError.missingRequiredField("message"))
|
2022-11-18 19:01:01 +00:00
|
|
|
else:
|
|
|
|
rpc.message = ?WakuMessage.decode(messageBuf)
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
ok(rpc)
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
proc encode*(rpc: PushResponse): ProtoBuffer =
|
2022-11-07 15:24:16 +00:00
|
|
|
var pb = initProtoBuffer()
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb.write3(1, uint64(rpc.isSuccess))
|
|
|
|
pb.write3(2, rpc.info)
|
|
|
|
pb.finish3()
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2023-02-20 14:03:32 +00:00
|
|
|
proc decode*(T: type PushResponse, buffer: seq[byte]): ProtobufResult[T] =
|
2022-11-07 15:24:16 +00:00
|
|
|
let pb = initProtoBuffer(buffer)
|
2022-11-18 19:01:01 +00:00
|
|
|
var rpc = PushResponse()
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
var isSuccess: uint64
|
2022-11-18 19:01:01 +00:00
|
|
|
if not ?pb.getField(1, isSuccess):
|
2023-02-20 14:03:32 +00:00
|
|
|
return err(ProtobufError.missingRequiredField("is_success"))
|
2022-11-18 19:01:01 +00:00
|
|
|
else:
|
2022-08-02 23:47:42 +00:00
|
|
|
rpc.isSuccess = bool(isSuccess)
|
|
|
|
|
|
|
|
var info: string
|
2022-11-18 19:01:01 +00:00
|
|
|
if not ?pb.getField(2, info):
|
|
|
|
rpc.info = none(string)
|
2023-02-20 14:03:32 +00:00
|
|
|
else:
|
2022-11-18 19:01:01 +00:00
|
|
|
rpc.info = some(info)
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
ok(rpc)
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
proc encode*(rpc: PushRPC): ProtoBuffer =
|
2022-11-07 15:24:16 +00:00
|
|
|
var pb = initProtoBuffer()
|
2023-02-20 14:03:32 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb.write3(1, rpc.requestId)
|
2022-11-18 19:01:01 +00:00
|
|
|
pb.write3(2, rpc.request.map(encode))
|
|
|
|
pb.write3(3, rpc.response.map(encode))
|
2022-11-07 15:24:16 +00:00
|
|
|
pb.finish3()
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2023-02-20 14:03:32 +00:00
|
|
|
proc decode*(T: type PushRPC, buffer: seq[byte]): ProtobufResult[T] =
|
2022-08-02 23:47:42 +00:00
|
|
|
let pb = initProtoBuffer(buffer)
|
|
|
|
var rpc = PushRPC()
|
|
|
|
|
|
|
|
var requestId: string
|
2022-11-18 19:01:01 +00:00
|
|
|
if not ?pb.getField(1, requestId):
|
2023-02-20 14:03:32 +00:00
|
|
|
return err(ProtobufError.missingRequiredField("request_id"))
|
2022-11-18 19:01:01 +00:00
|
|
|
else:
|
|
|
|
rpc.requestId = requestId
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
var requestBuffer: seq[byte]
|
2022-11-18 19:01:01 +00:00
|
|
|
if not ?pb.getField(2, requestBuffer):
|
|
|
|
rpc.request = none(PushRequest)
|
|
|
|
else:
|
|
|
|
let request = ?PushRequest.decode(requestBuffer)
|
|
|
|
rpc.request = some(request)
|
|
|
|
|
|
|
|
var responseBuffer: seq[byte]
|
|
|
|
if not ?pb.getField(3, responseBuffer):
|
|
|
|
rpc.response = none(PushResponse)
|
|
|
|
else:
|
|
|
|
let response = ?PushResponse.decode(responseBuffer)
|
|
|
|
rpc.response = some(response)
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2023-02-20 14:03:32 +00:00
|
|
|
ok(rpc)
|