//向服务端发送数据包,也就是一个结构体对象 public void SendHeartbeat() { if (gameSocket == null || !gameSocket.Connected) { return; } try { NetMessageHead headObj = new NetMessageHead(); headObj.bMainID = 1; headObj.uMessageSize = 20; headObj.bAssistantID = 1; headObj.bHandleCode = 0; //bReserve=7891 表示IOS发送,bReserve=7892表示安卓发送 #if UNITY_ANDROID headObj.bReserve = 7892; #elif UNITY_IPHONE headObj.bReserve = 7891; #endif byte[] head = LoSocket.StructToBytes(headObj); //计算出新的字节数组的长度 int length = head.Length; //向服务端异步发送这个字节数组 IAsyncResult asyncSend = gameSocket.BeginSend(head, 0, length, SocketFlags.None, new AsyncCallback(sendCallback), gameSocket); } catch (Exception e) { Debug.Log("Game send message error: " + e); } }
// Use this for initialization void Start() { socket = LoSocket.GetInstance(); MSG_GP_R_LogonResult test1 = new MSG_GP_R_LogonResult(); //str = Marshal.SizeOf(test1).ToString(); }
public static LoSocket GetInstance() { if (instance == null) { instance = new LoSocket(); } return(instance); }
void OnApplicationPause(bool Pause) { isPause = Pause; if (Pause) { Global.LastAppHeartBeatTime = 0; //防止网络检查与此处冲突 LoSocket.GetInstance().Pause(); } else { LoSocket.GetInstance().Resume(); } }
public override void OnRegister() { view.init(); ShowConfirmSignal.AddListener(OnShowSignal); lSignal.AddListener(OnRecieveQishu); GetpSignal.AddListener(OnRecieveAward); lrSignal.AddListener(OnLoRecordSignal); RefreshMoneySignal.AddListener(OnRefreshMoney);//刷新金额 监听器 LoSocket.GetInstance().connectSignal.AddListener(OnConnect); logoutSignal.AddListener(OnLogOut); view.UpdateYuelabel(UInfoModel.GetMoney()); recordBackSignal.AddListener(OnRecordBack); }
public override void OnRemove() { lSignal.RemoveListener(OnRecieveQishu); GetpSignal.RemoveListener(OnRecieveAward); lrSignal.RemoveListener(OnLoRecordSignal); RefreshMoneySignal.RemoveListener(OnRefreshMoney);//移出 刷新金额 监听器 ShowConfirmSignal.RemoveListener(OnShowSignal); logoutSignal.RemoveListener(OnLogOut); recordBackSignal.RemoveListener(OnRecordBack); TimeManager.Instance().UnRegister("SelecterMediatorTimer"); LoSocket.GetInstance().connectSignal.RemoveListener(OnConnect); DpanelSignal.Dispatch(); //顺便删除confirm panel Debug.Log("SelecterMediator Mediator OnRemove"); }
void ProcessSocketMsg() { if (socket == null) { socket = LoSocket.GetInstance(); } if (socket.rcevPackages != null && socket.rcevPackages.Count > 0) { Lopackage package = null; lock (socket.syncObj) { package = socket.rcevPackages.Dequeue(); } AnalzyPackage(package); } }
public override void Execute() { GameObject asset = (GameObject)AssetManager.Instance.LoadPrefab("GlobelPanel/Network"); GameObject panel = GameObject.Instantiate(asset) as GameObject; panel.transform.localScale = Vector3.one; asset = (GameObject)AssetManager.Instance.LoadPrefab("GlobelPanel/Sound"); panel = GameObject.Instantiate(asset) as GameObject; panel.transform.localScale = Vector3.one; GameObject go = new GameObject(); go.name = "LoginView"; go.AddComponent <LoginView>(); go.transform.parent = contextview.transform; if (LoSocket.GetInstance().Isconnected()) { go.SetActive(false); } GameObject msgGo = new GameObject(); msgGo.name = "MessageView"; msgGo.AddComponent <MessageView>(); msgGo.transform.parent = contextview.transform; GameObject mainGo = new GameObject(); mainGo.name = "MainView"; mainGo.AddComponent <MainView>(); mainGo.transform.parent = contextview.transform; }
private NetworkManager() { socket = LoSocket.GetInstance(); }
void OnLoginClick(GameObject go) { AudioController.Instance.SoundPlay("active_item"); string username = panelScript.username.value; string password = panelScript.password.value; if (string.IsNullOrEmpty(username.Trim())) { MsgSignal.Dispatch(new MsgPara("用户名不能为空", 2)); } else if (string.IsNullOrEmpty(password.Trim())) { MsgSignal.Dispatch(new MsgPara("密码不能为空", 2)); } else { string passMd5 = Util.GetMd5Hash(password); string temp = PlayerPrefs.GetString("Password", ""); if (!panelScript.IsInputPswd && !string.IsNullOrEmpty(temp)) { passMd5 = temp; } PlayerPrefs.SetString("UserName", username); if (panelScript.savePassword.value) { PlayerPrefs.SetString("Password", passMd5); PlayerPrefs.SetInt("savePassword", 1); } else { PlayerPrefs.SetInt("savePassword", 0); } if (Application.internetReachability == NetworkReachability.NotReachable) { MsgSignal.Dispatch(new MsgPara("无法连接,请检查您的网络", 2)); return; } Global.IsLoginApp = false; LoSocket.GetInstance().ManualConnect(); NetworkManager.Instance.Login(username, passMd5); Loading.GetInstance().ShowLoading("服务器正在验证账号密码中......"); Global.AppLoginViewTryReConnTimes = 0; //重新设定 TimeManager.Instance().Register("checklogin", 8, 4000, 4000, (c, t) => { if (!Global.IsLoginApp) { if (Global.AppLoginViewTryReConnTimes > 0) { PlayerPrefs.DeleteKey("ipListIndex"); } LoSocket.GetInstance().ManualShutDown(); //先关闭 LoSocket.GetInstance().ManualConnect(); //再连接 Global.AppLoginViewTryReConnTimes++; } }); TimeManager.Instance().Register("checkloginClose", 1, 1000, 37000, (c, t) => { if (!Global.IsLoginApp) { MsgSignal.Dispatch(new MsgPara("暂时无法登录服务器,请稍后再试!", 2)); Loading.GetInstance().HideLoading(); } }); } }
//按顺序发送数据包 public void SendPkgQueue() { if (!runSend) { return; } if (gameSocket == null || !gameSocket.Connected) { Debug.Log("Game SendPkgQueue " + gameSocket.Connected + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")); OnSocketProblem(); return; } if (sendPackages.Count > 0) { lock (_syncSendObj) { Sendpackage sendpkg = sendPackages.Peek(); if (sendpkg != null) { try { Debug.Log("Game start send packege mainId:" + sendpkg.msgId + " assId:" + sendpkg.assId); byte[] newByte = null; if (sendpkg.msgPackge != null) //有包体 包头 { byte[] head = LoSocket.StructToBytes(GenerateHead(sendpkg)); //把结构体对象转换成数据包,也就是字节数组 object sendobj = sendpkg.msgPackge; byte[] data = LoSocket.StructToBytes(sendobj); newByte = new byte[head.Length + data.Length]; Array.Copy(head, 0, newByte, 0, head.Length); Array.Copy(data, 0, newByte, head.Length, data.Length); } else //只有包头 { NetMessageHead headObj = new NetMessageHead(); headObj.bMainID = sendpkg.msgId; headObj.uMessageSize = 20; headObj.bAssistantID = sendpkg.assId; headObj.bHandleCode = 0; //bReserve=7891 表示IOS发送,bReserve=7892表示安卓发送 #if UNITY_ANDROID headObj.bReserve = 7892; #elif UNITY_IPHONE headObj.bReserve = 7891; #endif newByte = LoSocket.StructToBytes(headObj); } //计算出新的字节数组的长度 int length = newByte.Length; //向服务端异步发送这个字节数组 gameSocket.SendTimeout = 5000; int sendLength = gameSocket.Send(newByte, 0, length, SocketFlags.None); if (sendLength < length) { gameSocket.Close(); Debug.LogError("Game sendLength<length !"); return; } sendPackages.Dequeue(); } catch (Exception e) { OnSocketProblem(); Debug.Log("Game send message error: " + e); return; } } else { sendPackages.Dequeue(); } } } }
private void SplitPackage(byte[] bytespara) { byte[] bytes; if (buffer != null && buffer.Length > 0) { bytes = new byte[buffer.Length + bytespara.Length]; Array.Copy(buffer, 0, bytes, 0, buffer.Length); Array.Copy(bytespara, 0, bytes, buffer.Length, bytespara.Length); buffer = null; } else { bytes = bytespara; } int index = 0; while (true) { if (bytes.Length - index < HEADSIZE) //剩下的byte数不够一个包头长度 { //如果包头为0表示没有包了,那么跳出循环 buffer = new byte[bytes.Length - index]; Array.Copy(bytes, index, buffer, 0, buffer.Length); //Debug.LogWarning("不够包头跳出循环 buffer大小: " + buffer.Length); break; } //包头 byte[] head = new byte[HEADSIZE]; int headLengthIndex = index + HEADSIZE; //把数据包的包头拷贝出来 Array.Copy(bytes, index, head, 0, HEADSIZE); NetMessageHead headobj = new NetMessageHead(); headobj = (NetMessageHead)LoSocket.BytesToStruct(head, headobj.GetType()); short dataLength = (short)(headobj.uMessageSize - HEADSIZE); //当包体的长度大于0 那么需要依次把相同长度的byte数组拷贝出来 作为包体解析 if (dataLength > 0) { if (bytes.Length - headLengthIndex < dataLength) ////不够解析包体 这种情况为了处理方便连之前的包头也要放到缓存里 { buffer = new byte[bytes.Length - index]; Array.Copy(bytes, index, buffer, 0, buffer.Length); //Debug.LogWarning("跳出循环 buffer大小: " + buffer.Length); break; } } //将索引指向下一个包的包头 index = headLengthIndex + dataLength; // 这里将包头和包存在链表中 Lopackage package = new Lopackage(); package.head = headobj; if (dataLength > 0) { package.objectData = new byte[dataLength]; Array.Copy(bytes, headLengthIndex, package.objectData, 0, dataLength); } Debug.LogWarning("接收到数据: Game mainId: " + headobj.bMainID + " bAssistantID: " + headobj.bAssistantID + " uMessageSize:" + headobj.uMessageSize + " dataSize:" + bytes.Length); lock (_syncObj) rcevPackages.Enqueue(package); } }
public override void OnRemove() { MsgSignal.RemoveListener(OnMsgshow); LoSocket.GetInstance().msgSigal.RemoveListener(OnSocketSignal); }
void AnalzyPackage(Lopackage pkg, bool isGameSocket = false) { if (pkg == null) { return; } NetMessageHead head = pkg.head; //获取消息配置 MsgConfig msgCfg = msgCfgLoader.GetMsgConfig(head.bMainID); if (isGameSocket) { switch (Global.CurrentGameId) { case 10301800: //百家乐 if (baccaratMsgConfigLoader != null) { msgCfg = baccaratMsgConfigLoader.GetMsgConfig(head.bMainID); } break; } } if (msgCfg == null) { if (isGameSocket) { Debug.LogWarning("this messageid: " + head.bMainID + " this bAssistantID: " + head.bAssistantID + " doesn't has its config"); } return; } MsgSubCfg subCfg = msgCfg.GetMsgSubCfg(head.bAssistantID); if (subCfg == null) { if (isGameSocket) { Debug.LogWarning("this messageid: " + head.bMainID + " this bAssistantID: " + head.bAssistantID + " doesn't has its config"); } return; } //根据消息配置 获得处理类 以及 返回数据类型 Type rspType = Type.GetType(subCfg.rspType); if (rspType == null) { Debug.LogError("this messageid: " + head.bMainID + " this bAssistantID: " + head.bAssistantID + " do not have subCfg.rspType"); } Type InterfaceType = typeof(IHandler <>); InterfaceType = InterfaceType.MakeGenericType(rspType); Type handlerType = Type.GetType(subCfg.handler); if (handlerType == null) { Debug.LogError("this messageid: " + head.bMainID + " this bAssistantID: " + head.bAssistantID + " subCfg.handler error"); } object obj = Activator.CreateInstance(handlerType); //将处理类注入 context.injectionBinder.injector.Inject(obj); //调用处理类的 回调 object[] args = new object[2]; args[0] = head; if (subCfg.hasData) { if (pkg.objectData == null) { Debug.LogError("this messageid: " + head.bMainID + " this bAssistantID: " + head.bAssistantID + " pkg.objectData==null"); args[1] = null; //特殊处理 } else if (head.uMessageSize != pkg.objectData.Length + 20) { Debug.LogError("this messageid: " + head.bMainID + " this bAssistantID: " + head.bAssistantID + " pkg.head.uMessageSize != pkg.objectData.Length"); args[1] = null; //特殊处理 } else if (head.uMessageSize == pkg.objectData.Length + 20) { object dataObj = LoSocket.BytesToStruct(pkg.objectData, rspType); args[1] = dataObj; } else { Debug.LogError("this messageid: " + head.bMainID + " this bAssistantID: " + head.bAssistantID + " other error"); args[1] = null; } } else { args[1] = null; } InterfaceType.InvokeMember("OnReceive", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, args); obj = null; }