Skip to content

Commit

Permalink
破坏性改动:RpcID 发送时由正数改为负数,返回时由负数改为正数。
Browse files Browse the repository at this point in the history
0是普通消息。int.minValue是广播消息。
  • Loading branch information
KumoKyaku committed Oct 28, 2022
1 parent 1afe378 commit 92ce62d
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 21 deletions.
6 changes: 3 additions & 3 deletions Megumin.Remote/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void BroadCastTcp<R, T>(this R remotes, T message, object options
{
var writer = new TcpBufferWriter();

if (BroadCastHelper.TrySerialize(writer, 0, message, options))
if (BroadCastHelper.TrySerialize(writer, int.MinValue, message, options))
{
writer.WriteLengthOnHeader();
foreach (var item in remotes)
Expand All @@ -68,7 +68,7 @@ public static void BroadCastUdp<R, T>(this R remotes, T message, object options
{
var writer = new UdpBufferWriter(0x10000);
writer.WriteHeader(UdpRemoteMessageDefine.UdpData);
if (BroadCastHelper.TrySerialize(writer, 0, message, options))
if (BroadCastHelper.TrySerialize(writer, int.MinValue, message, options))
{
foreach (var item in remotes)
{
Expand All @@ -90,7 +90,7 @@ public static void BroadCastKcp<R, T>(this R remotes, T message, object options
where R : IEnumerable<IRemote>
{
var writer = new UdpBufferWriter(0x10000);
if (BroadCastHelper.TrySerialize(writer, 0, message, options))
if (BroadCastHelper.TrySerialize(writer, int.MinValue, message, options))
{
foreach (var item in remotes)
{
Expand Down
6 changes: 3 additions & 3 deletions Megumin.Remote/Megumin.Remote.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<Authors>云却</Authors>

<!--版本配置-->
<Version>3.0.0.0</Version>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<FileVersion>3.0.0.0</FileVersion>
<Version>3.1.0.0</Version>
<AssemblyVersion>3.1.0.0</AssemblyVersion>
<FileVersion>3.1.0.0</FileVersion>

<!--包配置-->
<Product>MeguminNet</Product>
Expand Down
19 changes: 11 additions & 8 deletions Megumin.Remote/Rpc/RpcLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,23 @@ public partial class RpcLayer :
readonly object rpcCursorLock = new object();

/// <summary>
/// 原子操作 取得RpcId,发送方的的RpcID为正数,回复的RpcID为负数,正负一一对应
/// <para>0,int.MinValue 为无效值</para>
/// 原子操作 取得RpcId,发送方的的RpcID为负数,回复的RpcID为正数,正负一一对应
/// <para>0,为无效值</para>
/// int.MinValue 为广播消息。
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override int GetRpcID()
{
lock (rpcCursorLock)
{
if (rpcCursor == int.MaxValue)
if (rpcCursor < -999999999)
{
rpcCursor = 1;
rpcCursor = -1000;
}
else
{
rpcCursor++;
rpcCursor--;
}

return rpcCursor;
Expand All @@ -69,7 +70,7 @@ public override (int rpcID, IMiniAwaitable<(object result, Exception exception)>
public partial class RpcLayer
{
/// <summary>
/// 如果rpcID为负数,是rpc返回回复,返回true,此消息由RpcLayer处理。
/// 如果rpcID大于0为正数,是rpc返回回复,返回true,此消息由RpcLayer处理。
/// <para> 否则返回false,RpcLayer忽略此消息。</para>
/// </summary>
/// <param name="rpcID"></param>
Expand All @@ -78,9 +79,11 @@ public partial class RpcLayer
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryInput(int rpcID, object message)
{
if (rpcID < 0)
//rpcID为负数为发送,正数改为回复,这样此处判断一次就可以了。不用额外判断 0 和 int.MinValue。
//获取ID时int.MinValue直接无效。
if (rpcID > 0)
{
//这个消息是rpc返回(回复的RpcID为负数
//这个消息是rpc返回(回复的RpcID为正数
TrySetResult(rpcID * -1, message);
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions Megumin.Remote/TcpBufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public void Free()
}
}

/// <summary>
/// 起始位置总是预留4个字节,写入消息总长度
/// </summary>
public class TcpBufferWriter : BaseBufferWriter
{
public TcpBufferWriter(int bufferLenght = 1024 * 8)
Expand Down
3 changes: 2 additions & 1 deletion Megumin.Remote/Transport/UdpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public virtual void Send<T>(T message, int rpcID, object options = null)
}
}

///发送时线程安全
///每个消息序列化时使用新的writer实例,保证发送时线程安全
var writer = new UdpBufferWriter(0x10000);
writer.WriteHeader(UdpRemoteMessageDefine.UdpData);
if (RemoteCore.TrySerialize(writer, rpcID, message, options))
Expand All @@ -299,6 +299,7 @@ public virtual void Send<T>(T message, int rpcID, object options = null)
/// <summary>
/// 网络层实际发送数据位置
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async void SocketSend(IBufferBlock sendBlock)
{
await SocketSend(sendBlock.BlockSegment).ConfigureAwait(false);
Expand Down
6 changes: 3 additions & 3 deletions NetRemoteStandard/NetRemoteStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<Authors>云却</Authors>

<!--版本配置-->
<Version>3.0.0.0</Version>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<FileVersion>3.0.0.0</FileVersion>
<Version>3.1.0.0</Version>
<AssemblyVersion>3.1.0.0</AssemblyVersion>
<FileVersion>3.1.0.0</FileVersion>

<!--包配置-->
<Product>Remote</Product>
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ Heartbeat,RTT,Timestamp Synchronization等功能都由此机制实现。
| 4byte | 4byte | 2byte | 4byte | byte[].Lenght |

- **与其他语言或者网络库对接**
**`当服务器不使用本库,或者不是C#语言时。满足报头格式,即可支持本库所有特性。`**
请求时RpcID大于0,响应消息时RpcID小于0,并且等于请求RpcID*-10int.MinValue表示非Rpc消息,为无效RpcID
**`当服务器不使用本库,或者不是C#语言时。满足报头格式,即可支持本库所有特性。`**

# ~~MessagePipeline是什么?~~
~~MessagePipelineMegumin.Remote 分离出来的一部分功能。
Expand Down Expand Up @@ -330,7 +329,9 @@ namespace Message
---

# 一些细节
- `RPC功能`:保证了请求和返回消息一对一匹配。发送时RPCID为正数,返回时RPCID*-1,用正负区分上下行。0int.minValueRPCID无效值。
- `RPC功能`:保证了请求和返回消息一对一匹配。发送时RPCID为负数,返回时RPCID*-1 为正数,用正负区分上下行。
+ 0int.minValue为无效RPCID值。
+ 0是普通消息。int.minValue是广播消息。
- `内存分配`:通过使用`内存池`,减少alloc
- ~~[发送过程数据拷贝](#jump1)了2次,接收过程数据无拷贝(各个序列化类库不同)。~~ 2.0版本中做了调整。
- `内存池`:标准库内存池,`ArrayPool<byte>.Shared`。
Expand Down
11 changes: 11 additions & 0 deletions UnitTest/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ string IntegerToBinaryString(short theNumber)
Assert.AreEqual(true, (cmd & 0b1111_1111_1111_1111) != 0);
Assert.AreEqual("0b1111_1111_1111_1111", str);
}

[TestMethod()]
public void TestN()
{
int a = int.MinValue;
var b = a * -1;
var c = int.MaxValue;
var d = c + 1;
var e = a -1;
e.ToString();
}
}
}

Expand Down

0 comments on commit 92ce62d

Please sign in to comment.