public static void SendMsg(int id, ProtoMessage msg) { if (msgBuffList.Count >= 3) { Debug.Log("消息队列缓冲已满"); return; } proto.MessageHead head = new proto.MessageHead(); head.pid = id; head.msgid = SerialNumber; if (id != ProtoID.C2SLogin) { head.session_id = SessionId; head.gid = PlayerSys.GetSN(); if (head.session_id == 0) { Debug.Log("Send Msg error: Have not session_id"); return; } } else { SerialNumber = 1; head.msgid = SerialNumber; msgBuffList.Clear(); } byte headSize = ( byte )head.ByteSize(); short msgSize = ( short )msg.ByteSize(); // 总包大小 System.Int64 allSize = (System.Int64)(headSize + msgSize + NetMsg.MsgSendHeadSize); byte[] sendBuf = new byte[allSize]; // 写包头 sendBuf[0] = ( byte )( uint )(allSize & 0xff); sendBuf[1] = ( byte )( uint )((allSize >> 8) & 0xff); sendBuf[2] = ( byte )( uint )((allSize >> 16) & 0xff); sendBuf[3] = ( byte )( uint )((allSize >> 32) & 0xff); sendBuf[4] = headSize; // 转换为2进制 head.Serialize(ProtoHeadBuf, 0); msg.Serialize(ProtoDataBuf, 0); System.Buffer.BlockCopy(ProtoHeadBuf, 0, sendBuf, NetMsg.MsgSendHeadSize, headSize); System.Buffer.BlockCopy(ProtoDataBuf, 0, sendBuf, NetMsg.MsgSendHeadSize + headSize, msgSize); // 心跳包不阻塞玩家输入 if (id != ProtoID.C2SHeartBeat && id != ProtoID.C2SSetCustomSetting) { NetworkIndicator.Instance.StartActivityIndicator(); } msgBuffList.Add(sendBuf); if (msgBuffList.Count == 1) { Instance.StartCoroutine(OnPost(msgBuffList[0])); } SerialNumber++; }
// 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(); } }