Merge branch 'master' of bitbucket.org:bluetradesys/spark
This commit is contained in:
commit
8f3a6b0fa0
5 files changed
+13
-8
No files matched your search
@@ -28,7 +28,7 @@ namespace bts.api.core
|
||||
}
|
||||
public abstract void dispatch(RequestHeader header, Stream request, Action<byte[]> on_response);
|
||||
|
||||
protected void respond<T>(int request_id, Task<T> task, Action<byte[]> on_response) where T : Google.Protobuf.IMessage {
|
||||
protected void respond<T>(int request_id, Task<T> task, Action<byte[]> on_response) where T : IMessage, IImmutableMessage {
|
||||
var header = new ResponseHeader() { RequestId = request_id };
|
||||
if (task.IsCanceled || task.IsFaulted) {
|
||||
header.Code = ApiError.ServerFault;
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace bts.api {
|
||||
internal static class ApiUtils {
|
||||
|
||||
|
||||
public static byte[] serialize<T>(this RequestHeader header, T body) where T : IMessage {
|
||||
public static byte[] serialize<T>(this RequestHeader header, T body) where T : IImmutableMessage {
|
||||
int body_size = body?.CalculateSize() ?? 0;
|
||||
int header_size = header.CalculateSize() + 1; // 1 byte header length
|
||||
var buffer = new byte[header_size+body_size];
|
||||
@@ -44,7 +44,7 @@ namespace bts.api {
|
||||
return mkresult(header, body);
|
||||
}
|
||||
|
||||
public static byte[] serialize<T>(this ResponseHeader header, T body) where T : IMessage {
|
||||
public static byte[] serialize<T>(this ResponseHeader header, T body) where T : IImmutableMessage {
|
||||
int header_size = 1 + header.CalculateSize();
|
||||
int body_size = body == null ? 0 : body.CalculateSize();
|
||||
var buffer = new byte[header_size + body_size];
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace bts.api.core {
|
||||
}
|
||||
public abstract void Dispose();
|
||||
public abstract Task<Result<Response>> send<Request, Response>(int method_id, Request request)
|
||||
where Request: IMessage where Response : class,IMessage,new();
|
||||
where Request: IImmutableMessage where Response : class,IMessage,new();
|
||||
public abstract IObservable<RawUpdate<T>> subscribe<T>(string method_and_qualifier, MessageParser<T> parser)
|
||||
where T : IMessage<T>;
|
||||
|
||||
|
||||
@@ -8,19 +8,20 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bts {
|
||||
internal interface IImmutableProto {
|
||||
internal interface IImmutableMessage {
|
||||
MessageDescriptor Descriptor { get; }
|
||||
int CalculateSize();
|
||||
void WriteTo(CodedOutputStream output);
|
||||
}
|
||||
|
||||
internal static class ImmutableProtoExtensions {
|
||||
public static void WriteTo(this IImmutableProto message, Stream output) {
|
||||
public static void WriteTo(this IImmutableMessage message, Stream output) {
|
||||
var stream = new CodedOutputStream(output);
|
||||
message.WriteTo(stream);
|
||||
stream.Flush();
|
||||
}
|
||||
|
||||
public static byte[] ToByteArray(this IImmutableProto message) {
|
||||
public static byte[] ToByteArray(this IImmutableMessage message) {
|
||||
var result = new byte[message.CalculateSize()];
|
||||
var output = new CodedOutputStream(result);
|
||||
message.WriteTo(output);
|
||||
|
||||
@@ -8,10 +8,14 @@ using Google.Protobuf.WellKnownTypes;
|
||||
|
||||
namespace {{package}} {
|
||||
{{#each message_type}}
|
||||
partial struct {{name}}Immutable : bts.IImmutableProto {
|
||||
partial class {{name}} : bts.IImmutableMessage {
|
||||
MessageDescriptor bts.IImmutableMessage.Descriptor => {{name}}.Descriptor;
|
||||
}
|
||||
partial struct {{name}}Immutable : bts.IImmutableMessage {
|
||||
readonly {{name}} _impl;
|
||||
|
||||
public static MessageDescriptor Descriptor => {{name}}.Descriptor;
|
||||
MessageDescriptor bts.IImmutableMessage.Descriptor => {{name}}.Descriptor;
|
||||
public int CalculateSize() => _impl.CalculateSize();
|
||||
public void WriteTo(CodedOutputStream output) => _impl.WriteTo(output);
|
||||
|
||||
|
||||
Reference in new issue
Block a user