From 4f9cb4d22d82a4bb1b9b47b2bf2e610e8d747016 Mon Sep 17 00:00:00 2001
From: sin365 <353374337@qq.com>
Date: Thu, 14 Dec 2023 18:15:04 +0800
Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=87=8D=E8=BF=9E=E5=92=8C?=
=?UTF-8?q?=E5=86=85=E5=AD=98=E6=B5=81=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../NetworkHelperCore.cs | 34 ++++++---
.../NetworkHelperP2PCore.cs | 20 ++++--
.../ProtobufHunterNetCore.cs | 9 ++-
.../NetworkHelperCore.cs | 21 ++++--
.../NetworkHelperP2PCore.cs | 35 +++++++---
.../ProtobufHunterNetCore.cs | 9 ++-
.../NetWork/AsyncUserToken.cs | 17 ++---
.../NetWork/BufferManager.cs | 6 +-
.../NetWork/TcpSaeaServer.cs | 70 +++++++++++++------
Simple/Client-Cli/Program.cs | 14 ++++
Simple/ClientCore/App.cs | 9 +--
Simple/ClientCore/Common/Helper.cs | 8 +--
Simple/ClientCore/Event/EEvent.cs | 8 +--
Simple/ClientCore/Event/EventSystem.cs | 9 +--
Simple/ClientCore/Manager/AppLogin.cs | 20 ++++++
Simple/ClientCore/Manager/UserDataManager.cs | 57 +++++++++++++++
Simple/ClientCore/Network/NetworkHelper.cs | 64 +++++++++++++++--
Simple/Server/Program.cs | 3 +-
Simple/ServerCore/Common/Helper.cs | 8 +--
Simple/ServerCore/Event/EEvent.cs | 8 +--
Simple/ServerCore/Event/EventSystem.cs | 6 --
Simple/ServerCore/Manager/ChatManager.cs | 2 +-
Simple/ServerCore/Manager/ServerManager.cs | 4 +-
Simple/ServerCore/NetWork/IOCPNetWork.cs | 4 +-
Simple/ServerCore/NetWork/NetMsg.cs | 6 --
25 files changed, 310 insertions(+), 141 deletions(-)
create mode 100644 Simple/ClientCore/Manager/UserDataManager.cs
diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs
index 2822504..5876d2b 100644
--- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs
+++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs
@@ -1,5 +1,4 @@
using Google.Protobuf;
-using Google.Protobuf.WellKnownTypes;
using HunterProtobufCore;
using System.Net;
using System.Net.Sockets;
@@ -37,9 +36,11 @@ namespace HaoYueNet.ClientNetwork
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;
@@ -52,10 +53,12 @@ namespace HaoYueNet.ClientNetwork
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
@@ -246,10 +249,12 @@ namespace HaoYueNet.ClientNetwork
OnReceiveData(_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();//开辟一个内存流
+ //MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
while (true)
{
@@ -278,8 +283,6 @@ namespace HaoYueNet.ClientNetwork
while (true)
{
-
-
if (effective > 0)//如果接受到的消息不为0(不为空)
{
int HeadLength = 0;//包头长度(包头+包体)
@@ -296,16 +299,29 @@ namespace HaoYueNet.ClientNetwork
//↓↓↓↓↓↓↓↓ ↓↓↓
if (getData.Length - StartIndex < HeadLength || HeadLength == -1)
{
+ /* 一种清空流的方式
memoryStream.Close();//关闭内存流
memoryStream.Dispose();//释放内存资源
memoryStream = new MemoryStream();//创建新的内存流
+ */
+
+ //流复用的方式 不用重新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());
+ //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;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
}
}
diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
index 4c7316e..ac50cc4 100644
--- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
+++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
@@ -1,5 +1,4 @@
using Google.Protobuf;
-using Google.Protobuf.WellKnownTypes;
using HunterProtobufCore;
using System.Net;
using System.Net.Sockets;
@@ -252,10 +251,11 @@ namespace HaoYueNet.ClientNetwork
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();//开辟一个内存流
+ //MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
while (true)
{
@@ -302,21 +302,33 @@ namespace HaoYueNet.ClientNetwork
//↓↓↓↓↓↓↓↓ ↓↓↓
if (getData.Length - StartIndex < HeadLength || HeadLength == -1)
{
+ /* 一种清空流的方式
memoryStream.Close();//关闭内存流
memoryStream.Dispose();//释放内存资源
memoryStream = new MemoryStream();//创建新的内存流
+ */
+
+ //流复用的方式 不用重新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());
+ //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;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
}
}
}
-
}
}
diff --git a/NetLib/HaoYueNet.ClientNetwork/ProtobufHunterNetCore.cs b/NetLib/HaoYueNet.ClientNetwork/ProtobufHunterNetCore.cs
index 97eeea6..0b401bd 100644
--- a/NetLib/HaoYueNet.ClientNetwork/ProtobufHunterNetCore.cs
+++ b/NetLib/HaoYueNet.ClientNetwork/ProtobufHunterNetCore.cs
@@ -6,13 +6,12 @@
#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 {
+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
diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs
index f4c50b4..6510e9e 100644
--- a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs
+++ b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs
@@ -249,10 +249,12 @@ namespace HaoYueNet.ClientNetworkNet4x
OnReceiveData(_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();//开辟一个内存流
+ //MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
while (true)
{
@@ -281,8 +283,6 @@ namespace HaoYueNet.ClientNetworkNet4x
while (true)
{
-
-
if (effective > 0)//如果接受到的消息不为0(不为空)
{
int HeadLength = 0;//包头长度(包头+包体)
@@ -299,16 +299,29 @@ namespace HaoYueNet.ClientNetworkNet4x
//↓↓↓↓↓↓↓↓ ↓↓↓
if (getData.Length - StartIndex < HeadLength || HeadLength == -1)
{
+ /* 一种清空流的方式
memoryStream.Close();//关闭内存流
memoryStream.Dispose();//释放内存资源
memoryStream = new MemoryStream();//创建新的内存流
+ */
+
+ //流复用的方式 不用重新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());
+ //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;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
}
}
diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs
index f7bfe13..182cf70 100644
--- a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs
+++ b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs
@@ -31,16 +31,16 @@ namespace HaoYueNet.ClientNetworkNet4x
private static int MaxSendIndexNum = 3;
//响应倒计时计数
- private static int RevIndex=0;
+ private static int RevIndex = 0;
//发送倒计时计数
- private static int SendIndex=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 void Init(string IP, int port, bool bBindReuseAddress = false, int bBindport = 0)
{
LogOut("==>初始化网络核心");
@@ -236,7 +236,7 @@ namespace HaoYueNet.ClientNetworkNet4x
{
OnCloseReady();
}
-
+
private void DataCallBackReady(byte[] data)
{
@@ -249,21 +249,22 @@ namespace HaoYueNet.ClientNetworkNet4x
//LogOut("收到心跳包");
return;
}
-
+
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();//开辟一个内存流
+ //MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
while (true)
{
byte[] buffer = new byte[1024 * 1024 * 2];
- int effective=0;
+ int effective = 0;
try
{
effective = client.Receive(buffer);
@@ -272,7 +273,7 @@ namespace HaoYueNet.ClientNetworkNet4x
continue;
}
}
- catch(Exception ex)
+ catch (Exception ex)
{
//远程主机强迫关闭了一个现有的连接
OnCloseReady();
@@ -305,21 +306,33 @@ namespace HaoYueNet.ClientNetworkNet4x
//↓↓↓↓↓↓↓↓ ↓↓↓
if (getData.Length - StartIndex < HeadLength || HeadLength == -1)
{
+ /* 一种清空流的方式
memoryStream.Close();//关闭内存流
memoryStream.Dispose();//释放内存资源
memoryStream = new MemoryStream();//创建新的内存流
+ */
+
+ //流复用的方式 不用重新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());
+ //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;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
}
}
}
-
}
}
diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/ProtobufHunterNetCore.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/ProtobufHunterNetCore.cs
index 97eeea6..0b401bd 100644
--- a/NetLib/HaoYueNet.ClientNetworkNet4x/ProtobufHunterNetCore.cs
+++ b/NetLib/HaoYueNet.ClientNetworkNet4x/ProtobufHunterNetCore.cs
@@ -6,13 +6,12 @@
#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 {
+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
diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs
index 3be9e65..1b6df2c 100644
--- a/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs
+++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs
@@ -1,10 +1,5 @@
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Linq;
-using System.Net;
+using System.Net;
using System.Net.Sockets;
-using System.Text;
namespace HaoYueNet.ServerNetwork
{
@@ -38,13 +33,15 @@ namespace HaoYueNet.ServerNetwork
///
/// 数据缓存区
///
- public List Buffer { get; set; }
-
+ //public List Buffer { get; set; }
+
+ public MemoryStream memoryStream { get; set; }
+
public AsyncUserToken()
{
- this.Buffer = new List();
+ //this.Buffer = new List();
+ this.memoryStream = new MemoryStream();
}
-
///
/// 响应倒计时计数
///
diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/BufferManager.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/BufferManager.cs
index 9114fc5..8e6c3a0 100644
--- a/NetLib/HaoYueNet.ServerNetwork/NetWork/BufferManager.cs
+++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/BufferManager.cs
@@ -1,8 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Net.Sockets;
-using System.Text;
+using System.Net.Sockets;
namespace HaoYueNet.ServerNetwork
{
diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs
index 098deef..270222b 100644
--- a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs
+++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs
@@ -1,15 +1,7 @@
using Google.Protobuf;
using HunterProtobufCore;
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
using System.Net;
using System.Net.Sockets;
-using System.Text;
-using System.Threading;
-using static Google.Protobuf.Reflection.FieldOptions.Types;
namespace HaoYueNet.ServerNetwork
@@ -185,7 +177,7 @@ namespace HaoYueNet.ServerNetwork
// post accepts on the listening socket
StartAccept(null);
- OutNetLog("监听:" + listenSocket.AddressFamily.ToString());
+ OutNetLog("监听:" + listenSocket.LocalEndPoint.ToString());
_heartTimer = new System.Timers.Timer();
_heartTimer.Interval = TimerInterval;
@@ -338,7 +330,12 @@ namespace HaoYueNet.ServerNetwork
// Get the socket for the accepted client connection and put it into the
//ReadEventArg object user token
SocketAsyncEventArgs readEventArgs = m_Receivepool.Pop();
- AsyncUserToken userToken = (AsyncUserToken)readEventArgs.UserToken;
+ //TODO readEventArgs.UserToken这里的 UserToken 有可能是空
+ AsyncUserToken userToken;
+ if (readEventArgs.UserToken == null)
+ readEventArgs.UserToken = new AsyncUserToken();
+
+ userToken = (AsyncUserToken)readEventArgs.UserToken;
userToken.Socket = e.AcceptSocket;
userToken.ConnectTime = DateTime.Now;
userToken.Remote = e.AcceptSocket.RemoteEndPoint;
@@ -381,39 +378,68 @@ namespace HaoYueNet.ServerNetwork
if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
{
//读取数据
- byte[] data = new byte[e.BytesTransferred];
- Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred);
- lock (token.Buffer)
+ //byte[] data = new byte[e.BytesTransferred];
+ //Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred);
+ //lock (token.Buffer)
+ lock(token.memoryStream)
{
- token.Buffer.AddRange(data);
+ //token.Buffer.AddRange(data);
+ token.memoryStream.Write(e.Buffer, e.Offset, e.BytesTransferred);
}
do
{
//如果包头不完整
- if (token.Buffer.Count < 4)
+ //if (token.Buffer.Count < 4)
+ if (token.memoryStream.Length < 4)
break;
//判断包的长度
- byte[] lenBytes = token.Buffer.GetRange(0, 4).ToArray();
+ //byte[] lenBytes = token.Buffer.GetRange(0, 4).ToArray();
+ //int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4;
+ //if (packageLen > token.Buffer.Count - 4)
+ //{ //长度不够时,退出循环,让程序继续接收
+ // break;
+ //}
+
+ long FristBeginPos = token.memoryStream.Position;
+ byte[] lenBytes = new byte[4];
+ token.memoryStream.Seek(0, SeekOrigin.Begin);
+ token.memoryStream.Read(lenBytes,0,4);
int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4;
- if (packageLen > token.Buffer.Count - 4)
+ if (packageLen > token.memoryStream.Length - 4)
{ //长度不够时,退出循环,让程序继续接收
break;
}
//包够长时,则提取出来,交给后面的程序去处理
- byte[] rev = token.Buffer.GetRange(4, packageLen).ToArray();
- //从数据池中移除这组数据
- lock (token.Buffer)
+ //byte[] rev = token.Buffer.GetRange(4, packageLen).ToArray();
+
+ byte[] rev = new byte[packageLen];
+ token.memoryStream.Seek(4, SeekOrigin.Begin);
+ token.memoryStream.Read(rev, 0, packageLen);
+
+ ////从数据池中移除这组数据
+ //lock (token.Buffer)
+ //{
+ // token.Buffer.RemoveRange(0, packageLen + 4);
+ //}
+
+ token.memoryStream.Seek(FristBeginPos, SeekOrigin.Begin);
+ //从数据池中移除这组数据
+ lock (token.memoryStream)
{
- token.Buffer.RemoveRange(0, packageLen + 4);
+ int numberOfBytesToRemove = packageLen + 4;
+ byte[] buf = token.memoryStream.GetBuffer();
+ Buffer.BlockCopy(buf, numberOfBytesToRemove, buf, 0, (int)token.memoryStream.Length - numberOfBytesToRemove);
+ token.memoryStream.SetLength(token.memoryStream.Length - numberOfBytesToRemove);
}
DataCallBackReady(token, rev);
//这里API处理完后,并没有返回结果,当然结果是要返回的,却不是在这里, 这里的代码只管接收.
//若要返回结果,可在API处理中调用此类对象的SendMessage方法,统一打包发送.不要被微软的示例给迷惑了.
- } while (token.Buffer.Count > 4);
+ //} while (token.Buffer.Count > 4);
+ } while (token.memoryStream.Length > 4);
//继续接收. 为什么要这么写,请看Socket.ReceiveAsync方法的说明
if (!token.Socket.ReceiveAsync(e))
diff --git a/Simple/Client-Cli/Program.cs b/Simple/Client-Cli/Program.cs
index e89f01a..a6f055e 100644
--- a/Simple/Client-Cli/Program.cs
+++ b/Simple/Client-Cli/Program.cs
@@ -33,6 +33,20 @@ while (true)
}
App.chat.SendChatMsg(CmdArr[1]);
break;
+ case "at":
+
+ //test
+ string guid = Guid.NewGuid().ToString();
+ App.login.Login(guid);
+ while (true)
+ {
+ if (!App.networkHelper.GetClientSocket().Connected)
+ return;
+ Thread.Sleep(10);
+ App.chat.SendChatMsg(guid);
+ }
+ //test end
+ break;
case "socket":
{
Socket socket = App.networkHelper.GetClientSocket();
diff --git a/Simple/ClientCore/App.cs b/Simple/ClientCore/App.cs
index f1fb5f3..3b4e226 100644
--- a/Simple/ClientCore/App.cs
+++ b/Simple/ClientCore/App.cs
@@ -1,9 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using ClientCore.Manager;
+using ClientCore.Manager;
using ClientCore.Network;
namespace ClientCore
@@ -18,6 +13,7 @@ namespace ClientCore
public static NetworkHelper networkHelper;
public static AppLogin login;
public static AppChat chat;
+ public static UserDataManager user;
public static void Init(string IP, int port)
{
@@ -25,6 +21,7 @@ namespace ClientCore
networkHelper = new NetworkHelper();
login = new AppLogin();
chat = new AppChat();
+ user = new UserDataManager();
networkHelper.Init(IP, port);
}
}
diff --git a/Simple/ClientCore/Common/Helper.cs b/Simple/ClientCore/Common/Helper.cs
index 0643fd4..772490e 100644
--- a/Simple/ClientCore/Common/Helper.cs
+++ b/Simple/ClientCore/Common/Helper.cs
@@ -1,10 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace ClientCore.Common
+namespace ClientCore.Common
{
public static class Helper
{
diff --git a/Simple/ClientCore/Event/EEvent.cs b/Simple/ClientCore/Event/EEvent.cs
index 17a2d22..126472a 100644
--- a/Simple/ClientCore/Event/EEvent.cs
+++ b/Simple/ClientCore/Event/EEvent.cs
@@ -1,10 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace ClientCore.Event
+namespace ClientCore.Event
{
public enum EEvent
{
diff --git a/Simple/ClientCore/Event/EventSystem.cs b/Simple/ClientCore/Event/EventSystem.cs
index dc0ee60..fee8162 100644
--- a/Simple/ClientCore/Event/EventSystem.cs
+++ b/Simple/ClientCore/Event/EventSystem.cs
@@ -1,11 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace ClientCore.Event
+namespace ClientCore.Event
{
public class EventData
{
diff --git a/Simple/ClientCore/Manager/AppLogin.cs b/Simple/ClientCore/Manager/AppLogin.cs
index 23a25a9..9286847 100644
--- a/Simple/ClientCore/Manager/AppLogin.cs
+++ b/Simple/ClientCore/Manager/AppLogin.cs
@@ -1,10 +1,15 @@
using AxibugProtobuf;
+using ClientCore.Common;
using ClientCore.Network;
namespace ClientCore.Manager
{
public class AppLogin
{
+ public AppLogin()
+ {
+ NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdLogin, RecvLoginMsg);
+ }
public void Login(string Account)
{
Protobuf_Login msg = new Protobuf_Login()
@@ -14,5 +19,20 @@ namespace ClientCore.Manager
};
App.networkHelper.SendToServer((int)CommandID.CmdLogin, NetworkHelper.Serizlize(msg));
}
+
+ public void RecvLoginMsg(byte[] reqData)
+ {
+ Protobuf_Login_RESP msg = ProtoBufHelper.DeSerizlize(reqData);
+ if (msg.Status == LoginResultStatus.Ok)
+ {
+ App.log.Debug("登录成功");
+ App.user.InitMainUserData(msg);
+ }
+ else
+ {
+ App.log.Debug("登录失败");
+ }
+ }
+
}
}
diff --git a/Simple/ClientCore/Manager/UserDataManager.cs b/Simple/ClientCore/Manager/UserDataManager.cs
new file mode 100644
index 0000000..526a2df
--- /dev/null
+++ b/Simple/ClientCore/Manager/UserDataManager.cs
@@ -0,0 +1,57 @@
+using AxibugProtobuf;
+
+namespace ClientCore.Manager
+{
+ public class UserDataBase
+ {
+ public long UID { get; set; }
+ public string Account { get; set; }
+ }
+
+ public class MainUserDataBase : UserDataBase
+ {
+ public bool IsLoggedIn { get; set; } = false;
+ }
+
+ public class UserDataManager
+ {
+ public UserDataManager()
+ {
+ //注册重连成功事件,以便后续自动登录
+ App.networkHelper.OnReConnected += OnReConnected;
+ }
+
+ MainUserDataBase user = new MainUserDataBase();
+ public bool IsLoggedIn => user.IsLoggedIn;
+
+ public void InitMainUserData(string UName)
+ {
+ user.Account = UName;
+ user.IsLoggedIn = true;
+ //以及其他数据初始化
+ //...
+ }
+
+ ///
+ /// 登出
+ ///
+ public void LoginOutData()
+ {
+ user.IsLoggedIn = false;
+ //以及其他数据清理
+ //...
+ }
+
+ ///
+ /// 当重连成功
+ ///
+ public void OnReConnected()
+ {
+ //如果之前已登录,则重新登录
+ if (user.IsLoggedIn)
+ {
+ App.login.Login(user.Account);
+ }
+ }
+ }
+}
diff --git a/Simple/ClientCore/Network/NetworkHelper.cs b/Simple/ClientCore/Network/NetworkHelper.cs
index 45e0e4b..5c9923b 100644
--- a/Simple/ClientCore/Network/NetworkHelper.cs
+++ b/Simple/ClientCore/Network/NetworkHelper.cs
@@ -13,6 +13,10 @@ namespace ClientCore.Network
///
public class NetworkHelper : NetworkHelperCore
{
+ ///
+ /// 网络库调试日志输出
+ ///
+ public event OnLogOutHandler OnLogOut;
public NetworkHelper()
{
//指定接收服务器数据事件
@@ -24,14 +28,34 @@ namespace ClientCore.Network
OnLogOut += NetworkDeBugLog;
}
+ public delegate void OnReConnectedHandler();
+ ///
+ /// 重连成功事件
+ ///
+ public event OnReConnectedHandler OnReConnected;
+ ///
+ /// 是否自动重连
+ ///
+ public bool bAutoReConnect = true;
+ ///
+ /// 重连尝试时间
+ ///
+ const int ReConnectTryTime = 1000;
+
public void NetworkConnected(bool IsConnect)
{
+ NetworkDeBugLog($"NetworkConnected:{IsConnect}");
if (IsConnect)
- NetworkDeBugLog("服务器连接成功");
+ {
+
+ }
else
{
- NetworkDeBugLog("服务器连接失败");
- //to do 重连逻辑
+ //连接失败
+ NetworkDeBugLog("连接失败!");
+ //自动重连开关
+ if (bAutoReConnect)
+ ReConnect();
}
}
@@ -39,7 +63,6 @@ namespace ClientCore.Network
{
//用于Unity内的输出
//Debug.Log("NetCoreDebug >> "+str);
-
Console.WriteLine("NetCoreDebug >> " + str);
}
@@ -70,6 +93,39 @@ namespace ClientCore.Network
public void OnConnectClose()
{
NetworkDeBugLog("OnConnectClose");
+
+ //自动重连开关
+ if (bAutoReConnect)
+ ReConnect();
+ }
+
+
+ bool bInReConnecting = false;
+ ///
+ /// 自动重连
+ ///
+ void ReConnect()
+ {
+ if (bInReConnecting)
+ return;
+ bInReConnecting = true;
+
+ bool bflagDone = false;
+ do
+ {
+ //等待时间
+ Thread.Sleep(ReConnectTryTime);
+ App.log.Debug($"尝试自动重连{LastConnectIP}:{LastConnectPort}……");
+ //第一步
+ if (Init(LastConnectIP, LastConnectPort))
+ {
+ App.log.Debug($"自动重连成功!");
+ bflagDone = true;
+ App.log.Debug($"触发重连后的自动逻辑!");
+ OnReConnected?.Invoke();
+ }
+ } while (!bflagDone);
+ bInReConnecting = false;
}
}
}
diff --git a/Simple/Server/Program.cs b/Simple/Server/Program.cs
index 764948e..027ea7e 100644
--- a/Simple/Server/Program.cs
+++ b/Simple/Server/Program.cs
@@ -1,5 +1,4 @@
-using ServerCore;
-using ServerCore.Manager;
+using ServerCore.Manager;
ServerManager.InitServer(23846);
diff --git a/Simple/ServerCore/Common/Helper.cs b/Simple/ServerCore/Common/Helper.cs
index 2cf4cfe..027c25a 100644
--- a/Simple/ServerCore/Common/Helper.cs
+++ b/Simple/ServerCore/Common/Helper.cs
@@ -1,10 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace ServerCore.Common
+namespace ServerCore.Common
{
public static class Helper
{
diff --git a/Simple/ServerCore/Event/EEvent.cs b/Simple/ServerCore/Event/EEvent.cs
index a2d126e..8e8f6e0 100644
--- a/Simple/ServerCore/Event/EEvent.cs
+++ b/Simple/ServerCore/Event/EEvent.cs
@@ -1,10 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace ServerCore.Event
+namespace ServerCore.Event
{
public enum EEvent
{
diff --git a/Simple/ServerCore/Event/EventSystem.cs b/Simple/ServerCore/Event/EventSystem.cs
index a011153..900c61a 100644
--- a/Simple/ServerCore/Event/EventSystem.cs
+++ b/Simple/ServerCore/Event/EventSystem.cs
@@ -1,10 +1,4 @@
using ServerCore.Manager;
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
namespace ServerCore.Event
{
diff --git a/Simple/ServerCore/Manager/ChatManager.cs b/Simple/ServerCore/Manager/ChatManager.cs
index c44ae7e..08b8516 100644
--- a/Simple/ServerCore/Manager/ChatManager.cs
+++ b/Simple/ServerCore/Manager/ChatManager.cs
@@ -15,7 +15,7 @@ namespace ServerCore.Manager
public void RecvPlayerChatMsg(Socket sk, byte[] reqData)
{
ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
- ServerManager.g_Log.Debug("收到新的登录请求");
+ ServerManager.g_Log.Debug("收到聊天消息请求");
Protobuf_ChatMsg msg = ProtoBufHelper.DeSerizlize(reqData);
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_ChatMsg_RESP()
{
diff --git a/Simple/ServerCore/Manager/ServerManager.cs b/Simple/ServerCore/Manager/ServerManager.cs
index 9397df6..51b7b55 100644
--- a/Simple/ServerCore/Manager/ServerManager.cs
+++ b/Simple/ServerCore/Manager/ServerManager.cs
@@ -1,5 +1,5 @@
-using System.Net;
-using ServerCore.NetWork;
+using ServerCore.NetWork;
+using System.Net;
namespace ServerCore.Manager
{
diff --git a/Simple/ServerCore/NetWork/IOCPNetWork.cs b/Simple/ServerCore/NetWork/IOCPNetWork.cs
index b257b43..f33b821 100644
--- a/Simple/ServerCore/NetWork/IOCPNetWork.cs
+++ b/Simple/ServerCore/NetWork/IOCPNetWork.cs
@@ -1,7 +1,5 @@
-using AxibugProtobuf;
-using HaoYueNet.ServerNetwork;
+using HaoYueNet.ServerNetwork;
using ServerCore.Manager;
-using System.Net;
using System.Net.Sockets;
namespace ServerCore.NetWork
diff --git a/Simple/ServerCore/NetWork/NetMsg.cs b/Simple/ServerCore/NetWork/NetMsg.cs
index 39acb0f..9391dd7 100644
--- a/Simple/ServerCore/NetWork/NetMsg.cs
+++ b/Simple/ServerCore/NetWork/NetMsg.cs
@@ -1,11 +1,5 @@
using ServerCore.Manager;
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
using System.Net.Sockets;
-using System.Text;
-using System.Threading.Tasks;
namespace ServerCore.NetWork
{