123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- namespace Engine.Net
- {
- /// <summary>网络控制器</summary>
- public class NetWorkManager
- {
- #region 单利
- private static NetWorkManager Instance;
- /// <summary>单利函数</summary>
- public static NetWorkManager GetInstance()
- {
- if (Instance == null)
- {
- Instance = new NetWorkManager();
- }
- return Instance;
- }
- #endregion
- /// <summary>接受消息委托</summary>
- public delegate bool HandleNetMessage(NetMsg iMsg);
- /// <summary>接受到消息事件</summary>
- private HandleNetMessage onHandleNetMessage = null;
- /// <summary>全都得游戏连接</summary>
- public List<NetSocket> mListSocket = new List<NetSocket>();
- /// <summary>连接个数</summary>
- private int nSocketCount = 0;
- public void OnDestroy()
- {
- DelSocket();
- }
- /// <summary>创建Socket连接</summary>
- 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());
- }
- }
- /// <summary>删除Socket连接</summary>
- 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--;
- }
- }
- /// <summary>添加接受消息事件</summary>
- public void AddNetMessageEvent(HandleNetMessage handleNetMessage)
- {
- onHandleNetMessage = handleNetMessage;
- }
- /// <summary>删除接受消息事件</summary>
- public void DelNetMessageEvent(HandleNetMessage handleNetMessage)
- {
- onHandleNetMessage += handleNetMessage;
- }
- /// <summary>发送消息</summary>
- 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;
- }
- }
- /// <summary>帧频函数</summary>
- 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);
- //}
- }
- /// <summary>获得一个已经接受的消息</summary>
- private NetMsg GetRecvMsg()
- {
- NetMsg msg = null;
- for (int i = 0; i < nSocketCount; i++)
- {
- msg = mListSocket[i].RecvMsg();
- if (msg != null)
- {
- break;
- }
- }
- return msg;
- }
- /// <summary>获得指定类型的Socket对象</summary>
- 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;
- }
- }
- }
|