Skip to content

Commit 2708fa7

Browse files
committed
add samples
1 parent b4343f2 commit 2708fa7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+3696
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="BeetleX.XRPC" Version="0.7.3.3" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using BeetleX.XRPC.Clients;
2+
using BeetleX.XRPC.Packets;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Threading.Tasks;
6+
7+
namespace Client
8+
{
9+
class Program
10+
{
11+
static XRPCClient client;
12+
static IAmount henry, ken, none;
13+
static void Main(string[] args)
14+
{
15+
client = new XRPCClient("localhost", 9090);
16+
client.Options.ParameterFormater = new JsonPacket();//default messagepack
17+
henry = client.Create<IAmount>("henry");
18+
ken = client.Create<IAmount>("ken");
19+
Test();
20+
Console.Read();
21+
}
22+
23+
static async void Test()
24+
{
25+
await Income();
26+
await Pay();
27+
Console.WriteLine($"henry actor:{await henry.GetValue()} | ken actor:{await ken.GetValue()}");
28+
}
29+
30+
static async Task Income()
31+
{
32+
List<Task> tasks = new List<Task>();
33+
for (int i = 0; i < 10; i++)
34+
{
35+
var t = Task.Run(async () =>
36+
{
37+
for (int k = 0; k < 100; k++)
38+
{
39+
await henry.Income(10);
40+
await ken.Income(10);
41+
42+
}
43+
});
44+
tasks.Add(t);
45+
}
46+
await Task.WhenAll(tasks.ToArray());
47+
48+
}
49+
50+
static async Task Pay()
51+
{
52+
List<Task> tasks = new List<Task>();
53+
for (int i = 0; i < 10; i++)
54+
{
55+
var t = Task.Run(async () =>
56+
{
57+
for (int k = 0; k < 100; k++)
58+
{
59+
await henry.Pay(10);
60+
await ken.Pay(10);
61+
62+
}
63+
});
64+
tasks.Add(t);
65+
}
66+
await Task.WhenAll(tasks);
67+
}
68+
}
69+
70+
public interface IAmount
71+
{
72+
Task Income(int value);
73+
Task Pay(int value);
74+
Task<int> GetValue();
75+
}
76+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using BeetleX.XRPC.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
using System;
4+
using System.Threading.Tasks;
5+
using EventNext;
6+
using BeetleX.XRPC.Packets;
7+
8+
namespace Server
9+
{
10+
class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
var builder = new HostBuilder()
15+
.ConfigureServices((hostContext, services) =>
16+
{
17+
services.UseXRPC(s =>
18+
{
19+
s.ServerOptions.LogLevel = BeetleX.EventArgs.LogType.Warring;
20+
s.ServerOptions.DefaultListen.Port = 9090;
21+
s.RPCOptions.ParameterFormater = new JsonPacket();//default messagepack
22+
},
23+
typeof(Program).Assembly);
24+
});
25+
builder.Build().Run();
26+
}
27+
}
28+
29+
public interface IAmount
30+
{
31+
Task Income(int value);
32+
Task Pay(int value);
33+
Task<int> GetValue();
34+
}
35+
[Service(typeof(IAmount))]
36+
public class AmountImpl : IAmount
37+
{
38+
39+
private int mAmount;
40+
41+
public Task<int> GetValue()
42+
{
43+
return mAmount.ToTask();
44+
}
45+
46+
public Task Income(int value)
47+
{
48+
mAmount -= value;
49+
return Task.CompletedTask;
50+
}
51+
52+
public Task Pay(int value)
53+
{
54+
mAmount += value;
55+
return Task.CompletedTask;
56+
}
57+
}
58+
59+
60+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="BeetleX.XRPC.Hosting" Version="0.7.3.3" />
10+
</ItemGroup>
11+
12+
</Project>

samples/XRPC.Actor/XRPC.Actor.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.705
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{750A98AD-7063-46AF-9272-42390A6491C0}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{4CC52F27-DBF2-4180-8D96-335A0D3E1C09}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{750A98AD-7063-46AF-9272-42390A6491C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{750A98AD-7063-46AF-9272-42390A6491C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{750A98AD-7063-46AF-9272-42390A6491C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{750A98AD-7063-46AF-9272-42390A6491C0}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{4CC52F27-DBF2-4180-8D96-335A0D3E1C09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{4CC52F27-DBF2-4180-8D96-335A0D3E1C09}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{4CC52F27-DBF2-4180-8D96-335A0D3E1C09}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{4CC52F27-DBF2-4180-8D96-335A0D3E1C09}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {9BA915BD-D999-4343-B741-0761F31F3223}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="BeetleX.XRPC" Version="0.7.3.3" />
10+
</ItemGroup>
11+
12+
</Project>

samples/XRPC.DI/Client/Program.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using BeetleX.XRPC.Clients;
2+
using BeetleX.XRPC.Packets;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace Client
7+
{
8+
class Program
9+
{
10+
static XRPCClient client;
11+
static IHello hello;
12+
static void Main(string[] args)
13+
{
14+
client = new XRPCClient("localhost", 9090);
15+
client.Options.ParameterFormater = new JsonPacket();//default messagepack
16+
hello = client.Create<IHello>();
17+
while(true)
18+
{
19+
Console.Write("Enter you name:");
20+
var name = Console.ReadLine();
21+
var task = hello.Hello(name);
22+
task.Wait();
23+
Console.WriteLine(task.Result);
24+
}
25+
Console.Read();
26+
}
27+
}
28+
29+
public interface IHello
30+
{
31+
Task<string> Hello(string name);
32+
}
33+
}

samples/XRPC.DI/Server/Program.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using BeetleX.EventArgs;
2+
using BeetleX.XRPC.Hosting;
3+
using BeetleX.XRPC.Packets;
4+
using EventNext;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
using System;
8+
using System.Threading.Tasks;
9+
10+
namespace Server
11+
{
12+
class Program
13+
{
14+
static void Main(string[] args)
15+
{
16+
var builder = new HostBuilder()
17+
.ConfigureServices((hostContext, services) =>
18+
{
19+
services.AddSingleton(new User { Name = "BeetleX" });
20+
services.UseXRPC(s =>
21+
{
22+
23+
s.ServerOptions.LogLevel = BeetleX.EventArgs.LogType.Trace;
24+
s.ServerOptions.DefaultListen.Port = 9090;
25+
s.RPCOptions.ParameterFormater = new JsonPacket();//default messagepack
26+
},
27+
typeof(Program).Assembly);
28+
});
29+
builder.Build().Run();
30+
}
31+
}
32+
33+
public interface IHello
34+
{
35+
Task<string> Hello(string name);
36+
}
37+
38+
[Service(typeof(IHello))]
39+
public class HelloImpl : IHello
40+
{
41+
public HelloImpl(BeetleX.XRPC.XRPCServer server, User user)
42+
{
43+
mServer = server;
44+
mUser = user;
45+
}
46+
47+
private BeetleX.XRPC.XRPCServer mServer;
48+
49+
private User mUser;
50+
51+
public Task<string> Hello(string name)
52+
{
53+
return $"hello {name} {DateTime.Now}\r\n{mServer}\r\n{mUser.Name}".ToTask();
54+
}
55+
}
56+
57+
58+
public class User
59+
{
60+
61+
public string Name { get; set; }
62+
}
63+
64+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="BeetleX.XRPC.Hosting" Version="0.7.3.3" />
10+
</ItemGroup>
11+
12+
</Project>

samples/XRPC.DI/XRPC.DI.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.705
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{B2C1FB4C-3082-448A-BB13-AB1E056AE7AB}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{F42DE94D-03D2-4146-A918-8E677802BDE2}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{B2C1FB4C-3082-448A-BB13-AB1E056AE7AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{B2C1FB4C-3082-448A-BB13-AB1E056AE7AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{B2C1FB4C-3082-448A-BB13-AB1E056AE7AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{B2C1FB4C-3082-448A-BB13-AB1E056AE7AB}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{F42DE94D-03D2-4146-A918-8E677802BDE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{F42DE94D-03D2-4146-A918-8E677802BDE2}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{F42DE94D-03D2-4146-A918-8E677802BDE2}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{F42DE94D-03D2-4146-A918-8E677802BDE2}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {5B8C9CC2-FBF1-4130-AB28-2747109FA907}
30+
EndGlobalSection
31+
EndGlobal

0 commit comments

Comments
 (0)