├── .gitattributes
├── .gitignore
├── LICENSE
├── README.md
├── Rpc.sln
└── src
├── Rabbit.Rpc.ProxyGenerator
├── IServiceProxyFactory.cs
├── IServiceProxyGenerater.cs
├── Implementation
│ ├── ServiceProxyBase.cs
│ ├── ServiceProxyFactory.cs
│ └── ServiceProxyGenerater.cs
├── Properties
│ └── AssemblyInfo.cs
├── Rabbit.Rpc.ProxyGenerator.csproj
├── RpcServiceCollectionExtensions.cs
└── Utilitys
│ └── CompilationUtilitys.cs
├── Rabbit.Rpc
├── Address
│ ├── AddressModel.cs
│ └── IpAddressModel.cs
├── Convertibles
│ ├── ITypeConvertibleProvider.cs
│ ├── ITypeConvertibleService.cs
│ └── Implementation
│ │ ├── DefaultTypeConvertibleProvider.cs
│ │ └── DefaultTypeConvertibleService.cs
├── Exceptions
│ ├── RpcCommunicationException.cs
│ ├── RpcException.cs
│ └── RpcRemoteException.cs
├── Ids
│ ├── IServiceIdGenerator.cs
│ └── Implementation
│ │ └── DefaultServiceIdGenerator.cs
├── Logging
│ ├── ConsoleLogger.cs
│ ├── ILogger.cs
│ └── NullLogger.cs
├── Messages
│ ├── RemoteInvokeMessage.cs
│ ├── RemoteInvokeResultMessage.cs
│ └── TransportMessage.cs
├── Properties
│ └── AssemblyInfo.cs
├── Rabbit.Rpc.csproj
├── Routing
│ ├── IServiceRouteFactory.cs
│ ├── IServiceRouteManager.cs
│ ├── Implementation
│ │ ├── DefaultServiceRouteFactory.cs
│ │ ├── ServiceRouteManagerBase.cs
│ │ └── SharedFileServiceRouteManager.cs
│ ├── ServiceRoute.cs
│ └── ServiceRouteDescriptor.cs
├── RpcServiceCollectionExtensions.cs
├── Runtime
│ ├── Client
│ │ ├── Address
│ │ │ └── Resolvers
│ │ │ │ ├── IAddressResolver.cs
│ │ │ │ └── Implementation
│ │ │ │ ├── DefaultAddressResolver.cs
│ │ │ │ └── Selectors
│ │ │ │ ├── IAddressSelector.cs
│ │ │ │ └── Implementation
│ │ │ │ ├── AddressSelectorBase.cs
│ │ │ │ ├── PollingAddressSelector.cs
│ │ │ │ └── RandomAddressSelector.cs
│ │ ├── HealthChecks
│ │ │ ├── IHealthCheckService.cs
│ │ │ └── Implementation
│ │ │ │ └── DefaultHealthCheckService.cs
│ │ ├── IRemoteInvokeService.cs
│ │ ├── Implementation
│ │ │ └── RemoteInvokeService.cs
│ │ └── RemoteInvokeContext.cs
│ └── Server
│ │ ├── IServiceEntryLocate.cs
│ │ ├── IServiceEntryManager.cs
│ │ ├── IServiceEntryProvider.cs
│ │ ├── IServiceExecutor.cs
│ │ ├── IServiceHost.cs
│ │ ├── Implementation
│ │ ├── DefaultServiceEntryLocate.cs
│ │ ├── DefaultServiceEntryManager.cs
│ │ ├── DefaultServiceExecutor.cs
│ │ ├── DefaultServiceHost.cs
│ │ ├── ServiceDiscovery
│ │ │ ├── Attributes
│ │ │ │ ├── AttributeServiceEntryProvider.cs
│ │ │ │ ├── RpcServiceAttribute.cs
│ │ │ │ ├── RpcServiceBundleAttribute.cs
│ │ │ │ ├── RpcServiceDescriptorAttribute.cs
│ │ │ │ └── RpcServiceMetadataAttribute.cs
│ │ │ ├── IClrServiceEntryFactory.cs
│ │ │ └── Implementation
│ │ │ │ └── ClrServiceEntryFactory.cs
│ │ └── ServiceHostAbstract.cs
│ │ └── ServiceEntry.cs
├── Serialization
│ ├── ISerializer.cs
│ └── Implementation
│ │ ├── JsonSerializer.cs
│ │ ├── StringByteArraySerializer.cs
│ │ └── StringObjectSerializer.cs
├── ServiceDescriptor.cs
└── Transport
│ ├── Codec
│ ├── ITransportMessageCodecFactory.cs
│ ├── ITransportMessageDecoder.cs
│ ├── ITransportMessageEncoder.cs
│ └── Implementation
│ │ ├── JsonTransportMessageCodecFactory.cs
│ │ ├── JsonTransportMessageDecoder.cs
│ │ └── JsonTransportMessageEncoder.cs
│ ├── IMessageListener.cs
│ ├── IMessageSender.cs
│ ├── ITransportClient.cs
│ ├── ITransportClientFactory.cs
│ └── Implementation
│ ├── MessageListener.cs
│ └── TransportClient.cs
├── examples
├── Echo.Client
│ ├── Echo.Client.csproj
│ ├── Program.cs
│ └── Properties
│ │ ├── AssemblyInfo.cs
│ │ └── PublishProfiles
│ │ ├── Default-publish.ps1
│ │ └── publish-module.psm1
├── Echo.Common
│ ├── Echo.Common.csproj
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── UserService.cs
├── Echo.Server
│ ├── Echo.Server.csproj
│ ├── Program.cs
│ └── Properties
│ │ └── AssemblyInfo.cs
└── performances
│ ├── Performances.Net.Client
│ ├── Performances.Net.Client.csproj
│ ├── Program.cs
│ └── Properties
│ │ └── AssemblyInfo.cs
│ ├── Performances.Net.Server
│ ├── Performances.Net.Server.csproj
│ ├── Program.cs
│ └── Properties
│ │ └── AssemblyInfo.cs
│ ├── Performances.NetCoreApp.Client
│ ├── Performances.NetCoreApp.Client.csproj
│ ├── Program.cs
│ └── Properties
│ │ └── AssemblyInfo.cs
│ └── Performances.NetCoreApp.Server
│ ├── Performances.NetCoreApp.Server.csproj
│ ├── Program.cs
│ └── Properties
│ └── AssemblyInfo.cs
├── extensions
├── codecs
│ └── Rabbit.Rpc.Codec.ProtoBuffer
│ │ ├── Messages
│ │ ├── ProtoBufferDynamicItem.cs
│ │ ├── ProtoBufferRemoteInvokeMessage.cs
│ │ ├── ProtoBufferRemoteInvokeResultMessage.cs
│ │ └── ProtoBufferTransportMessage.cs
│ │ ├── Properties
│ │ └── AssemblyInfo.cs
│ │ ├── ProtoBufferTransportMessageCodecFactory.cs
│ │ ├── ProtoBufferTransportMessageDecoder.cs
│ │ ├── ProtoBufferTransportMessageEncoder.cs
│ │ ├── Rabbit.Rpc.Codec.ProtoBuffer.csproj
│ │ ├── Rabbit.Rpc.Codec.ProtoBuffer.csproj.migration_in_place_backup
│ │ ├── RpcServiceCollectionExtensions.cs
│ │ └── Utilitys
│ │ └── SerializerUtilitys.cs
├── coordinates
│ └── Rabbit.Rpc.Coordinate.Zookeeper
│ │ ├── Properties
│ │ └── AssemblyInfo.cs
│ │ ├── Rabbit.Rpc.Coordinate.Zookeeper.csproj
│ │ ├── RpcServiceCollectionExtensions.cs
│ │ └── ZooKeeperServiceRouteManager.cs
└── transports
│ └── Rabbit.Transport.DotNetty
│ ├── Adaper
│ └── TransportMessageChannelHandlerAdapter.cs
│ ├── DotNettyMessageSender.cs
│ ├── DotNettyServerMessageListener.cs
│ ├── DotNettyTransportClientFactory.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ ├── Rabbit.Transport.DotNetty.csproj
│ └── RpcServiceCollectionExtensions.cs
├── tests
└── Rabbit.Rpc.Tests
│ ├── AddressSelectors
│ ├── PollingAddressSelectorTests.cs
│ └── RandomAddressSelectorTests.cs
│ ├── ModelEqualsTests.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ ├── Rabbit.Rpc.Tests.csproj
│ └── ServiceRouteManagers
│ ├── ServiceRouteManagerTests.cs
│ ├── SharedFileServiceRouteManagerTests.cs
│ └── ZooKeeperServiceRouteManagerTests.cs
└── tools
└── Rabbit.Rpc.ClientGenerator
├── Program.cs
├── Properties
└── AssemblyInfo.cs
└── Rabbit.Rpc.ClientGenerator.csproj
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 | [Ll]og/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | artifacts/
46 |
47 | *_i.c
48 | *_p.c
49 | *_i.h
50 | *.ilk
51 | *.meta
52 | *.obj
53 | *.pch
54 | *.pdb
55 | *.pgc
56 | *.pgd
57 | *.rsp
58 | *.sbr
59 | *.tlb
60 | *.tli
61 | *.tlh
62 | *.tmp
63 | *.tmp_proj
64 | *.log
65 | *.vspscc
66 | *.vssscc
67 | .builds
68 | *.pidb
69 | *.svclog
70 | *.scc
71 |
72 | # Chutzpah Test files
73 | _Chutzpah*
74 |
75 | # Visual C++ cache files
76 | ipch/
77 | *.aps
78 | *.ncb
79 | *.opendb
80 | *.opensdf
81 | *.sdf
82 | *.cachefile
83 | *.VC.db
84 | *.VC.VC.opendb
85 |
86 | # Visual Studio profiler
87 | *.psess
88 | *.vsp
89 | *.vspx
90 | *.sap
91 |
92 | # TFS 2012 Local Workspace
93 | $tf/
94 |
95 | # Guidance Automation Toolkit
96 | *.gpState
97 |
98 | # ReSharper is a .NET coding add-in
99 | _ReSharper*/
100 | *.[Rr]e[Ss]harper
101 | *.DotSettings.user
102 |
103 | # JustCode is a .NET coding add-in
104 | .JustCode
105 |
106 | # TeamCity is a build add-in
107 | _TeamCity*
108 |
109 | # DotCover is a Code Coverage Tool
110 | *.dotCover
111 |
112 | # NCrunch
113 | _NCrunch_*
114 | .*crunch*.local.xml
115 | nCrunchTemp_*
116 |
117 | # MightyMoose
118 | *.mm.*
119 | AutoTest.Net/
120 |
121 | # Web workbench (sass)
122 | .sass-cache/
123 |
124 | # Installshield output folder
125 | [Ee]xpress/
126 |
127 | # DocProject is a documentation generator add-in
128 | DocProject/buildhelp/
129 | DocProject/Help/*.HxT
130 | DocProject/Help/*.HxC
131 | DocProject/Help/*.hhc
132 | DocProject/Help/*.hhk
133 | DocProject/Help/*.hhp
134 | DocProject/Help/Html2
135 | DocProject/Help/html
136 |
137 | # Click-Once directory
138 | publish/
139 |
140 | # Publish Web Output
141 | *.[Pp]ublish.xml
142 | *.azurePubxml
143 | # TODO: Comment the next line if you want to checkin your web deploy settings
144 | # but database connection strings (with potential passwords) will be unencrypted
145 | *.pubxml
146 | *.publishproj
147 |
148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
149 | # checkin your Azure Web App publish settings, but sensitive information contained
150 | # in these scripts will be unencrypted
151 | PublishScripts/
152 |
153 | # NuGet Packages
154 | *.nupkg
155 | # The packages folder can be ignored because of Package Restore
156 | **/packages/*
157 | # except build/, which is used as an MSBuild target.
158 | !**/packages/build/
159 | # Uncomment if necessary however generally it will be regenerated when needed
160 | #!**/packages/repositories.config
161 | # NuGet v3's project.json files produces more ignoreable files
162 | *.nuget.props
163 | *.nuget.targets
164 |
165 | # Microsoft Azure Build Output
166 | csx/
167 | *.build.csdef
168 |
169 | # Microsoft Azure Emulator
170 | ecf/
171 | rcf/
172 |
173 | # Windows Store app package directories and files
174 | AppPackages/
175 | BundleArtifacts/
176 | Package.StoreAssociation.xml
177 | _pkginfo.txt
178 |
179 | # Visual Studio cache files
180 | # files ending in .cache can be ignored
181 | *.[Cc]ache
182 | # but keep track of directories ending in .cache
183 | !*.[Cc]ache/
184 |
185 | # Others
186 | ClientBin/
187 | ~$*
188 | *~
189 | *.dbmdl
190 | *.dbproj.schemaview
191 | *.pfx
192 | *.publishsettings
193 | node_modules/
194 | orleans.codegen.cs
195 |
196 | # Since there are multiple workflows, uncomment next line to ignore bower_components
197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
198 | #bower_components/
199 |
200 | # RIA/Silverlight projects
201 | Generated_Code/
202 |
203 | # Backup & report files from converting an old project file
204 | # to a newer Visual Studio version. Backup files are not needed,
205 | # because we have git ;-)
206 | _UpgradeReport_Files/
207 | Backup*/
208 | UpgradeLog*.XML
209 | UpgradeLog*.htm
210 |
211 | # SQL Server files
212 | *.mdf
213 | *.ldf
214 |
215 | # Business Intelligence projects
216 | *.rdl.data
217 | *.bim.layout
218 | *.bim_*.settings
219 |
220 | # Microsoft Fakes
221 | FakesAssemblies/
222 |
223 | # GhostDoc plugin setting file
224 | *.GhostDoc.xml
225 |
226 | # Node.js Tools for Visual Studio
227 | .ntvs_analysis.dat
228 |
229 | # Visual Studio 6 build log
230 | *.plg
231 |
232 | # Visual Studio 6 workspace options file
233 | *.opt
234 |
235 | # Visual Studio LightSwitch build output
236 | **/*.HTMLClient/GeneratedArtifacts
237 | **/*.DesktopClient/GeneratedArtifacts
238 | **/*.DesktopClient/ModelManifest.xml
239 | **/*.Server/GeneratedArtifacts
240 | **/*.Server/ModelManifest.xml
241 | _Pvt_Extensions
242 |
243 | # Paket dependency manager
244 | .paket/paket.exe
245 | paket-files/
246 |
247 | # FAKE - F# Make
248 | .fake/
249 |
250 | # JetBrains Rider
251 | .idea/
252 | *.sln.iml
253 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Rabbit RPC
2 | A lightweight cross-platform RPC.
3 | ## Features
4 | 1. Apache License 2.0 protocol open source
5 | 2. Supports client load balancing (polling and random)
6 | 3. Support ZooKeeper and file sharing the service coordination
7 | 4. Runtime client proxy generation (based Roslyn)
8 | 5. Pre-generated client agent (based Roslyn)
9 | 6. Abstract codec (JSON and ProtoBuffer)
10 | 7. Abstract transmission channel (DotNetty)
11 | 8. Exception information transfer (Local exceptions to the server runtime can be passed to the client)
12 | 9. **NET Core Project structure**
13 | 10. **Cross-platform**
14 |
15 | ## Overview
16 | 
17 | ### Rabbit.Rpc
18 | 1. Rpc core class library, has the following functions:
19 | 2. Service Id generation
20 | 3. Transfer the message model
21 | 4. Type conversion
22 | 5. Service routing abstraction
23 | 6. Serializer abstraction (the default provides JSON serializer)
24 | 7. Transport abstraction
25 | 8. Codec abstraction (default provides JSON codec implementation)
26 | 9. Client runtime (address resolver, address selector, remote call service)
27 | 10. Service-side runtime (service entry management, service executor, service discovery abstraction, RpcServiceAttribute tagging service discovery implementation)
28 |
29 | ### Rabbit.Rpc.ProxyGenerator
30 | Service Agent Builder, provides features:
31 |
32 | 1. Service agent implementation generation
33 | 2. Service agent instance creation
34 |
35 | ### extensions
36 | #### Rabbit.Rpc.Codec.ProtoBuffer
37 | ProtoBuffer protocol codec implementation.
38 |
39 | #### Rabbit.Rpc.Coordinate.Zookeeper
40 | Service Routing Management Based on ZooKeeper.
41 |
42 | #### Rabbit.Transport.DotNetty
43 | Implementation of DotNetty Transmission.
44 |
45 | ### tools
46 | #### Rabbit.Rpc.Tests
47 | Unit test project.
48 |
49 | #### Rabbit.Rpc.ClientGenerator
50 | Pre-production service agent tool, provides the following functions:
51 |
52 | 1. Generate the service proxy implementation code file
53 | 2. Generate the service agent to implement the assembly file
54 |
55 | ## Performance Testing
56 | Test environment
57 |
58 | OS | CPU | Memory | disk | network | VM
59 | ------------ | ------------- | ------------- | ------------- | ------------- | -------------
60 | Windows 10 x64 | I7 3610QM | 16GB | SSD | 127.0.0.1 | no
61 | Ubuntu 16.04 x64 | I7 3610QM | 4GB | SSD | 127.0.0.1 | yes
62 |
63 | ### Windows10 + NETCoreApp1.0 + JSON protocol
64 | loop 10,000
65 | first 2626ms
66 | second 2597ms
67 | third 2581ms
68 |
69 | ### Windows10 + NETCoreApp1.0 + ProtoBuffer protocol
70 | loop 10,000
71 | first 2567ms
72 | second 2617ms
73 | third 2474ms
74 |
75 | ### Ubuntu16.04-x64 + NETCoreApp1.0 + JSON protocol
76 | loop 10,000
77 | first 3205ms
78 | second 3252ms
79 | third 2837ms
80 |
81 | ### Ubuntu16.04-x64 + NETCoreApp1.0 + ProtoBuffer protocol
82 | loop 10,000
83 | first 3391ms
84 | second 3391ms
85 | third 3574ms
86 |
87 | ## related articles
88 | * [拥抱.NET Core,跨平台的轻量级RPC:Rabbit.Rpc](http://www.cnblogs.com/ants/p/5652132.html)
89 | * [.NET轻量级RPC框架:Rabbit.Rpc](http://www.cnblogs.com/ants/p/5605754.html)
90 |
91 | ## communication
92 | * [QQ Group:384413261(RabbitHub)](http://jq.qq.com/?_wv=1027&k=29DzAfj)
93 | * [Email:majian159@live.com](mailto:majian159@live.com)
94 |
--------------------------------------------------------------------------------
/src/Rabbit.Rpc.ProxyGenerator/IServiceProxyFactory.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Rabbit.Rpc.ProxyGenerator
4 | {
5 | ///
6 | /// 一个抽象的服务代理工厂。
7 | ///
8 | public interface IServiceProxyFactory
9 | {
10 | ///
11 | /// 创建服务代理。
12 | ///
13 | /// 代理类型。
14 | /// 服务代理实例。
15 | object CreateProxy(Type proxyType);
16 | }
17 |
18 | ///
19 | /// 服务代理工厂扩展。
20 | ///
21 | public static class ServiceProxyFactoryExtensions
22 | {
23 | ///
24 | /// 创建服务代理。
25 | ///
26 | /// 服务接口类型。
27 | /// 服务代理工厂。
28 | /// 代理类型。
29 | /// 服务代理实例。
30 | public static T CreateProxy(this IServiceProxyFactory serviceProxyFactory, Type proxyType)
31 | {
32 | return (T)serviceProxyFactory.CreateProxy(proxyType);
33 | }
34 | }
35 | }
--------------------------------------------------------------------------------
/src/Rabbit.Rpc.ProxyGenerator/IServiceProxyGenerater.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.CodeAnalysis;
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | namespace Rabbit.Rpc.ProxyGenerator
6 | {
7 | ///
8 | /// 一个抽象的服务代理生成器。
9 | ///
10 | public interface IServiceProxyGenerater
11 | {
12 | ///
13 | /// 生成服务代理。
14 | ///
15 | /// 需要被代理的接口类型。
16 | /// 服务代理实现。
17 | IEnumerable GenerateProxys(IEnumerable interfacTypes);
18 |
19 | ///
20 | /// 生成服务代理代码树。
21 | ///
22 | /// 需要被代理的接口类型。
23 | /// 代码树。
24 | SyntaxTree GenerateProxyTree(Type interfaceType);
25 | }
26 | }
--------------------------------------------------------------------------------
/src/Rabbit.Rpc.ProxyGenerator/Implementation/ServiceProxyBase.cs:
--------------------------------------------------------------------------------
1 | using Rabbit.Rpc.Convertibles;
2 | using Rabbit.Rpc.Messages;
3 | using Rabbit.Rpc.Runtime.Client;
4 | using System.Collections.Generic;
5 | using System.Threading.Tasks;
6 |
7 | namespace Rabbit.Rpc.ProxyGenerator.Implementation
8 | {
9 | ///
10 | /// 一个抽象的服务代理基类。
11 | ///
12 | public abstract class ServiceProxyBase
13 | {
14 | #region Field
15 |
16 | private readonly IRemoteInvokeService _remoteInvokeService;
17 | private readonly ITypeConvertibleService _typeConvertibleService;
18 |
19 | #endregion Field
20 |
21 | #region Constructor
22 |
23 | protected ServiceProxyBase(IRemoteInvokeService remoteInvokeService, ITypeConvertibleService typeConvertibleService)
24 | {
25 | _remoteInvokeService = remoteInvokeService;
26 | _typeConvertibleService = typeConvertibleService;
27 | }
28 |
29 | #endregion Constructor
30 |
31 | #region Protected Method
32 |
33 | ///
34 | /// 远程调用。
35 | ///
36 | /// 返回类型。
37 | /// 参数字典。
38 | /// 服务Id。
39 | /// 调用结果。
40 | protected async Task Invoke(IDictionary parameters, string serviceId)
41 | {
42 | var message = await _remoteInvokeService.InvokeAsync(new RemoteInvokeContext
43 | {
44 | InvokeMessage = new RemoteInvokeMessage
45 | {
46 | Parameters = parameters,
47 | ServiceId = serviceId
48 | }
49 | });
50 |
51 | if (message == null)
52 | return default(T);
53 |
54 | var result = _typeConvertibleService.Convert(message.Result, typeof(T));
55 |
56 | return (T)result;
57 | }
58 |
59 | ///
60 | /// 远程调用。
61 | ///
62 | /// 参数字典。
63 | /// 服务Id。
64 | /// 调用任务。
65 | protected async Task Invoke(IDictionary parameters, string serviceId)
66 | {
67 | await _remoteInvokeService.InvokeAsync(new RemoteInvokeContext
68 | {
69 | InvokeMessage = new RemoteInvokeMessage
70 | {
71 | Parameters = parameters,
72 | ServiceId = serviceId
73 | }
74 | });
75 | }
76 |
77 | #endregion Protected Method
78 | }
79 | }
--------------------------------------------------------------------------------
/src/Rabbit.Rpc.ProxyGenerator/Implementation/ServiceProxyFactory.cs:
--------------------------------------------------------------------------------
1 | using Rabbit.Rpc.Convertibles;
2 | using Rabbit.Rpc.Runtime.Client;
3 | using System;
4 | using System.Linq;
5 | using System.Reflection;
6 |
7 | namespace Rabbit.Rpc.ProxyGenerator.Implementation
8 | {
9 | ///
10 | /// 默认的服务代理工厂实现。
11 | ///
12 | public class ServiceProxyFactory : IServiceProxyFactory
13 | {
14 | #region Field
15 |
16 | private readonly IRemoteInvokeService _remoteInvokeService;
17 | private readonly ITypeConvertibleService _typeConvertibleService;
18 |
19 | #endregion Field
20 |
21 | #region Constructor
22 |
23 | public ServiceProxyFactory(IRemoteInvokeService remoteInvokeService, ITypeConvertibleService typeConvertibleService)
24 | {
25 | _remoteInvokeService = remoteInvokeService;
26 | _typeConvertibleService = typeConvertibleService;
27 | }
28 |
29 | #endregion Constructor
30 |
31 | #region Implementation of IServiceProxyFactory
32 |
33 | ///
34 | /// 创建服务代理。
35 | ///
36 | /// 代理类型。
37 | /// 服务代理实例。
38 | public object CreateProxy(Type proxyType)
39 | {
40 | var instance = proxyType.GetTypeInfo().GetConstructors().First().Invoke(new object[] { _remoteInvokeService, _typeConvertibleService });
41 | return instance;
42 | }
43 |
44 | #endregion Implementation of IServiceProxyFactory
45 | }
46 | }
--------------------------------------------------------------------------------
/src/Rabbit.Rpc.ProxyGenerator/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 |
4 | // General Information about an assembly is controlled through the following
5 | // set of attributes. Change these attribute values to modify the information
6 | // associated with an assembly.
7 | [assembly: AssemblyConfiguration("")]
8 | [assembly: AssemblyCompany("")]
9 | [assembly: AssemblyProduct("Rabbit.Rpc.ProxyGenerator")]
10 | [assembly: AssemblyTrademark("")]
11 |
12 | // Setting ComVisible to false makes the types in this assembly not visible
13 | // to COM components. If you need to access a type in this assembly from
14 | // COM, set the ComVisible attribute to true on that type.
15 | [assembly: ComVisible(false)]
16 |
17 | // The following GUID is for the ID of the typelib if this project is exposed to COM
18 | [assembly: Guid("5105cb2c-5bfa-4106-84e9-f1101323c560")]
--------------------------------------------------------------------------------
/src/Rabbit.Rpc.ProxyGenerator/Rabbit.Rpc.ProxyGenerator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Rpc客户端代理生成器。
5 | © RabbitHub. All rights reserved.
6 | 1.0.0-alpha1
7 | majian
8 | net45;netstandard1.6
9 | Rabbit.Rpc.ProxyGenerator
10 | Rabbit.Rpc.ProxyGenerator
11 | RabbitHub;RPC
12 | http://www.rabbithub.com/icon.png
13 | https://github.com/RabbitTeam/Rpc
14 | https://github.com/RabbitTeam/Rpc/blob/master/LICENSE
15 | 1.6.0
16 | false
17 | false
18 | false
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 | $(DefineConstants);NET
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/src/Rabbit.Rpc.ProxyGenerator/RpcServiceCollectionExtensions.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.DependencyInjection;
2 | using Rabbit.Rpc.ProxyGenerator.Implementation;
3 |
4 | namespace Rabbit.Rpc.ProxyGenerator
5 | {
6 | public static class RpcServiceCollectionExtensions
7 | {
8 | public static IRpcBuilder AddClientProxy(this IRpcBuilder builder)
9 | {
10 | var services = builder.Services;
11 |
12 | services.AddSingleton();
13 | services.AddSingleton();
14 |
15 | return builder;
16 | }
17 |
18 | public static IRpcBuilder AddClient(this IServiceCollection services)
19 | {
20 | return services
21 | .AddRpcCore()
22 | .AddClientRuntime()
23 | .AddClientProxy();
24 | }
25 | }
26 | }
--------------------------------------------------------------------------------
/src/Rabbit.Rpc/Address/AddressModel.cs:
--------------------------------------------------------------------------------
1 | using System.Net;
2 |
3 | namespace Rabbit.Rpc.Address
4 | {
5 | ///
6 | /// 一个抽象的地址模型。
7 | ///
8 | public abstract class AddressModel
9 | {
10 | ///
11 | /// 创建终结点。
12 | ///
13 | ///
14 | public abstract EndPoint CreateEndPoint();
15 |
16 | ///
17 | /// 重写后的标识。
18 | ///
19 | /// 一个字符串。
20 | public abstract override string ToString();
21 |
22 | #region Equality members
23 |
24 | /// Determines whether the specified object is equal to the current object.
25 | /// true if the specified object is equal to the current object; otherwise, false.
26 | /// The object to compare with the current object.
27 | public override bool Equals(object obj)
28 | {
29 | var model = obj as AddressModel;
30 | if (model == null)
31 | return false;
32 |
33 | if (obj.GetType() != GetType())
34 | return false;
35 |
36 | return model.ToString() == ToString();
37 | }
38 |
39 | /// Serves as the default hash function.
40 | /// A hash code for the current object.
41 | public override int GetHashCode()
42 | {
43 | return ToString().GetHashCode();
44 | }
45 |
46 | public static bool operator ==(AddressModel model1, AddressModel model2)
47 | {
48 | return Equals(model1, model2);
49 | }
50 |
51 | public static bool operator !=(AddressModel model1, AddressModel model2)
52 | {
53 | return !Equals(model1, model2);
54 | }
55 |
56 | #endregion Equality members
57 | }
58 | }
--------------------------------------------------------------------------------
/src/Rabbit.Rpc/Address/IpAddressModel.cs:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RabbitTeam/RabbitCloud/8d42a11d1208f55117a411b4248a5abf336161e6/src/Rabbit.Rpc/Address/IpAddressModel.cs
--------------------------------------------------------------------------------
/src/Rabbit.Rpc/Convertibles/ITypeConvertibleProvider.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Rabbit.Rpc.Convertibles
5 | {
6 | ///
7 | /// 类型转换。
8 | ///
9 | /// 需要转换的实例。
10 | /// 转换的类型。
11 | /// 转换之后的类型,如果无法转换则返回null。
12 | public delegate object TypeConvertDelegate(object instance, Type conversionType);
13 |
14 | ///
15 | /// 一个抽象的类型转换提供程序。
16 | ///
17 | public interface ITypeConvertibleProvider
18 | {
19 | ///
20 | /// 获取类型转换器。
21 | ///
22 | /// 类型转换器集合。
23 | IEnumerable GetConverters();
24 | }
25 | }
--------------------------------------------------------------------------------
/src/Rabbit.Rpc/Convertibles/ITypeConvertibleService.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Rabbit.Rpc.Convertibles
4 | {
5 | ///
6 | /// 一个抽象的类型转换服务。
7 | ///
8 | public interface ITypeConvertibleService
9 | {
10 | ///
11 | /// 转换。
12 | ///
13 | /// 需要转换的实例。
14 | /// 转换的类型。
15 | /// 转换之后的类型,如果无法转换则返回null。
16 | object Convert(object instance, Type conversionType);
17 | }
18 | }
--------------------------------------------------------------------------------
/src/Rabbit.Rpc/Convertibles/Implementation/DefaultTypeConvertibleProvider.cs:
--------------------------------------------------------------------------------
1 | using Rabbit.Rpc.Serialization;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Reflection;
5 |
6 | namespace Rabbit.Rpc.Convertibles.Implementation
7 | {
8 | ///
9 | /// 一个默认的类型转换提供程序。
10 | ///
11 | public class DefaultTypeConvertibleProvider : ITypeConvertibleProvider
12 | {
13 | private readonly ISerializer