diff --git a/NetLib/HaoYueNet.ClientNetwork/BaseData.cs b/NetLib/HaoYueNet.ClientNetwork/BaseData.cs new file mode 100644 index 0000000..bf2e832 --- /dev/null +++ b/NetLib/HaoYueNet.ClientNetwork/BaseData.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HaoYueNet.ClientNetwork +{ + public static class BaseData + { + public static class HunterNet_S2C + { + public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] data) + { + byte[] newdata = new byte[2 + 2 + data.Length]; + BitConverter.GetBytes(CmdID).CopyTo(newdata, 0); + BitConverter.GetBytes(Error).CopyTo(newdata, 2); + Array.Copy(data, 0, newdata, 4, data.Length); + return newdata; + } + + public static void AnalysisPkgData(byte[] srcdata,out UInt16 CmdID,out UInt16 Error,out byte[] data) + { + data = new byte[srcdata.Length - 2 - 2]; + CmdID = BitConverter.ToUInt16(srcdata, 0); + Error = BitConverter.ToUInt16(srcdata, 2); + Array.Copy(srcdata, 4, data, 0, data.Length); + } + } + + public static class HunterNet_C2S + { + public static byte[] CreatePkgData(UInt16 CmdID, byte[] data) + { + byte[] newdata = new byte[2 + data.Length]; + BitConverter.GetBytes(CmdID).CopyTo(newdata, 0); + Array.Copy(data, 0, newdata, 2, data.Length); + return newdata; + } + + public static void AnalysisPkgData(byte[] srcdata, out UInt16 CmdID, out byte[] data) + { + data = new byte[srcdata.Length - 2]; + CmdID = BitConverter.ToUInt16(srcdata, 0); + Array.Copy(srcdata, 2, data, 0, data.Length); + } + } + } +} diff --git a/NetLib/HaoYueNet.ClientNetwork/HaoYueNet.ClientNetwork.csproj b/NetLib/HaoYueNet.ClientNetwork/HaoYueNet.ClientNetwork.csproj index 2238ead..cfadb03 100644 --- a/NetLib/HaoYueNet.ClientNetwork/HaoYueNet.ClientNetwork.csproj +++ b/NetLib/HaoYueNet.ClientNetwork/HaoYueNet.ClientNetwork.csproj @@ -6,10 +6,4 @@ enable - - - ..\Google.Protobuf.dll - - - diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs index 5876d2b..5eea8bc 100644 --- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs +++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs @@ -1,7 +1,7 @@ -using Google.Protobuf; -using HunterProtobufCore; +//using HunterProtobufCore; using System.Net; using System.Net.Sockets; +using static HaoYueNet.ClientNetwork.BaseData; namespace HaoYueNet.ClientNetwork { @@ -168,10 +168,13 @@ namespace HaoYueNet.ClientNetwork public void SendToServer(int CMDID,byte[] data) { //LogOut("准备数据 CMDID=> "+CMDID); + /* HunterNet_C2S _c2sdata = new HunterNet_C2S(); _c2sdata.HunterNetCoreCmdID = CMDID; _c2sdata.HunterNetCoreData = ByteString.CopyFrom(data); byte[] _finaldata = Serizlize(_c2sdata); + */ + byte[] _finaldata = HunterNet_C2S.CreatePkgData((ushort)CMDID, data); SendToSocket(_finaldata); } @@ -243,10 +246,15 @@ namespace HaoYueNet.ClientNetwork //LogOut("收到心跳包"); return; } - + + /* HunterNet_S2C _c2s = DeSerizlize(data); OnReceiveData(_c2s.HunterNetCoreCmdID, _c2s.HunterNetCoreERRORCode, _c2s.HunterNetCoreData.ToArray()); + */ + + HunterNet_S2C.AnalysisPkgData(data, out ushort CmdID, out ushort Error, out byte[] resultdata); + OnReceiveData(CmdID, Error, resultdata); } @@ -352,18 +360,6 @@ namespace HaoYueNet.ClientNetwork } } - public static byte[] Serizlize(IMessage msg) - { - return msg.ToByteArray(); - } - - public static T DeSerizlize(byte[] bytes) - { - var msgType = typeof(T); - object msg = Activator.CreateInstance(msgType); - ((IMessage)msg).MergeFrom(bytes); - return (T)msg; - } public void LogOut(string Msg) { diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs index ac50cc4..60734f8 100644 --- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs +++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs @@ -1,7 +1,6 @@ -using Google.Protobuf; -using HunterProtobufCore; -using System.Net; +using System.Net; using System.Net.Sockets; +using static HaoYueNet.ClientNetwork.BaseData; namespace HaoYueNet.ClientNetwork { @@ -165,11 +164,13 @@ namespace HaoYueNet.ClientNetwork public void SendToSocket(int CMDID, int ERRCODE, byte[] data) { //LogOut("准备数据 CMDID=> "+CMDID); - HunterNet_S2C _s2sdata = new HunterNet_S2C(); + /*HunterNet_S2C _s2sdata = new HunterNet_S2C(); _s2sdata.HunterNetCoreCmdID = CMDID; _s2sdata.HunterNetCoreERRORCode = ERRCODE; _s2sdata.HunterNetCoreData = ByteString.CopyFrom(data); - byte[] _finaldata = Serizlize(_s2sdata); + byte[] _finaldata = Serizlize(_s2sdata);*/ + + byte[] _finaldata = HunterNet_S2C.CreatePkgData((ushort)CMDID, (ushort)ERRCODE, data); SendToSocket(_finaldata); } @@ -245,10 +246,13 @@ namespace HaoYueNet.ClientNetwork //LogOut("收到心跳包"); return; } - + + /* HunterNet_S2C _c2s = DeSerizlize(data); - OnDataCallBack(_c2s.HunterNetCoreCmdID, _c2s.HunterNetCoreERRORCode, _c2s.HunterNetCoreData.ToArray()); + */ + HunterNet_S2C.AnalysisPkgData(data, out ushort CmdID, out ushort Error, out byte[] resultdata); + OnDataCallBack(CmdID, Error, resultdata); } MemoryStream memoryStream = new MemoryStream();//开辟一个内存流 @@ -354,19 +358,6 @@ namespace HaoYueNet.ClientNetwork } } - public static byte[] Serizlize(IMessage msg) - { - return msg.ToByteArray(); - } - - public static T DeSerizlize(byte[] bytes) - { - var msgType = typeof(T); - object msg = Activator.CreateInstance(msgType); - ((IMessage)msg).MergeFrom(bytes); - return (T)msg; - } - public void LogOut(string Msg) { //Console.WriteLine(Msg); diff --git a/NetLib/HaoYueNet.ClientNetwork/ProtobufHunterNetCore.cs b/NetLib/HaoYueNet.ClientNetwork/ProtobufHunterNetCore.cs deleted file mode 100644 index 0b401bd..0000000 --- a/NetLib/HaoYueNet.ClientNetwork/ProtobufHunterNetCore.cs +++ /dev/null @@ -1,505 +0,0 @@ -// -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: protobuf_HunterNetCore.proto -// -#pragma warning disable 1591, 0612, 3021 -#region Designer generated code - -using pb = global::Google.Protobuf; -using pbr = global::Google.Protobuf.Reflection; -namespace HunterProtobufCore -{ - - /// Holder for reflection information generated from protobuf_HunterNetCore.proto - public static partial class ProtobufHunterNetCoreReflection { - - #region Descriptor - /// File descriptor for protobuf_HunterNetCore.proto - public static pbr::FileDescriptor Descriptor { - get { return descriptor; } - } - private static pbr::FileDescriptor descriptor; - - static ProtobufHunterNetCoreReflection() { - byte[] descriptorData = global::System.Convert.FromBase64String( - string.Concat( - "Chxwcm90b2J1Zl9IdW50ZXJOZXRDb3JlLnByb3RvEhJIdW50ZXJQcm90b2J1", - "ZkNvcmUiSAoNSHVudGVyTmV0X0MyUxIbChNIdW50ZXJOZXRDb3JlX0NtZElE", - "GAEgASgFEhoKEkh1bnRlck5ldENvcmVfRGF0YRgCIAEoDCJpCg1IdW50ZXJO", - "ZXRfUzJDEhsKE0h1bnRlck5ldENvcmVfQ21kSUQYASABKAUSHwoXSHVudGVy", - "TmV0Q29yZV9FUlJPUkNvZGUYAiABKAUSGgoSSHVudGVyTmV0Q29yZV9EYXRh", - "GAMgASgMQgJIAWIGcHJvdG8z")); - descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, - new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::HunterProtobufCore.HunterNet_C2S), global::HunterProtobufCore.HunterNet_C2S.Parser, new[]{ "HunterNetCoreCmdID", "HunterNetCoreData" }, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::HunterProtobufCore.HunterNet_S2C), global::HunterProtobufCore.HunterNet_S2C.Parser, new[]{ "HunterNetCoreCmdID", "HunterNetCoreERRORCode", "HunterNetCoreData" }, null, null, null, null) - })); - } - #endregion - - } - #region Messages - /// - ///上行 - /// - public sealed partial class HunterNet_C2S : pb::IMessage - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - , pb::IBufferMessage - #endif - { - private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new HunterNet_C2S()); - private pb::UnknownFieldSet _unknownFields; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pb::MessageParser Parser { get { return _parser; } } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pbr::MessageDescriptor Descriptor { - get { return global::HunterProtobufCore.ProtobufHunterNetCoreReflection.Descriptor.MessageTypes[0]; } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - pbr::MessageDescriptor pb::IMessage.Descriptor { - get { return Descriptor; } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_C2S() { - OnConstruction(); - } - - partial void OnConstruction(); - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_C2S(HunterNet_C2S other) : this() { - hunterNetCoreCmdID_ = other.hunterNetCoreCmdID_; - hunterNetCoreData_ = other.hunterNetCoreData_; - _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_C2S Clone() { - return new HunterNet_C2S(this); - } - - /// Field number for the "HunterNetCore_CmdID" field. - public const int HunterNetCoreCmdIDFieldNumber = 1; - private int hunterNetCoreCmdID_; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int HunterNetCoreCmdID { - get { return hunterNetCoreCmdID_; } - set { - hunterNetCoreCmdID_ = value; - } - } - - /// Field number for the "HunterNetCore_Data" field. - public const int HunterNetCoreDataFieldNumber = 2; - private pb::ByteString hunterNetCoreData_ = pb::ByteString.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public pb::ByteString HunterNetCoreData { - get { return hunterNetCoreData_; } - set { - hunterNetCoreData_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); - } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override bool Equals(object other) { - return Equals(other as HunterNet_C2S); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public bool Equals(HunterNet_C2S other) { - if (ReferenceEquals(other, null)) { - return false; - } - if (ReferenceEquals(other, this)) { - return true; - } - if (HunterNetCoreCmdID != other.HunterNetCoreCmdID) return false; - if (HunterNetCoreData != other.HunterNetCoreData) return false; - return Equals(_unknownFields, other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override int GetHashCode() { - int hash = 1; - if (HunterNetCoreCmdID != 0) hash ^= HunterNetCoreCmdID.GetHashCode(); - if (HunterNetCoreData.Length != 0) hash ^= HunterNetCoreData.GetHashCode(); - if (_unknownFields != null) { - hash ^= _unknownFields.GetHashCode(); - } - return hash; - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override string ToString() { - return pb::JsonFormatter.ToDiagnosticString(this); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void WriteTo(pb::CodedOutputStream output) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - output.WriteRawMessage(this); - #else - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(18); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(output); - } - #endif - } - - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) { - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(18); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(ref output); - } - } - #endif - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int CalculateSize() { - int size = 0; - if (HunterNetCoreCmdID != 0) { - size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreCmdID); - } - if (HunterNetCoreData.Length != 0) { - size += 1 + pb::CodedOutputStream.ComputeBytesSize(HunterNetCoreData); - } - if (_unknownFields != null) { - size += _unknownFields.CalculateSize(); - } - return size; - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(HunterNet_C2S other) { - if (other == null) { - return; - } - if (other.HunterNetCoreCmdID != 0) { - HunterNetCoreCmdID = other.HunterNetCoreCmdID; - } - if (other.HunterNetCoreData.Length != 0) { - HunterNetCoreData = other.HunterNetCoreData; - } - _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(pb::CodedInputStream input) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - input.ReadRawMessage(this); - #else - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 18: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - #endif - } - - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) { - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 18: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - } - #endif - - } - - /// - ///下行 - /// - public sealed partial class HunterNet_S2C : pb::IMessage - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - , pb::IBufferMessage - #endif - { - private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new HunterNet_S2C()); - private pb::UnknownFieldSet _unknownFields; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pb::MessageParser Parser { get { return _parser; } } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pbr::MessageDescriptor Descriptor { - get { return global::HunterProtobufCore.ProtobufHunterNetCoreReflection.Descriptor.MessageTypes[1]; } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - pbr::MessageDescriptor pb::IMessage.Descriptor { - get { return Descriptor; } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_S2C() { - OnConstruction(); - } - - partial void OnConstruction(); - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_S2C(HunterNet_S2C other) : this() { - hunterNetCoreCmdID_ = other.hunterNetCoreCmdID_; - hunterNetCoreERRORCode_ = other.hunterNetCoreERRORCode_; - hunterNetCoreData_ = other.hunterNetCoreData_; - _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_S2C Clone() { - return new HunterNet_S2C(this); - } - - /// Field number for the "HunterNetCore_CmdID" field. - public const int HunterNetCoreCmdIDFieldNumber = 1; - private int hunterNetCoreCmdID_; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int HunterNetCoreCmdID { - get { return hunterNetCoreCmdID_; } - set { - hunterNetCoreCmdID_ = value; - } - } - - /// Field number for the "HunterNetCore_ERRORCode" field. - public const int HunterNetCoreERRORCodeFieldNumber = 2; - private int hunterNetCoreERRORCode_; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int HunterNetCoreERRORCode { - get { return hunterNetCoreERRORCode_; } - set { - hunterNetCoreERRORCode_ = value; - } - } - - /// Field number for the "HunterNetCore_Data" field. - public const int HunterNetCoreDataFieldNumber = 3; - private pb::ByteString hunterNetCoreData_ = pb::ByteString.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public pb::ByteString HunterNetCoreData { - get { return hunterNetCoreData_; } - set { - hunterNetCoreData_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); - } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override bool Equals(object other) { - return Equals(other as HunterNet_S2C); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public bool Equals(HunterNet_S2C other) { - if (ReferenceEquals(other, null)) { - return false; - } - if (ReferenceEquals(other, this)) { - return true; - } - if (HunterNetCoreCmdID != other.HunterNetCoreCmdID) return false; - if (HunterNetCoreERRORCode != other.HunterNetCoreERRORCode) return false; - if (HunterNetCoreData != other.HunterNetCoreData) return false; - return Equals(_unknownFields, other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override int GetHashCode() { - int hash = 1; - if (HunterNetCoreCmdID != 0) hash ^= HunterNetCoreCmdID.GetHashCode(); - if (HunterNetCoreERRORCode != 0) hash ^= HunterNetCoreERRORCode.GetHashCode(); - if (HunterNetCoreData.Length != 0) hash ^= HunterNetCoreData.GetHashCode(); - if (_unknownFields != null) { - hash ^= _unknownFields.GetHashCode(); - } - return hash; - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override string ToString() { - return pb::JsonFormatter.ToDiagnosticString(this); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void WriteTo(pb::CodedOutputStream output) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - output.WriteRawMessage(this); - #else - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreERRORCode != 0) { - output.WriteRawTag(16); - output.WriteInt32(HunterNetCoreERRORCode); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(26); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(output); - } - #endif - } - - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) { - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreERRORCode != 0) { - output.WriteRawTag(16); - output.WriteInt32(HunterNetCoreERRORCode); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(26); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(ref output); - } - } - #endif - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int CalculateSize() { - int size = 0; - if (HunterNetCoreCmdID != 0) { - size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreCmdID); - } - if (HunterNetCoreERRORCode != 0) { - size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreERRORCode); - } - if (HunterNetCoreData.Length != 0) { - size += 1 + pb::CodedOutputStream.ComputeBytesSize(HunterNetCoreData); - } - if (_unknownFields != null) { - size += _unknownFields.CalculateSize(); - } - return size; - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(HunterNet_S2C other) { - if (other == null) { - return; - } - if (other.HunterNetCoreCmdID != 0) { - HunterNetCoreCmdID = other.HunterNetCoreCmdID; - } - if (other.HunterNetCoreERRORCode != 0) { - HunterNetCoreERRORCode = other.HunterNetCoreERRORCode; - } - if (other.HunterNetCoreData.Length != 0) { - HunterNetCoreData = other.HunterNetCoreData; - } - _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(pb::CodedInputStream input) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - input.ReadRawMessage(this); - #else - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 16: { - HunterNetCoreERRORCode = input.ReadInt32(); - break; - } - case 26: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - #endif - } - - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) { - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 16: { - HunterNetCoreERRORCode = input.ReadInt32(); - break; - } - case 26: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - } - #endif - - } - - #endregion - -} - -#endregion Designer generated code diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/BaseData.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/BaseData.cs new file mode 100644 index 0000000..dc30902 --- /dev/null +++ b/NetLib/HaoYueNet.ClientNetworkNet4x/BaseData.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HaoYueNet.ClientNetworkNet4x +{ + public static class BaseData + { + public static class HunterNet_S2C + { + public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] data) + { + byte[] newdata = new byte[2 + 2 + data.Length]; + BitConverter.GetBytes(CmdID).CopyTo(newdata, 0); + BitConverter.GetBytes(Error).CopyTo(newdata, 2); + Array.Copy(data, 0, newdata, 4, data.Length); + return newdata; + } + + public static void AnalysisPkgData(byte[] srcdata,out UInt16 CmdID,out UInt16 Error,out byte[] data) + { + data = new byte[srcdata.Length - 2 - 2]; + CmdID = BitConverter.ToUInt16(srcdata, 0); + Error = BitConverter.ToUInt16(srcdata, 2); + Array.Copy(srcdata, 4, data, 0, data.Length); + } + } + + public static class HunterNet_C2S + { + public static byte[] CreatePkgData(UInt16 CmdID, byte[] data) + { + byte[] newdata = new byte[2 + data.Length]; + BitConverter.GetBytes(CmdID).CopyTo(newdata, 0); + Array.Copy(data, 0, newdata, 2, data.Length); + return newdata; + } + + public static void AnalysisPkgData(byte[] srcdata, out UInt16 CmdID, out byte[] data) + { + data = new byte[srcdata.Length - 2]; + CmdID = BitConverter.ToUInt16(srcdata, 0); + Array.Copy(srcdata, 2, data, 0, data.Length); + } + } + } +} diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj b/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj index 394d435..70bb126 100644 --- a/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj +++ b/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj @@ -32,40 +32,18 @@ 4 - - ..\Google.Protobuf.dll - - - ..\..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll - - - - ..\..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll - - - - ..\..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll - - - ..\..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll - - - - - + - - \ No newline at end of file diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs index 6510e9e..5d7aa03 100644 --- a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs +++ b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs @@ -1,11 +1,9 @@ -using Google.Protobuf; -using HunterProtobufCore; -using System; +using System; using System.IO; -using System.Linq; using System.Net; using System.Net.Sockets; using System.Threading; +using static HaoYueNet.ClientNetworkNet4x.BaseData; namespace HaoYueNet.ClientNetworkNet4x { @@ -40,9 +38,11 @@ namespace HaoYueNet.ClientNetworkNet4x private System.Timers.Timer _heartTimer; - public void Init(string IP, int port, bool bBindReuseAddress = false, int bBindport = 0) - { + public static string LastConnectIP; + public static int LastConnectPort; + public bool Init(string IP, int port, bool bBindReuseAddress = false, int bBindport = 0) + { LogOut("==>初始化网络核心"); RevIndex = MaxRevIndexNum; @@ -55,10 +55,12 @@ namespace HaoYueNet.ClientNetworkNet4x IPEndPoint ipe = new IPEndPoint(IPAddress.Any, Convert.ToInt32(bBindport)); client.Bind(ipe); } - Connect(IP, port); + LastConnectIP = IP; + LastConnectPort = port; + return Connect(IP, port); } - public bool Connect(string IP, int port) + bool Connect(string IP, int port) { //带回调的 try @@ -168,10 +170,13 @@ namespace HaoYueNet.ClientNetworkNet4x public void SendToServer(int CMDID, byte[] data) { //LogOut("准备数据 CMDID=> "+CMDID); + /* HunterNet_C2S _c2sdata = new HunterNet_C2S(); _c2sdata.HunterNetCoreCmdID = CMDID; _c2sdata.HunterNetCoreData = ByteString.CopyFrom(data); byte[] _finaldata = Serizlize(_c2sdata); + */ + byte[] _finaldata = HunterNet_C2S.CreatePkgData((ushort)CMDID, data); SendToSocket(_finaldata); } @@ -244,9 +249,14 @@ namespace HaoYueNet.ClientNetworkNet4x return; } + /* HunterNet_S2C _c2s = DeSerizlize(data); OnReceiveData(_c2s.HunterNetCoreCmdID, _c2s.HunterNetCoreERRORCode, _c2s.HunterNetCoreData.ToArray()); + */ + + HunterNet_S2C.AnalysisPkgData(data, out ushort CmdID, out ushort Error, out byte[] resultdata); + OnReceiveData(CmdID, Error, resultdata); } @@ -352,18 +362,6 @@ namespace HaoYueNet.ClientNetworkNet4x } } - public static byte[] Serizlize(IMessage msg) - { - return msg.ToByteArray(); - } - - public static T DeSerizlize(byte[] bytes) - { - var msgType = typeof(T); - object msg = Activator.CreateInstance(msgType); - ((IMessage)msg).MergeFrom(bytes); - return (T)msg; - } public void LogOut(string Msg) { diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs index 182cf70..d58c7bd 100644 --- a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs +++ b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs @@ -1,385 +1,378 @@ -using Google.Protobuf; -using HunterProtobufCore; -using System; +using System; using System.IO; -using System.Linq; using System.Net; using System.Net.Sockets; using System.Threading; +using static HaoYueNet.ClientNetworkNet4x.BaseData; namespace HaoYueNet.ClientNetworkNet4x { - public class NetworkHelperP2PCore + namespace HaoYueNet.ClientNetwork { - private Socket client; - - /// - /// 心跳包数据 - /// - private byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 }; - - ////响应倒计时计数最大值 - //private static int MaxRevIndexNum = 6; - - ////发送倒计时计数最大值 - //private static int MaxSendIndexNum = 3; - - //响应倒计时计数最大值 - private static int MaxRevIndexNum = 50; - - //发送倒计时计数最大值 - private static int MaxSendIndexNum = 3; - - //响应倒计时计数 - private static int RevIndex = 0; - //发送倒计时计数 - private static int SendIndex = 0; - - //计时器间隔 - private static int TimerInterval = 3000; - - private System.Timers.Timer _heartTimer; - - public void Init(string IP, int port, bool bBindReuseAddress = false, int bBindport = 0) + public class NetworkHelperP2PCore { + private Socket client; - LogOut("==>初始化网络核心"); + /// + /// 心跳包数据 + /// + private byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 }; - RevIndex = MaxRevIndexNum; - SendIndex = MaxSendIndexNum; + ////响应倒计时计数最大值 + //private static int MaxRevIndexNum = 6; - client = new Socket(SocketType.Stream, ProtocolType.Tcp); - if (bBindReuseAddress) + ////发送倒计时计数最大值 + //private static int MaxSendIndexNum = 3; + + //响应倒计时计数最大值 + private static int MaxRevIndexNum = 50; + + //发送倒计时计数最大值 + private static int MaxSendIndexNum = 3; + + //响应倒计时计数 + private static int RevIndex = 0; + //发送倒计时计数 + private static int SendIndex = 0; + + //计时器间隔 + private static int TimerInterval = 3000; + + private System.Timers.Timer _heartTimer; + + public void Init(string IP, int port, bool bBindReuseAddress = false, int bBindport = 0) { - client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); - IPEndPoint ipe = new IPEndPoint(IPAddress.Any, Convert.ToInt32(bBindport)); - client.Bind(ipe); - } - Connect(IP, port); - } - public bool Connect(string IP, int port) - { - //带回调的 - try - { - LogOut("连接到远程IP " + IP + ":" + port); - client.Connect(IP, port); - Thread thread = new Thread(Recive); - thread.IsBackground = true; - thread.Start(client); - int localport = ((IPEndPoint)client.LocalEndPoint).Port; - LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}"); + LogOut("==>初始化网络核心"); - if (_heartTimer == null) + RevIndex = MaxRevIndexNum; + SendIndex = MaxSendIndexNum; + + client = new Socket(SocketType.Stream, ProtocolType.Tcp); + if (bBindReuseAddress) { - _heartTimer = new System.Timers.Timer(); + client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + IPEndPoint ipe = new IPEndPoint(IPAddress.Any, Convert.ToInt32(bBindport)); + client.Bind(ipe); } - _heartTimer.Interval = TimerInterval; - _heartTimer.Elapsed += CheckUpdatetimer_Elapsed; - _heartTimer.AutoReset = true; - _heartTimer.Enabled = true; - LogOut("开启心跳包检测"); - - OnConnected(true); - return true; - } - catch (Exception ex) - { - LogOut("连接失败:" + ex.ToString()); - OnConnected(false); - return false; - } - } - - ~NetworkHelperP2PCore() - { - client.Close(); - } - - private void SendToSocket(byte[] data) - { - data = SendDataWithHead(data); - try - { - SendWithIndex(data); - } - catch (Exception ex) - { - //连接断开 - OnCloseReady(); - return; - } - //LogOut("发送消息,消息长度=> "+data.Length); - } - - private void SendHeartbeat() - { - try - { - SendWithIndex(HeartbeatData); - } - catch (Exception ex) - { - //连接断开 - OnCloseReady(); - return; - } - //LogOut("发送心跳包"); - } - - /// - /// 发送数据并计数 - /// - /// - private void SendWithIndex(byte[] data) - { - //增加发送计数 - SendIndex = MaxSendIndexNum; - //发送数据 - client.Send(data); - } - - //拼接头长度 - private byte[] SendDataWithHead(byte[] message) - { - - MemoryStream memoryStream = new MemoryStream();//创建一个内存流 - - byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度 - - memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流 - - memoryStream.Write(message, 0, message.Length);//将消息体写入内存流 - - byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组 - - memoryStream.Close();//关闭内存 - memoryStream.Dispose();//释放资源 - - return HeadAndBody; - } - - /// - /// 供外部调用 发送消息 - /// - /// - /// - /// - public void SendToSocket(int CMDID, int ERRCODE, byte[] data) - { - //LogOut("准备数据 CMDID=> "+CMDID); - HunterNet_S2C _s2sdata = new HunterNet_S2C(); - _s2sdata.HunterNetCoreCmdID = CMDID; - _s2sdata.HunterNetCoreERRORCode = ERRCODE; - _s2sdata.HunterNetCoreData = ByteString.CopyFrom(data); - byte[] _finaldata = Serizlize(_s2sdata); - SendToSocket(_finaldata); - } - - public delegate void OnDataCallBack_Data(int CMDID, int ERRCODE, byte[] data); - - public event OnDataCallBack_Data OnDataCallBack; - - public delegate void delegate_NoData(); - - public delegate void delegate_Bool(bool IsConnected); - - public event delegate_NoData OnClose; - - public event delegate_Bool OnConnected; - - public delegate void delegate_str(string Msg); - - /// - /// 网络库调试日志输出 - /// - public event delegate_str OnLogOut; - - ///// - ///// 用于调用者回调的虚函数 - ///// - ///// - //public virtual void DataCallBack(int CMDID,int ERRCODE,byte[] data) - //{ - - //} - - ///// - ///// 断开连接 - ///// - ///// - //public virtual void OnClose() - //{ - - //} - - /// - /// 做好处理的连接管理 - /// - private void OnCloseReady() - { - LogOut("关闭心跳包计数"); - _heartTimer.Enabled = false; - _heartTimer.Elapsed -= CheckUpdatetimer_Elapsed; - LogOut("关闭连接"); - //关闭Socket连接 - client.Close(); - OnClose(); - } - - - /// - /// 主动关闭连接 - /// - public void CloseConntect() - { - OnCloseReady(); - } - - private void DataCallBackReady(byte[] data) - { - - //增加接收计数 - RevIndex = MaxRevIndexNum; - - //不处理心跳包 - if (data.Length == 1 && data[0] == 0x00) - { - //LogOut("收到心跳包"); - return; + Connect(IP, port); } - HunterNet_S2C _c2s = DeSerizlize(data); - - OnDataCallBack(_c2s.HunterNetCoreCmdID, _c2s.HunterNetCoreERRORCode, _c2s.HunterNetCoreData.ToArray()); - } - - MemoryStream memoryStream = new MemoryStream();//开辟一个内存流 - private void Recive(object o) - { - var client = o as Socket; - //MemoryStream memoryStream = new MemoryStream();//开辟一个内存流 - - while (true) + public bool Connect(string IP, int port) { - byte[] buffer = new byte[1024 * 1024 * 2]; - int effective = 0; + //带回调的 try { - effective = client.Receive(buffer); - if (effective == 0) + LogOut("连接到远程IP " + IP + ":" + port); + client.Connect(IP, port); + Thread thread = new Thread(Recive); + thread.IsBackground = true; + thread.Start(client); + int localport = ((IPEndPoint)client.LocalEndPoint).Port; + LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}"); + + if (_heartTimer == null) { - continue; + _heartTimer = new System.Timers.Timer(); } + _heartTimer.Interval = TimerInterval; + _heartTimer.Elapsed += CheckUpdatetimer_Elapsed; + _heartTimer.AutoReset = true; + _heartTimer.Enabled = true; + LogOut("开启心跳包检测"); + + OnConnected(true); + return true; } catch (Exception ex) { - //远程主机强迫关闭了一个现有的连接 + LogOut("连接失败:" + ex.ToString()); + OnConnected(false); + return false; + } + } + + ~NetworkHelperP2PCore() + { + client.Close(); + } + + private void SendToSocket(byte[] data) + { + data = SendDataWithHead(data); + try + { + SendWithIndex(data); + } + catch (Exception ex) + { + //连接断开 OnCloseReady(); return; - //断开连接 + } + //LogOut("发送消息,消息长度=> "+data.Length); + } + + private void SendHeartbeat() + { + try + { + SendWithIndex(HeartbeatData); + } + catch (Exception ex) + { + //连接断开 + OnCloseReady(); + return; + } + //LogOut("发送心跳包"); + } + + /// + /// 发送数据并计数 + /// + /// + private void SendWithIndex(byte[] data) + { + //增加发送计数 + SendIndex = MaxSendIndexNum; + //发送数据 + client.Send(data); + } + + //拼接头长度 + private byte[] SendDataWithHead(byte[] message) + { + + MemoryStream memoryStream = new MemoryStream();//创建一个内存流 + + byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度 + + memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流 + + memoryStream.Write(message, 0, message.Length);//将消息体写入内存流 + + byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组 + + memoryStream.Close();//关闭内存 + memoryStream.Dispose();//释放资源 + + return HeadAndBody; + } + + /// + /// 供外部调用 发送消息 + /// + /// + /// + /// + public void SendToSocket(int CMDID, int ERRCODE, byte[] data) + { + //LogOut("准备数据 CMDID=> "+CMDID); + /*HunterNet_S2C _s2sdata = new HunterNet_S2C(); + _s2sdata.HunterNetCoreCmdID = CMDID; + _s2sdata.HunterNetCoreERRORCode = ERRCODE; + _s2sdata.HunterNetCoreData = ByteString.CopyFrom(data); + byte[] _finaldata = Serizlize(_s2sdata);*/ + + byte[] _finaldata = HunterNet_S2C.CreatePkgData((ushort)CMDID, (ushort)ERRCODE, data); + SendToSocket(_finaldata); + } + + public delegate void OnDataCallBack_Data(int CMDID, int ERRCODE, byte[] data); + + public event OnDataCallBack_Data OnDataCallBack; + + public delegate void delegate_NoData(); + + public delegate void delegate_Bool(bool IsConnected); + + public event delegate_NoData OnClose; + + public event delegate_Bool OnConnected; + + public delegate void delegate_str(string Msg); + + /// + /// 网络库调试日志输出 + /// + public event delegate_str OnLogOut; + + ///// + ///// 用于调用者回调的虚函数 + ///// + ///// + //public virtual void DataCallBack(int CMDID,int ERRCODE,byte[] data) + //{ + + //} + + ///// + ///// 断开连接 + ///// + ///// + //public virtual void OnClose() + //{ + + //} + + /// + /// 做好处理的连接管理 + /// + private void OnCloseReady() + { + LogOut("关闭心跳包计数"); + _heartTimer.Enabled = false; + _heartTimer.Elapsed -= CheckUpdatetimer_Elapsed; + LogOut("关闭连接"); + //关闭Socket连接 + client.Close(); + OnClose(); + } + + + /// + /// 主动关闭连接 + /// + public void CloseConntect() + { + OnCloseReady(); + } + + private void DataCallBackReady(byte[] data) + { + + //增加接收计数 + RevIndex = MaxRevIndexNum; + + //不处理心跳包 + if (data.Length == 1 && data[0] == 0x00) + { + //LogOut("收到心跳包"); + return; } + /* + HunterNet_S2C _c2s = DeSerizlize(data); + OnDataCallBack(_c2s.HunterNetCoreCmdID, _c2s.HunterNetCoreERRORCode, _c2s.HunterNetCoreData.ToArray()); + */ + HunterNet_S2C.AnalysisPkgData(data, out ushort CmdID, out ushort Error, out byte[] resultdata); + OnDataCallBack(CmdID, Error, resultdata); + } - memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中 - byte[] getData = memoryStream.ToArray();//将内存流中的消息体写入字节数组 - int StartIndex = 0;//设置一个读取数据的起始下标 + MemoryStream memoryStream = new MemoryStream();//开辟一个内存流 + private void Recive(object o) + { + var client = o as Socket; + //MemoryStream memoryStream = new MemoryStream();//开辟一个内存流 while (true) { - - - if (effective > 0)//如果接受到的消息不为0(不为空) + byte[] buffer = new byte[1024 * 1024 * 2]; + int effective = 0; + try { - int HeadLength = 0;//包头长度(包头+包体) - if (getData.Length - StartIndex < 4)//包头接受不完整 + effective = client.Receive(buffer); + if (effective == 0) { - HeadLength = -1; + continue; } - else - { - //如果包头接受完整 转换成int类型的数值 - HeadLength = BitConverter.ToInt32(getData, StartIndex); - } - //包头接受完整但是消息体不完整 //包头接受不完整 - //↓↓↓↓↓↓↓↓ ↓↓↓ - if (getData.Length - StartIndex < HeadLength || HeadLength == -1) - { - /* 一种清空流的方式 - memoryStream.Close();//关闭内存流 - memoryStream.Dispose();//释放内存资源 - memoryStream = new MemoryStream();//创建新的内存流 - */ + } + catch (Exception ex) + { + //远程主机强迫关闭了一个现有的连接 + OnCloseReady(); + return; + //断开连接 + } - //流复用的方式 不用重新new申请 - memoryStream.Position = 0; - memoryStream.SetLength(0); - memoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流 - break; - } - else + memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中 + byte[] getData = memoryStream.ToArray();//将内存流中的消息体写入字节数组 + int StartIndex = 0;//设置一个读取数据的起始下标 + + while (true) + { + + + if (effective > 0)//如果接受到的消息不为0(不为空) { - //把头去掉,就可以吃了,蛋白质是牛肉的六倍 - //DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray()); + int HeadLength = 0;//包头长度(包头+包体) + if (getData.Length - StartIndex < 4)//包头接受不完整 + { + HeadLength = -1; + } + else + { + //如果包头接受完整 转换成int类型的数值 + HeadLength = BitConverter.ToInt32(getData, StartIndex); + } + //包头接受完整但是消息体不完整 //包头接受不完整 + //↓↓↓↓↓↓↓↓ ↓↓↓ + if (getData.Length - StartIndex < HeadLength || HeadLength == -1) + { + /* 一种清空流的方式 + memoryStream.Close();//关闭内存流 + memoryStream.Dispose();//释放内存资源 + memoryStream = new MemoryStream();//创建新的内存流 + */ - //改为Array.Copy 提升效率 - int CoreLenght = HeadLength - 4; - byte[] retData = new byte[CoreLenght]; - Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght); - DataCallBackReady(retData); - StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部) + //流复用的方式 不用重新new申请 + memoryStream.Position = 0; + memoryStream.SetLength(0); + + memoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流 + break; + } + else + { + //把头去掉,就可以吃了,蛋白质是牛肉的六倍 + //DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray()); + + //改为Array.Copy 提升效率 + int CoreLenght = HeadLength - 4; + byte[] retData = new byte[CoreLenght]; + Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght); + DataCallBackReady(retData); + StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部) + } } } } } - } - private void CheckUpdatetimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) - { - //接收服务器数据计数 - RevIndex--; - if (RevIndex <= 0) + private void CheckUpdatetimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { - //判定掉线 - OnCloseReady(); - return; + //接收服务器数据计数 + RevIndex--; + if (RevIndex <= 0) + { + //判定掉线 + OnCloseReady(); + return; + } + + //发送计数 + SendIndex--; + if (SendIndex <= 0)//需要发送心跳包了 + { + //重置倒计时计数 + SendIndex = MaxSendIndexNum; + + SendHeartbeat(); + } } - //发送计数 - SendIndex--; - if (SendIndex <= 0)//需要发送心跳包了 + public void LogOut(string Msg) { - //重置倒计时计数 - SendIndex = MaxSendIndexNum; - - SendHeartbeat(); + //Console.WriteLine(Msg); + OnLogOut(Msg); } - } - public static byte[] Serizlize(IMessage msg) - { - return msg.ToByteArray(); - } - - public static T DeSerizlize(byte[] bytes) - { - var msgType = typeof(T); - object msg = Activator.CreateInstance(msgType); - ((IMessage)msg).MergeFrom(bytes); - return (T)msg; - } - - public void LogOut(string Msg) - { - //Console.WriteLine(Msg); - OnLogOut(Msg); - } - - public Socket GetClientSocket() - { - return client; + public Socket GetClientSocket() + { + return client; + } } } } diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/ProtobufHunterNetCore.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/ProtobufHunterNetCore.cs deleted file mode 100644 index 0b401bd..0000000 --- a/NetLib/HaoYueNet.ClientNetworkNet4x/ProtobufHunterNetCore.cs +++ /dev/null @@ -1,505 +0,0 @@ -// -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: protobuf_HunterNetCore.proto -// -#pragma warning disable 1591, 0612, 3021 -#region Designer generated code - -using pb = global::Google.Protobuf; -using pbr = global::Google.Protobuf.Reflection; -namespace HunterProtobufCore -{ - - /// Holder for reflection information generated from protobuf_HunterNetCore.proto - public static partial class ProtobufHunterNetCoreReflection { - - #region Descriptor - /// File descriptor for protobuf_HunterNetCore.proto - public static pbr::FileDescriptor Descriptor { - get { return descriptor; } - } - private static pbr::FileDescriptor descriptor; - - static ProtobufHunterNetCoreReflection() { - byte[] descriptorData = global::System.Convert.FromBase64String( - string.Concat( - "Chxwcm90b2J1Zl9IdW50ZXJOZXRDb3JlLnByb3RvEhJIdW50ZXJQcm90b2J1", - "ZkNvcmUiSAoNSHVudGVyTmV0X0MyUxIbChNIdW50ZXJOZXRDb3JlX0NtZElE", - "GAEgASgFEhoKEkh1bnRlck5ldENvcmVfRGF0YRgCIAEoDCJpCg1IdW50ZXJO", - "ZXRfUzJDEhsKE0h1bnRlck5ldENvcmVfQ21kSUQYASABKAUSHwoXSHVudGVy", - "TmV0Q29yZV9FUlJPUkNvZGUYAiABKAUSGgoSSHVudGVyTmV0Q29yZV9EYXRh", - "GAMgASgMQgJIAWIGcHJvdG8z")); - descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, - new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::HunterProtobufCore.HunterNet_C2S), global::HunterProtobufCore.HunterNet_C2S.Parser, new[]{ "HunterNetCoreCmdID", "HunterNetCoreData" }, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::HunterProtobufCore.HunterNet_S2C), global::HunterProtobufCore.HunterNet_S2C.Parser, new[]{ "HunterNetCoreCmdID", "HunterNetCoreERRORCode", "HunterNetCoreData" }, null, null, null, null) - })); - } - #endregion - - } - #region Messages - /// - ///上行 - /// - public sealed partial class HunterNet_C2S : pb::IMessage - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - , pb::IBufferMessage - #endif - { - private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new HunterNet_C2S()); - private pb::UnknownFieldSet _unknownFields; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pb::MessageParser Parser { get { return _parser; } } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pbr::MessageDescriptor Descriptor { - get { return global::HunterProtobufCore.ProtobufHunterNetCoreReflection.Descriptor.MessageTypes[0]; } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - pbr::MessageDescriptor pb::IMessage.Descriptor { - get { return Descriptor; } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_C2S() { - OnConstruction(); - } - - partial void OnConstruction(); - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_C2S(HunterNet_C2S other) : this() { - hunterNetCoreCmdID_ = other.hunterNetCoreCmdID_; - hunterNetCoreData_ = other.hunterNetCoreData_; - _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_C2S Clone() { - return new HunterNet_C2S(this); - } - - /// Field number for the "HunterNetCore_CmdID" field. - public const int HunterNetCoreCmdIDFieldNumber = 1; - private int hunterNetCoreCmdID_; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int HunterNetCoreCmdID { - get { return hunterNetCoreCmdID_; } - set { - hunterNetCoreCmdID_ = value; - } - } - - /// Field number for the "HunterNetCore_Data" field. - public const int HunterNetCoreDataFieldNumber = 2; - private pb::ByteString hunterNetCoreData_ = pb::ByteString.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public pb::ByteString HunterNetCoreData { - get { return hunterNetCoreData_; } - set { - hunterNetCoreData_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); - } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override bool Equals(object other) { - return Equals(other as HunterNet_C2S); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public bool Equals(HunterNet_C2S other) { - if (ReferenceEquals(other, null)) { - return false; - } - if (ReferenceEquals(other, this)) { - return true; - } - if (HunterNetCoreCmdID != other.HunterNetCoreCmdID) return false; - if (HunterNetCoreData != other.HunterNetCoreData) return false; - return Equals(_unknownFields, other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override int GetHashCode() { - int hash = 1; - if (HunterNetCoreCmdID != 0) hash ^= HunterNetCoreCmdID.GetHashCode(); - if (HunterNetCoreData.Length != 0) hash ^= HunterNetCoreData.GetHashCode(); - if (_unknownFields != null) { - hash ^= _unknownFields.GetHashCode(); - } - return hash; - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override string ToString() { - return pb::JsonFormatter.ToDiagnosticString(this); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void WriteTo(pb::CodedOutputStream output) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - output.WriteRawMessage(this); - #else - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(18); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(output); - } - #endif - } - - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) { - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(18); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(ref output); - } - } - #endif - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int CalculateSize() { - int size = 0; - if (HunterNetCoreCmdID != 0) { - size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreCmdID); - } - if (HunterNetCoreData.Length != 0) { - size += 1 + pb::CodedOutputStream.ComputeBytesSize(HunterNetCoreData); - } - if (_unknownFields != null) { - size += _unknownFields.CalculateSize(); - } - return size; - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(HunterNet_C2S other) { - if (other == null) { - return; - } - if (other.HunterNetCoreCmdID != 0) { - HunterNetCoreCmdID = other.HunterNetCoreCmdID; - } - if (other.HunterNetCoreData.Length != 0) { - HunterNetCoreData = other.HunterNetCoreData; - } - _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(pb::CodedInputStream input) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - input.ReadRawMessage(this); - #else - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 18: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - #endif - } - - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) { - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 18: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - } - #endif - - } - - /// - ///下行 - /// - public sealed partial class HunterNet_S2C : pb::IMessage - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - , pb::IBufferMessage - #endif - { - private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new HunterNet_S2C()); - private pb::UnknownFieldSet _unknownFields; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pb::MessageParser Parser { get { return _parser; } } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pbr::MessageDescriptor Descriptor { - get { return global::HunterProtobufCore.ProtobufHunterNetCoreReflection.Descriptor.MessageTypes[1]; } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - pbr::MessageDescriptor pb::IMessage.Descriptor { - get { return Descriptor; } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_S2C() { - OnConstruction(); - } - - partial void OnConstruction(); - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_S2C(HunterNet_S2C other) : this() { - hunterNetCoreCmdID_ = other.hunterNetCoreCmdID_; - hunterNetCoreERRORCode_ = other.hunterNetCoreERRORCode_; - hunterNetCoreData_ = other.hunterNetCoreData_; - _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_S2C Clone() { - return new HunterNet_S2C(this); - } - - /// Field number for the "HunterNetCore_CmdID" field. - public const int HunterNetCoreCmdIDFieldNumber = 1; - private int hunterNetCoreCmdID_; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int HunterNetCoreCmdID { - get { return hunterNetCoreCmdID_; } - set { - hunterNetCoreCmdID_ = value; - } - } - - /// Field number for the "HunterNetCore_ERRORCode" field. - public const int HunterNetCoreERRORCodeFieldNumber = 2; - private int hunterNetCoreERRORCode_; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int HunterNetCoreERRORCode { - get { return hunterNetCoreERRORCode_; } - set { - hunterNetCoreERRORCode_ = value; - } - } - - /// Field number for the "HunterNetCore_Data" field. - public const int HunterNetCoreDataFieldNumber = 3; - private pb::ByteString hunterNetCoreData_ = pb::ByteString.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public pb::ByteString HunterNetCoreData { - get { return hunterNetCoreData_; } - set { - hunterNetCoreData_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); - } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override bool Equals(object other) { - return Equals(other as HunterNet_S2C); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public bool Equals(HunterNet_S2C other) { - if (ReferenceEquals(other, null)) { - return false; - } - if (ReferenceEquals(other, this)) { - return true; - } - if (HunterNetCoreCmdID != other.HunterNetCoreCmdID) return false; - if (HunterNetCoreERRORCode != other.HunterNetCoreERRORCode) return false; - if (HunterNetCoreData != other.HunterNetCoreData) return false; - return Equals(_unknownFields, other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override int GetHashCode() { - int hash = 1; - if (HunterNetCoreCmdID != 0) hash ^= HunterNetCoreCmdID.GetHashCode(); - if (HunterNetCoreERRORCode != 0) hash ^= HunterNetCoreERRORCode.GetHashCode(); - if (HunterNetCoreData.Length != 0) hash ^= HunterNetCoreData.GetHashCode(); - if (_unknownFields != null) { - hash ^= _unknownFields.GetHashCode(); - } - return hash; - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override string ToString() { - return pb::JsonFormatter.ToDiagnosticString(this); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void WriteTo(pb::CodedOutputStream output) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - output.WriteRawMessage(this); - #else - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreERRORCode != 0) { - output.WriteRawTag(16); - output.WriteInt32(HunterNetCoreERRORCode); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(26); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(output); - } - #endif - } - - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) { - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreERRORCode != 0) { - output.WriteRawTag(16); - output.WriteInt32(HunterNetCoreERRORCode); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(26); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(ref output); - } - } - #endif - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int CalculateSize() { - int size = 0; - if (HunterNetCoreCmdID != 0) { - size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreCmdID); - } - if (HunterNetCoreERRORCode != 0) { - size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreERRORCode); - } - if (HunterNetCoreData.Length != 0) { - size += 1 + pb::CodedOutputStream.ComputeBytesSize(HunterNetCoreData); - } - if (_unknownFields != null) { - size += _unknownFields.CalculateSize(); - } - return size; - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(HunterNet_S2C other) { - if (other == null) { - return; - } - if (other.HunterNetCoreCmdID != 0) { - HunterNetCoreCmdID = other.HunterNetCoreCmdID; - } - if (other.HunterNetCoreERRORCode != 0) { - HunterNetCoreERRORCode = other.HunterNetCoreERRORCode; - } - if (other.HunterNetCoreData.Length != 0) { - HunterNetCoreData = other.HunterNetCoreData; - } - _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(pb::CodedInputStream input) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - input.ReadRawMessage(this); - #else - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 16: { - HunterNetCoreERRORCode = input.ReadInt32(); - break; - } - case 26: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - #endif - } - - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) { - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 16: { - HunterNetCoreERRORCode = input.ReadInt32(); - break; - } - case 26: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - } - #endif - - } - - #endregion - -} - -#endregion Designer generated code diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/packages.config b/NetLib/HaoYueNet.ClientNetworkNet4x/packages.config deleted file mode 100644 index b184c7a..0000000 --- a/NetLib/HaoYueNet.ClientNetworkNet4x/packages.config +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/NetLib/HaoYueNet.ServerNetwork/BaseData.cs b/NetLib/HaoYueNet.ServerNetwork/BaseData.cs new file mode 100644 index 0000000..a7149d8 --- /dev/null +++ b/NetLib/HaoYueNet.ServerNetwork/BaseData.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HaoYueNet.ServerNetwork +{ + public static class BaseData + { + public static class HunterNet_S2C + { + public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] data) + { + byte[] newdata = new byte[2 + 2 + data.Length]; + BitConverter.GetBytes(CmdID).CopyTo(newdata, 0); + BitConverter.GetBytes(Error).CopyTo(newdata, 2); + Array.Copy(data, 0, newdata, 4, data.Length); + return newdata; + } + + public static void AnalysisPkgData(byte[] srcdata, out UInt16 CmdID, out UInt16 Error, out byte[] data) + { + data = new byte[srcdata.Length - 2 - 2]; + CmdID = BitConverter.ToUInt16(srcdata, 0); + Error = BitConverter.ToUInt16(srcdata, 2); + Array.Copy(srcdata, 4, data, 0, data.Length); + } + } + + public static class HunterNet_C2S + { + public static byte[] CreatePkgData(UInt16 CmdID, byte[] data) + { + byte[] newdata = new byte[2 + data.Length]; + BitConverter.GetBytes(CmdID).CopyTo(newdata, 0); + Array.Copy(data, 0, newdata, 2, data.Length); + return newdata; + } + + public static void AnalysisPkgData(byte[] srcdata, out UInt16 CmdID, out byte[] data) + { + data = new byte[srcdata.Length - 2]; + CmdID = BitConverter.ToUInt16(srcdata, 0); + Array.Copy(srcdata, 2, data, 0, data.Length); + } + } + } +} diff --git a/NetLib/HaoYueNet.ServerNetwork/HaoYueNet.ServerNetwork.csproj b/NetLib/HaoYueNet.ServerNetwork/HaoYueNet.ServerNetwork.csproj index 2238ead..cfadb03 100644 --- a/NetLib/HaoYueNet.ServerNetwork/HaoYueNet.ServerNetwork.csproj +++ b/NetLib/HaoYueNet.ServerNetwork/HaoYueNet.ServerNetwork.csproj @@ -6,10 +6,4 @@ enable - - - ..\Google.Protobuf.dll - - - diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs index 130c118..f003a96 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs @@ -1,8 +1,7 @@ -using Google.Protobuf; -using HunterProtobufCore; +//using HunterProtobufCore; using System.Net; using System.Net.Sockets; - +using static HaoYueNet.ServerNetwork.BaseData; namespace HaoYueNet.ServerNetwork { @@ -646,11 +645,12 @@ namespace HaoYueNet.ServerNetwork public void SendToSocket(Socket sk, int CMDID, int ERRCODE, byte[] data) { AsyncUserToken token = GetAsyncUserTokenForSocket(sk); - HunterNet_S2C _s2cdata = new HunterNet_S2C(); + /*HunterNet_S2C _s2cdata = new HunterNet_S2C(); _s2cdata.HunterNetCoreCmdID = CMDID; _s2cdata.HunterNetCoreData = ByteString.CopyFrom(data); _s2cdata.HunterNetCoreERRORCode = ERRCODE; - byte[] _finaldata = Serizlize(_s2cdata); + byte[] _finaldata = Serizlize(_s2cdata);*/ + byte[] _finaldata = HunterNet_S2C.CreatePkgData((ushort)CMDID, (ushort)ERRCODE, data); SendWithIndex(token, _finaldata); } @@ -668,10 +668,14 @@ namespace HaoYueNet.ServerNetwork { try { - HunterNet_C2S _s2c = DeSerizlize(data); //将数据包交给后台处理,这里你也可以新开个线程来处理.加快速度. + /* + HunterNet_C2S _s2c = DeSerizlize(data); OnReceive?.Invoke(sk, (int)_s2c.HunterNetCoreCmdID, _s2c.HunterNetCoreData.ToArray()); //DataCallBack(sk, (int)_s2c.HunterNetCoreCmdID, _s2c.HunterNetCoreData.ToArray()); + */ + HunterNet_C2S.AnalysisPkgData(data, out ushort CmdID, out byte[] resultdata); + OnReceive?.Invoke(sk, CmdID, resultdata); } catch (Exception ex) { @@ -738,17 +742,5 @@ namespace HaoYueNet.ServerNetwork } #endregion - public static byte[] Serizlize(IMessage msg) - { - return msg.ToByteArray(); - } - - public static T DeSerizlize(byte[] bytes) - { - var msgType = typeof(T); - object msg = Activator.CreateInstance(msgType); - ((IMessage)msg).MergeFrom(bytes); - return (T)msg; - } } } diff --git a/NetLib/HaoYueNet.ServerNetwork/ProtobufHunterNetCore.cs b/NetLib/HaoYueNet.ServerNetwork/ProtobufHunterNetCore.cs index 97eeea6..b2904aa 100644 --- a/NetLib/HaoYueNet.ServerNetwork/ProtobufHunterNetCore.cs +++ b/NetLib/HaoYueNet.ServerNetwork/ProtobufHunterNetCore.cs @@ -1,506 +1,506 @@ -// -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: protobuf_HunterNetCore.proto -// -#pragma warning disable 1591, 0612, 3021 -#region Designer generated code +//// +//// Generated by the protocol buffer compiler. DO NOT EDIT! +//// source: protobuf_HunterNetCore.proto +//// +//#pragma warning disable 1591, 0612, 3021 +//#region Designer generated code -using pb = global::Google.Protobuf; -using pbc = global::Google.Protobuf.Collections; -using pbr = global::Google.Protobuf.Reflection; -using scg = global::System.Collections.Generic; -namespace HunterProtobufCore { +//using pb = global::Google.Protobuf; +//using pbc = global::Google.Protobuf.Collections; +//using pbr = global::Google.Protobuf.Reflection; +//using scg = global::System.Collections.Generic; +//namespace HunterProtobufCore { - /// Holder for reflection information generated from protobuf_HunterNetCore.proto - public static partial class ProtobufHunterNetCoreReflection { +// /// Holder for reflection information generated from protobuf_HunterNetCore.proto +// public static partial class ProtobufHunterNetCoreReflection { - #region Descriptor - /// File descriptor for protobuf_HunterNetCore.proto - public static pbr::FileDescriptor Descriptor { - get { return descriptor; } - } - private static pbr::FileDescriptor descriptor; +// #region Descriptor +// /// File descriptor for protobuf_HunterNetCore.proto +// public static pbr::FileDescriptor Descriptor { +// get { return descriptor; } +// } +// private static pbr::FileDescriptor descriptor; - static ProtobufHunterNetCoreReflection() { - byte[] descriptorData = global::System.Convert.FromBase64String( - string.Concat( - "Chxwcm90b2J1Zl9IdW50ZXJOZXRDb3JlLnByb3RvEhJIdW50ZXJQcm90b2J1", - "ZkNvcmUiSAoNSHVudGVyTmV0X0MyUxIbChNIdW50ZXJOZXRDb3JlX0NtZElE", - "GAEgASgFEhoKEkh1bnRlck5ldENvcmVfRGF0YRgCIAEoDCJpCg1IdW50ZXJO", - "ZXRfUzJDEhsKE0h1bnRlck5ldENvcmVfQ21kSUQYASABKAUSHwoXSHVudGVy", - "TmV0Q29yZV9FUlJPUkNvZGUYAiABKAUSGgoSSHVudGVyTmV0Q29yZV9EYXRh", - "GAMgASgMQgJIAWIGcHJvdG8z")); - descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, - new pbr::FileDescriptor[] { }, - new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::HunterProtobufCore.HunterNet_C2S), global::HunterProtobufCore.HunterNet_C2S.Parser, new[]{ "HunterNetCoreCmdID", "HunterNetCoreData" }, null, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::HunterProtobufCore.HunterNet_S2C), global::HunterProtobufCore.HunterNet_S2C.Parser, new[]{ "HunterNetCoreCmdID", "HunterNetCoreERRORCode", "HunterNetCoreData" }, null, null, null, null) - })); - } - #endregion +// static ProtobufHunterNetCoreReflection() { +// byte[] descriptorData = global::System.Convert.FromBase64String( +// string.Concat( +// "Chxwcm90b2J1Zl9IdW50ZXJOZXRDb3JlLnByb3RvEhJIdW50ZXJQcm90b2J1", +// "ZkNvcmUiSAoNSHVudGVyTmV0X0MyUxIbChNIdW50ZXJOZXRDb3JlX0NtZElE", +// "GAEgASgFEhoKEkh1bnRlck5ldENvcmVfRGF0YRgCIAEoDCJpCg1IdW50ZXJO", +// "ZXRfUzJDEhsKE0h1bnRlck5ldENvcmVfQ21kSUQYASABKAUSHwoXSHVudGVy", +// "TmV0Q29yZV9FUlJPUkNvZGUYAiABKAUSGgoSSHVudGVyTmV0Q29yZV9EYXRh", +// "GAMgASgMQgJIAWIGcHJvdG8z")); +// descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, +// new pbr::FileDescriptor[] { }, +// new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] { +// new pbr::GeneratedClrTypeInfo(typeof(global::HunterProtobufCore.HunterNet_C2S), global::HunterProtobufCore.HunterNet_C2S.Parser, new[]{ "HunterNetCoreCmdID", "HunterNetCoreData" }, null, null, null, null), +// new pbr::GeneratedClrTypeInfo(typeof(global::HunterProtobufCore.HunterNet_S2C), global::HunterProtobufCore.HunterNet_S2C.Parser, new[]{ "HunterNetCoreCmdID", "HunterNetCoreERRORCode", "HunterNetCoreData" }, null, null, null, null) +// })); +// } +// #endregion - } - #region Messages - /// - ///上行 - /// - public sealed partial class HunterNet_C2S : pb::IMessage - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - , pb::IBufferMessage - #endif - { - private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new HunterNet_C2S()); - private pb::UnknownFieldSet _unknownFields; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pb::MessageParser Parser { get { return _parser; } } +// } +// #region Messages +// /// +// ///上行 +// /// +// public sealed partial class HunterNet_C2S : pb::IMessage +// #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE +// , pb::IBufferMessage +// #endif +// { +// private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new HunterNet_C2S()); +// private pb::UnknownFieldSet _unknownFields; +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public static pb::MessageParser Parser { get { return _parser; } } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pbr::MessageDescriptor Descriptor { - get { return global::HunterProtobufCore.ProtobufHunterNetCoreReflection.Descriptor.MessageTypes[0]; } - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public static pbr::MessageDescriptor Descriptor { +// get { return global::HunterProtobufCore.ProtobufHunterNetCoreReflection.Descriptor.MessageTypes[0]; } +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - pbr::MessageDescriptor pb::IMessage.Descriptor { - get { return Descriptor; } - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// pbr::MessageDescriptor pb::IMessage.Descriptor { +// get { return Descriptor; } +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_C2S() { - OnConstruction(); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public HunterNet_C2S() { +// OnConstruction(); +// } - partial void OnConstruction(); +// partial void OnConstruction(); - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_C2S(HunterNet_C2S other) : this() { - hunterNetCoreCmdID_ = other.hunterNetCoreCmdID_; - hunterNetCoreData_ = other.hunterNetCoreData_; - _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public HunterNet_C2S(HunterNet_C2S other) : this() { +// hunterNetCoreCmdID_ = other.hunterNetCoreCmdID_; +// hunterNetCoreData_ = other.hunterNetCoreData_; +// _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_C2S Clone() { - return new HunterNet_C2S(this); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public HunterNet_C2S Clone() { +// return new HunterNet_C2S(this); +// } - /// Field number for the "HunterNetCore_CmdID" field. - public const int HunterNetCoreCmdIDFieldNumber = 1; - private int hunterNetCoreCmdID_; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int HunterNetCoreCmdID { - get { return hunterNetCoreCmdID_; } - set { - hunterNetCoreCmdID_ = value; - } - } +// /// Field number for the "HunterNetCore_CmdID" field. +// public const int HunterNetCoreCmdIDFieldNumber = 1; +// private int hunterNetCoreCmdID_; +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public int HunterNetCoreCmdID { +// get { return hunterNetCoreCmdID_; } +// set { +// hunterNetCoreCmdID_ = value; +// } +// } - /// Field number for the "HunterNetCore_Data" field. - public const int HunterNetCoreDataFieldNumber = 2; - private pb::ByteString hunterNetCoreData_ = pb::ByteString.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public pb::ByteString HunterNetCoreData { - get { return hunterNetCoreData_; } - set { - hunterNetCoreData_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); - } - } +// /// Field number for the "HunterNetCore_Data" field. +// public const int HunterNetCoreDataFieldNumber = 2; +// private pb::ByteString hunterNetCoreData_ = pb::ByteString.Empty; +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public pb::ByteString HunterNetCoreData { +// get { return hunterNetCoreData_; } +// set { +// hunterNetCoreData_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); +// } +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override bool Equals(object other) { - return Equals(other as HunterNet_C2S); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public override bool Equals(object other) { +// return Equals(other as HunterNet_C2S); +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public bool Equals(HunterNet_C2S other) { - if (ReferenceEquals(other, null)) { - return false; - } - if (ReferenceEquals(other, this)) { - return true; - } - if (HunterNetCoreCmdID != other.HunterNetCoreCmdID) return false; - if (HunterNetCoreData != other.HunterNetCoreData) return false; - return Equals(_unknownFields, other._unknownFields); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public bool Equals(HunterNet_C2S other) { +// if (ReferenceEquals(other, null)) { +// return false; +// } +// if (ReferenceEquals(other, this)) { +// return true; +// } +// if (HunterNetCoreCmdID != other.HunterNetCoreCmdID) return false; +// if (HunterNetCoreData != other.HunterNetCoreData) return false; +// return Equals(_unknownFields, other._unknownFields); +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override int GetHashCode() { - int hash = 1; - if (HunterNetCoreCmdID != 0) hash ^= HunterNetCoreCmdID.GetHashCode(); - if (HunterNetCoreData.Length != 0) hash ^= HunterNetCoreData.GetHashCode(); - if (_unknownFields != null) { - hash ^= _unknownFields.GetHashCode(); - } - return hash; - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public override int GetHashCode() { +// int hash = 1; +// if (HunterNetCoreCmdID != 0) hash ^= HunterNetCoreCmdID.GetHashCode(); +// if (HunterNetCoreData.Length != 0) hash ^= HunterNetCoreData.GetHashCode(); +// if (_unknownFields != null) { +// hash ^= _unknownFields.GetHashCode(); +// } +// return hash; +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override string ToString() { - return pb::JsonFormatter.ToDiagnosticString(this); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public override string ToString() { +// return pb::JsonFormatter.ToDiagnosticString(this); +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void WriteTo(pb::CodedOutputStream output) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - output.WriteRawMessage(this); - #else - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(18); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(output); - } - #endif - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public void WriteTo(pb::CodedOutputStream output) { +// #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE +// output.WriteRawMessage(this); +// #else +// if (HunterNetCoreCmdID != 0) { +// output.WriteRawTag(8); +// output.WriteInt32(HunterNetCoreCmdID); +// } +// if (HunterNetCoreData.Length != 0) { +// output.WriteRawTag(18); +// output.WriteBytes(HunterNetCoreData); +// } +// if (_unknownFields != null) { +// _unknownFields.WriteTo(output); +// } +// #endif +// } - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) { - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(18); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(ref output); - } - } - #endif +// #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) { +// if (HunterNetCoreCmdID != 0) { +// output.WriteRawTag(8); +// output.WriteInt32(HunterNetCoreCmdID); +// } +// if (HunterNetCoreData.Length != 0) { +// output.WriteRawTag(18); +// output.WriteBytes(HunterNetCoreData); +// } +// if (_unknownFields != null) { +// _unknownFields.WriteTo(ref output); +// } +// } +// #endif - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int CalculateSize() { - int size = 0; - if (HunterNetCoreCmdID != 0) { - size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreCmdID); - } - if (HunterNetCoreData.Length != 0) { - size += 1 + pb::CodedOutputStream.ComputeBytesSize(HunterNetCoreData); - } - if (_unknownFields != null) { - size += _unknownFields.CalculateSize(); - } - return size; - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public int CalculateSize() { +// int size = 0; +// if (HunterNetCoreCmdID != 0) { +// size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreCmdID); +// } +// if (HunterNetCoreData.Length != 0) { +// size += 1 + pb::CodedOutputStream.ComputeBytesSize(HunterNetCoreData); +// } +// if (_unknownFields != null) { +// size += _unknownFields.CalculateSize(); +// } +// return size; +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(HunterNet_C2S other) { - if (other == null) { - return; - } - if (other.HunterNetCoreCmdID != 0) { - HunterNetCoreCmdID = other.HunterNetCoreCmdID; - } - if (other.HunterNetCoreData.Length != 0) { - HunterNetCoreData = other.HunterNetCoreData; - } - _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public void MergeFrom(HunterNet_C2S other) { +// if (other == null) { +// return; +// } +// if (other.HunterNetCoreCmdID != 0) { +// HunterNetCoreCmdID = other.HunterNetCoreCmdID; +// } +// if (other.HunterNetCoreData.Length != 0) { +// HunterNetCoreData = other.HunterNetCoreData; +// } +// _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(pb::CodedInputStream input) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - input.ReadRawMessage(this); - #else - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 18: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - #endif - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public void MergeFrom(pb::CodedInputStream input) { +// #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE +// input.ReadRawMessage(this); +// #else +// uint tag; +// while ((tag = input.ReadTag()) != 0) { +// switch(tag) { +// default: +// _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); +// break; +// case 8: { +// HunterNetCoreCmdID = input.ReadInt32(); +// break; +// } +// case 18: { +// HunterNetCoreData = input.ReadBytes(); +// break; +// } +// } +// } +// #endif +// } - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) { - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 18: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - } - #endif +// #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) { +// uint tag; +// while ((tag = input.ReadTag()) != 0) { +// switch(tag) { +// default: +// _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input); +// break; +// case 8: { +// HunterNetCoreCmdID = input.ReadInt32(); +// break; +// } +// case 18: { +// HunterNetCoreData = input.ReadBytes(); +// break; +// } +// } +// } +// } +// #endif - } +// } - /// - ///下行 - /// - public sealed partial class HunterNet_S2C : pb::IMessage - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - , pb::IBufferMessage - #endif - { - private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new HunterNet_S2C()); - private pb::UnknownFieldSet _unknownFields; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pb::MessageParser Parser { get { return _parser; } } +// /// +// ///下行 +// /// +// public sealed partial class HunterNet_S2C : pb::IMessage +// #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE +// , pb::IBufferMessage +// #endif +// { +// private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new HunterNet_S2C()); +// private pb::UnknownFieldSet _unknownFields; +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public static pb::MessageParser Parser { get { return _parser; } } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pbr::MessageDescriptor Descriptor { - get { return global::HunterProtobufCore.ProtobufHunterNetCoreReflection.Descriptor.MessageTypes[1]; } - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public static pbr::MessageDescriptor Descriptor { +// get { return global::HunterProtobufCore.ProtobufHunterNetCoreReflection.Descriptor.MessageTypes[1]; } +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - pbr::MessageDescriptor pb::IMessage.Descriptor { - get { return Descriptor; } - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// pbr::MessageDescriptor pb::IMessage.Descriptor { +// get { return Descriptor; } +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_S2C() { - OnConstruction(); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public HunterNet_S2C() { +// OnConstruction(); +// } - partial void OnConstruction(); +// partial void OnConstruction(); - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_S2C(HunterNet_S2C other) : this() { - hunterNetCoreCmdID_ = other.hunterNetCoreCmdID_; - hunterNetCoreERRORCode_ = other.hunterNetCoreERRORCode_; - hunterNetCoreData_ = other.hunterNetCoreData_; - _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public HunterNet_S2C(HunterNet_S2C other) : this() { +// hunterNetCoreCmdID_ = other.hunterNetCoreCmdID_; +// hunterNetCoreERRORCode_ = other.hunterNetCoreERRORCode_; +// hunterNetCoreData_ = other.hunterNetCoreData_; +// _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public HunterNet_S2C Clone() { - return new HunterNet_S2C(this); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public HunterNet_S2C Clone() { +// return new HunterNet_S2C(this); +// } - /// Field number for the "HunterNetCore_CmdID" field. - public const int HunterNetCoreCmdIDFieldNumber = 1; - private int hunterNetCoreCmdID_; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int HunterNetCoreCmdID { - get { return hunterNetCoreCmdID_; } - set { - hunterNetCoreCmdID_ = value; - } - } +// /// Field number for the "HunterNetCore_CmdID" field. +// public const int HunterNetCoreCmdIDFieldNumber = 1; +// private int hunterNetCoreCmdID_; +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public int HunterNetCoreCmdID { +// get { return hunterNetCoreCmdID_; } +// set { +// hunterNetCoreCmdID_ = value; +// } +// } - /// Field number for the "HunterNetCore_ERRORCode" field. - public const int HunterNetCoreERRORCodeFieldNumber = 2; - private int hunterNetCoreERRORCode_; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int HunterNetCoreERRORCode { - get { return hunterNetCoreERRORCode_; } - set { - hunterNetCoreERRORCode_ = value; - } - } +// /// Field number for the "HunterNetCore_ERRORCode" field. +// public const int HunterNetCoreERRORCodeFieldNumber = 2; +// private int hunterNetCoreERRORCode_; +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public int HunterNetCoreERRORCode { +// get { return hunterNetCoreERRORCode_; } +// set { +// hunterNetCoreERRORCode_ = value; +// } +// } - /// Field number for the "HunterNetCore_Data" field. - public const int HunterNetCoreDataFieldNumber = 3; - private pb::ByteString hunterNetCoreData_ = pb::ByteString.Empty; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public pb::ByteString HunterNetCoreData { - get { return hunterNetCoreData_; } - set { - hunterNetCoreData_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); - } - } +// /// Field number for the "HunterNetCore_Data" field. +// public const int HunterNetCoreDataFieldNumber = 3; +// private pb::ByteString hunterNetCoreData_ = pb::ByteString.Empty; +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public pb::ByteString HunterNetCoreData { +// get { return hunterNetCoreData_; } +// set { +// hunterNetCoreData_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); +// } +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override bool Equals(object other) { - return Equals(other as HunterNet_S2C); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public override bool Equals(object other) { +// return Equals(other as HunterNet_S2C); +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public bool Equals(HunterNet_S2C other) { - if (ReferenceEquals(other, null)) { - return false; - } - if (ReferenceEquals(other, this)) { - return true; - } - if (HunterNetCoreCmdID != other.HunterNetCoreCmdID) return false; - if (HunterNetCoreERRORCode != other.HunterNetCoreERRORCode) return false; - if (HunterNetCoreData != other.HunterNetCoreData) return false; - return Equals(_unknownFields, other._unknownFields); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public bool Equals(HunterNet_S2C other) { +// if (ReferenceEquals(other, null)) { +// return false; +// } +// if (ReferenceEquals(other, this)) { +// return true; +// } +// if (HunterNetCoreCmdID != other.HunterNetCoreCmdID) return false; +// if (HunterNetCoreERRORCode != other.HunterNetCoreERRORCode) return false; +// if (HunterNetCoreData != other.HunterNetCoreData) return false; +// return Equals(_unknownFields, other._unknownFields); +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override int GetHashCode() { - int hash = 1; - if (HunterNetCoreCmdID != 0) hash ^= HunterNetCoreCmdID.GetHashCode(); - if (HunterNetCoreERRORCode != 0) hash ^= HunterNetCoreERRORCode.GetHashCode(); - if (HunterNetCoreData.Length != 0) hash ^= HunterNetCoreData.GetHashCode(); - if (_unknownFields != null) { - hash ^= _unknownFields.GetHashCode(); - } - return hash; - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public override int GetHashCode() { +// int hash = 1; +// if (HunterNetCoreCmdID != 0) hash ^= HunterNetCoreCmdID.GetHashCode(); +// if (HunterNetCoreERRORCode != 0) hash ^= HunterNetCoreERRORCode.GetHashCode(); +// if (HunterNetCoreData.Length != 0) hash ^= HunterNetCoreData.GetHashCode(); +// if (_unknownFields != null) { +// hash ^= _unknownFields.GetHashCode(); +// } +// return hash; +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override string ToString() { - return pb::JsonFormatter.ToDiagnosticString(this); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public override string ToString() { +// return pb::JsonFormatter.ToDiagnosticString(this); +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void WriteTo(pb::CodedOutputStream output) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - output.WriteRawMessage(this); - #else - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreERRORCode != 0) { - output.WriteRawTag(16); - output.WriteInt32(HunterNetCoreERRORCode); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(26); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(output); - } - #endif - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public void WriteTo(pb::CodedOutputStream output) { +// #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE +// output.WriteRawMessage(this); +// #else +// if (HunterNetCoreCmdID != 0) { +// output.WriteRawTag(8); +// output.WriteInt32(HunterNetCoreCmdID); +// } +// if (HunterNetCoreERRORCode != 0) { +// output.WriteRawTag(16); +// output.WriteInt32(HunterNetCoreERRORCode); +// } +// if (HunterNetCoreData.Length != 0) { +// output.WriteRawTag(26); +// output.WriteBytes(HunterNetCoreData); +// } +// if (_unknownFields != null) { +// _unknownFields.WriteTo(output); +// } +// #endif +// } - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) { - if (HunterNetCoreCmdID != 0) { - output.WriteRawTag(8); - output.WriteInt32(HunterNetCoreCmdID); - } - if (HunterNetCoreERRORCode != 0) { - output.WriteRawTag(16); - output.WriteInt32(HunterNetCoreERRORCode); - } - if (HunterNetCoreData.Length != 0) { - output.WriteRawTag(26); - output.WriteBytes(HunterNetCoreData); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(ref output); - } - } - #endif +// #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) { +// if (HunterNetCoreCmdID != 0) { +// output.WriteRawTag(8); +// output.WriteInt32(HunterNetCoreCmdID); +// } +// if (HunterNetCoreERRORCode != 0) { +// output.WriteRawTag(16); +// output.WriteInt32(HunterNetCoreERRORCode); +// } +// if (HunterNetCoreData.Length != 0) { +// output.WriteRawTag(26); +// output.WriteBytes(HunterNetCoreData); +// } +// if (_unknownFields != null) { +// _unknownFields.WriteTo(ref output); +// } +// } +// #endif - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int CalculateSize() { - int size = 0; - if (HunterNetCoreCmdID != 0) { - size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreCmdID); - } - if (HunterNetCoreERRORCode != 0) { - size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreERRORCode); - } - if (HunterNetCoreData.Length != 0) { - size += 1 + pb::CodedOutputStream.ComputeBytesSize(HunterNetCoreData); - } - if (_unknownFields != null) { - size += _unknownFields.CalculateSize(); - } - return size; - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public int CalculateSize() { +// int size = 0; +// if (HunterNetCoreCmdID != 0) { +// size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreCmdID); +// } +// if (HunterNetCoreERRORCode != 0) { +// size += 1 + pb::CodedOutputStream.ComputeInt32Size(HunterNetCoreERRORCode); +// } +// if (HunterNetCoreData.Length != 0) { +// size += 1 + pb::CodedOutputStream.ComputeBytesSize(HunterNetCoreData); +// } +// if (_unknownFields != null) { +// size += _unknownFields.CalculateSize(); +// } +// return size; +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(HunterNet_S2C other) { - if (other == null) { - return; - } - if (other.HunterNetCoreCmdID != 0) { - HunterNetCoreCmdID = other.HunterNetCoreCmdID; - } - if (other.HunterNetCoreERRORCode != 0) { - HunterNetCoreERRORCode = other.HunterNetCoreERRORCode; - } - if (other.HunterNetCoreData.Length != 0) { - HunterNetCoreData = other.HunterNetCoreData; - } - _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public void MergeFrom(HunterNet_S2C other) { +// if (other == null) { +// return; +// } +// if (other.HunterNetCoreCmdID != 0) { +// HunterNetCoreCmdID = other.HunterNetCoreCmdID; +// } +// if (other.HunterNetCoreERRORCode != 0) { +// HunterNetCoreERRORCode = other.HunterNetCoreERRORCode; +// } +// if (other.HunterNetCoreData.Length != 0) { +// HunterNetCoreData = other.HunterNetCoreData; +// } +// _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); +// } - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(pb::CodedInputStream input) { - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - input.ReadRawMessage(this); - #else - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 16: { - HunterNetCoreERRORCode = input.ReadInt32(); - break; - } - case 26: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - #endif - } +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// public void MergeFrom(pb::CodedInputStream input) { +// #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE +// input.ReadRawMessage(this); +// #else +// uint tag; +// while ((tag = input.ReadTag()) != 0) { +// switch(tag) { +// default: +// _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); +// break; +// case 8: { +// HunterNetCoreCmdID = input.ReadInt32(); +// break; +// } +// case 16: { +// HunterNetCoreERRORCode = input.ReadInt32(); +// break; +// } +// case 26: { +// HunterNetCoreData = input.ReadBytes(); +// break; +// } +// } +// } +// #endif +// } - #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) { - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input); - break; - case 8: { - HunterNetCoreCmdID = input.ReadInt32(); - break; - } - case 16: { - HunterNetCoreERRORCode = input.ReadInt32(); - break; - } - case 26: { - HunterNetCoreData = input.ReadBytes(); - break; - } - } - } - } - #endif +// #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE +// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] +// void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) { +// uint tag; +// while ((tag = input.ReadTag()) != 0) { +// switch(tag) { +// default: +// _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input); +// break; +// case 8: { +// HunterNetCoreCmdID = input.ReadInt32(); +// break; +// } +// case 16: { +// HunterNetCoreERRORCode = input.ReadInt32(); +// break; +// } +// case 26: { +// HunterNetCoreData = input.ReadBytes(); +// break; +// } +// } +// } +// } +// #endif - } +// } - #endregion +// #endregion -} +//} -#endregion Designer generated code +//#endregion Designer generated code diff --git a/README.md b/README.md index 0fc7179..e708e2f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@ # HaoYueNet .Net 7 的,自建基于IOCP的TCP的高性能网络库 -使用Protobuff作为基础协议 + +使用Protobuff作为基础协议(网络库本身不依赖Protobuff,仅上层示例依赖) + +网络库本身无依赖,可以用于任何数据通讯 包含服务端和客户端双端库,可直接用于各类.Net程序或Unity程序,做TCP通讯底层库。 diff --git a/Simple/ClientCore/Common/ProtoBufHelper.cs b/Simple/ClientCore/Common/ProtoBufHelper.cs index 27e19a4..a3e8045 100644 --- a/Simple/ClientCore/Common/ProtoBufHelper.cs +++ b/Simple/ClientCore/Common/ProtoBufHelper.cs @@ -4,12 +4,10 @@ namespace ClientCore.Common { public static class ProtoBufHelper { - public static byte[] Serizlize(IMessage msg) { return msg.ToByteArray(); } - public static T DeSerizlize(byte[] bytes) { var msgType = typeof(T); diff --git a/Simple/ClientCore/Manager/AppLogin.cs b/Simple/ClientCore/Manager/AppLogin.cs index abd5660..8f58fbb 100644 --- a/Simple/ClientCore/Manager/AppLogin.cs +++ b/Simple/ClientCore/Manager/AppLogin.cs @@ -17,7 +17,7 @@ namespace ClientCore.Manager LoginType = 0, Account = Account, }; - App.networkHelper.SendToServer((int)CommandID.CmdLogin, NetworkHelper.Serizlize(msg)); + App.networkHelper.SendToServer((int)CommandID.CmdLogin, ProtoBufHelper.Serizlize(msg)); } public void RecvLoginMsg(byte[] reqData) diff --git a/Simple/ClientCore/Network/NetworkHelper.cs b/Simple/ClientCore/Network/NetworkHelper.cs index dd33bea..dd0219e 100644 --- a/Simple/ClientCore/Network/NetworkHelper.cs +++ b/Simple/ClientCore/Network/NetworkHelper.cs @@ -1,4 +1,5 @@ using AxibugProtobuf; +using Google.Protobuf; using HaoYueNet.ClientNetwork; using System; using System.Collections.Generic;