| File was renamed from Core/NetworkPackage/Socket/ClientSocketController.cs |
| | |
| | | using System.Net.Sockets;
|
| | | using System.Threading;
|
| | |
|
| | | public class ClientSocketController
|
| | | public class ClientSocket |
| | | {
|
| | | GameNetEncode encoder = new GameNetEncode(); |
| | | Socket m_Socket;
|
| | | public Socket socket {
|
| | | get { return 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 connected { get { return m_Socket == null ? false : m_Socket.Connected; } } |
| | |
|
| | | public bool isStopTreading { get; private set; }
|
| | | 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 ClientSocketController()
|
| | | public ClientSocket(GameNetSystem.SocketType type) |
| | | {
|
| | | this.socketType = type; |
| | | }
|
| | |
|
| | | public void Connect(string _ip, int _port, Action _onConnected)
|
| | |
| | | Debug.Log("IpEndPoint is null");
|
| | | }
|
| | |
|
| | | var state = new SocketAsyncState();
|
| | | state.IsAsync = true;
|
| | | m_Socket.BeginConnect(ipEndPoint, new AsyncCallback(ConnectCallBack), state);
|
| | | m_Socket.BeginConnect(ipEndPoint, new AsyncCallback(ConnectCallBack), null); |
| | | }
|
| | |
|
| | | /// <summary>
|
| | |
| | | m_Socket.Shutdown(SocketShutdown.Both);
|
| | | m_Socket.Close();
|
| | | }
|
| | |
|
| | | }
|
| | | catch (System.Exception ex)
|
| | | {
|
| | |
| | |
|
| | | sendQueue.Clear();
|
| | | m_Socket = null;
|
| | |
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | |
| | | 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()
|
| | | {
|
| | | 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)
|
| | | 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();
|
| | | break;
|
| | | }
|
| | | getBytesTotal += vDataLeng;
|
| | | vBytes = new byte[vDataLeng];
|
| | | Array.Copy(bufferBytes, 0, vBytes, 0, vDataLeng);
|
| | | ReadInfo(vBytes);
|
| | | }
|
| | | catch (Exception e)
|
| | | {
|
| | |
| | | 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;
|
| | |
| | | Array.Copy(fixBytes, vReadIndex + 6, vPackBytes, 0, vBodyLeng); // 提取包的字节内容
|
| | | // 完整的包则读包
|
| | |
|
| | | vPackBytes = GameNetEncode.BaseXorSub(vPackBytes);
|
| | | vPackBytes = encoder.BaseXorSub(vPackBytes); |
| | | Array.Copy(vPackBytes, 0, vCmdBytes, 0, 2);
|
| | | var cmd = (ushort)((ushort)(vCmdBytes[0] << 8) + vCmdBytes[1]);
|
| | | if (PackageRegedit.Contain(cmd))
|
| | |
| | | DebugEx.NetLogFormat("收包:{0}", vNetpack.GetType().Name);
|
| | | }
|
| | |
|
| | | GameNetSystem.Instance.PushPackage(vNetpack);
|
| | | m_LastPackageTime = DateTime.Now; |
| | | GameNetSystem.Instance.PushPackage(vNetpack, this.socketType); |
| | | }
|
| | | }
|
| | | else
|
| | |
| | | /// <summary>
|
| | | /// 发送信息
|
| | | /// </summary>
|
| | | /// <param name="vNetPack"></param>
|
| | | public void SendInfo(GameNetPackBasic vNetPack)
|
| | | public void SendInfo(GameNetPackBasic protocol) |
| | | {
|
| | | if (!Connected)
|
| | | if (!connected) |
| | | { |
| | | return;
|
| | | if (vNetPack == null)
|
| | | } |
| | | |
| | | if (protocol == null) |
| | | {
|
| | | DebugEx.LogError("要发的信息对象为空");
|
| | | return;
|
| | |
| | |
|
| | | if (DebugEx.EnableNetLog)
|
| | | {
|
| | | DebugEx.NetLogFormat("发包:{0}", vNetPack.GetType().Name);
|
| | | DebugEx.NetLogFormat("发包:{0}", protocol.GetType().Name); |
| | | }
|
| | |
|
| | | if (vNetPack.cmd == (ushort)0x03FE || vNetPack.cmd == (ushort)0x1801)
|
| | | if (protocol.combineBytes == null) |
| | | {
|
| | | protocol.WriteToBytes(); |
| | | }
|
| | |
|
| | | if (vNetPack.combineBytes == null)
|
| | | {
|
| | | vNetPack.WriteToBytes();
|
| | | }
|
| | | vNetPack.CombineDatas();
|
| | | protocol.CombineDatas(encoder); |
| | | #if UNITY_EDITOR
|
| | | NetPkgCtl.AddNetPkg(vNetPack.vInfoCont, NetPkgType.Client, vNetPack.ToString(), FieldPrint.PrintFields(vNetPack), FieldPrint.PrintFieldsExpand(vNetPack, true));
|
| | | NetPkgCtl.AddNetPkg(protocol.vInfoCont, NetPkgType.Client, protocol.ToString(), FieldPrint.PrintFields(protocol), FieldPrint.PrintFieldsExpand(protocol, true)); |
| | | #endif
|
| | | sendBytesTotal += vNetPack.combineBytes.Length;
|
| | | SendBytes(vNetPack.combineBytes);
|
| | | sendBytesTotal += protocol.combineBytes.Length; |
| | | SendBytes(protocol.combineBytes); |
| | | }
|
| | |
|
| | | /// <summary>
|
| | |
| | | /// <param name="vBytes"></param>
|
| | | public void SendInfo(byte[] vBytes)
|
| | | {
|
| | | if (!Connected)
|
| | | if (!connected) |
| | | {
|
| | | DebugEx.LogError("尚未与该后端链接!无法发送信息");
|
| | | return;
|
| | | }
|
| | | |
| | | if (vBytes == null || vBytes.Length < 2)
|
| | | {
|
| | | DebugEx.LogError("要发的信息数据为空或数据不足");
|
| | | return;
|
| | | }
|
| | |
|
| | | vBytes = GameNetEncode.BaseXorAdd(vBytes);
|
| | | vBytes = encoder.BaseXorAdd(vBytes); |
| | | byte[] vFrameHead = new byte[] { 255, 204 };
|
| | | byte[] vMsgBodyLength = BitConverter.GetBytes(vBytes.Length);
|
| | | byte[] vTotal = new byte[vBytes.Length + 6];
|
| | |
| | | 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
|
| | |
| | | }
|
| | | catch
|
| | | {
|
| | | DebugEx.LogError("发送时发生异常");
|
| | | DebugEx.LogError("网络数据包发送异常"); |
| | | }
|
| | | }
|
| | |
|
| | |
| | | {
|
| | | 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; }
|
| | | }
|
| | |
|
| | |
|
| | | }
|