Skip to content

Commit b4343f2

Browse files
committed
no message
1 parent 2da2605 commit b4343f2

20 files changed

+1023
-397
lines changed

src/Awaiter/AwaiterFactory.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ static AwaiterFactory()
1717

1818
const int COUNT = 1024 * 1024;
1919

20-
public AwaiterFactory(int startid = 1, int end = 100000000)
20+
public const int CLIENT_START = 10000000;
21+
22+
public const int CLIENT_END = 19999999;
23+
24+
public const int SERVER_START = 20000000;
25+
26+
public const int SERVER_END = 29999999;
27+
28+
public AwaiterFactory(int startid, int end)
2129
{
2230
mID = startid;
2331
mStartID = startid;
@@ -40,8 +48,8 @@ public AwaiterFactory(int startid = 1, int end = 100000000)
4048

4149
private void OnTimeProcess(AwaiterItem item)
4250
{
43-
Response response = new Response();
44-
response.Status = (short)ResponseCode.REQUEST_TIMEOUT;
51+
RPCPacket response = new RPCPacket();
52+
response.Status = (short)StatusCode.REQUEST_TIMEOUT;
4553
response.Data = new object[] { $"Request {item.Request.Url} time out!" };
4654
Completed(item, response);
4755
}
@@ -51,15 +59,15 @@ private void OnTimeout(object state)
5159
try
5260
{
5361
mTimer.Change(-1, -1);
54-
long timeout = TimeWatch.GetElapsedMilliseconds();
62+
long timeout = TimeWatch.GetElapsedMilliseconds();
5563
var items = mAwaiterItemGroup.GetTimeouts(timeout);
5664
if (items.Count > 0)
5765
{
5866
for (int i = 0; i < items.Count; i++)
5967
{
6068
mTimeDispatch.Enqueue(items[i]);
6169
}
62-
70+
6371
}
6472
}
6573
catch
@@ -77,8 +85,9 @@ internal AwaiterItem GetItem(int id)
7785
return mAwaiterItemGroup.Get(id);
7886
}
7987

80-
public (int, TaskCompletionSource<Response>) Create(Request request, Type[] resultType, int timeout = 1000 * 100)
88+
public (int, TaskCompletionSource<RPCPacket>) Create(RPCPacket request, Type[] resultType, int timeout = 1000 * 10)
8189
{
90+
request.NeedReply = true;
8291
int id = 0;
8392
long expiredTime;
8493
lock (this)
@@ -87,7 +96,7 @@ internal AwaiterItem GetItem(int id)
8796
if (mID >= mEndID)
8897
mID = mStartID;
8998
id = mID;
90-
99+
91100
}
92101
expiredTime = TimeWatch.GetElapsedMilliseconds() + timeout;
93102
var item = new AwaiterItem();
@@ -98,7 +107,7 @@ internal AwaiterItem GetItem(int id)
98107
return (id, item.Create(expiredTime));
99108
}
100109

101-
public bool Completed(AwaiterItem item, Response data)
110+
public bool Completed(AwaiterItem item, RPCPacket data)
102111
{
103112
if (item.Completed(data))
104113
{
@@ -161,13 +170,14 @@ public AwaiterItem Get(int id)
161170

162171
public void GetTimeouts(List<AwaiterItem> items, double time)
163172
{
164-
foreach (var item in mItems.Values)
165-
{
166-
if (time > item.TimeOut)
173+
if (mItems.Count > 0)
174+
foreach (var item in mItems.Values)
167175
{
168-
items.Add(Get(item.ID));
176+
if (time > item.TimeOut)
177+
{
178+
items.Add(Get(item.ID));
179+
}
169180
}
170-
}
171181
}
172182
}
173183
}

src/Awaiter/AwaiterItem.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public AwaiterItem()
1212

1313
}
1414

15-
private TaskCompletionSource<Response> completionSource;
15+
private TaskCompletionSource<RPCPacket> completionSource;
1616

1717
public int ID { get; set; }
1818

@@ -22,14 +22,14 @@ public AwaiterItem()
2222

2323
public Type[] ResultType { get; set; }
2424

25-
public TaskCompletionSource<Response> Create(long expiredTime)
25+
public TaskCompletionSource<RPCPacket> Create(long expiredTime)
2626
{
2727
TimeOut = expiredTime;
28-
completionSource = new TaskCompletionSource<Response>();
28+
completionSource = new TaskCompletionSource<RPCPacket>();
2929
return completionSource;
3030
}
3131

32-
public bool Completed(Response data)
32+
public bool Completed(RPCPacket data)
3333
{
3434
if (System.Threading.Interlocked.CompareExchange(ref mFree, 1, 0) == 0)
3535
{
@@ -40,9 +40,9 @@ public bool Completed(Response data)
4040
return false;
4141
}
4242

43-
public Request Request { get; set; }
43+
public RPCPacket Request { get; set; }
4444

45-
public Response Response { get; set; }
45+
public RPCPacket Response { get; set; }
4646

4747
}
4848
}

src/BeetleX.XRPC.csproj

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5-
<Version>0.7.3.3</Version>
5+
<Version>0.8.2.3</Version>
66
<Authors>henryfan</Authors>
77
<Company>ikende.com</Company>
8-
<Copyright>Copyright © ikende.com 2019-2020 email:henryfan@msn.com</Copyright>
8+
<Copyright>Copyright © beetlex.io 2019-2020 email:henryfan@msn.com</Copyright>
99
<Description>high performance remote interface invoke(RPC) communication components for netcore</Description>
1010
<PackageReleaseNotes>dotnet high performance remote interface invoke(RPC) communication components</PackageReleaseNotes>
1111
<PackageProjectUrl>https://github.com/IKende/XRPC</PackageProjectUrl>
1212
<PackageLicenseUrl></PackageLicenseUrl>
13+
<SignAssembly>true</SignAssembly>
14+
<AssemblyOriginatorKeyFile>XRPC.pfx</AssemblyOriginatorKeyFile>
1315
</PropertyGroup>
1416

1517
<ItemGroup>
@@ -19,13 +21,10 @@
1921
</ItemGroup>
2022

2123
<ItemGroup>
24+
<PackageReference Include="BeetleX" Version="1.4.8.3" />
25+
<PackageReference Include="EventNext" Version="0.9.7.8" />
2226
<PackageReference Include="MessagePack" Version="2.0.323" />
2327
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
2428
</ItemGroup>
2529

26-
<ItemGroup>
27-
<ProjectReference Include="..\BeetleX.VNext\EventNext.csproj" />
28-
<ProjectReference Include="..\src\BeetleX\BeetleX.csproj" />
29-
</ItemGroup>
30-
3130
</Project>

src/BeetleX.XRPC.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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}") = "BeetleX.XRPC", "BeetleX.XRPC.csproj", "{589CFE31-137B-4727-917C-B674CAB2F012}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{589CFE31-137B-4727-917C-B674CAB2F012}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{589CFE31-137B-4727-917C-B674CAB2F012}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{589CFE31-137B-4727-917C-B674CAB2F012}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{589CFE31-137B-4727-917C-B674CAB2F012}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {5EBABBFA-2A40-4E90-98A0-1D7EBA663EE6}
24+
EndGlobalSection
25+
EndGlobal

src/Clients/AnyCompletionSource.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface IAnyCompletionSource
99
{
1010
void Success(object data);
1111
void Error(Exception error);
12-
void WaitResponse(Task<Response> task);
12+
void WaitResponse(Task<RPCPacket> task);
1313

1414
ClientActionHandler ClientActionHandler { get; set; }
1515

@@ -30,10 +30,10 @@ public void Error(Exception error)
3030

3131
public ClientActionHandler ClientActionHandler { get; set; }
3232

33-
public async void WaitResponse(Task<Response> task)
33+
public async void WaitResponse(Task<RPCPacket> task)
3434
{
3535
var response = await task;
36-
if (response.Status != (short)ResponseCode.SUCCESS)
36+
if (response.Status != (short)StatusCode.SUCCESS)
3737
{
3838
XRPCException error = new XRPCException((string)response.Data[0]);
3939
Error(error);

src/Clients/ClientActionHandler.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ public ClientActionHandler(MethodInfo method)
1515
ResultType = method.ReturnType;
1616
PropertyInfo pi = method.ReturnType.GetProperty("Result", BindingFlags.Public | BindingFlags.Instance);
1717
if (pi != null)
18+
{
1819
ResultProperty = new PropertyHandler(pi);
19-
20+
ResultPropertyInfo = pi;
21+
}
22+
2023
IsTaskResult = ResultType.BaseType == typeof(Task) || ResultType == typeof(Task);
2124
IsVoid = ResultType == typeof(void);
2225
ResponseType = GetResponseType();
@@ -30,6 +33,8 @@ public ClientActionHandler(MethodInfo method)
3033

3134
public MethodInfo MethodInfo { get; set; }
3235

36+
public PropertyInfo ResultPropertyInfo { get; set; }
37+
3338
internal MethodHandler MethodHandler { get; set; }
3439

3540
public Type ResultType { get; set; }
@@ -64,6 +69,11 @@ private Type[] GetResponseType()
6469

6570
private Type mCompletionSourceType;
6671

72+
public object Execute(object controller, params object[] parameters)
73+
{
74+
return MethodHandler.Execute(controller, parameters);
75+
}
76+
6777
internal IAnyCompletionSource GetCompletionSource()
6878
{
6979
if (mCompletionSourceType == null)

src/Clients/ControllerCenter.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Collections.Concurrent;
5+
using System.Reflection;
6+
using System.Linq;
7+
namespace BeetleX.XRPC.Clients
8+
{
9+
public class ControllerCenter
10+
{
11+
12+
private ConcurrentDictionary<string, HandlerItem> mHandlers = new ConcurrentDictionary<string, HandlerItem>(
13+
StringComparer.OrdinalIgnoreCase);
14+
15+
public void Register<Service>(Service serviceImpl)
16+
{
17+
Type type = typeof(Service);
18+
if (!type.IsInterface)
19+
{
20+
throw new XRPCException($"{type} not interface!");
21+
}
22+
if (!serviceImpl.GetType().IsClass)
23+
{
24+
throw new XRPCException($"{serviceImpl} not the implementer!");
25+
}
26+
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance))
27+
{
28+
string url = $"/{type.Name}/{method.Name}";
29+
var item = new HandlerItem { Controller = serviceImpl, Url = url, Handler = new ClientActionHandler(method) };
30+
item.Parameters = (from a in method.GetParameters() select a.ParameterType).ToArray();
31+
mHandlers[url] = item;
32+
}
33+
}
34+
35+
public HandlerItem GetHandler(string url)
36+
{
37+
mHandlers.TryGetValue(url, out HandlerItem result);
38+
return result;
39+
}
40+
41+
public class HandlerItem
42+
{
43+
public string Url { get; internal set; }
44+
45+
public object Controller { get; internal set; }
46+
47+
public ClientActionHandler Handler { get; internal set; }
48+
49+
public Type[] Parameters { get; internal set; }
50+
51+
public object GetValue(object result)
52+
{
53+
if (Handler.IsVoid)
54+
return null;
55+
if (Handler.IsTaskResult)
56+
{
57+
if (Handler.ResultProperty != null)
58+
return Handler.ResultPropertyInfo.GetValue(result);
59+
return null;
60+
}
61+
return null;
62+
}
63+
}
64+
65+
}
66+
}

src/Clients/TcpClientItem.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using BeetleX.Clients;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace BeetleX.XRPC.Clients
7+
{
8+
public class TcpClientItem
9+
{
10+
public TcpClientItem(AsyncTcpClient client, XRPCClient xrpc)
11+
{
12+
TcpClient = client;
13+
XRPCClient = xrpc;
14+
}
15+
16+
public XRPCClient XRPCClient { get; private set; }
17+
18+
public AsyncTcpClient TcpClient { get; private set; }
19+
20+
public bool Connected => TcpClient.IsConnected;
21+
22+
public long PingTime { get; set; }
23+
24+
25+
public bool TimeOut(long time)
26+
{
27+
return (TimeWatch.GetElapsedMilliseconds() - PingTime) > time;
28+
}
29+
30+
private int mPingStatus = 0;
31+
32+
public async void Ping()
33+
{
34+
if (System.Threading.Interlocked.CompareExchange(ref mPingStatus, 1, 0) == 0)
35+
{
36+
if (TcpClient.IsConnected)
37+
{
38+
try
39+
{
40+
RPCPacket request = new RPCPacket();
41+
request.Url = "/__System/Ping";
42+
var response = await XRPCClient.SendWait(request, TcpClient, null);
43+
PingTime = TimeWatch.GetElapsedMilliseconds();
44+
}
45+
catch (Exception e_)
46+
{
47+
48+
}
49+
finally
50+
{
51+
if (TimeOut(XRPCClient.PingTimeout * 1000))
52+
{
53+
TcpClient.DisConnect();
54+
bool isnew;
55+
TcpClient.Connect(out isnew);
56+
}
57+
}
58+
}
59+
else
60+
{
61+
PingTime = TimeWatch.GetElapsedMilliseconds();
62+
}
63+
System.Threading.Interlocked.Exchange(ref mPingStatus, 0);
64+
}
65+
}
66+
67+
}
68+
}

0 commit comments

Comments
 (0)