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); } }
void ProcessGameSocketMsg() { if (gameSocket == null) { gameSocket = GameSocket.GetInstance(); } gameSocket.SendPkgQueue(); if (gameSocket.rcevPackages != null && gameSocket.rcevPackages.Count > 0) { Lopackage package = null; lock (socket.syncObj) { package = gameSocket.rcevPackages.Dequeue(); } AnalzyPackage(package, true); } }
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)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); } lock (syncObj) rcevPackages.Enqueue(package); } }
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; }