mirror of
https://github.com/FabInfra/capnproto-dotnetcore_Runtime.git
synced 2025-03-12 23:01:44 +01:00
Issue #20
This commit is contained in:
parent
1bc2c31d1d
commit
9a12124574
@ -15,6 +15,7 @@
|
|||||||
<Compile Include="..\Capnp.Net.Runtime.Tests\FramePumpTests.cs" Link="FramePumpTests.cs" />
|
<Compile Include="..\Capnp.Net.Runtime.Tests\FramePumpTests.cs" Link="FramePumpTests.cs" />
|
||||||
<Compile Include="..\Capnp.Net.Runtime.Tests\General.cs" Link="General.cs" />
|
<Compile Include="..\Capnp.Net.Runtime.Tests\General.cs" Link="General.cs" />
|
||||||
<Compile Include="..\Capnp.Net.Runtime.Tests\Issue19.cs" Link="Issue19.cs" />
|
<Compile Include="..\Capnp.Net.Runtime.Tests\Issue19.cs" Link="Issue19.cs" />
|
||||||
|
<Compile Include="..\Capnp.Net.Runtime.Tests\Issue20.cs" Link="Issue20.cs" />
|
||||||
<Compile Include="..\Capnp.Net.Runtime.Tests\JobUtil.cs" Link="JobUtil.cs" />
|
<Compile Include="..\Capnp.Net.Runtime.Tests\JobUtil.cs" Link="JobUtil.cs" />
|
||||||
<Compile Include="..\Capnp.Net.Runtime.Tests\MessageBuilderTests.cs" Link="MessageBuilderTests.cs" />
|
<Compile Include="..\Capnp.Net.Runtime.Tests\MessageBuilderTests.cs" Link="MessageBuilderTests.cs" />
|
||||||
<Compile Include="..\Capnp.Net.Runtime.Tests\ProvidedCapabilityMock.cs" Link="ProvidedCapabilityMock.cs" />
|
<Compile Include="..\Capnp.Net.Runtime.Tests\ProvidedCapabilityMock.cs" Link="ProvidedCapabilityMock.cs" />
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using CapnpGen;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace Capnp.Net.Runtime.Tests
|
namespace Capnp.Net.Runtime.Tests
|
||||||
{
|
{
|
||||||
@ -923,5 +925,37 @@ namespace Capnp.Net.Runtime.Tests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Issue20()
|
||||||
|
{
|
||||||
|
RpcRequest<ArithmeticOperationRequest> rpcRequest = new RpcRequest<ArithmeticOperationRequest>();
|
||||||
|
rpcRequest.Method = "AddTwoNumbers";
|
||||||
|
|
||||||
|
ArithmeticOperationRequest request = new ArithmeticOperationRequest();
|
||||||
|
|
||||||
|
request.NumA = 5;
|
||||||
|
request.NumB = 8;
|
||||||
|
|
||||||
|
rpcRequest.Request = request;
|
||||||
|
|
||||||
|
var msg = MessageBuilder.Create();
|
||||||
|
var root = msg.BuildRoot<RpcRequest<ArithmeticOperationRequest>.WRITER>();
|
||||||
|
rpcRequest.serialize(root);
|
||||||
|
|
||||||
|
var mems = new MemoryStream();
|
||||||
|
var pump = new FramePump(mems);
|
||||||
|
pump.Send(msg.Frame);
|
||||||
|
mems.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
var frame = Framing.ReadSegments(mems);
|
||||||
|
var deserializer = DeserializerState.CreateRoot(frame);
|
||||||
|
var mainRequest = new RpcRequest<ArithmeticOperationRequest>.READER(deserializer);
|
||||||
|
var innerRequest = new ArithmeticOperationRequest.READER(mainRequest.Request);
|
||||||
|
|
||||||
|
Console.WriteLine("Method Name: " + mainRequest.Method);
|
||||||
|
Console.WriteLine("NumA: " + innerRequest.NumA.ToString());
|
||||||
|
Console.WriteLine("NumB: " + innerRequest.NumB.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
219
Capnp.Net.Runtime.Tests/Issue20.cs
Normal file
219
Capnp.Net.Runtime.Tests/Issue20.cs
Normal file
@ -0,0 +1,219 @@
|
|||||||
|
using Capnp;
|
||||||
|
using Capnp.Rpc;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CapnpGen
|
||||||
|
{
|
||||||
|
[TypeId(0xb706e295e5860f3dUL)]
|
||||||
|
public class RpcRequest<TRequest> : ICapnpSerializable where TRequest : class
|
||||||
|
{
|
||||||
|
public const UInt64 typeId = 0xb706e295e5860f3dUL;
|
||||||
|
void ICapnpSerializable.Deserialize(DeserializerState arg_)
|
||||||
|
{
|
||||||
|
var reader = READER.create(arg_);
|
||||||
|
Method = reader.Method;
|
||||||
|
Request = CapnpSerializable.Create<TRequest>(reader.Request);
|
||||||
|
applyDefaults();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void serialize(WRITER writer)
|
||||||
|
{
|
||||||
|
writer.Method = Method;
|
||||||
|
writer.Request.SetObject(Request);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ICapnpSerializable.Serialize(SerializerState arg_)
|
||||||
|
{
|
||||||
|
serialize(arg_.Rewrap<WRITER>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void applyDefaults()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Method
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TRequest Request
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct READER
|
||||||
|
{
|
||||||
|
readonly DeserializerState ctx;
|
||||||
|
public READER(DeserializerState ctx)
|
||||||
|
{
|
||||||
|
this.ctx = ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static READER create(DeserializerState ctx) => new READER(ctx);
|
||||||
|
public static implicit operator DeserializerState(READER reader) => reader.ctx;
|
||||||
|
public static implicit operator READER(DeserializerState ctx) => new READER(ctx);
|
||||||
|
public string Method => ctx.ReadText(0, "");
|
||||||
|
public DeserializerState Request => ctx.StructReadPointer(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class WRITER : SerializerState
|
||||||
|
{
|
||||||
|
public WRITER()
|
||||||
|
{
|
||||||
|
this.SetStruct(0, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Method
|
||||||
|
{
|
||||||
|
get => this.ReadText(0, "");
|
||||||
|
set => this.WriteText(0, value, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
public DynamicSerializerState Request
|
||||||
|
{
|
||||||
|
get => BuildPointer<DynamicSerializerState>(1);
|
||||||
|
set => Link(1, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TypeId(0xca749dac8d513c9fUL)]
|
||||||
|
public class ArithmeticOperationRequest : ICapnpSerializable
|
||||||
|
{
|
||||||
|
public const UInt64 typeId = 0xca749dac8d513c9fUL;
|
||||||
|
void ICapnpSerializable.Deserialize(DeserializerState arg_)
|
||||||
|
{
|
||||||
|
var reader = READER.create(arg_);
|
||||||
|
NumA = reader.NumA;
|
||||||
|
NumB = reader.NumB;
|
||||||
|
applyDefaults();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void serialize(WRITER writer)
|
||||||
|
{
|
||||||
|
writer.NumA = NumA;
|
||||||
|
writer.NumB = NumB;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ICapnpSerializable.Serialize(SerializerState arg_)
|
||||||
|
{
|
||||||
|
serialize(arg_.Rewrap<WRITER>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void applyDefaults()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public int NumA
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int NumB
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct READER
|
||||||
|
{
|
||||||
|
readonly DeserializerState ctx;
|
||||||
|
public READER(DeserializerState ctx)
|
||||||
|
{
|
||||||
|
this.ctx = ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static READER create(DeserializerState ctx) => new READER(ctx);
|
||||||
|
public static implicit operator DeserializerState(READER reader) => reader.ctx;
|
||||||
|
public static implicit operator READER(DeserializerState ctx) => new READER(ctx);
|
||||||
|
public int NumA => ctx.ReadDataInt(0UL, 0);
|
||||||
|
public int NumB => ctx.ReadDataInt(32UL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class WRITER : SerializerState
|
||||||
|
{
|
||||||
|
public WRITER()
|
||||||
|
{
|
||||||
|
this.SetStruct(1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int NumA
|
||||||
|
{
|
||||||
|
get => this.ReadDataInt(0UL, 0);
|
||||||
|
set => this.WriteData(0UL, value, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int NumB
|
||||||
|
{
|
||||||
|
get => this.ReadDataInt(32UL, 0);
|
||||||
|
set => this.WriteData(32UL, value, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TypeId(0xc64f52df07418506UL)]
|
||||||
|
public class ArithmeticOperationReply : ICapnpSerializable
|
||||||
|
{
|
||||||
|
public const UInt64 typeId = 0xc64f52df07418506UL;
|
||||||
|
void ICapnpSerializable.Deserialize(DeserializerState arg_)
|
||||||
|
{
|
||||||
|
var reader = READER.create(arg_);
|
||||||
|
Result = reader.Result;
|
||||||
|
applyDefaults();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void serialize(WRITER writer)
|
||||||
|
{
|
||||||
|
writer.Result = Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ICapnpSerializable.Serialize(SerializerState arg_)
|
||||||
|
{
|
||||||
|
serialize(arg_.Rewrap<WRITER>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void applyDefaults()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Result
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct READER
|
||||||
|
{
|
||||||
|
readonly DeserializerState ctx;
|
||||||
|
public READER(DeserializerState ctx)
|
||||||
|
{
|
||||||
|
this.ctx = ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static READER create(DeserializerState ctx) => new READER(ctx);
|
||||||
|
public static implicit operator DeserializerState(READER reader) => reader.ctx;
|
||||||
|
public static implicit operator READER(DeserializerState ctx) => new READER(ctx);
|
||||||
|
public int Result => ctx.ReadDataInt(0UL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class WRITER : SerializerState
|
||||||
|
{
|
||||||
|
public WRITER()
|
||||||
|
{
|
||||||
|
this.SetStruct(1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Result
|
||||||
|
{
|
||||||
|
get => this.ReadDataInt(0UL, 0);
|
||||||
|
set => this.WriteData(0UL, value, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -92,26 +92,38 @@ namespace Capnp
|
|||||||
|
|
||||||
ts = new TS();
|
ts = new TS();
|
||||||
|
|
||||||
InvalidOperationException InvalidWrap() =>
|
if (Kind != ObjectKind.Nil)
|
||||||
new InvalidOperationException("Incompatible cast");
|
|
||||||
|
|
||||||
switch (ts.Kind)
|
|
||||||
{
|
{
|
||||||
case ObjectKind.Struct:
|
InvalidOperationException InvalidWrap() =>
|
||||||
case ObjectKind.ListOfStructs:
|
new InvalidOperationException("Incompatible cast");
|
||||||
if (ts.Kind != Kind ||
|
|
||||||
ts.StructDataCount != StructDataCount ||
|
|
||||||
ts.StructPtrCount != StructPtrCount)
|
|
||||||
throw InvalidWrap();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ObjectKind.Nil:
|
switch (ts.Kind)
|
||||||
break;
|
{
|
||||||
|
case ObjectKind.Struct:
|
||||||
|
case ObjectKind.ListOfStructs:
|
||||||
|
if (ts.Kind != Kind ||
|
||||||
|
ts.StructDataCount != StructDataCount ||
|
||||||
|
ts.StructPtrCount != StructPtrCount)
|
||||||
|
throw InvalidWrap();
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
case ObjectKind.Nil:
|
||||||
if (ts.Kind != Kind)
|
break;
|
||||||
throw InvalidWrap();
|
|
||||||
break;
|
default:
|
||||||
|
if (ts.Kind != Kind)
|
||||||
|
throw InvalidWrap();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.SegmentIndex = SegmentIndex;
|
||||||
|
ts.Offset = Offset;
|
||||||
|
ts.ListElementCount = ListElementCount;
|
||||||
|
ts.StructDataCount = StructDataCount;
|
||||||
|
ts.StructPtrCount = StructPtrCount;
|
||||||
|
ts.Kind = Kind;
|
||||||
|
ts.CapabilityIndex = CapabilityIndex;
|
||||||
|
ts._linkedStates = _linkedStates;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Owner != null)
|
if (Owner != null)
|
||||||
@ -119,15 +131,6 @@ namespace Capnp
|
|||||||
else
|
else
|
||||||
ts.Bind(MsgBuilder);
|
ts.Bind(MsgBuilder);
|
||||||
|
|
||||||
ts.SegmentIndex = SegmentIndex;
|
|
||||||
ts.Offset = Offset;
|
|
||||||
ts.ListElementCount = ListElementCount;
|
|
||||||
ts.StructDataCount = StructDataCount;
|
|
||||||
ts.StructPtrCount = StructPtrCount;
|
|
||||||
ts.Kind = Kind;
|
|
||||||
ts.CapabilityIndex = CapabilityIndex;
|
|
||||||
ts._linkedStates = _linkedStates;
|
|
||||||
|
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user