Esempio n. 1
0
        internal void EndAcceptControlConnection(IAsyncResult ar)
        {
            try {
                string _filePath     = (string)ar.AsyncState;
                Socket _clientSocket = controlSocket.EndAccept(ar);
                NetworkCommunicationManagers.Disconnect(controlSocket);
                controlSocket = _clientSocket;
                byte[] _buffer = packFileMetadata(_filePath);

                NetworkCommunicationManagers.SendIntOverSocket(dataSocket, _buffer.Length);
                NetworkCommunicationManagers.SendByteArrayOverSocket(dataSocket, _buffer);

                fileTransferContainer.FileTransferClassInstance = this;
                managedSendFileOverSockets(_filePath);
            }
            catch (Exception) {
                NetworkCommunicationManagers.Disconnect(controlSocket);
                NetworkCommunicationManagers.Disconnect(dataSocket);
                lock (RunningTransfers) {
                    if (fileTransferContainer.status != FileTransferStatus.Cancelled)
                    {
                        fileTransferContainer.status = FileTransferStatus.Error;
                    }
                }
                return;
            }
        }
Esempio n. 2
0
        internal void EndAcceptDataConnection(IAsyncResult ar)
        {
            try {
                port2 = NetworkCommunicationManagers.FindNextFreeTcpPort();
                Socket _clientSocket = dataSocket.EndAccept(ar);
                NetworkCommunicationManagers.Disconnect(dataSocket);
                dataSocket = _clientSocket;
                NetworkCommunicationManagers.SendIntOverSocket(_clientSocket, port2);

                controlSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                controlSocket.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, port2));
                controlSocket.Listen(1);
                controlSocket.BeginAccept(new AsyncCallback(EndAcceptControlConnection), ar.AsyncState);
            }
            catch (Exception) {
                NetworkCommunicationManagers.Disconnect(dataSocket);
                lock (RunningTransfers) {
                    if (fileTransferContainer.status != FileTransferStatus.Cancelled)
                    {
                        fileTransferContainer.status = FileTransferStatus.Error;
                    }
                }
                return;
            }
        }
Esempio n. 3
0
        internal void managedSendFileOverSockets(string filePath)
        {
            fileTransferContainer.filePath = filePath;

            short       _count   = 0;
            const short _INITIAL = 5000;

            Thread _thread = new Thread(() => monitorControlChannel());

            _thread.Name         = "Control Socket monitor for " + dataSocket.RemoteEndPoint.ToString();
            _thread.IsBackground = true;
            _thread.Start();

            using (FileStream _fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                byte[] _fileBuffer = new byte[FILE_BUFFER_SIZE];

                long _size = _fs.Length;

                try {
                    long _index = 0;

                    //Send the initial chunk of data
                    while (dataSocket.Connected && controlSocket.Connected && _index < _size)
                    {
                        if (fileTransferContainer.status == FileTransferStatus.Paused)
                        {
                            resetEvent.WaitOne();
                        }

                        _fs.Position = _index;
                        int _read = _fs.Read(_fileBuffer, 0, _fileBuffer.Length);

                        try {
                            NetworkCommunicationManagers.SendIntOverSocket(dataSocket, _read);

                            if (_read != _fileBuffer.Length)
                            {
                                byte[] _temp = new byte[_read];
                                Buffer.BlockCopy(_fileBuffer, 0, _temp, 0, _read);
                                NetworkCommunicationManagers.SendByteArrayOverSocket(dataSocket, _temp);
                            }
                            else
                            {
                                NetworkCommunicationManagers.SendByteArrayOverSocket(dataSocket, _fileBuffer);
                            }
                        }
                        catch (SocketException) {
                            continue;
                        }

                        _index += _read;
                        _count++;
                        if (_count % 200 == 0)
                        {
                            lock (RunningTransfers) {
                                if (Math.Round(((((double)_index / _size) * 100) - fileTransferContainer.progress), 1) >= 0.1)
                                {
                                    fileTransferContainer.progress = (float)Math.Round(((double)_index / _size) * 100, 1);
                                }
                            }
                        }
                        if (_count == _INITIAL)
                        {
                            _count = 0;
                            break;
                        }
                    }

                    //Send the remaining data
                    while (dataSocket.Connected && controlSocket.Connected && _index < _size)
                    {
                        _fs.Position = _index;
                        int _read = _fs.Read(_fileBuffer, 0, _fileBuffer.Length);

                        try {
                            NetworkCommunicationManagers.SendIntOverSocket(dataSocket, _read);
                            if (_read != _fileBuffer.Length)
                            {
                                byte[] _temp = new byte[_read];
                                Buffer.BlockCopy(_fileBuffer, 0, _temp, 0, _read);
                                NetworkCommunicationManagers.SendByteArrayOverSocket(dataSocket, _temp);
                            }
                            else
                            {
                                NetworkCommunicationManagers.SendByteArrayOverSocket(dataSocket, _fileBuffer);
                            }
                        }
                        catch (SocketException) {
                            continue;
                        }

                        _index += _read;
                        _count++;
                        if (_count % 200 == 0)
                        {
                            lock (RunningTransfers){
                                if (Math.Round(((((double)_index / _size) * 100) - fileTransferContainer.progress), 1) >= 0.1)
                                {
                                    fileTransferContainer.progress = (float)Math.Round((((double)_index / _size) * 100), 1);
                                }
                            }
                            bool _wait = true;
                            while (true)
                            {
                                lock (syncObject) {
                                    if (acknowledgedTransfers > 0)
                                    {
                                        acknowledgedTransfers--;
                                        _wait = false;
                                        break;
                                    }
                                }
                                if (_wait)
                                {
                                    Thread.Sleep(100);
                                }
                            }
                        }
                    }
                    if (_index == _size)
                    {
                        lock (RunningTransfers) {
                            fileTransferContainer.progress = 100;
                            fileTransferContainer.status   = FileTransferStatus.Finished;
                        }
                    }
                    else
                    {
                        lock (RunningTransfers) {
                            if (fileTransferContainer.status != FileTransferStatus.Cancelled)
                            {
                                fileTransferContainer.status = FileTransferStatus.Error;
                            }
                        }
                    }
                }
                catch {
                    lock (RunningTransfers) {
                        if (fileTransferContainer.status != FileTransferStatus.Cancelled)
                        {
                            fileTransferContainer.status = FileTransferStatus.Error;
                        }
                    }
                }
            }
        }