Pārlūkot izejas kodu

代码微调,以及golang调试.

王刚 3 gadi atpakaļ
vecāks
revīzija
7c879a567f

+ 1 - 1
CSserver/Lib1/Config.cs

@@ -5,7 +5,7 @@ using StackExchange.Redis;
    public class Config
     {
 
-        public const bool GameOnline = true;  
+        public const bool GameOnline = false;  
 
         public ConfigurationOptions redis;
 

+ 10 - 9
CSserver/PBReferens/DataBuff.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Net;
 
 namespace ProtoDataBuff
 {
@@ -22,10 +23,10 @@ namespace ProtoDataBuff
     public struct sSocketData
     {
         public Int32 PackLen;
-        public UInt32 TS;
+        //public UInt32 TS;
         public eProtocalCommand _protocallType;
         public byte[] _data;
-     
+
         /// <summary>
         /// 网络结构转数据
         /// </summary>
@@ -34,12 +35,12 @@ namespace ProtoDataBuff
         public byte[] ToBytes()
         {
             byte[] _tmpBuff = new byte[PackLen];
-            byte[] _tmpBuffLength = BitConverter.GetBytes(PackLen);
-            byte[] _tmpDataType = BitConverter.GetBytes((UInt16)_protocallType);
+            byte[] _tmpBuffLength = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(PackLen));
+            byte[] _tmpDataType = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int16)_protocallType));
 
-            Array.Copy(_tmpBuffLength, 0, _tmpBuff, 0, Constants.HEAD_DATA_LEN);//缓存总长度
+            Array.Copy(_tmpBuffLength, 0, _tmpBuff, 0, Constants.HEAD_DATA_LEN);                    //缓存总长度
             Array.Copy(_tmpDataType, 0, _tmpBuff, Constants.HEAD_DATA_LEN, Constants.HEAD_TYPE_LEN);//协议类型
-            Array.Copy(_data, 0, _tmpBuff, Constants.HEAD_LEN, DataLen());//协议数据
+            Array.Copy(_data, 0, _tmpBuff, Constants.HEAD_LEN, DataLen());                          //协议数据
 
             return _tmpBuff;
         }
@@ -55,7 +56,7 @@ namespace ProtoDataBuff
         {
             sSocketData tmpSocketData = new();
             tmpSocketData.PackLen = Constants.HEAD_LEN + _data.Length;
-          //  tmpSocketData._dataLength = _data.Length;
+            //  tmpSocketData._dataLength = _data.Length;
             tmpSocketData._protocallType = _protocalType;
             tmpSocketData._data = _data;
             return tmpSocketData;
@@ -124,11 +125,11 @@ namespace ProtoDataBuff
             {
                 byte[] tmpDataLen = new byte[Constants.HEAD_DATA_LEN];
                 Array.Copy(_buff, 0, tmpDataLen, 0, Constants.HEAD_DATA_LEN);
-                _buffLength = BitConverter.ToInt32(tmpDataLen, 0);
+                _buffLength =IPAddress.NetworkToHostOrder( BitConverter.ToInt32(tmpDataLen, 0));
 
                 byte[] tmpProtocalType = new byte[Constants.HEAD_TYPE_LEN];
                 Array.Copy(_buff, Constants.HEAD_DATA_LEN, tmpProtocalType, 0, Constants.HEAD_TYPE_LEN);
-                _protocalType = BitConverter.ToUInt16(tmpProtocalType, 0);
+                _protocalType = (ushort)IPAddress.NetworkToHostOrder( BitConverter.ToInt16(tmpProtocalType, 0));
 
                 _dataLength = _buffLength - Constants.HEAD_LEN;
             }

+ 3 - 0
CSserver/PBReferens/pbcs/MsgTypeEnum.cs

@@ -46,6 +46,9 @@ public static partial class MsgTypeEnumReflection {
 
 }
 #region Enums
+/// <summary>
+/// 注意不要超过65535(在socket传输时只分配了2字节 -gwang)
+/// </summary>
 public enum eProtocalCommand {
   /// <summary>
   /// 无操作

+ 1 - 0
CSserver/PBReferens/pbgo/pb/MsgTypeEnum.pb.go

@@ -20,6 +20,7 @@ const (
 	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
 )
 
+// 注意不要超过65535(在socket传输时只分配了2字节 -gwang)
 type EProtocalCommand int32
 
 const (

+ 3 - 3
CSserver/clientTest/Program.cs

@@ -168,12 +168,12 @@ namespace clientTest.bossfight
         static private byte[] SocketDataToBytes(sSocketData tmpSocketData)
         {
             byte[] _tmpBuff = new byte[tmpSocketData.PackLen];
-            byte[] _tmpBuffLength = BitConverter.GetBytes(tmpSocketData.PackLen);
-            byte[] _tmpDataLenght = BitConverter.GetBytes((UInt16)tmpSocketData._protocallType);
+            byte[] _tmpBuffLength = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(tmpSocketData.PackLen));
+            byte[] _tmpDataLenght = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int16)tmpSocketData._protocallType));
 
             Array.Copy(_tmpBuffLength, 0, _tmpBuff, 0, Constants.HEAD_DATA_LEN);//缓存总长度
             Array.Copy(_tmpDataLenght, 0, _tmpBuff, Constants.HEAD_DATA_LEN, Constants.HEAD_TYPE_LEN);//协议类型
-            Array.Copy(tmpSocketData._data, 0, _tmpBuff, Constants.HEAD_LEN, tmpSocketData.DataLen);//协议数据
+            Array.Copy(tmpSocketData._data, 0, _tmpBuff, Constants.HEAD_LEN, tmpSocketData.DataLen());//协议数据
 
             return _tmpBuff;
         }

+ 9 - 56
CSserver/clientTest/Program_Chat.cs

@@ -124,8 +124,8 @@ namespace clientTest.chat
 
             var port = 6000;
 
-            //var endPoint = new IPEndPoint(IPAddress.Parse("192.168.10.17"), port);
-            var endPoint = new IPEndPoint(IPAddress.Loopback, port);
+            var endPoint = new IPEndPoint(IPAddress.Parse("192.168.10.86"), port);
+            //var endPoint = new IPEndPoint(IPAddress.Loopback, port);
             //var endPoint = new IPEndPoint(IPAddress.Parse("115.159.121.129"), port);
 
             using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
@@ -145,6 +145,7 @@ namespace clientTest.chat
                 {
                     try
                     {
+                       
                         await sendMsg(client);
                         Thread.Sleep(r.Next(1000, 1100));
                     }
@@ -162,7 +163,7 @@ namespace clientTest.chat
             try
             {
                 var msg = new CS_ChatLogin() { Uid = Guid.NewGuid().ToString(), Zoneid = 1, Name = names[r.Next(0, 3)] + r.Next() };
-                var data = SocketDataToBytes(BytesToSocketData(eProtocalCommand.CsChatLogin, IMsg2Bytes(msg)));
+                var data = sSocketData.FromBytes(eProtocalCommand.CsChatLogin, IMsg2Bytes(msg)).ToBytes();
                 await Sock.SendAsync(new ArraySegment<byte>(data), SocketFlags.None);
             }
             catch (Exception e)
@@ -177,13 +178,14 @@ namespace clientTest.chat
 
             var info = texts[r.Next(0, texts.Length)];
             var msg = new CS_ChatSendMsg() { ToChannel = c, Msg = info };
-
-            var data = SocketDataToBytes(BytesToSocketData(eProtocalCommand.CsChatSendMsg, IMsg2Bytes(msg)));
+            Console.WriteLine($"发送消息{info}");
+            var data = sSocketData.FromBytes(eProtocalCommand.CsChatSendMsg, IMsg2Bytes(msg)).ToBytes();
             await sock.SendAsync(new ArraySegment<byte>(data), SocketFlags.None);
         }
 
         static byte[] IMsg2Bytes(pb::IMessage msg)
         {
+            
             using var ms = new MemoryStream();
             using var goutstream = new pb::CodedOutputStream(ms);
             msg.WriteTo(goutstream);
@@ -211,62 +213,13 @@ namespace clientTest.chat
             }
         }
 
-        /// <summary>
-        /// 向客户端写入消息
-        /// </summary>
-        static async void WriteToserver(Socket Sock)
-        {
-            while (true)
-            {
-                var msg = await sendDataBuffer.Reader.ReadAsync();
-                var data = SocketDataToBytes(msg);
-                await Sock.SendAsync(new ArraySegment<byte>(data), SocketFlags.None);
-            }
-        }
-        /// <summary>
-        /// 网络结构转数据
-        /// </summary>
-        /// <param name="tmpSocketData"></param>
-        /// <returns></returns>
-        static private byte[] SocketDataToBytes(sSocketData tmpSocketData)
-        {
-            byte[] _tmpBuff = new byte[tmpSocketData.PackLen];
-            byte[] _tmpBuffLength = BitConverter.GetBytes(tmpSocketData.PackLen);
-            byte[] _tmpDataLenght = BitConverter.GetBytes((UInt16)tmpSocketData._protocallType);
-
-            Array.Copy(_tmpBuffLength, 0, _tmpBuff, 0, Constants.HEAD_DATA_LEN);//缓存总长度
-            Array.Copy(_tmpDataLenght, 0, _tmpBuff, Constants.HEAD_DATA_LEN, Constants.HEAD_TYPE_LEN);//协议类型
-            Array.Copy(tmpSocketData._data, 0, _tmpBuff, Constants.HEAD_LEN, tmpSocketData.DataLen());//协议数据
-
-            return _tmpBuff;
-        }
-
-
-        /// <summary>
-        /// 数据转网络结构
-        /// </summary>
-        /// <param name="_protocalType"></param>
-        /// <param name="_data"></param>
-        /// <returns></returns>
-        static private sSocketData BytesToSocketData(eProtocalCommand _protocalType, byte[] _data)
-        {
-            sSocketData tmpSocketData = new sSocketData();
-            tmpSocketData.PackLen = Constants.HEAD_LEN + _data.Length;
-          //  tmpSocketData._dataLength = _data.Length;
-            tmpSocketData._protocallType = _protocalType;
-            tmpSocketData._data = _data;
-            return tmpSocketData;
-        }
-
+ 
 
         /// <summary>
         /// 接收buffer
         /// </summary>
         static private Channel<sSocketData> recvDataBuffer = Channel.CreateUnbounded<sSocketData>();
-        /// <summary>
-        /// 发送buffer
-        /// </summary>
-        static private Channel<sSocketData> sendDataBuffer = Channel.CreateUnbounded<sSocketData>();
+       
 
         /// <summary>
         /// 接收客户端发来的信息,客户端套接字对象

+ 2 - 2
CSserver/clientTest/Program_MultiDup.cs

@@ -306,8 +306,8 @@ namespace clientTest.multiDup
         static private byte[] SocketDataToBytes(sSocketData tmpSocketData)
         {
             byte[] _tmpBuff = new byte[tmpSocketData.PackLen];
-            byte[] _tmpBuffLength = BitConverter.GetBytes(tmpSocketData.PackLen);
-            byte[] _tmpDataLenght = BitConverter.GetBytes((UInt16)tmpSocketData._protocallType);
+            byte[] _tmpBuffLength = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(tmpSocketData.PackLen));
+            byte[] _tmpDataLenght = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int16)tmpSocketData._protocallType));
 
             Array.Copy(_tmpBuffLength, 0, _tmpBuff, 0, Constants.HEAD_DATA_LEN);//缓存总长度
             Array.Copy(_tmpDataLenght, 0, _tmpBuff, Constants.HEAD_DATA_LEN, Constants.HEAD_TYPE_LEN);//协议类型

+ 1 - 1
CSserver/clientTest/clientTest.csproj

@@ -3,7 +3,7 @@
   <PropertyGroup>
     <OutputType>Exe</OutputType>
     <TargetFramework>net6.0</TargetFramework>
-    <StartupObject>clientTest.multiDup.Program</StartupObject>
+    <StartupObject>clientTest.chat.Program</StartupObject>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

+ 1 - 0
CSserver/csserver/Program.cs

@@ -43,6 +43,7 @@ namespace ChatServer
             Console.WriteLine("ctrl + c 退出程序......");
             Task.Run(MsgLoop);
             Task.Run(WatchConnecting).Wait();
+            Console.ReadLine();
             SocketWatch.Close();
         }