Bläddra i källkod

增加一个GMTools协议库,提供给运营开发工具使用

tanghai 12 år sedan
förälder
incheckning
cfe47a2bdd

+ 1 - 0
.gitignore

@@ -8,6 +8,7 @@
 *.user
 
 .settings/
+.localhistory/
 Dbg/
 Opt/
 

+ 96 - 0
CSharp/App/BossMonit/BossMonit.cs

@@ -0,0 +1,96 @@
+
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Net.Sockets;
+using System.Runtime.InteropServices;
+using ProtoBuf;
+
+// 这个库要用到protobuf-net库,用Nuget安装即可
+
+namespace BossMonit
+{
+	static class InternalApi
+	{
+		[DllImport("internal.dll")]
+		public static extern ulong hash_string(string data, int size);
+	};
+
+	public static class ProtobufHelper
+	{
+		public static byte[] ToBytes<T>(T message)
+		{
+			var ms = new MemoryStream();
+			Serializer.Serialize(ms, message);
+			return ms.ToArray();
+		}
+
+		public static T FromBytes<T>(byte[] bytes)
+		{
+			var ms = new MemoryStream(bytes, 0, bytes.Length);
+			return Serializer.Deserialize<T>(ms);
+		}
+
+		public static T FromBytes<T>(byte[] bytes, int index, int length)
+		{
+			var ms = new MemoryStream(bytes, index, length);
+			return Serializer.Deserialize<T>(ms);
+		}
+	}
+
+	public class BossMonit: IDisposable
+	{
+		private readonly TcpClient tcpClient;
+
+		public BossMonit(string host, ushort port)
+		{
+			this.tcpClient = new TcpClient();
+			this.tcpClient.Connect(host, port);
+		}
+
+		public GmResult GmControl(GmRequest request)
+		{
+			var stream = tcpClient.GetStream();
+
+			// 发送
+			// magic_num
+			stream.WriteByte(0xAA);
+
+			// call_guid, 同步调用这个字段随意设置
+			var bytes = BitConverter.GetBytes((ulong) 0);
+			stream.Write(bytes, 0, bytes.Length);
+
+			// method_id
+			const string method_full_name = "boss.GmControl";
+			ulong method_id = InternalApi.hash_string(method_full_name, method_full_name.Length);
+			bytes = BitConverter.GetBytes(method_id);
+
+			var requestBytes = ProtobufHelper.ToBytes(request);
+
+			// size
+			bytes = BitConverter.GetBytes(requestBytes.Length);
+			stream.Write(bytes, 0, bytes.Length);
+
+			// request content
+			stream.Write(requestBytes, 0, requestBytes.Length);
+
+			// 接收
+			var recvBuffer = new byte[21];
+			stream.Read(recvBuffer, 0, recvBuffer.Length);
+
+			Debug.Assert(recvBuffer[0] == 0xAA);
+			uint return_size = BitConverter.ToUInt32(recvBuffer, 1 + 8 + 8);
+
+			recvBuffer = new byte[return_size];
+			stream.Read(recvBuffer, 0, recvBuffer.Length);
+
+			var response = ProtobufHelper.FromBytes<GmResult>(recvBuffer);
+			return response;
+		}
+
+		public void Dispose()
+		{
+			tcpClient.Close();
+		}
+	}
+}

+ 64 - 0
CSharp/App/BossMonit/BossMonit.csproj

@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>BossMonit</RootNamespace>
+    <AssemblyName>BossMonit</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+    <RestorePackages>true</RestorePackages>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>..\..\Bin\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="protobuf-net">
+      <HintPath>..\..\packages\protobuf-net.2.0.0.621\lib\net40\protobuf-net.dll</HintPath>
+    </Reference>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Runtime.Serialization" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="BossMonit.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Protocol.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="Packages.config" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 4 - 0
CSharp/App/BossMonit/Packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="protobuf-net" version="2.0.0.621" targetFramework="net45" />
+</packages>

+ 36 - 0
CSharp/App/BossMonit/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// 有关程序集的常规信息通过以下
+// 特性集控制。更改这些特性值可修改
+// 与程序集关联的信息。
+[assembly: AssemblyTitle("BossMonit")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("BossMonit")]
+[assembly: AssemblyCopyright("Copyright ©  2013")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// 将 ComVisible 设置为 false 使此程序集中的类型
+// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
+// 则将该类型上的 ComVisible 特性设置为 true。
+[assembly: ComVisible(false)]
+
+// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
+[assembly: Guid("8c6d59b7-1d93-4d10-bf66-fc8bc0af2adb")]
+
+// 程序集的版本信息由下面四个值组成:
+//
+//      主版本
+//      次版本 
+//      生成号
+//      修订号
+//
+// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
+// 方法是按如下所示使用“*”:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 45 - 0
CSharp/App/BossMonit/Protocol.cs

@@ -0,0 +1,45 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.Serialization;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace BossMonit
+{
+	[DataContract]
+	public class KVItem
+	{
+		[DataMember(Order = 1, IsRequired = true)]
+		public byte[] Key { get; set; }
+
+		[DataMember(Order = 2, IsRequired = false)]
+		public byte[] Value { get; set; }
+	}
+
+	[DataContract]
+	public class GmRequest
+	{
+		[DataMember(Order = 1, IsRequired = true)]
+		public byte[] Cmd { get; set; }
+
+		[DataMember(Order = 2, IsRequired = false)]
+		public byte[] Param { get; set; }
+
+		[DataMember(Order = 3)]
+		public List<KVItem> ParamList { get; set; }
+	}
+
+	[DataContract]
+	public class GmResult
+	{
+		[DataMember(Order = 1, IsRequired = true)]
+		public int Result { get; set; }
+
+		[DataMember(Order = 2, IsRequired = false)]
+		public byte[] Data { get; set; }
+
+		[DataMember(Order = 3)]
+		public KVItem DataList { get; set; }
+	}
+}

+ 15 - 0
CSharp/CSharp.sln

@@ -66,6 +66,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RealmClient", "Client\Realm
 		{15B3E0D2-6217-493A-A690-158C497F5318} = {15B3E0D2-6217-493A-A690-158C497F5318}
 	EndProjectSection
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BossMonit", "App\BossMonit\BossMonit.csproj", "{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -310,6 +312,18 @@ Global
 		{3EF3AE01-5EF3-4C5C-AB49-87BB9D06C007}.Release|Mixed Platforms.Build.0 = Release|Any CPU
 		{3EF3AE01-5EF3-4C5C-AB49-87BB9D06C007}.Release|Win32.ActiveCfg = Release|Any CPU
 		{3EF3AE01-5EF3-4C5C-AB49-87BB9D06C007}.Release|x86.ActiveCfg = Release|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Debug|Win32.ActiveCfg = Debug|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Debug|x86.ActiveCfg = Debug|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Release|Any CPU.Build.0 = Release|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Release|Win32.ActiveCfg = Release|Any CPU
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2}.Release|x86.ActiveCfg = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -321,6 +335,7 @@ Global
 		{8650195A-7904-4EBC-9D81-B392A7E9B9B3} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
 		{999910D3-4E7D-45B1-BD2C-47289CD4D1AB} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
 		{6C16281F-5550-4024-9504-295C63889E4F} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
+		{E8C290C1-D4FE-44A5-81A2-507706CF7DF2} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
 		{3A98B35C-DEA8-489C-9203-263FFB6B065D} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{72E16572-FC1F-4A9E-BC96-035417239298} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{24233CD5-A5DF-484B-A482-B79CB7A0D9CB} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}