// 游戏服务器 public static void ConnectGameServer() { ProtoMsg.MsgNetAddress address = LoginDC.GetGameServerNetInfo(); if (address == null) { IGGDebug.Log("未获取游戏服务器ip"); return; } // 发送登录请求 Communicate.Disconnect(); if (Communicate.IsConnected() == false) { Communicate.SetGSConnetorGame(true); NetConnectState.SetConnetOverTime(3.0f, OnConnectOverTime); string ip = address.u32Ip.ToString(); if (!isDomainAddrIp(address.u32Ip.ToString().ToCharArray())) { // data.netAdd.ip 转 ip 地址 ip = IntToIp((Int32)address.u32Ip); } IGGDebug.Log("游戏服务器:" + ip + "," + address.u32Port); Communicate.Connect2GS(ip, (int)address.u32Port); } }
private void AsyncConnectCallback(IAsyncResult ar) { Socket s = (Socket)ar.AsyncState; try { s.EndConnect(ar); } catch (SocketException e) { IGGDebug.Log(e.ToString()); } if (!mIsConnecting) { this.Disconnect(); NotifyConnectResult(false); return; } bool success = s.Connected; mIsConnected = success; NotifyConnectResult(success); if (!success) { this.Disconnect(); } }
/// <summary> /// 异步连接 /// </summary> /// <param name="ip">目标IP</param> /// <param name="port">目标端口</param> public void NonblockConnect(string ip, int port) { if (mSocket == null) { mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 512 * 1024); mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 512 * 1024); } if (mSocket.Connected) { // 已经处于连接状态了 return; } try { IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(ip), port); mSocket.Blocking = false; // 标记正处于连接中 mIsConnecting = true; mSocket.BeginConnect(remoteEndPoint, new AsyncCallback(AsyncConnectCallback), mSocket); } catch (Exception e) { IGGDebug.Log(e.ToString()); } }
/// <summary> /// 载入场景 /// </summary> /// <param name="sceneName">待载入的场景名称</param> /// <param name="data">进入场景的传入数据<see cref="ILoading"/></param> /// <param name="sync">通过异步(false)方式还是同步(true)方式载入</param> /// <param name="force">是否需要保证载入成功(如果当前正在载入某个场景中,会仍到队列而不是直接失败)</param> /// <returns>成功处理则返回true</returns> public static bool Load(string sceneName, SceneData data = null, bool sync = false, bool force = false) { if (bLoading == true) { return(false); } // 如果当前正处于目标场景,无视 // 任务副本需要做特殊处理 IScene scene = sceneLoadControl[sceneName]; // 标记正在载入场景 s_loadingIScene = scene; bLoading = false; if (scene == s_CurIScene) { return(true); } bLoading = true; // 异步载入方式 if (!sync) { ResourceManger.AsyncGo.StartCoroutine(NonsyncLoad(sceneName, data)); return(true); } // 取得载入器 IScene preScene = s_CurIScene; s_CurIScene = scene; // 同步载入(不会播放任何动画) try { if (null != preScene) { preScene.Clear(); } // // 欲载入的处理 scene.PrepareLoad(); // 2 载入新的场景 scene.Load(data); // 3 数据切换回来 s_preScene = preScene; scene.BuildScene(); scene.Start(); } catch (Exception e) { IGGDebug.Log(e.ToString()); } // 载入完毕,进行垃圾回收 bLoading = false; Resources.UnloadUnusedAssets(); System.GC.Collect(); return(true); }
private static void OnConnect(bool succ) { if (succ == true) { if (Communicate.IsConnectedGS() == true) { IGGDebug.Log("连接游戏服务器"); LoginDC.SendGSMsgCL2GS(); } else { IGGDebug.Log("连接登录服务器"); LoginDC.SendLoginMsgCL2LS(); } } else { IGGDebug.Log("连接服务器失败"); if (g_failfp != null) { g_failfp(); g_failfp = null; } } }
private static void OnDisConnect() { IGGDebug.Log("失去连接"); if (g_failLiknfp != null) { g_failLiknfp(); } }
private static void OnConnectOverTime() { IGGDebug.Log("连接超时"); if (g_failfp != null) { g_failfp(); g_failfp = null; } }
/// <summary> /// 登入GS 回调 /// </summary> // 保存登录gs 服务器消息 public static void RecvMsgGS2CLLoginReply(eMsgTypes Type, object Info) { if (Info == null) { return; } g_GSLogin = Info as ProtoMsg.MsgCl2gsLoginReply; IGGDebug.Log("连接游戏服务器成功"); }
//[MenuItem("版本发布/拷贝AssetBundle到StreamingAssets目录")] /*public static void CopyAssets() * { * FullVersionResource.CopyAssets(); * }*/ // 整包版本。 public static void CreateFullVersion(string pathPlatform = "", string pathPackage = "", bool development = false, bool autoConnectProfiler = false, bool isApk = true) { /*FullVersionResource.CopyAssets(); * if (ConstantData.EnablePatch) { * FullVersionResource.InitPatch(); * }*/ FullVersionResource.Build(pathPlatform, pathPackage, development, autoConnectProfiler, isApk); IGGDebug.Log(PlayerSettings.productName + "游戏发布完成"); OpenApp(); }
/// <summary> /// 请求登入回调 /// </summary> public static void RecvMsgLS2CLLoginReply(eMsgTypes Type, object Info) { if (Info == null) { return; } g_LSLogin = Info as ProtoMsg.MsgCl2lgLoginReply; IGGDebug.Log("连接登入服务器成功"); // 请求链接GS LoginM.ConnectGameServer(); }
// 获取一个缓冲 public static NetBuffer GetNetBuffer(int BufferSize) { for (int i = 0; i < g_SendCache.Count; i++) { if (g_SendCache [i].IsUsed == false && BufferSize < g_SendCache [i].BufferSize) { return(g_SendCache[i]); } } IGGDebug.Log("缓冲池开小了,请扩容"); return(null); }
/// <summary> /// 注册服务端触发事件 /// </summary> public static void RegisterHooks(eMsgTypes type, DataHook pf) { if (pf == null) { IGGDebug.Log("注册的Hook 非法"); return; } bool bHaveDataHook = false; int nAddHookIndex = 0; Dictionary <int, DataHook> .Enumerator iter = g_IndexDataHook.GetEnumerator(); while (iter.MoveNext()) { if (iter.Current.Value == pf) { bHaveDataHook = true; nAddHookIndex = iter.Current.Key; break; } } iter.Dispose(); if (!bHaveDataHook) { g_IndexDataHook[g_nHookIndex] = pf; nAddHookIndex = g_nHookIndex; g_nHookIndex++; } if (g_DataHookAddTemp == null) { g_DataHookAddTemp = new Dictionary <eMsgTypes, List <int> >(new msgtypeMsgTypeComparer()); } if (!g_DataHookAddTemp.ContainsKey(type)) { g_DataHookAddTemp[type] = new List <int>(); } List <int> listHookTemp = g_DataHookAddTemp[type]; if (!listHookTemp.Contains(nAddHookIndex)) { listHookTemp.Add(nAddHookIndex); } }
/// <summary> /// 抽取子字节串 /// </summary> public static void ByteArrayTake(byte[] srcBytes, byte[] destBytes, int destIndex, int takeCount) { if ((takeCount < 0) || (srcBytes.Length < takeCount)) { throw new Exception(string.Format("Bad ByteArrayTake {0} {1}.", srcBytes.Length, takeCount)); } try { Array.Copy(srcBytes, 0, destBytes, destIndex, takeCount); } catch (Exception e) { IGGDebug.Log(e.ToString()); } }
public void SendRawPkt(NetBuffer nb) { //非法判断 if (mSocket == null) { IGGDebug.Log("socket is invalid."); return; } // 如果根本就没有连接,那么不用发送了 if (!this.IsConnected) { return; } // 当前还有没发出去的包,为了保证顺序,我们这次也别尝试了,直接插入到Pending列表中 if (mPendingSendPkt.Count > 0) { mPendingSendPkt.Add(nb); } // 尝试发送一次 else { // 尝试发送这个数据包 try { int sentBytes = mSocket.Send(nb.Pbuffer, nb.HaveSendBytes, nb.NeedSendBuffSize, SocketFlags.None); // 如果没有完整地把包发送出去 if (sentBytes < nb.NeedSendBuffSize) { NetConnectState.SendDataState(false); } else { NetConnectState.SendDataState(true); } nb.CGNetBuffer(); } catch (Exception e) { IGGDebug.LogError(e); } } }
/// <summary> /// 注册DC到数据中心 /// </summary> public static void RegisterDCHooks(eMsgTypes type, DataHook pf) { if (pf == null) { IGGDebug.Log("注册的Hook 非法"); return; } if (g_DcHook.ContainsKey(type) == false) { g_DcHook.Add(type, pf); } else { IGGDebug.Log("不同dc有注册到相同的存储函数"); } }
// 登录服务器 public static void ConnectLoginServer() { // 发送登录请求 if (Communicate.IsConnected() == false) { Communicate.Disconnect(); Communicate.SetGSConnetorGame(false); NetConnectState.SetConnetOverTime(3.0f, OnConnectOverTime); //Communicate.Connect2GS(g_ip, g_port); ServerInfo sInfo = GetServerInfo(); if (null != sInfo) { Communicate.Connect2GS(sInfo.iP, sInfo.Port); } else { IGGDebug.Log("未获取登入服务器ip"); } } }
/// <summary> /// 派发服务端事件,也可以用于模拟服务端触发事件 /// </summary> /// <param name="type">proto 枚举类型</param> /// <param name="obj">proto协议结构</param> public static void DispatchEvent(eMsgTypes type, object obj) { RegisterHooksTempToRealHooks(); if (g_DataHook.ContainsKey(type)) { List <int> listHook = g_DataHook[type]; for (int nCnt = 0; nCnt < listHook.Count; ++nCnt) { int nHookIndex = listHook[nCnt]; #if UNITY_EDITOR if (g_IndexDataHook.ContainsKey(nHookIndex)) { g_IndexDataHook[nHookIndex](type, obj); } #else try { if (g_IndexDataHook.ContainsKey(nHookIndex)) { g_IndexDataHook[nHookIndex](type, obj); } } catch (Exception e) { IGGDebug.Log(e.ToString()); //当上面s_IndexDataHook回调出现问题时,此回调就会被移除,小心成为坑 listHook.Remove(nHookIndex); --nCnt; if (listHook.Count == 0) { g_DataHook.Remove(type); break; } } #endif } } AntiRegisterHooksFrDelTemp(); }
/// <summary> /// 同步连接 /// </summary> /// <param name="ip">目标IP</param> /// <param name="port">目标端口</param> public void Connect(string ip, int port) { if (mSocket == null) { mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 512 * 1024); mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 512 * 1024); } if (mSocket.Connected) { return; } // 标记正处于连接中 mIsConnecting = true; mSocket.Blocking = false; try { mSocket.Connect(ip, port); } catch (SocketException e) { IGGDebug.Log(e.ToString()); } bool success = mSocket.Connected; mIsConnected = success; NotifyConnectResult(success); if (!success) { this.Disconnect(); } }
// 数据包反馈超时 public static void WaitDataRespOverTime(int cmd) { IGGDebug.Log("数据反馈包超时"); }
// 网络连接方式切换 public static void NetWorkStateChange(NetworkReachability oldState, NetworkReachability newState) { IGGDebug.Log("网络连接方式切换"); g_bNetWorkStateChange = true; }