@@ -592,6 +592,8 @@ private async Task<int> readData(Stream stream, byte[] buffer, int startIndex, i
592592 var count = 0 ;
593593 while ( count < totalCount )
594594 {
595+ if ( cancellationToken . IsCancellationRequested )
596+ break ;
595597 var readTask = stream . ReadAsync ( buffer , count + startIndex , totalCount - count , cancellationToken ) ;
596598 ret = await await TaskUtils . TaskWait ( readTask , options . InternalTransportTimeout ) ;
597599 if ( readTask . IsCanceled || ret == 0 )
@@ -629,11 +631,12 @@ protected async Task<ArraySegment<byte>> ReadPackageAsync(CancellationToken toke
629631 MemoryStream splitMs = null ;
630632 while ( true )
631633 {
632- var currentRecvBuffer = recvBuffer ;
633- //读取包头
634- var ret = await readData ( stream , currentRecvBuffer , 0 , PACKAGE_TOTAL_LENGTH_LENGTH , token ) ;
635634 if ( token . IsCancellationRequested )
636635 return nullArraySegment ;
636+
637+ var currentRecvBuffer = recvBuffer ;
638+ //读取包头
639+ var ret = await readData ( stream , currentRecvBuffer , 0 , PACKAGE_TOTAL_LENGTH_LENGTH , token ) ;
637640 if ( ret == 0 )
638641 throw new IOException ( "未读取到数据!" ) ;
639642 if ( ret < PACKAGE_TOTAL_LENGTH_LENGTH )
@@ -653,11 +656,12 @@ protected async Task<ArraySegment<byte>> ReadPackageAsync(CancellationToken toke
653656 throw new ProtocolException ( new ArraySegment < byte > ( recvBuffer , 0 , ret ) , $ "数据包总长度[{ packageTotalLength } ]大于缓存大小[{ recvBuffer . Length } ]") ;
654657 //包体长度
655658 var packageBodyLength = packageTotalLength - PACKAGE_TOTAL_LENGTH_LENGTH ;
656- //读取包体
657- ret = await readData ( stream , recvBuffer , PACKAGE_TOTAL_LENGTH_LENGTH , packageBodyLength , token ) ;
658659
659660 if ( token . IsCancellationRequested )
660661 return nullArraySegment ;
662+ //读取包体
663+ ret = await readData ( stream , recvBuffer , PACKAGE_TOTAL_LENGTH_LENGTH , packageBodyLength , token ) ;
664+
661665 if ( ret < packageBodyLength )
662666 throw new ProtocolException ( new ArraySegment < byte > ( recvBuffer , 0 , PACKAGE_HEAD_LENGTH + ret ) , $ "包体读取错误!包体长度:{ packageBodyLength } ,读取数据长度:{ ret } ") ;
663667
@@ -746,9 +750,6 @@ protected async Task<ArraySegment<byte>> ReadPackageAsync(CancellationToken toke
746750
747751 protected void BeginHeartBeat ( CancellationToken cancellationToken )
748752 {
749- if ( QpPackageHandler_Stream == null )
750- return ;
751-
752753 if ( options . HeartBeatInterval > 0 )
753754 Task . Delay ( options . HeartBeatInterval , cancellationToken ) . ContinueWith ( t =>
754755 {
@@ -932,8 +933,6 @@ protected void BeginReadPackage(CancellationToken token)
932933 var readPackageTask = ReadPackageAsync ( token ) ;
933934 readPackageTask . ContinueWith ( t =>
934935 {
935- if ( QpPackageHandler_Stream == null )
936- return ;
937936 //如果已经取消
938937 if ( t . IsCanceled || token . IsCancellationRequested )
939938 return ;
0 commit comments