diff --git a/Assets/OxGFrame/CHANGELOG.md b/Assets/OxGFrame/CHANGELOG.md
index 706aaee9..19274add 100644
--- a/Assets/OxGFrame/CHANGELOG.md
+++ b/Assets/OxGFrame/CHANGELOG.md
@@ -1,5 +1,17 @@
# CHANGELOG
+## [2.9.11] - 2024-01-09
+- Optimized NetFrame.
+- Added TcpNetOption.
+- Added WebsocketNetOption.
+- Modified NetOption.
+- Modified SetResponseHandler to SetResponseBinaryHandler and SetResponseMessageHandler.
+- Modified typo SetOutReciveAction to SetOutReceiveAction.
+- Renamed TcpSock to TcpNetProvider.
+- Renamed WebSock to WebsocketNetProvider.
+- Renamed method CloseSocket to Close.
+- Renamed ISocket to INetProvider.
+
## [2.9.10] - 2023-12-28
- Updated YooAsset to v2.1.0 ([CHANGELOG](https://github.com/tuyoogame/YooAsset/releases/tag/2.1.0)).
- Organized coding style ([Wiki](https://github.com/michael811125/OxGFrame/wiki/Coding-Style)).
diff --git a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Concrete.meta b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetNode.meta
similarity index 77%
rename from Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Concrete.meta
rename to Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetNode.meta
index 53b8d27d..0c17dbd7 100644
--- a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Concrete.meta
+++ b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetNode.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
-guid: 50417cf26854a7149877cf173febce03
+guid: 0e5cce3f7dd8e61438eb08b31078caad
folderAsset: yes
DefaultImporter:
externalObjects: {}
diff --git a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Concrete/NetNode.cs b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetNode/NetNode.cs
similarity index 55%
rename from Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Concrete/NetNode.cs
rename to Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetNode/NetNode.cs
index b61b4ec0..fb6f2a00 100644
--- a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Concrete/NetNode.cs
+++ b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetNode/NetNode.cs
@@ -4,40 +4,8 @@
namespace OxGFrame.NetFrame
{
- public delegate void ResponseHandler(byte[] data);
- public delegate void FirstSendHandler();
-
- public class NetOption
- {
- public string url { get; set; }
- public string host { get; set; }
- public int port { get; set; }
- public int autoReconnectCount { get; set; }
-
- ///
- /// TCP/IP 初始連線位置參數
- ///
- ///
- ///
- ///
- public NetOption(string host, int port, int autoReconnectCount = -1)
- {
- this.host = host;
- this.port = port;
- this.autoReconnectCount = autoReconnectCount;
- }
-
- ///
- /// Websocket 初始連線位置參數
- ///
- ///
- ///
- public NetOption(string url, int autoReconnectCount = -1)
- {
- this.url = url;
- this.autoReconnectCount = autoReconnectCount;
- }
- }
+ public delegate void ResponseHandler(T data);
+ public delegate void ConnectingHandler();
public enum NetStatus
{
@@ -50,111 +18,109 @@ public enum NetStatus
public class NetNode : IDisposable
{
- protected NetStatus _netStatus; // Net 狀態
- protected NetOption _netOption = null; // 網路設置選項
- protected ISocket _socket = null; // Socket 介面
- protected INetTips _netTips = null; // 網路狀態提示介面
+ protected NetStatus _netStatus; // Net 狀態
+ protected NetOption _netOption = null; // 網路設置選項
+ protected INetProvider _netProvider = null; // 網路供應者 (TCP/IP, websocket or other)
+ protected INetTips _netTips = null; // 網路狀態提示介面
- private bool _isCloseForce = false; // 是否強制斷線
+ private bool _isCloseForce = false; // 是否強制斷線
- protected RealTimer _hearBeatTicker = null; // 心跳檢測計循環計時器
- private float _heartBeatTick = 10f; // 心跳檢測時間, 預設 = 10秒
- protected Action _heartBeatAction = null; // 心跳檢測 Callback
+ protected RealTimer _hearBeatTicker = null; // 心跳檢測計循環計時器
+ private float _heartBeatTick = 10f; // 心跳檢測時間, 預設 = 10秒
+ protected Action _heartBeatAction = null; // 心跳檢測 Callback
- protected RealTimer _outReceiveTicker = null; // 超時檢測循環計時器
- private float _outReceiveTick = 60f; // 超時檢測時間, 預設 = 60秒
- protected Action _outReceiveAction = null; // 超時檢測 Callback
+ protected RealTimer _outReceiveTicker = null; // 超時檢測循環計時器
+ private float _outReceiveTick = 60f; // 超時檢測時間, 預設 = 60秒
+ protected Action _outReceiveAction = null; // 超時檢測 Callback
- protected RealTimer _reconnectTicker = null; // 斷線重連循環計時器
- private float _reconnectTick = 5f; // 斷線重連時間, 預設 = 5秒
- private int _autoReconnectCount = 0; // 自動連線次數 (由 NetOption 帶入)
- protected Action _reconnectAction = null; // 斷線重連 Callback
+ protected RealTimer _reconnectTicker = null; // 斷線重連循環計時器
+ private float _reconnectTick = 5f; // 斷線重連時間, 預設 = 5秒
+ private int _autoReconnectCount = 0; // 自動連線次數
+ protected Action _reconnectAction = null; // 斷線重連 Callback
- protected ResponseHandler _responseHandler = null; // 接收的回調
- protected FirstSendHandler _firstSendHandler = null; // 第一次封包的回調
+ protected ResponseHandler _responseBinaryHandler = null; // 接收的回調 (Binary)
+ protected ResponseHandler _responseMessageHandler; // 接收的回調 (Text)
+ protected ConnectingHandler _connectingHandler = null; // 連線中的回調
- public NetNode()
+ protected NetNode()
{
- this._hearBeatTicker = new RealTimer();
- this._outReceiveTicker = new RealTimer();
- this._reconnectTicker = new RealTimer();
}
- public NetNode(ISocket socket, INetTips netTips = null)
+ public NetNode(INetProvider socket, INetTips netTips = null)
{
this._hearBeatTicker = new RealTimer();
this._outReceiveTicker = new RealTimer();
this._reconnectTicker = new RealTimer();
- this.Init(socket, netTips);
+ this._Initialize(socket, netTips);
}
- public T GetSocket() where T : ISocket
+ public T GetNetProvider() where T : INetProvider
{
- T socket = (T)this._socket;
+ T socket = (T)this._netProvider;
return socket;
}
- public void Init(ISocket socket, INetTips netTips = null)
+ private void _Initialize(INetProvider socket, INetTips netTips = null)
{
- this._socket = socket;
- this._InitSocketEvents();
+ this._netProvider = socket;
+ this._InitNetEvents();
this._netTips = netTips;
this._netStatus = NetStatus.DISCONNECTED;
}
- private void _InitSocketEvents()
+ private void _InitNetEvents()
{
- this._socket.OnOpen += (sender, e) =>
+ this._netProvider.OnOpen += (sender, status) =>
{
- this._OnOpen(e);
+ this._OnOpen(status);
};
- this._socket.OnMessage += (sender, data) =>
+ this._netProvider.OnBinary += (sender, binary) =>
{
- this._OnMessage(data);
+ this._OnBinary(binary);
};
- this._socket.OnError += (sender, e) =>
+ this._netProvider.OnMessage += (sender, text) =>
{
- this._OnError(e);
+ this._OnMessage(text);
};
- this._socket.OnClose += (sender, e) =>
+ this._netProvider.OnError += (sender, error) =>
{
- this._OnClose(e);
+ this._OnError(error);
+ };
+
+ this._netProvider.OnClose += (sender, status) =>
+ {
+ this._OnClose(status);
};
}
public void Connect(NetOption netOption)
{
- if (this._socket == null)
+ if (this._netProvider == null)
{
Logging.PrintError("The socket cannot be null, Please init first.");
return;
}
- if (this._netStatus == NetStatus.DISCONNECTED || this._netStatus == NetStatus.RECONNECTING)
+ if (this._netStatus == NetStatus.DISCONNECTED ||
+ this._netStatus == NetStatus.RECONNECTING)
{
- this._netStatus = NetStatus.CONNECTING; // 目前處於 CONNECTING 狀態
- this._NetStatusHandler();
-
- this._firstSendHandler?.Invoke(); // 重連時重新初始第一次封包
-
- this._netOption = netOption; // 設置 NetOption (連線配置)
- this._socket.CreateConnect(netOption); // 最後再建立 Socket 連線 (TCP/IP => 需先 InitNetSocket 註冊後才能 Handle, Websocket => 透過原先 EventHandler 再進行註冊)
+ this._netStatus = NetStatus.CONNECTING;
+ this._NetStatusHandler(null);
+ this._netOption = netOption;
+ this._connectingHandler?.Invoke();
+ this._netProvider.CreateConnect(netOption);
}
}
- ///
- /// 設置 NetTips (設置其他實作的 NetTips)
- ///
- ///
public void SetNetTips(INetTips netTips)
{
this._netTips = netTips;
}
- private void _NetStatusHandler(object args = null)
+ private void _NetStatusHandler(object args)
{
switch (this._netStatus)
{
@@ -162,13 +128,13 @@ private void _NetStatusHandler(object args = null)
this._netTips?.OnConnecting();
break;
case NetStatus.CONNECTED:
- this._netTips?.OnConnected(args as EventArgs);
+ this._netTips?.OnConnected(args);
break;
case NetStatus.CONNECTION_ERROR:
this._netTips?.OnConnectionError(Convert.ToString(args));
break;
case NetStatus.DISCONNECTED:
- this._netTips?.OnDisconnected(Convert.ToUInt16(args));
+ this._netTips?.OnDisconnected(args);
break;
case NetStatus.RECONNECTING:
this._netTips?.OnReconnecting();
@@ -176,17 +142,17 @@ private void _NetStatusHandler(object args = null)
}
}
- public void OnUpdate()
+ public void OnUpdate(float dt)
{
this._ProcessOutReceive();
this._ProcessHeartBeat();
this._ProcessAutoReconnect();
}
- private void _OnOpen(EventArgs e)
+ private void _OnOpen(object status)
{
this._netStatus = NetStatus.CONNECTED;
- this._NetStatusHandler(e);
+ this._NetStatusHandler(status);
this._isCloseForce = false;
this._ResetAutoReconnect();
@@ -194,11 +160,16 @@ private void _OnOpen(EventArgs e)
this._ResetHeartBeatTicker();
}
- private void _OnMessage(byte[] data)
+ private void _OnBinary(byte[] binary)
{
this._ResetOutReceiveTicker();
+ this._responseBinaryHandler?.Invoke(binary);
+ }
- this._responseHandler?.Invoke(data);
+ private void _OnMessage(string text)
+ {
+ this._ResetOutReceiveTicker();
+ this._responseMessageHandler?.Invoke(text);
}
private void _OnError(string msg)
@@ -207,33 +178,31 @@ private void _OnError(string msg)
this._NetStatusHandler(msg);
}
- private void _OnClose(ushort code)
+ private void _OnClose(object status)
{
this._netStatus = NetStatus.DISCONNECTED;
- this._NetStatusHandler(code);
+ this._NetStatusHandler(status);
this._StopTicker();
if (this._isCloseForce) return;
this._StartAutoReconnect();
}
- ///
- /// 傳送 Binary Data 至 Server
- ///
- ///
- ///
public bool Send(byte[] buffer)
{
- return this._socket.Send(buffer);
+ return this._netProvider.SendBinary(buffer);
}
- ///
- /// 關閉 Socket
- ///
- public void CloseSocket()
+ public bool Send(string text)
+ {
+ return this._netProvider.SendMessage(text);
+ }
+
+ public void Close()
{
this._isCloseForce = true;
- if (this._socket != null) this._socket.Close();
+ if (this._netProvider != null)
+ this._netProvider.Close();
}
///
@@ -242,32 +211,41 @@ public void CloseSocket()
///
public bool IsConnected()
{
- if (this._socket == null) return false;
- return this._socket.IsConnected();
+ if (this._netProvider == null) return false;
+ return this._netProvider.IsConnected();
+ }
+
+ ///
+ /// 設置接收的 Handler (Binary)
+ ///
+ ///
+ public void SetResponseBinaryHandler(ResponseHandler handler)
+ {
+ this._responseBinaryHandler = handler;
}
///
- /// 設置接收的 Handler
+ /// 設置接收的 Handler (Text)
///
- ///
- public void SetResponseHandler(ResponseHandler rh)
+ ///
+ public void SetResponseMessageHandler(ResponseHandler handler)
{
- this._responseHandler = rh;
+ this._responseMessageHandler = handler;
}
///
- /// 設置第一次初始寄送封包的 Handler
+ /// 設置每次連線中的 Handler
///
- ///
- public void SetFirstSendHandler(FirstSendHandler fsh)
+ ///
+ public void SetConnectingHandler(ConnectingHandler handler)
{
- this._firstSendHandler = fsh;
+ this._connectingHandler = handler;
}
#region 超時 Ticker 處理
- public void SetOutReciveAction(Action outReciveAction)
+ public void SetOutReceiveAction(Action outReceiveAction)
{
- this._outReceiveAction = outReciveAction;
+ this._outReceiveAction = outReceiveAction;
}
public void SetOutReceiveTickerTime(float time)
@@ -350,7 +328,7 @@ private void _ResetAutoReconnect()
private void _StartAutoReconnect()
{
this._netStatus = NetStatus.RECONNECTING;
- this._NetStatusHandler();
+ this._NetStatusHandler(null);
this._reconnectTicker.Play();
this._reconnectTicker.SetTick(this._reconnectTick);
@@ -369,7 +347,7 @@ private void _ProcessAutoReconnect()
{
if (this._reconnectTicker.IsTickTimeout())
{
- this._socket.Close();
+ this._netProvider.Close();
this.Connect(this._netOption);
if (this._autoReconnectCount > 0) this._autoReconnectCount -= 1;
@@ -382,7 +360,7 @@ private void _ProcessAutoReconnect()
else
{
this._netStatus = NetStatus.DISCONNECTED;
- this._NetStatusHandler();
+ this._NetStatusHandler(null);
this._reconnectTicker.Stop();
}
}
@@ -400,9 +378,9 @@ private void _StopTicker()
public void Dispose()
{
- if (this._socket != null)
- this._socket.Close();
- this._socket = null;
+ if (this._netProvider != null)
+ this._netProvider.Close();
+ this._netProvider = null;
this._netTips = null;
this._netOption = null;
this._hearBeatTicker = null;
@@ -411,7 +389,7 @@ public void Dispose()
this._outReceiveAction = null;
this._reconnectTicker = null;
this._reconnectAction = null;
- this._responseHandler = null;
+ this._responseBinaryHandler = null;
}
~NetNode()
@@ -419,5 +397,4 @@ public void Dispose()
this.Dispose();
}
}
-}
-
+}
\ No newline at end of file
diff --git a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Concrete/NetNode.cs.meta b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetNode/NetNode.cs.meta
similarity index 100%
rename from Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Concrete/NetNode.cs.meta
rename to Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetNode/NetNode.cs.meta
diff --git a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption.meta b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption.meta
new file mode 100644
index 00000000..a38ba231
--- /dev/null
+++ b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption.meta
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 00a41bb16b92ee84ba58e93f7671a4f4
+folderAsset: yes
+DefaultImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/TcpNetOption.cs b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/TcpNetOption.cs
new file mode 100644
index 00000000..25794add
--- /dev/null
+++ b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/TcpNetOption.cs
@@ -0,0 +1,14 @@
+namespace OxGFrame.NetFrame
+{
+ public class TcpNetOption : NetOption
+ {
+ public string host { get; set; }
+ public int port { get; set; }
+
+ public TcpNetOption(string host, int port, int autoReconnectCount = -1) : base(autoReconnectCount)
+ {
+ this.host = host;
+ this.port = port;
+ }
+ }
+}
diff --git a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/TcpNetOption.cs.meta b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/TcpNetOption.cs.meta
new file mode 100644
index 00000000..0878bfd5
--- /dev/null
+++ b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/TcpNetOption.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: c5099630645b1cf43a28e887372a2bce
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/WebsocketNetOption.cs b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/WebsocketNetOption.cs
new file mode 100644
index 00000000..75b6826b
--- /dev/null
+++ b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/WebsocketNetOption.cs
@@ -0,0 +1,12 @@
+namespace OxGFrame.NetFrame
+{
+ public class WebsocketNetOption : NetOption
+ {
+ public string url { get; set; }
+
+ public WebsocketNetOption(string url, int autoReconnectCount = -1) : base(autoReconnectCount)
+ {
+ this.url = url;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/WebsocketNetOption.cs.meta b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/WebsocketNetOption.cs.meta
new file mode 100644
index 00000000..15680669
--- /dev/null
+++ b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetOption/WebsocketNetOption.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 1f97631c1af44be4c8d2df64cf85da9c
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetProvider.meta b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetProvider.meta
new file mode 100644
index 00000000..68593eba
--- /dev/null
+++ b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetProvider.meta
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 81ed76e9e53b0114496135c64f45f642
+folderAsset: yes
+DefaultImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/TcpSock.cs b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetProvider/TcpNetProvider.cs
similarity index 79%
rename from Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/TcpSock.cs
rename to Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetProvider/TcpNetProvider.cs
index ead64c83..c4a58cbc 100644
--- a/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/TcpSock.cs
+++ b/Assets/OxGFrame/NetFrame/Scripts/Runtime/Core/Implement/NetProvider/TcpNetProvider.cs
@@ -8,30 +8,42 @@ namespace OxGFrame.NetFrame
{
public delegate void WaitReadNetPacket();
- public class TcpSock : ISocket
+ public class TcpNetProvider : INetProvider
{
private const int _CONNECTING_TIMEOUT_MSEC = 10000;
private const int _MAX_BUFFER_SIZE = 65536;
private const int _MAX_FAILED_CONNECTION_COUNT = 3;
private TcpClient _tcp = null;
- private NetOption _netOption = null;
private int _failedConnectionCount;
private int _readBufferOffset = 0;
private byte[] _readBuffer = null;
- public event EventHandler OnOpen;
- public event EventHandler OnMessage;
+ public event EventHandler