Пример #1
0
        /// <summary>
        /// Processes incoming data and attempts to recreate the objects that were sent.
        /// Then raises the ReceiveProgress and/or DataArrival events.
        /// Runs in it's own thread to allow receiving to continue unhindered.
        /// </summary>
        private void ProcessIncoming()
        {
            ReceivedPacket packet = null;

            while (true)
            {
                // First lets get the incoming packet we are dealing with
                packet = null;
                lock (ReceivedPackets.SyncRoot)
                {
                    if (ReceivedPackets.Count < 1)
                    {
                        IsProcessingIncomingData = false;
                        return;
                    }
                    packet = ReceivedPackets.PopFront();
                }

                if (Parent.LegacySupport)
                {
                    // Legacy support is enabled, so just alert that it arrived
                    lock (((ICollection)ReceivedBuffer).SyncRoot)
                        ReceivedBuffer.Enqueue(packet.Data);
                    Parent.OnDataArrival(Parent, new DataArrivalEventArgs(packet.Data.LongLength, packet.RemoteEndPoint));
                    continue;
                }

                // Let's get the header
                byte[] data             = packet.Data;
                bool   throwLegacyError = false;
                while (!Header.Completed && (packet.Data != null & packet.Data.Length > 0))
                {
                    if (!Header.ProcessHeader(ref data, ref ProcessingByteBuffer))
                    {
                        throwLegacyError = true;
                        break;
                    }
                    if (data == null || data.Length < 1)
                    {
                        break;
                    }
                }

                // Check for header error
                if (throwLegacyError || (!Header.Completed && ProcessingByteBuffer.Count > 10))
                {
                    Parent.OnErrorReceived(Parent, ErrorReceivedEventArgs.Create(new WinsockException("Unable to determine the size of the incoming packet. You may need to turn on Legacy Support.")));
                    Close();
                    break;
                }

                if (data != null)
                {
                    int receivedSize = data.Length;
                    if (Header.Completed && ProcessingByteBuffer.Count + data.Length >= Header.Size)
                    {
                        // We have the full object that was sent
                        data = ProcessingByteBuffer.Combine(data);
                        ProcessingByteBuffer.Clear();

                        byte[] objectData = null;
                        if (data.Length > Header.Size)
                        {
                            // There is extra data here - get only what we need
                            // then push the rest back on the queue
                            objectData  = ArrayMethods.Shrink(ref data, Header.Size);
                            packet.Data = data;
                            lock (ReceivedPackets.SyncRoot)
                                ReceivedPackets.PushFront(packet);
                        }
                        else
                        {
                            objectData = data;
                        }

                        // Try converting the bytes back to the object.
                        var receivedObject = ObjectPacker.Unpack(objectData);
                        var receivedType   = receivedObject.GetType();
                        if (receivedType == typeof(FileData) || receivedType == typeof(FileDataPart))
                        {
                            // Looks like we are dealing with an incoming file
                            // Handle the data and get a reference to the incoming file
                            FileData file = null;
                            try
                            {
                                file = (receivedType == typeof(FileData)) ?
                                       HandleIncomingFile((FileData)receivedObject) :
                                       HandleIncomingFile((FileDataPart)receivedObject);
                            }
                            catch (Exception ex)
                            {
                                Parent.OnErrorReceived(Parent, ex.AsEventArgs());
                                Close();
                                break;
                            }

                            // This part of the file is done, so we can
                            // reset the header for the next object
                            Header.Reset();
                            if (file != null)
                            {
                                // We've got the file, raise the events
                                // and remove our in progress reference to the file
                                Parent.OnReceiveProgress(Parent, new ReceiveProgressEventArgs(file.LastReceivedSize, file.ReceivedBytes, file.FileSize, packet.RemoteEndPoint));
                                if (file.ReceiveCompleted)
                                {
                                    IncomingFiles.Remove(file.Guid);
                                    lock (((ICollection)ReceivedBuffer).SyncRoot)
                                        ReceivedBuffer.Enqueue(file);
                                    Parent.OnDataArrival(Parent, new DataArrivalEventArgs(file.FileSize, packet.RemoteEndPoint));
                                }
                            }
                        }
                        else
                        {
                            // Incoming object was not a file (could be a byte[])
                            // Store it in the queue and raise the events
                            lock (((ICollection)ReceivedBuffer).SyncRoot)
                                ReceivedBuffer.Enqueue(receivedObject);

                            Parent.OnReceiveProgress(Parent, new ReceiveProgressEventArgs(receivedSize, objectData.Length, Header.Size, packet.RemoteEndPoint));
                            Header.Reset();
                            Parent.OnDataArrival(Parent, new DataArrivalEventArgs(objectData.Length, packet.RemoteEndPoint));
                        }
                    }
                    else
                    {
                        // Either the header wasn't completed, or we haven't got
                        // all of the object yet, either way we need more data
                        // store what we've got into a temporary buffer
                        ProcessingByteBuffer.Add(data);
                        Parent.OnReceiveProgress(Parent, new ReceiveProgressEventArgs(receivedSize, ProcessingByteBuffer.Count, Header.Size, packet.RemoteEndPoint));
                    }
                }
            }

            // Exit the processing thread, and allow another one to be created
            lock (ReceivedPackets.SyncRoot)
                IsProcessingIncomingData = false;
        }