using UnityEngine; using System.Collections; using System.Collections.Generic; namespace Engine.Net { /// 网络控制器 public class NetWorkManager { #region 单利 private static NetWorkManager Instance; /// 单利函数 public static NetWorkManager GetInstance() { if (Instance == null) { Instance = new NetWorkManager(); } return Instance; } #endregion /// 接受消息委托 public delegate bool HandleNetMessage(NetMsg iMsg); /// 接受到消息事件 private HandleNetMessage onHandleNetMessage = null; /// 全都得游戏连接 public List mListSocket = new List(); /// 连接个数 private int nSocketCount = 0; public void OnDestroy() { DelSocket(); } /// 创建Socket连接 public void CreateSocket(string strIp, int nPort, ESockeType socketType = ESockeType.Game) { if (!ServerEnum.IsConnectServer) { return; } if (GetNetSocket(socketType) != null) { DelSocket(socketType); /* NetSocket netSocket1 = GetNetSocket(socketType); netSocket1.Connect(strIp, nPort); CDebug.LogError("网络重连 socketType=" + socketType.ToString()); return; CDebug.LogError("网络连接重复 socketType=" + socketType.ToString()); return; */ } NetSocket netSocket = new NetSocket(socketType, ESocketConnectType.TCP); netSocket.Connect(strIp, nPort); mListSocket.Add(netSocket); nSocketCount++; } public void ReConnectSocket(string strIp, int nPort, ESockeType socketType = ESockeType.Game) { if (!ServerEnum.IsConnectServer) { return; } if (GetNetSocket(socketType) != null) { NetSocket netSocket1 = GetNetSocket(socketType); netSocket1.Connect(strIp, nPort); CDebug.LogError("网络重连 socketType=" + socketType.ToString()); } } /// 删除Socket连接 public void DelSocket(ESockeType socketType = ESockeType.Game, bool bReceiveDisconnMsg = true) { NetSocket socket = this.GetNetSocket(socketType); if(socket!=null) { socket.DoDisconnect(bReceiveDisconnMsg); mListSocket.Remove(socket); nSocketCount--; } } /// 添加接受消息事件 public void AddNetMessageEvent(HandleNetMessage handleNetMessage) { onHandleNetMessage = handleNetMessage; } /// 删除接受消息事件 public void DelNetMessageEvent(HandleNetMessage handleNetMessage) { onHandleNetMessage += handleNetMessage; } /// 发送消息 public bool SendMessage(NetMsg netMsg, ESockeType type = ESockeType.Game, int orginId = -1) { if(!ServerEnum.IsConnectServer) { return false; } netMsg.UserId = orginId; //获得消息对象 NetSocket netSocket = this.GetNetSocket(type); //如果连接已经创建 if (netSocket != null) { return netSocket.SendMsg(netMsg); } else { CDebug.LogError("网络连接不存在 socketType=" + type.ToString()); return false; } } /// 帧频函数 public void OnFixedUpdate(int nTime) { //不存在消息处理逻辑,跳出,缓存全部的已经收到消息 if (onHandleNetMessage == null || nSocketCount == 0) { return; } int msgCount = 0; do { //消息对象 NetMsg msg = this.GetRecvMsg(); //没有需要处理的消息了 if(msg==null) { break; } msgCount++; //抛出消息 onHandleNetMessage(msg); } while (true); //if(msgCount>3) //{ //Debug.Log("msgCount=" + msgCount); //} } /// 获得一个已经接受的消息 private NetMsg GetRecvMsg() { NetMsg msg = null; for (int i = 0; i < nSocketCount; i++) { msg = mListSocket[i].RecvMsg(); if (msg != null) { break; } } return msg; } /// 获得指定类型的Socket对象 public NetSocket GetNetSocket(ESockeType type) { for (int i = 0; i < nSocketCount; i++) { return mListSocket[i]; /*if (mListSocket[i].socketType == type) { return mListSocket[i]; }*/ } return null; } } }