ntminer 5 年 前
コミット
1997680605

+ 6 - 0
src/NTMinerServer/Core/Mq/MinerClientMqBodyUtil.cs

@@ -4,6 +4,7 @@ using System.Text;
 
 namespace NTMiner.Core.Mq {
     public static class MinerClientMqBodyUtil {
+        #region ClientId
         public static byte[] GetClientIdMqSendBody(Guid clientId) {
             return Encoding.UTF8.GetBytes(clientId.ToString());
         }
@@ -17,14 +18,18 @@ namespace NTMiner.Core.Mq {
             }
             return Guid.Empty;
         }
+        #endregion
 
+        #region MinerId
         public static byte[] GetMinerIdMqSendBody(string minerId) {
             return Encoding.UTF8.GetBytes(minerId);
         }
         public static string GetMinerIdMqReciveBody(byte[] body) {
             return Encoding.UTF8.GetString(body);
         }
+        #endregion
 
+        #region ChangeMinerSign
         public static byte[] GetChangeMinerSignMqSendBody(MinerSign minerSign) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(minerSign));
         }
@@ -33,5 +38,6 @@ namespace NTMiner.Core.Mq {
             string json = Encoding.UTF8.GetString(body);
             return VirtualRoot.JsonSerializer.Deserialize<MinerSign>(json);
         }
+        #endregion
     }
 }

+ 61 - 25
src/NTMinerServer/Core/Mq/OperationMqBodyUtil.cs

@@ -7,6 +7,7 @@ using System.Text;
 
 namespace NTMiner.Core.Mq {
     public static class OperationMqBodyUtil {
+        #region GetConsoleOutLines
         public static byte[] GetGetConsoleOutLinesMqSendBody(long afterTime) {
             return Encoding.UTF8.GetBytes(afterTime.ToString());
         }
@@ -17,29 +18,9 @@ namespace NTMiner.Core.Mq {
             }
             return 0;
         }
+        #endregion
 
-        public static byte[] GetGetLocalMessagesMqSendBody(long afterTime) {
-            return Encoding.UTF8.GetBytes(afterTime.ToString());
-        }
-        public static long GetGetLocalMessagesMqReceiveBody(byte[] body) {
-            string s = Encoding.UTF8.GetString(body);
-            if (long.TryParse(s, out long value)) {
-                return value;
-            }
-            return 0;
-        }
-
-        public static byte[] GetGetOperationResultsMqSendBody(long afterTime) {
-            return Encoding.UTF8.GetBytes(afterTime.ToString());
-        }
-        public static long GetGetOperationResultsMqReceiveBody(byte[] body) {
-            string s = Encoding.UTF8.GetString(body);
-            if (long.TryParse(s, out long value)) {
-                return value;
-            }
-            return 0;
-        }
-
+        #region ConsoleOutLines
         public static byte[] GetConsoleOutLinesMqSendBody(List<ConsoleOutLine> datas) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(datas));
         }
@@ -54,7 +35,22 @@ namespace NTMiner.Core.Mq {
             }
             return result;
         }
+        #endregion
 
+        #region GetLocalMessages
+        public static byte[] GetGetLocalMessagesMqSendBody(long afterTime) {
+            return Encoding.UTF8.GetBytes(afterTime.ToString());
+        }
+        public static long GetGetLocalMessagesMqReceiveBody(byte[] body) {
+            string s = Encoding.UTF8.GetString(body);
+            if (long.TryParse(s, out long value)) {
+                return value;
+            }
+            return 0;
+        }
+        #endregion
+
+        #region LocalMessages
         public static byte[] GetLocalMessagesMqSendBody(List<LocalMessageDto> datas) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(datas));
         }
@@ -69,7 +65,22 @@ namespace NTMiner.Core.Mq {
             }
             return result;
         }
+        #endregion
+
+        #region GetOperationResults
+        public static byte[] GetGetOperationResultsMqSendBody(long afterTime) {
+            return Encoding.UTF8.GetBytes(afterTime.ToString());
+        }
+        public static long GetGetOperationResultsMqReceiveBody(byte[] body) {
+            string s = Encoding.UTF8.GetString(body);
+            if (long.TryParse(s, out long value)) {
+                return value;
+            }
+            return 0;
+        }
+        #endregion
 
+        #region Drives
         public static byte[] GetDrivesMqSendBody(List<DriveDto> datas) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(datas));
         }
@@ -84,7 +95,9 @@ namespace NTMiner.Core.Mq {
             }
             return result;
         }
+        #endregion
 
+        #region LocalIps
         public static byte[] GetLocalIpsMqSendBody(List<LocalIpDto> datas) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(datas));
         }
@@ -99,7 +112,9 @@ namespace NTMiner.Core.Mq {
             }
             return result;
         }
+        #endregion
 
+        #region OperationResults
         public static byte[] GetOperationResultsMqSendBody(List<OperationResultData> datas) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(datas));
         }
@@ -114,7 +129,9 @@ namespace NTMiner.Core.Mq {
             }
             return result;
         }
+        #endregion
 
+        #region GetSpeed
         public static byte[] GetGetSpeedMqSendBody(List<Guid> clientIds) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(clientIds));
         }
@@ -122,15 +139,19 @@ namespace NTMiner.Core.Mq {
             string json = Encoding.UTF8.GetString(body);
             return VirtualRoot.JsonSerializer.Deserialize<List<Guid>>(json);
         }
-        
-        public static byte[] GetSpeedDataMqSendBody(SpeedData data) {
+        #endregion
+
+        #region Speed
+        public static byte[] GetSpeedMqSendBody(SpeedData data) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(data));
         }
-        public static SpeedData GetSpeedDataMqReceiveBody(byte[] body) {
+        public static SpeedData GetSpeedMqReceiveBody(byte[] body) {
             string json = Encoding.UTF8.GetString(body);
             return VirtualRoot.JsonSerializer.Deserialize<SpeedData>(json);
         }
+        #endregion
 
+        #region SwitchRadeonGpu
         public static byte[] GetSwitchRadeonGpuMqSendBody(bool on) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(on.ToString()));
         }
@@ -139,7 +160,9 @@ namespace NTMiner.Core.Mq {
             bool.TryParse(str, out bool result);
             return result;
         }
+        #endregion
 
+        #region SetVirtualMemory
         public static byte[] GetSetVirtualMemoryMqSendBody(Dictionary<string, int> datas) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(datas));
         }
@@ -147,7 +170,9 @@ namespace NTMiner.Core.Mq {
             string json = Encoding.UTF8.GetString(body);
             return VirtualRoot.JsonSerializer.Deserialize<Dictionary<string, int>>(json);
         }
+        #endregion
 
+        #region SetLocalIps
         public static byte[] GetSetLocalIpsMqSendBody(List<LocalIpInput> datas) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(datas));
         }
@@ -155,28 +180,36 @@ namespace NTMiner.Core.Mq {
             string json = Encoding.UTF8.GetString(body);
             return VirtualRoot.JsonSerializer.Deserialize<List<LocalIpInput>>(json);
         }
+        #endregion
 
+        #region GpuProfilesJson
         public static byte[] GetGpuProfilesJsonMqSendBody(string json) {
             return Encoding.UTF8.GetBytes(json);
         }
         public static string GetGpuProfilesJsonMqReceiveBody(byte[] body) {
             return Encoding.UTF8.GetString(body);
         }
+        #endregion
 
+        #region SaveGpuProfilesJson
         public static byte[] GetSaveGpuProfilesJsonMqSendBody(string json) {
             return Encoding.UTF8.GetBytes(json);
         }
         public static string GetSaveGpuProfilesJsonMqReceiveBody(byte[] body) {
             return Encoding.UTF8.GetString(body);
         }
+        #endregion
 
+        #region UpgradeNTMiner
         public static byte[] GetUpgradeNTMinerMqSendBody(string ntminerFileName) {
             return Encoding.UTF8.GetBytes(ntminerFileName);
         }
         public static string GetUpgradeNTMinerMqReceiveBody(byte[] body) {
             return Encoding.UTF8.GetString(body);
         }
+        #endregion
 
+        #region SetAutoBootStart
         public static byte[] GetSetAutoBootStartMqSendBody(SetAutoBootStartRequest body) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(body));
         }
@@ -187,7 +220,9 @@ namespace NTMiner.Core.Mq {
             }
             return VirtualRoot.JsonSerializer.Deserialize<SetAutoBootStartRequest>(json);
         }
+        #endregion
 
+        #region StartMine
         public static byte[] GetStartMineMqSendBody(Guid workId) {
             return Encoding.UTF8.GetBytes(workId.ToString());
         }
@@ -201,5 +236,6 @@ namespace NTMiner.Core.Mq {
             }
             return Guid.Empty;
         }
+        #endregion
     }
 }

+ 4 - 0
src/NTMinerServer/Core/Mq/README.md

@@ -0,0 +1,4 @@
+## 关于**MqBodyUtil类型
+* 后缀为Util的类型全是静态类型;
+* **MqBodyUtil类型中的方法每两个是一对:一个MqSendBody方法一个MqReceiveBody方法;
+* **MqBodyUtil类型的目的是集中放置同类的方法,目的是拉近MqSendBody和MqReceiveBody方法对的源代码距离;

+ 5 - 0
src/NTMinerServer/Core/Mq/UserMqBodyUtil.cs

@@ -2,12 +2,16 @@
 
 namespace NTMiner.Core.Mq {
     public static class UserMqBodyUtil {
+        #region LoginName
         public static byte[] GetLoginNameMqSendBody(string loginName) {
             return Encoding.UTF8.GetBytes(loginName);
         }
         public static string GetLoginNameMqReceiveBody(byte[] data) {
             return Encoding.UTF8.GetString(data);
         }
+        #endregion
+
+        #region UpdateUserRSAKey
         public static byte[] GetUpdateUserRSAKeyMqSendBody(Cryptography.RSAKey key) {
             return Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(key));
         }
@@ -18,5 +22,6 @@ namespace NTMiner.Core.Mq {
             }
             return VirtualRoot.JsonSerializer.Deserialize<Cryptography.RSAKey>(json);
         }
+        #endregion
     }
 }

+ 2 - 0
src/NTMinerServer/Core/Mq/WsServerNodeMqBodyUtil.cs

@@ -2,11 +2,13 @@
 
 namespace NTMiner.Core.Mq {
     public static class WsServerNodeMqBodyUtil {
+        #region WsServerNodeAddress
         public static byte[] GetWsServerNodeAddressMqSendBody(string wsServerNodeAddress) {
             return Encoding.UTF8.GetBytes(wsServerNodeAddress);
         }
         public static string GetWsServerNodeAddressMqReceiveBody(byte[] body) {
             return Encoding.UTF8.GetString(body);
         }
+        #endregion
     }
 }

+ 1 - 0
src/NTMinerServer/NTMinerServer.csproj

@@ -95,6 +95,7 @@
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
+    <None Include="Core\Mq\README.md" />
     <None Include="packages.config" />
     <None Include="README.md" />
   </ItemGroup>

+ 1 - 1
src/WebApiServer/Core/Mq/MqMessagePaths/MinerClientMqMessagePath.cs

@@ -22,7 +22,7 @@ namespace NTMiner.Core.Mq.MqMessagePaths {
             switch (ea.RoutingKey) {
                 // 上报的算力放在这里消费,因为只有WebApiServer消费该类型的消息,WsServer不消费该类型的消息
                 case MqKeyword.SpeedRoutingKey: {
-                        SpeedData speedData = OperationMqBodyUtil.GetSpeedDataMqReceiveBody(ea.Body);
+                        SpeedData speedData = OperationMqBodyUtil.GetSpeedMqReceiveBody(ea.Body);
                         DateTime timestamp = Timestamp.FromTimestamp(ea.BasicProperties.Timestamp.UnixTime);
                         string appId = ea.BasicProperties.AppId;
                         string minerIp = ea.BasicProperties.ReadHeaderString(MqKeyword.MinerIpHeaderName);

+ 2 - 1
src/WsServer/Core/Mq/Senders/IOperationMqSender.cs

@@ -6,6 +6,7 @@ using System.Collections.Generic;
 
 namespace NTMiner.Core.Mq.Senders {
     // Mq消息上带上loginName的意义是为了验证权限,确保用户只能操作自己的矿机不能操作别人的矿机。
+    // TODO:以后根据业务和性能情况再决定是否消减Mq消息量,消减的方法是让各WsServer、WebApiServer互相建立tcp连接根据clientId计算目标分片槽直接转发给目标节点从而不用通过Mq转发。
     public interface IOperationMqSender {
         void SendGetConsoleOutLines(string loginName, Guid clientId, long afterTime);
         void SendConsoleOutLines(string loginName, Guid clientId, List<ConsoleOutLine> datas);
@@ -24,7 +25,7 @@ namespace NTMiner.Core.Mq.Senders {
         void SendOperationReceived(string loginName, Guid clientId);
 
         void SendGetSpeed(string loginName, List<Guid> clientIds);
-        void SendSpeedData(string loginName, SpeedData speedData, string minerIp);
+        void SendSpeed(string loginName, SpeedData speedData, string minerIp);
 
         void SendEnableRemoteDesktop(string loginName, Guid clientId);
         void SendBlockWAU(string loginName, Guid clientId);

+ 2 - 2
src/WsServer/Core/Mq/Senders/Impl/OperationMqSender.cs

@@ -145,7 +145,7 @@ namespace NTMiner.Core.Mq.Senders.Impl {
                 body: OperationMqBodyUtil.GetGetSpeedMqSendBody(clientIds));
         }
 
-        public void SendSpeedData(string loginName, SpeedData speedData, string minerIp) {
+        public void SendSpeed(string loginName, SpeedData speedData, string minerIp) {
             if (string.IsNullOrEmpty(loginName) || speedData == null || string.IsNullOrEmpty(minerIp)) {
                 return;
             }
@@ -155,7 +155,7 @@ namespace NTMiner.Core.Mq.Senders.Impl {
                 exchange: MqKeyword.NTMinerExchange,
                 routingKey: MqKeyword.SpeedRoutingKey,
                 basicProperties: basicProperties,
-                body: OperationMqBodyUtil.GetSpeedDataMqSendBody(speedData));
+                body: OperationMqBodyUtil.GetSpeedMqSendBody(speedData));
         }
 
         public void SendEnableRemoteDesktop(string loginName, Guid clientId) {

+ 1 - 1
src/WsServer/MinerClientWsMessageHandler.cs

@@ -53,7 +53,7 @@ namespace NTMiner {
             {WsMessage.Speed,
                 (wsBehavior, loginName, clientId, message) => {
                     if (message.TryGetData(out SpeedData speedData)) {
-                        WsRoot.OperationMqSender.SendSpeedData(loginName, speedData, wsBehavior.Context.UserEndPoint.ToString());
+                        WsRoot.OperationMqSender.SendSpeed(loginName, speedData, wsBehavior.Context.UserEndPoint.ToString());
                     }
                 }
             },