Exemple #1
0
        public async Task <KeyValuePair <long, Task <object> > > SendRequestAsync(string method, List <object> parameters, string peerId = null, int timeoutSec = 0)
        {
            if (!isConnected && method != "wc_sessionRequest")
            {
                await Connect();
            }
            long id      = DateTime.UtcNow.ToUnixTime() * 1000; // in ms
            var  jsonRpc = new JsonRpc()
            {
                id = id, method = method, parameters = parameters
            };
            string ivHex     = RandomBytes(16).ToHexString();
            var    wcRequest = WCEncrypt(jsonRpc.ToJson(), keyHex, ivHex);

            WCDecrypt(wcRequest.data, keyHex, wcRequest.iv, wcRequest.hmac);
            var wcRequestPub = new WCPubSub()
            {
                topic = peerId ?? theirPeerId, payload = wcRequest.ToJson(), silent = true, type = "pub"
            };
            var completer = new TaskCompletionSource <dynamic>();

            if (method != "wc_sessionUpdate")
            {
                outstandingRpc[id] = new KeyValuePair <JsonRpc, TaskCompletionSource <dynamic> >(jsonRpc, completer);
            }
            await wsClient.SendMessageAsync(wcRequestPub.ToJson());

            var requestResult = completer.Task.TimeoutAfter(timeoutSec);

            return(new KeyValuePair <long, Task <object> >(id, requestResult));
        }
Exemple #2
0
        public async Task <KeyValuePair <long, Task <object> > > SendResponseAsync(long id, string method, dynamic result = null, dynamic error = null)
        {
            if (!isConnected && method != "wc_sessionRequest")
            {
                await Connect();
            }
            var jsonRpc = new JsonRpc()
            {
                id = id, result = result, error = error
            };
            string ivHex         = RandomBytes(16).ToHexString();
            var    wcResponse    = WCEncrypt(jsonRpc.ToJson(), keyHex, ivHex);
            var    wcResponsePub = new WCPubSub()
            {
                topic = theirPeerId, payload = wcResponse.ToJson(), silent = true, type = "pub"
            };
            await wsClient.SendMessageAsync(wcResponsePub.ToJson());

            return(new KeyValuePair <long, Task <object> >(id, Task.FromResult((object)true)));
        }