private void RcvBodyData(IAsyncResult ar) { try { WZPkg pack = (WZPkg)ar.AsyncState; int len = skt.EndReceive(ar); if (len > 0) { pack.bodyIndex += len; if (pack.bodyIndex < pack.bodyLen) { skt.BeginReceive(pack.bodyBuff, pack.bodyIndex, pack.bodyLen - pack.bodyIndex, SocketFlags.None, new AsyncCallback(RcvBodyData), pack); } else { T msg = WZTool.DeSerialize <T>(pack.bodyBuff); OnReciveMsg(msg); //loop recive pack.ResetData(); skt.BeginReceive( pack.headBuff, 0, pack.headLen, SocketFlags.None, new AsyncCallback(RcvHeadData), pack); } } else { OnDisConnected(); Clear(); } } catch (Exception e) { WZTool.LogMsg("RcvBodyError:" + e.Message, LogLevel.Error); } }
public void StartRcvData(Socket skt, Action closeCB) { try { this.skt = skt; this.closeCB = closeCB; OnConnected(); WZPkg pack = new WZPkg(); skt.BeginReceive( pack.headBuff, 0, pack.headLen, SocketFlags.None, new AsyncCallback(RcvHeadData), pack); } catch (Exception e) { WZTool.LogMsg("StartRcvData:" + e.Message, LogLevel.Error); } }