Skip to content
This repository was archived by the owner on Sep 19, 2019. It is now read-only.

Commit eb66d83

Browse files
committed
Message writer almost finished.
1 parent 09c28bb commit eb66d83

23 files changed

+270
-56
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,4 @@ FakesAssemblies/
194194

195195
# Visual Studio 6 workspace options file
196196
*.opt
197+
src/Tools/
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Mono.Cecil;
5+
6+
namespace RampUp.Enveloper.Fody
7+
{
8+
public class ModuleWeaver
9+
{
10+
public ModuleDefinition ModuleDefinition { get; set; }
11+
12+
public IAssemblyResolver AssemblyResolver { get; set; }
13+
14+
public Action<string> LogInfo { get; set; }
15+
public Action<string> LogWarning { get; set; }
16+
17+
public void Execute()
18+
{
19+
var isRampUp = ModuleDefinition.Assembly.Name.Name == "RampUp";
20+
21+
var rampUp = isRampUp ? ModuleDefinition.Assembly : AssemblyResolver.Resolve("RampUp");
22+
var envelopeType = rampUp.MainModule.Types.Single(t => t.FullName == "RampUp.Actors.Envelope");
23+
var messageMarkupInterface = rampUp.MainModule.Types.Single(t => t.Name == "IMessage");
24+
var markupFullName = messageMarkupInterface.FullName;
25+
var messageTypes = ModuleDefinition.Types.SelectMany(AllTypes).Where(t => t.IsValueType && t.Interfaces.Any() && t.Interfaces.Any(i => i.FullName == markupFullName)).ToArray();
26+
27+
if (messageTypes.Any() == false)
28+
{
29+
LogWarning(
30+
"No RampUp messages were found. Ensure that your messages implement an interface with a full name 'RampUp.Actors.IMessage'");
31+
return;
32+
}
33+
34+
var envelopeImported = isRampUp? envelopeType : ModuleDefinition.Import(envelopeType);
35+
36+
foreach (var messageType in messageTypes)
37+
{
38+
messageType.Fields.Add(new FieldDefinition("_____envelope", FieldAttributes.Private, envelopeImported));
39+
}
40+
}
41+
42+
private static IEnumerable<TypeDefinition> AllTypes(TypeDefinition t)
43+
{
44+
yield return t;
45+
46+
foreach (var nested in t.NestedTypes.SelectMany(AllTypes))
47+
{
48+
yield return nested;
49+
}
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("RampUp.Enveloper.Fody")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("RampUp.Enveloper.Fody")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("c5136cfb-bb51-4f06-bfef-0a690fbd7d50")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{C5136CFB-BB51-4F06-BFEF-0A690FBD7D50}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>RampUp.Enveloper.Fody</RootNamespace>
11+
<AssemblyName>RampUp.Enveloper.Fody</AssemblyName>
12+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>..\Tools\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>..\Tools\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="Mono.Cecil, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
35+
<HintPath>..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.dll</HintPath>
36+
<Private>True</Private>
37+
</Reference>
38+
<Reference Include="Mono.Cecil.Mdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
39+
<HintPath>..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Mdb.dll</HintPath>
40+
<Private>True</Private>
41+
</Reference>
42+
<Reference Include="Mono.Cecil.Pdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
43+
<HintPath>..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Pdb.dll</HintPath>
44+
<Private>True</Private>
45+
</Reference>
46+
<Reference Include="Mono.Cecil.Rocks, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
47+
<HintPath>..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Rocks.dll</HintPath>
48+
<Private>True</Private>
49+
</Reference>
50+
<Reference Include="System" />
51+
<Reference Include="System.Core" />
52+
<Reference Include="System.Xml.Linq" />
53+
<Reference Include="System.Data.DataSetExtensions" />
54+
<Reference Include="Microsoft.CSharp" />
55+
<Reference Include="System.Data" />
56+
<Reference Include="System.Net.Http" />
57+
<Reference Include="System.Xml" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<Compile Include="ModuleWeaver.cs" />
61+
<Compile Include="Properties\AssemblyInfo.cs" />
62+
</ItemGroup>
63+
<ItemGroup>
64+
<None Include="packages.config" />
65+
</ItemGroup>
66+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
67+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
68+
Other similar extension points exist, see Microsoft.Common.targets.
69+
<Target Name="BeforeBuild">
70+
</Target>
71+
<Target Name="AfterBuild">
72+
</Target>
73+
-->
74+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Mono.Cecil" version="0.9.6.1" targetFramework="net461" />
4+
</packages>

src/RampUp.Tests/Actors/Impl/ActorRegistryTestsBase.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void Handle(ref Envelope envelope, ref A msg)
3131
}
3232
}
3333

34-
public struct A
34+
public struct A : IMessage
3535
{
3636
}
3737

@@ -51,7 +51,7 @@ public void Handle(ref Envelope envelope, ref B msg)
5151
}
5252
}
5353

54-
public struct B
54+
public struct B : IMessage
5555
{
5656
}
5757

src/RampUp.Tests/Actors/Impl/MessageReaderTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public void GivenEmptyActor_WhenAnyMessage_ThenPasses()
2727
reader.MessageHandlerImpl(4, new ByteChunk(bytes, 5));
2828
}
2929

30-
public struct A
30+
public struct A : IMessage
3131
{
3232
}
3333

34-
public struct B
34+
public struct B : IMessage
3535
{
3636
}
3737

src/RampUp.Tests/Actors/Impl/MessageWriterTests.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Reflection;
44
using System.Reflection.Emit;
5+
using System.Runtime.InteropServices;
56
using NSubstitute;
67
using NUnit.Framework;
78
using RampUp.Actors;
@@ -13,7 +14,8 @@ namespace RampUp.Tests.Actors.Impl
1314
{
1415
public class MessageWriterTests
1516
{
16-
public struct A
17+
[StructLayout(LayoutKind.Sequential, Size = 64)]
18+
public struct A : IMessage
1719
{
1820
public int Value;
1921
}
@@ -22,10 +24,10 @@ public struct A
2224
public unsafe void Test()
2325
{
2426
var counter = Substitute.For<IStructSizeCounter>();
25-
const int aSize = 3;
26-
counter.GetSize(typeof (A)).Returns(aSize);
27+
const int aSize = 64;
28+
counter.GetSize(typeof(A)).Returns(aSize);
2729
const int envelopeSize = 4;
28-
counter.GetSize(typeof (Envelope)).Returns(envelopeSize);
30+
counter.GetSize(typeof(Envelope)).Returns(envelopeSize);
2931

3032
var buffer = Substitute.For<IRingBuffer>();
3133
buffer.Write(0, new ByteChunk()).ReturnsForAnyArgs(true);
@@ -34,7 +36,7 @@ public unsafe void Test()
3436
var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("AnythingForTests"),
3537
AssemblyBuilderAccess.Run);
3638
var main = asm.DefineDynamicModule("main");
37-
var writer = BaseMessageWriter.Build(counter, l => messageId, new[] {typeof (A)}, main);
39+
var writer = BaseMessageWriter.Build(counter, l => messageId, new[] { typeof(A) }, main);
3840

3941
var e = new Envelope(new ActorId(1));
4042
var a = new A();
@@ -45,8 +47,7 @@ public unsafe void Test()
4547
var args = call.GetArguments();
4648
Assert.AreEqual("Write", call.GetMethodInfo().Name);
4749
Assert.AreEqual(messageId, args[0]);
48-
Assert.AreEqual(new ByteChunk((byte*) &e, envelopeSize), args[1]);
49-
Assert.AreEqual(new ByteChunk((byte*) &a, aSize), args[2]);
50+
Assert.AreEqual(new ByteChunk((byte*)&a, aSize), args[1]);
5051
}
5152
}
5253
}

src/RampUp.Tests/FodyWeavers.xml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Weavers>
3+
<RampUp.Enveloper/>
4+
</Weavers>

src/RampUp.Tests/PerformanceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace RampUp.Tests
1616
[Explicit]
1717
public class PerformanceTests
1818
{
19-
public struct A
19+
public struct A : IMessage
2020
{
2121
public int Value;
2222
}

src/RampUp.Tests/RampUp.Tests.csproj

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<TargetFrameworkProfile />
15+
<NuGetPackageImportStamp>
16+
</NuGetPackageImportStamp>
1517
</PropertyGroup>
1618
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1719
<DebugSymbols>true</DebugSymbols>
@@ -81,7 +83,17 @@
8183
<ItemGroup>
8284
<None Include="packages.config" />
8385
</ItemGroup>
86+
<ItemGroup>
87+
<Content Include="FodyWeavers.xml" />
88+
</ItemGroup>
8489
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
90+
<Import Project="..\packages\Fody.1.29.4\build\dotnet\Fody.targets" Condition="Exists('..\packages\Fody.1.29.4\build\dotnet\Fody.targets')" />
91+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
92+
<PropertyGroup>
93+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
94+
</PropertyGroup>
95+
<Error Condition="!Exists('..\packages\Fody.1.29.4\build\dotnet\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.1.29.4\build\dotnet\Fody.targets'))" />
96+
</Target>
8597
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
8698
Other similar extension points exist, see Microsoft.Common.targets.
8799
<Target Name="BeforeBuild">

src/RampUp.Tests/Ring/ManyToOneBufferTests.cs

-10
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,6 @@ public void ShouldThrowExceptionWhenMaxMessageSizeExceeded()
311311
Assert.Throws<ArgumentException>(() => { _ringBuffer.Write(MessageTypeId, new ByteChunk(null, _ringBuffer.MaximumMessageLength + 1)); });
312312
}
313313

314-
[Test]
315-
public void ShouldThrowWhenTwoChunksWrittenAndFirstIsMisallingned()
316-
{
317-
Assert.Throws<ArgumentException>(()=>
318-
{
319-
_ringBuffer.Write(MessageTypeId, new ByteChunk(null, RecordAlignment + 1),
320-
new ByteChunk(null, RecordAlignment));
321-
});
322-
}
323-
324314
[Test]
325315
public void ShouldInsertPaddingAndWriteToBuffer()
326316
{

src/RampUp.Tests/packages.config

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package buffer="NSubstitute" version="1.9.2.0" targetFramework="net451" />
4-
<package buffer="NUnit" version="2.6.4" targetFramework="net451" />
3+
<package id="Fody" version="1.29.4" targetFramework="net461" developmentDependency="true" />
4+
<package id="NSubstitute" version="1.9.2.0" targetFramework="net461" />
5+
<package id="NUnit" version="2.6.4" targetFramework="net461" />
56
</packages>

src/RampUp.sln

+14
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@ Microsoft Visual Studio Solution File, Format Version 12.00
44
VisualStudioVersion = 14.0.24720.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RampUp", "RampUp\RampUp.csproj", "{50C9F77D-5BA9-47AC-A531-F31EDF3A259D}"
7+
ProjectSection(ProjectDependencies) = postProject
8+
{C5136CFB-BB51-4F06-BFEF-0A690FBD7D50} = {C5136CFB-BB51-4F06-BFEF-0A690FBD7D50}
9+
EndProjectSection
710
EndProject
811
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RampUp.Tests", "RampUp.Tests\RampUp.Tests.csproj", "{D592804F-592B-4C35-B6C1-B6F3D9B3CE87}"
12+
ProjectSection(ProjectDependencies) = postProject
13+
{C5136CFB-BB51-4F06-BFEF-0A690FBD7D50} = {C5136CFB-BB51-4F06-BFEF-0A690FBD7D50}
14+
EndProjectSection
915
EndProject
1016
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{C88F7D8D-75BD-4274-A2B2-A6117A2D0D6F}"
1117
ProjectSection(SolutionItems) = preProject
1218
.nuget\packages.config = .nuget\packages.config
1319
EndProjectSection
1420
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RampUp.Enveloper.Fody", "RampUp.Enveloper.Fody\RampUp.Enveloper.Fody.csproj", "{C5136CFB-BB51-4F06-BFEF-0A690FBD7D50}"
22+
EndProject
1523
Global
1624
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1725
Debug|Any CPU = Debug|Any CPU
@@ -31,6 +39,12 @@ Global
3139
{D592804F-592B-4C35-B6C1-B6F3D9B3CE87}.Release_NoTests|Any CPU.Build.0 = Release_NoTests|Any CPU
3240
{D592804F-592B-4C35-B6C1-B6F3D9B3CE87}.Release|Any CPU.ActiveCfg = Release|Any CPU
3341
{D592804F-592B-4C35-B6C1-B6F3D9B3CE87}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{C5136CFB-BB51-4F06-BFEF-0A690FBD7D50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43+
{C5136CFB-BB51-4F06-BFEF-0A690FBD7D50}.Debug|Any CPU.Build.0 = Debug|Any CPU
44+
{C5136CFB-BB51-4F06-BFEF-0A690FBD7D50}.Release_NoTests|Any CPU.ActiveCfg = Release|Any CPU
45+
{C5136CFB-BB51-4F06-BFEF-0A690FBD7D50}.Release_NoTests|Any CPU.Build.0 = Release|Any CPU
46+
{C5136CFB-BB51-4F06-BFEF-0A690FBD7D50}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{C5136CFB-BB51-4F06-BFEF-0A690FBD7D50}.Release|Any CPU.Build.0 = Release|Any CPU
3448
EndGlobalSection
3549
GlobalSection(SolutionProperties) = preSolution
3650
HideSolutionNode = FALSE

src/RampUp/Actors/Envelope.cs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace RampUp.Actors
99
[StructLayout(LayoutKind.Explicit, Size = RingBufferDescriptor.RecordAlignment)]
1010
public struct Envelope
1111
{
12+
internal static readonly string FieldName = "_____envelope";
13+
1214
/// <summary>
1315
/// The sender.
1416
/// </summary>

src/RampUp/Actors/IMessage.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace RampUp.Actors
2+
{
3+
/// <summary>
4+
/// Markup interface for a message type. You can create your own <see cref="IMessage"/> interface if you match the full name of this type of it.
5+
/// </summary>
6+
public interface IMessage
7+
{ }
8+
}

0 commit comments

Comments
 (0)