From 5635b0d770383333238f2dcdc0824392aefdc537 Mon Sep 17 00:00:00 2001
From: client_Wu Xijin <364452445@qq.com>
Date: 星期一, 10 十二月 2018 11:39:55 +0800
Subject: [PATCH] 3335 准备跨服登录流程。
---
Core/NetworkPackage/Socket/ClientSocket.cs | 825 ++++++++++++++++++++++++++++------------------------------
1 files changed, 395 insertions(+), 430 deletions(-)
diff --git a/Core/NetworkPackage/Socket/ClientSocketController.cs b/Core/NetworkPackage/Socket/ClientSocket.cs
similarity index 76%
rename from Core/NetworkPackage/Socket/ClientSocketController.cs
rename to Core/NetworkPackage/Socket/ClientSocket.cs
index c006144..243d4f2 100644
--- a/Core/NetworkPackage/Socket/ClientSocketController.cs
+++ b/Core/NetworkPackage/Socket/ClientSocket.cs
@@ -1,430 +1,395 @@
-锘縰sing UnityEngine;
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Net;
-using System.Net.Sockets;
-using System.Threading;
-
-public class ClientSocketController
-{
- Socket m_Socket;
- public Socket socket {
- get { return m_Socket; }
- }
-
- private int connectWaitTime = 2000; // 10绉�
- private Thread m_packageThread;
- private byte[] bufferBytes = new byte[4096]; // 4K锛屽崟鍖呭瓧鑺傛暟缁勭紦瀛�
- private byte[] fragmentBytes; //鐣欏寘鍚庣殑鍐呭
- private long getBytesTotal = 0; //鍙戦�佺殑鏁版嵁鎬婚噺
- private long sendBytesTotal = 0; //鍙戦�佺殑鏁版嵁鎬婚噺
-
- public bool Connected { get { return m_Socket == null ? false : m_Socket.Connected; } }
-
- public bool isStopTreading { get; private set; }
-
- string ip;
- int port;
- Action onConnected = null;
-
- public ClientSocketController()
- {
- }
-
- public void Connect(string _ip, int _port, Action _onConnected)
- {
- try
- {
- ip = _ip;
- port = _port;
- onConnected = _onConnected;
- Dns.BeginGetHostAddresses(_ip, OnGetHostAddresses, null);
- }
- catch (Exception e)
- {
- DebugEx.LogError(e.Message);
- }
- }
-
- private void OnGetHostAddresses(IAsyncResult _result)
- {
- var ipAddresses = Dns.EndGetHostAddresses(_result);
-
- if (ipAddresses[0].AddressFamily == AddressFamily.InterNetworkV6)
- {
- DebugEx.Log("褰撳墠浣跨敤鐨勭綉缁�: IPV6");
- m_Socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- }
- else
- {
- DebugEx.Log("褰撳墠浣跨敤鐨勭綉缁�: IPV4");
- m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- }
-
- var ipEndPoint = new IPEndPoint(ipAddresses[0], port);
- if (ipEndPoint == null)
- {
- Debug.Log("IpEndPoint is null");
- }
-
- var state = new SocketAsyncState();
- state.IsAsync = true;
- m_Socket.BeginConnect(ipEndPoint, new AsyncCallback(ConnectCallBack), state);
- }
-
- /// <summary>
- /// 閾炬帴鎴愬姛鏃剁殑鍥炶皟
- /// </summary>
- /// <param name="_result"></param>
- private void ConnectCallBack(IAsyncResult _result)
- {
- if (!_result.IsCompleted)
- {
- DebugEx.Log("閾炬帴瓒呮椂锛�");
- CloseConnect();
- }
- else
- {
- if (m_Socket != null && m_Socket.Connected)
- {
- DebugEx.Log("纭鐨勯摼鎺ュ疄鐜�");
- OnConnectSuccess();
-
- if (onConnected != null)
- {
- onConnected();
- onConnected = null;
- }
- }
- else
- {
- if (m_Socket != null)
- {
- m_Socket.Disconnect(true);
- }
-
- onConnected = null;
- }
- }
- }
-
- /// <summary>
- /// 鍏抽棴閾炬帴
- /// </summary>
- public void CloseConnect()
- {
- try
- {
- isStopTreading = true;
- if (m_packageThread != null)
- {
- m_packageThread.Abort();
- }
- }
- catch (System.Exception ex)
- {
- DebugEx.Log(ex);
- }
-
- try
- {
- if (m_Socket != null && m_Socket.Connected)
- {
- m_Socket.Shutdown(SocketShutdown.Both);
- m_Socket.Close();
- }
-
- }
- catch (System.Exception ex)
- {
- DebugEx.Log(ex);
- }
-
- sendQueue.Clear();
- m_Socket = null;
-
- }
-
- /// <summary>
- /// 閾炬帴鎴愬姛
- /// </summary>
- private void OnConnectSuccess()
- {
- if (m_packageThread != null)
- {
- m_packageThread.Abort();
- m_packageThread = null;
- }
- m_packageThread = new Thread(new ThreadStart(ReceiveInfo)); // 鍚姩绾跨▼鎺ユ敹淇℃伅
- m_packageThread.IsBackground = true;
- m_packageThread.Start();
- isStopTreading = false;
- }
-
-
- /// <summary>
- /// 鎺ユ敹淇℃伅
- /// </summary>
- private void ReceiveInfo()
- {
- int vDataLeng;
- byte[] vBytes;
-
- while (!isStopTreading)
- {
- if (!m_Socket.Connected)
- {
- isStopTreading = true;
- m_Socket.Shutdown(SocketShutdown.Both);
- m_Socket.Close();
- break;
- }
-
- try
- {
- vDataLeng = m_Socket.Receive(bufferBytes);
- if (vDataLeng <= 0)
- {
- isStopTreading = true;
- m_Socket.Shutdown(SocketShutdown.Both);
- m_Socket.Close();
- break;
- }
- getBytesTotal += vDataLeng;
- vBytes = new byte[vDataLeng];
- Array.Copy(bufferBytes, 0, vBytes, 0, vDataLeng);
- ReadInfo(vBytes);
- }
- catch (Exception e)
- {
- DebugEx.Log(e);
- }
- }
-
- }
-
- static byte[] vCmdBytes = new byte[2];
- /// <summary>
- /// 闃呰淇℃伅
- /// </summary>
- /// <param name="vBytes"></param>
- private void ReadInfo(byte[] vBytes)
- {
- try
- {
- byte[] fixBytes = vBytes;
- // 濡傛灉瀛樺湪鐣欏寘锛屽垯骞跺寘
- if (fragmentBytes != null && fragmentBytes.Length > 0)
- {
- Array.Resize(ref fixBytes, vBytes.Length + fragmentBytes.Length);
- Array.Copy(fragmentBytes, 0, fixBytes, 0, fragmentBytes.Length);
- Array.Copy(vBytes, 0, fixBytes, fragmentBytes.Length, vBytes.Length);
- }
- fragmentBytes = null; // 娓呯悊鎺夌暀鍖�
- // 鍒嗗寘
- int vReadIndex = 0; // 鍒濆鎸囬拡
- byte[] vPackBytes;
- int vLeavingLeng = 0;
- int vBodyLeng = 0;
- int vTotalLeng = fixBytes.Length;
- GameNetPackBasic vNetpack;
-
- while (vReadIndex < vTotalLeng)
- {
- vLeavingLeng = vTotalLeng - vReadIndex;
- if (vLeavingLeng < 6) // 鏈鍚堝寘鐨勬渶浣庨檺搴﹀瓧鑺傞噺, 鐣欏寘
- {
- fragmentBytes = new byte[vLeavingLeng];
- Array.Copy(fixBytes, vReadIndex, fragmentBytes, 0, vLeavingLeng);
- break;
- }
- vBodyLeng = BitConverter.ToInt32(fixBytes, vReadIndex + 2);
- if (vBodyLeng > vLeavingLeng - 6)// 鏈畬鏁寸殑鍖呭垯鐣欏寘
- {
- fragmentBytes = new byte[vLeavingLeng];
- Array.Copy(fixBytes, vReadIndex, fragmentBytes, 0, vLeavingLeng);
- break;
- }
- vPackBytes = new byte[vBodyLeng];
- Array.Copy(fixBytes, vReadIndex + 6, vPackBytes, 0, vBodyLeng); // 鎻愬彇鍖呯殑瀛楄妭鍐呭
- // 瀹屾暣鐨勫寘鍒欒鍖�
-
- vPackBytes = GameNetEncode.BaseXorSub(vPackBytes);
- Array.Copy(vPackBytes, 0, vCmdBytes, 0, 2);
- var cmd = (ushort)((ushort)(vCmdBytes[0] << 8) + vCmdBytes[1]);
- if (PackageRegedit.Contain(cmd))
- {
- vNetpack = PackageRegedit.TransPack(cmd, vPackBytes);
- if (vNetpack != null)
- {
- if (DebugEx.EnableNetLog)
- {
- DebugEx.NetLogFormat("鏀跺寘锛歿0}", vNetpack.GetType().Name);
- }
-
- GameNetSystem.Instance.PushPackage(vNetpack);
- }
- }
- else
- {
-#if UNITY_EDITOR
- PackageRegedit.TransPack(cmd, vPackBytes);
-#endif
- CSharpCallLua.OnRecieveLuaNetPackage(cmd, vPackBytes);
- }
-
- vReadIndex += 6 + vBodyLeng;
- }
- }
- catch (Exception ex)
- {
- Debug.LogFormat("鏀跺寘寮傚父锛歿0}", ex);
- }
-
- }
-
- /// <summary>
- /// 鍙戦�佷俊鎭�
- /// </summary>
- /// <param name="vNetPack"></param>
- public void SendInfo(GameNetPackBasic vNetPack)
- {
- if (!Connected)
- return;
- if (vNetPack == null)
- {
- DebugEx.LogError("瑕佸彂鐨勪俊鎭璞′负绌�");
- return;
- }
-
- if (DebugEx.EnableNetLog)
- {
- DebugEx.NetLogFormat("鍙戝寘锛歿0}", vNetPack.GetType().Name);
- }
-
- if (vNetPack.cmd == (ushort)0x03FE || vNetPack.cmd == (ushort)0x1801)
- {
- }
-
- if (vNetPack.combineBytes == null)
- {
- vNetPack.WriteToBytes();
- }
- vNetPack.CombineDatas();
-#if UNITY_EDITOR
- NetPkgCtl.AddNetPkg(vNetPack.vInfoCont, NetPkgType.Client, vNetPack.ToString(), FieldPrint.PrintFields(vNetPack), FieldPrint.PrintFieldsExpand(vNetPack, true));
-#endif
- sendBytesTotal += vNetPack.combineBytes.Length;
- SendBytes(vNetPack.combineBytes);
- }
-
- /// <summary>
- /// 鍙戦�佷俊鎭�
- /// </summary>
- /// <param name="vBytes"></param>
- public void SendInfo(byte[] vBytes)
- {
- if (!Connected)
- {
- DebugEx.LogError("灏氭湭涓庤鍚庣閾炬帴锛佹棤娉曞彂閫佷俊鎭�");
- return;
- }
- if (vBytes == null || vBytes.Length < 2)
- {
- DebugEx.LogError("瑕佸彂鐨勪俊鎭暟鎹负绌烘垨鏁版嵁涓嶈冻");
- return;
- }
-
- vBytes = GameNetEncode.BaseXorAdd(vBytes);
- byte[] vFrameHead = new byte[] { 255, 204 };
- byte[] vMsgBodyLength = BitConverter.GetBytes(vBytes.Length);
- byte[] vTotal = new byte[vBytes.Length + 6];
- Array.Copy(vFrameHead, 0, vTotal, 0, vFrameHead.Length);
- Array.Copy(vMsgBodyLength, 0, vTotal, 2, vMsgBodyLength.Length);
- Array.Copy(vBytes, 0, vTotal, 6, vBytes.Length);
-
- SendBytes(vTotal);
- }
-
- /// <summary>
- /// 鍙戦�佸凡鍔犲瘑瀛楄妭
- /// </summary>
- /// <param name="vBytes"></param>
- public void SendNoEncrypInfo(byte[] vBytes)
- {
- if (!Connected)
- {
- DebugEx.LogError("灏氭湭涓庤鍚庣閾炬帴锛佹棤娉曞彂閫佷俊鎭�");
- return;
- }
- if (vBytes == null || vBytes.Length < 2)
- {
- DebugEx.LogError("瑕佸彂鐨勪俊鎭暟鎹负绌烘垨鏁版嵁涓嶈冻");
- return;
- }
-
- SendBytes(vBytes);
- }
-
- Queue<byte[]> sendQueue = new Queue<byte[]>();
-
- private void SendBytes(byte[] bytes)
- {
- try
- {
- if (sendQueue.Count > 0)
- {
- sendQueue.Enqueue(bytes);
- }
- else
- {
- m_Socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(SendInfoCallBack), m_Socket);
- }
- }
- catch
- {
- DebugEx.LogError("鍙戦�佹椂鍙戠敓寮傚父");
- }
- }
-
- /// <summary>
- /// 鍙戦�佸畬鎴愮殑鍥炶皟
- /// </summary>
- /// <param name="vAsyncSend"></param>
- private void SendInfoCallBack(IAsyncResult vAsyncSend)
- {
- try
- {
- if (sendQueue.Count > 0)
- {
- var bytes = sendQueue.Dequeue();
- m_Socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(SendInfoCallBack), m_Socket);
- }
- }
- catch (Exception ex)
- {
- DebugEx.Log(ex);
- }
-
- }
-
- internal class SocketAsyncState
- {
- /// <summary>
- /// 鏄惁瀹屾垚銆�
- /// </summary>
- public bool Completed { get; set; }
-
- /// <summary>
- /// 鏁版嵁
- /// </summary>
- public byte[] Data { get; set; }
- /// <summary>
- /// 鏄惁寮傛
- /// </summary>
- public bool IsAsync { get; set; }
- }
-
-
-}
+锘縰sing UnityEngine;
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Net;
+using System.Net.Sockets;
+using System.Threading;
+
+public class ClientSocket
+{
+ GameNetEncode encoder = new GameNetEncode();
+ Socket m_Socket;
+ public Socket socket { get { return m_Socket; } }
+
+ private Thread m_packageThread;
+ private byte[] bufferBytes = new byte[4096]; // 4K锛屽崟鍖呭瓧鑺傛暟缁勭紦瀛�
+ private byte[] fragmentBytes; //鐣欏寘鍚庣殑鍐呭
+ private long getBytesTotal = 0; //鍙戦�佺殑鏁版嵁鎬婚噺
+ private long sendBytesTotal = 0; //鍙戦�佺殑鏁版嵁鎬婚噺
+
+ public bool connected { get { return m_Socket == null ? false : m_Socket.Connected; } }
+
+ GameNetSystem.SocketType socketType = GameNetSystem.SocketType.Main;
+ DateTime m_LastPackageTime;
+ public DateTime lastPackageTime { get { return m_LastPackageTime; } }
+
+ bool isStopTreading = false;
+
+ string ip;
+ int port;
+ Action onConnected = null;
+
+ public ClientSocket(GameNetSystem.SocketType type)
+ {
+ this.socketType = type;
+ }
+
+ public void Connect(string _ip, int _port, Action _onConnected)
+ {
+ try
+ {
+ ip = _ip;
+ port = _port;
+ onConnected = _onConnected;
+ Dns.BeginGetHostAddresses(_ip, OnGetHostAddresses, null);
+ }
+ catch (Exception e)
+ {
+ DebugEx.LogError(e.Message);
+ }
+ }
+
+ private void OnGetHostAddresses(IAsyncResult _result)
+ {
+ var ipAddresses = Dns.EndGetHostAddresses(_result);
+
+ if (ipAddresses[0].AddressFamily == AddressFamily.InterNetworkV6)
+ {
+ DebugEx.Log("褰撳墠浣跨敤鐨勭綉缁�: IPV6");
+ m_Socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
+ }
+ else
+ {
+ DebugEx.Log("褰撳墠浣跨敤鐨勭綉缁�: IPV4");
+ m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ }
+
+ var ipEndPoint = new IPEndPoint(ipAddresses[0], port);
+ if (ipEndPoint == null)
+ {
+ Debug.Log("IpEndPoint is null");
+ }
+
+ m_Socket.BeginConnect(ipEndPoint, new AsyncCallback(ConnectCallBack), null);
+ }
+
+ /// <summary>
+ /// 閾炬帴鎴愬姛鏃剁殑鍥炶皟
+ /// </summary>
+ /// <param name="_result"></param>
+ private void ConnectCallBack(IAsyncResult _result)
+ {
+ if (!_result.IsCompleted)
+ {
+ DebugEx.Log("閾炬帴瓒呮椂锛�");
+ CloseConnect();
+ }
+ else
+ {
+ if (m_Socket != null && m_Socket.Connected)
+ {
+ DebugEx.Log("纭鐨勯摼鎺ュ疄鐜�");
+ OnConnectSuccess();
+
+ if (onConnected != null)
+ {
+ onConnected();
+ onConnected = null;
+ }
+ }
+ else
+ {
+ if (m_Socket != null)
+ {
+ m_Socket.Disconnect(true);
+ }
+
+ onConnected = null;
+ }
+ }
+ }
+
+ /// <summary>
+ /// 鍏抽棴閾炬帴
+ /// </summary>
+ public void CloseConnect()
+ {
+ try
+ {
+ isStopTreading = true;
+ if (m_packageThread != null)
+ {
+ m_packageThread.Abort();
+ }
+ }
+ catch (System.Exception ex)
+ {
+ DebugEx.Log(ex);
+ }
+
+ try
+ {
+ if (m_Socket != null && m_Socket.Connected)
+ {
+ m_Socket.Shutdown(SocketShutdown.Both);
+ m_Socket.Close();
+ }
+ }
+ catch (System.Exception ex)
+ {
+ DebugEx.Log(ex);
+ }
+
+ sendQueue.Clear();
+ m_Socket = null;
+ }
+
+ /// <summary>
+ /// 閾炬帴鎴愬姛
+ /// </summary>
+ private void OnConnectSuccess()
+ {
+ if (m_packageThread != null)
+ {
+ m_packageThread.Abort();
+ m_packageThread = null;
+ }
+
+ m_LastPackageTime = DateTime.Now;
+ m_packageThread = new Thread(new ThreadStart(ReceiveInfo)); // 鍚姩绾跨▼鎺ユ敹淇℃伅
+ m_packageThread.IsBackground = true;
+ m_packageThread.Start();
+ isStopTreading = false;
+ }
+
+ /// <summary>
+ /// 鎺ユ敹淇℃伅
+ /// </summary>
+ private void ReceiveInfo()
+ {
+ while (!isStopTreading)
+ {
+ try
+ {
+ var shutdown = false;
+ if (!m_Socket.Connected)
+ {
+ shutdown = true;
+ }
+
+ if (!shutdown)
+ {
+ var dataLength = m_Socket.Receive(bufferBytes);
+ if (dataLength <= 0)
+ {
+ shutdown = true;
+ }
+ else
+ {
+ getBytesTotal += dataLength;
+ var bytes = new byte[dataLength];
+ Array.Copy(bufferBytes, 0, bytes, 0, dataLength);
+ ReadInfo(bytes);
+ }
+ }
+
+ if (shutdown)
+ {
+ isStopTreading = true;
+ m_Socket.Shutdown(SocketShutdown.Both);
+ m_Socket.Close();
+ }
+ }
+ catch (Exception e)
+ {
+ DebugEx.Log(e);
+ }
+ }
+
+ }
+
+ static byte[] vCmdBytes = new byte[2];
+ /// <summary>
+ /// 闃呰淇℃伅
+ /// </summary>
+ /// <param name="vBytes"></param>
+ private void ReadInfo(byte[] vBytes)
+ {
+ try
+ {
+ byte[] fixBytes = vBytes;
+ // 濡傛灉瀛樺湪鐣欏寘锛屽垯骞跺寘
+ if (fragmentBytes != null && fragmentBytes.Length > 0)
+ {
+ Array.Resize(ref fixBytes, vBytes.Length + fragmentBytes.Length);
+ Array.Copy(fragmentBytes, 0, fixBytes, 0, fragmentBytes.Length);
+ Array.Copy(vBytes, 0, fixBytes, fragmentBytes.Length, vBytes.Length);
+ }
+
+ fragmentBytes = null; // 娓呯悊鎺夌暀鍖�
+ int vReadIndex = 0; // 鍒濆鎸囬拡
+ byte[] vPackBytes;
+ int vLeavingLeng = 0;
+ int vBodyLeng = 0;
+ int vTotalLeng = fixBytes.Length;
+ GameNetPackBasic vNetpack;
+
+ while (vReadIndex < vTotalLeng)
+ {
+ vLeavingLeng = vTotalLeng - vReadIndex;
+ if (vLeavingLeng < 6) // 鏈鍚堝寘鐨勬渶浣庨檺搴﹀瓧鑺傞噺, 鐣欏寘
+ {
+ fragmentBytes = new byte[vLeavingLeng];
+ Array.Copy(fixBytes, vReadIndex, fragmentBytes, 0, vLeavingLeng);
+ break;
+ }
+ vBodyLeng = BitConverter.ToInt32(fixBytes, vReadIndex + 2);
+ if (vBodyLeng > vLeavingLeng - 6)// 鏈畬鏁寸殑鍖呭垯鐣欏寘
+ {
+ fragmentBytes = new byte[vLeavingLeng];
+ Array.Copy(fixBytes, vReadIndex, fragmentBytes, 0, vLeavingLeng);
+ break;
+ }
+ vPackBytes = new byte[vBodyLeng];
+ Array.Copy(fixBytes, vReadIndex + 6, vPackBytes, 0, vBodyLeng); // 鎻愬彇鍖呯殑瀛楄妭鍐呭
+ // 瀹屾暣鐨勫寘鍒欒鍖�
+
+ vPackBytes = encoder.BaseXorSub(vPackBytes);
+ Array.Copy(vPackBytes, 0, vCmdBytes, 0, 2);
+ var cmd = (ushort)((ushort)(vCmdBytes[0] << 8) + vCmdBytes[1]);
+ if (PackageRegedit.Contain(cmd))
+ {
+ vNetpack = PackageRegedit.TransPack(cmd, vPackBytes);
+ if (vNetpack != null)
+ {
+ if (DebugEx.EnableNetLog)
+ {
+ DebugEx.NetLogFormat("鏀跺寘锛歿0}", vNetpack.GetType().Name);
+ }
+
+ m_LastPackageTime = DateTime.Now;
+ GameNetSystem.Instance.PushPackage(vNetpack, this.socketType);
+ }
+ }
+ else
+ {
+#if UNITY_EDITOR
+ PackageRegedit.TransPack(cmd, vPackBytes);
+#endif
+ CSharpCallLua.OnRecieveLuaNetPackage(cmd, vPackBytes);
+ }
+
+ vReadIndex += 6 + vBodyLeng;
+ }
+ }
+ catch (Exception ex)
+ {
+ Debug.LogFormat("鏀跺寘寮傚父锛歿0}", ex);
+ }
+
+ }
+
+ /// <summary>
+ /// 鍙戦�佷俊鎭�
+ /// </summary>
+ public void SendInfo(GameNetPackBasic protocol)
+ {
+ if (!connected)
+ {
+ return;
+ }
+
+ if (protocol == null)
+ {
+ DebugEx.LogError("瑕佸彂鐨勪俊鎭璞′负绌�");
+ return;
+ }
+
+ if (DebugEx.EnableNetLog)
+ {
+ DebugEx.NetLogFormat("鍙戝寘锛歿0}", protocol.GetType().Name);
+ }
+
+ if (protocol.combineBytes == null)
+ {
+ protocol.WriteToBytes();
+ }
+ protocol.CombineDatas(encoder);
+#if UNITY_EDITOR
+ NetPkgCtl.AddNetPkg(protocol.vInfoCont, NetPkgType.Client, protocol.ToString(), FieldPrint.PrintFields(protocol), FieldPrint.PrintFieldsExpand(protocol, true));
+#endif
+ sendBytesTotal += protocol.combineBytes.Length;
+ SendBytes(protocol.combineBytes);
+ }
+
+ /// <summary>
+ /// 鍙戦�佷俊鎭�
+ /// </summary>
+ /// <param name="vBytes"></param>
+ public void SendInfo(byte[] vBytes)
+ {
+ if (!connected)
+ {
+ DebugEx.LogError("灏氭湭涓庤鍚庣閾炬帴锛佹棤娉曞彂閫佷俊鎭�");
+ return;
+ }
+
+ if (vBytes == null || vBytes.Length < 2)
+ {
+ DebugEx.LogError("瑕佸彂鐨勪俊鎭暟鎹负绌烘垨鏁版嵁涓嶈冻");
+ return;
+ }
+
+ vBytes = encoder.BaseXorAdd(vBytes);
+ byte[] vFrameHead = new byte[] { 255, 204 };
+ byte[] vMsgBodyLength = BitConverter.GetBytes(vBytes.Length);
+ byte[] vTotal = new byte[vBytes.Length + 6];
+ Array.Copy(vFrameHead, 0, vTotal, 0, vFrameHead.Length);
+ Array.Copy(vMsgBodyLength, 0, vTotal, 2, vMsgBodyLength.Length);
+ Array.Copy(vBytes, 0, vTotal, 6, vBytes.Length);
+
+ SendBytes(vTotal);
+ }
+
+ Queue<byte[]> sendQueue = new Queue<byte[]>();
+ private void SendBytes(byte[] bytes)
+ {
+ try
+ {
+ if (sendQueue.Count > 0)
+ {
+ sendQueue.Enqueue(bytes);
+ }
+ else
+ {
+ m_Socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(SendInfoCallBack), m_Socket);
+ }
+ }
+ catch
+ {
+ DebugEx.LogError("缃戠粶鏁版嵁鍖呭彂閫佸紓甯�");
+ }
+ }
+
+ /// <summary>
+ /// 鍙戦�佸畬鎴愮殑鍥炶皟
+ /// </summary>
+ /// <param name="vAsyncSend"></param>
+ private void SendInfoCallBack(IAsyncResult vAsyncSend)
+ {
+ try
+ {
+ if (sendQueue.Count > 0)
+ {
+ var bytes = sendQueue.Dequeue();
+ m_Socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(SendInfoCallBack), m_Socket);
+ }
+ }
+ catch (Exception ex)
+ {
+ DebugEx.Log(ex);
+ }
+ }
+
+}
--
Gitblit v1.8.0