public static bool Register(int MsgID, PackHandle fn) { if (mDispatcher.ContainsKey(MsgID)) { Debug.Log("msgid:" + MsgID + " is registered"); return(false); } mDispatcher.Add(MsgID, fn); return(true); }
// private static void Post ( byte[] buf ) { // try { // HttpWebRequest r = WebRequest.Create ( string.Format ( "http://{0}:{1}/client", RuntimeSetting.GameServerIp, RuntimeSetting.GameServerPort ) ) as HttpWebRequest; // r.Method = "POST"; // r.ContentType = "application/x-www-form-urlencoded"; // r.AuthenticationLevel = AuthenticationLevel.None; // Stream s = r.GetRequestStream (); // s.Write ( buf, 0, buf.Length ); // s.Close (); // HttpWebResponse response = r.GetResponse () as HttpWebResponse; // if ( response == null ) { // return; // } // s = response.GetResponseStream (); // int len = s.Read ( TempBuffer, 0, 1024 * 32 ); // s.Close (); // OnResponse ( TempBuffer, len ); // } // catch ( System.Exception e ) { // UnityEngine.Debug.LogException ( e ); // } // } // void OnGUI () { // if(GUI.Button(new Rect(0, 0, 100,50), "消息单发")){ // proto.C2SChat msg = new proto.C2SChat (); // msg.channel = Def.ChannelType.World; // msg.content = "你好"; // SendMsg (ProtoID.C2SChat, msg); // } // // if (GUI.Button(new Rect(0,50, 100, 50), "消息补发")){ // StartCoroutine(OnPost(LastPostMsgBuff)); // } // // } private static void OnResponse(byte[] buf, int totalsize) { try { int pid = 0; //Debug.Log("OnResponse:" + totalsize); if (totalsize < NetMsg.MsgReceiveHeadSize) { Debug.LogError("size < NetMsg.MsgReceiveHeadSize:" + totalsize); NetworkIndicator.Instance.StopActivityIndicator(); return; } int correctsize = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 32); if (totalsize != correctsize) { Debug.LogError("size != correctsize," + totalsize + "," + correctsize); NetworkIndicator.Instance.StopActivityIndicator(); return; } // 初始化开始点 int byteIndex = NetMsg.MsgReceiveHeadSize; // 有可能一个包里有多个消息,所以需要循环 while (byteIndex < totalsize) { // proto消息包头 int protoHeadSize = buf[byteIndex]; byteIndex++; // 初始化proto消息头 byte[] mProHeadBuf = new byte[protoHeadSize]; int mProHeadSize = 0; while (byteIndex < totalsize && mProHeadSize < protoHeadSize) { mProHeadBuf[mProHeadSize] = buf[byteIndex]; ++mProHeadSize; ++byteIndex; } // 读完后的大小与服务器的大小是否相同 if (mProHeadSize != protoHeadSize) { Debug.Log("error : mSession.mProHeadSize : " + mProHeadSize + " != ProHeadSize : " + protoHeadSize); return; } // 数据 proto.MessageHead tempHead = new proto.MessageHead(); tempHead.Parse(mProHeadBuf, 0, protoHeadSize); pid = tempHead.pid; if (tempHead.session_id != 0) { SessionId = tempHead.session_id; Gid = tempHead.gid; } // 包体 int first = buf[byteIndex]; ++byteIndex; int second = (buf[byteIndex] << 8); ++byteIndex; // 大小 int DataBufSize = first + second; // 初始化包体 byte[] mDataBuf = new byte[DataBufSize]; int mDataSize = 0; // 取包体 while (byteIndex < totalsize && mDataSize < DataBufSize) { mDataBuf[mDataSize] = buf[byteIndex]; ++mDataSize; ++byteIndex; } // 验证大小 if (mDataSize != DataBufSize) { Debug.Log("error : mSession.mDataSize : " + mDataSize + " != DataBufSize : " + DataBufSize); return; } PackHandle fn = null; //Debug.LogError ( "OnReceivePack:" + pid ); if (!mDispatcher.TryGetValue(pid, out fn)) { //Debug.LogError("dispatcher of pid:" + pid + ", not found"); continue; } if (null == fn) { //Debug.LogError( "dispatcher of pid:" + pid + ", fn == null" ); continue; } // 解析内容 fn(pid, SessionId.ToString(), mDataBuf, mDataSize); if (pid != ProtoID.S2CHeartBeat && pid != ProtoID.S2CSetCustomSetting) { NetworkIndicator.Instance.StopActivityIndicator(); } } } catch (System.Exception er) { Debug.LogException(er); NetworkIndicator.Instance.StopActivityIndicator(); } }