ProtocolBackModel IProtocol.RecieveDataProcess(ConnectModel connect, int len) { ProtocolBackModel model = new ProtocolBackModel(); var strArray = Encoding.Default.GetBytes("hello world"); byte[] array = new byte[len + strArray.Length]; Array.Copy(strArray, array, strArray.Length); Array.Copy(connect.rData, 0, array, strArray.Length, len); model.Array = array; model.Status = BackStatus.WAITNEXT; return(model); }
void ResultCallBace(IAsyncResult asyncCall) { try { ProtocolBackModel res = null; ConnectModel connect = asyncCall.AsyncState as ConnectModel; var len = connect.client.EndReceive(asyncCall); for (int i = 0; i < Protocol.Count; i++) { var _protocol = Protocol[i]; res = _protocol.RecieveDataProcess(connect, len); if (res.Array != null && res.Array.Length > 0) { connect.client.Send(res.Array); } if (res.Status != BackStatus.ABANDONED) //这个协议对本次请求有特殊要求 { break; //跳出循环 } } //此处预留后期扩展成协议池 switch (res.Status) { case BackStatus.ABANDONED: connect.client.Shutdown(SocketShutdown.Both); break; case BackStatus.NOOP: break; case BackStatus.WAITNEXT: connect.client.BeginReceive(connect.rData, 0, BufLen, 0, ResultCallBace, connect); break; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }