Exemple #1
0
        protected async Task ReceiveAsync()
        {
            NetworkStream netstream = client.GetStream();
            int           receivedNum;

            try
            {
                receivedNum = await netstream.ReadAsync(buffer, 0, packager.Length + sizeof(int));
            }
            catch (SocketException se)
            {
                ReceivingAsyncStopEvent?.Invoke(this);
                throw se;
            }
            if (receivedNum == 0)
            {
                //Lost connection
                LostConnectionEvent?.Invoke(this);
                ReceivingAsyncStopEvent?.Invoke(this);
                receiving = false;
                return;
            }
            if (receivedNum < packager.Length + sizeof(int))
            {
                ReceiveHeaderFailEvent?.Invoke(this);
                ReceivingAsyncStopEvent?.Invoke(this);
                receiving = false;
                return;
            }
            byte[] tem = new byte[packager.Length + sizeof(int)];
            Array.Copy(buffer, 0, tem, 0, packager.Length + sizeof(int));
            if (!packager.Check(tem))
            {
                ReceiveHeaderFailEvent?.Invoke(this);
                receiving = false;
                return;
            }
            byte[] len = new byte[4];
            Array.Copy(tem, packager.Length, len, 0, sizeof(int));
            int length = ByteConverter.Byte2Int(len);

            remainReceiveLength = length;
            int readsize = remainReceiveLength < bufferSize ? remainReceiveLength : bufferSize;

            while (remainReceiveLength > 0)
            {
                try
                {
                    receivedNum = await netstream.ReadAsync(buffer, 0, readsize);
                }
                catch (SocketException se)
                {
                    ReceivingAsyncStopEvent?.Invoke(this);
                    receiving = false;
                    throw se;
                }
                if (receivedNum == 0)
                {
                    //Lost connection
                    LostConnectionEvent?.Invoke(this);
                    ReceivingAsyncStopEvent?.Invoke(this);
                    receiving = false;
                    return;
                }
                remainReceiveLength -= receivedNum;
                tem = new byte[receivedNum];
                Array.Copy(buffer, tem, receivedNum);
                if (receivedData == null)
                {
                    receivedData = new byte[receivedNum];
                    Array.Copy(tem, 0, receivedData, 0, receivedNum);
                }
                else
                {
                    receivedData = receivedData.Concat(tem).ToArray();
                }
                readsize = remainReceiveLength < bufferSize ? remainReceiveLength : bufferSize;
            }
            ReceiveObjectDoneEvent?.Invoke(this, packager.UnPack(receivedData));
            ReceiveDoneEvent?.Invoke(this, receivedData);
            receivedData = null;
        }
Exemple #2
0
 public void StopReceivingAsync()
 {
     ReceivingAsyncStopEvent?.Invoke(this);
     receiving = false;
 }