mirror of
https://github.com/StarBeat/JsonRpc.git
synced 2026-03-08 12:05:29 +08:00
46 lines
1.0 KiB
C#
46 lines
1.0 KiB
C#
|
|
using JsonRPC.Protocol;
|
|||
|
|
using Newtonsoft.Json;
|
|||
|
|
|
|||
|
|
namespace JsonRPC.RPC
|
|||
|
|
{
|
|||
|
|
public class Sender : AbsSingle<Sender>
|
|||
|
|
{
|
|||
|
|
Func<string, string> sendFunc = null!;
|
|||
|
|
Action<JsonRPCError> errorFunc = null!;
|
|||
|
|
int id;
|
|||
|
|
|
|||
|
|
public int GetId()
|
|||
|
|
{
|
|||
|
|
return Interlocked.Increment(ref id);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void SetSendFunction(Func<string, string> send)
|
|||
|
|
{
|
|||
|
|
sendFunc = send;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool HnadleResponseError(JsonRPCResponse response)
|
|||
|
|
{
|
|||
|
|
if(response.Error != null)
|
|||
|
|
{
|
|||
|
|
errorFunc?.Invoke(response.Error);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string Send(JsonRPCRequest request)
|
|||
|
|
{
|
|||
|
|
var reqJs = JsonConvert.SerializeObject(request);
|
|||
|
|
|
|||
|
|
return sendFunc.Invoke(reqJs);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public T JsonDeserialize<T>(string json) where T : JsonRPCResponse
|
|||
|
|
{
|
|||
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|