/// <summary>
        /// Send a get file properties request. Block until response has arrived.
        /// </summary>
        public bool GetFileProperties(string remoteFilePathAndName, out FileProperties properties)
        {
            _lastReceivedFileProperties = null;
            mreListFileProperties.Reset();
            bool sendSuccess = FileTunnelClient.SendMessageCarrier(new MessageCarrier(MessageType.FilePropertiesRequest)
            {
                Payload = remoteFilePathAndName
            });

            if (!sendSuccess)
            {
                properties = null; return(false);
            }
            mreListFileProperties.WaitOne();

            if (_lastReceivedFileProperties != null)
            {
                properties = _lastReceivedFileProperties;
                return(true);
            }

            else
            {
                properties = null;
                return(false);
            }
        }
        public void OnClientMessageCarrierReceived(object sender, MessageCarrier mc)
        {
            if (mc.Type == MessageType.FileNotFound)
            {
                _lastResponseFileNotFound = true;
                _lastReceiveFileSuccess   = false;
                mreDownload.Set();
                mreListFiles.Set();
                mreListDirectories.Set();
                mreListFileProperties.Set();
            }

            else
            {
                _lastResponseFileNotFound = false;
            }

            if (mc.Type == MessageType.FileContents)
            {
                try
                {
                    File.WriteAllBytes(DownloadDirectory + _lastRequestedFile, mc.Payload as byte[]);
                    _lastReceiveFileSuccess = true;
                }
                catch (Exception ex)
                {
                    _lastReceiveFileSuccess = false;
                }

                mreDownload.Set();
            }

            if (mc.Type == MessageType.ListFilesResponse)
            {
                _lastReceivedFileList = mc.Payload as List <string>;
                mreListFiles.Set();
            }

            if (mc.Type == MessageType.ListDirectoriesResponse)
            {
                _lastReceivedDirectoriesList = mc.Payload as List <string>;
                mreListDirectories.Set();
            }

            if (mc.Type == MessageType.FilePropertiesResponse)
            {
                _lastReceivedFileProperties = mc.Payload as FileProperties;
                mreListFileProperties.Set();
            }

            if (mc.Type == MessageType.WatcherFileModified)
            {
                WatcherFileModified.Invoke(mc.Payload as string);
            }
        }
Ejemplo n.º 3
0
        private void OnServerMessageCarrierReceived(object sender, MessageCarrier mc)
        {
            if (mc.Type == MessageType.FileRequest)
            {
                _fileTransferRequestStack.Push(sender as FileTunnelServerConnection, AddPaths(RootDirectory, mc.Payload as string));
            }

            if (mc.Type == MessageType.ListFilesRequest)
            {
                var ftsc = sender as FileTunnelServerConnection;

                try
                {
                    // Run this just to throw exception if path doesn't exist
                    Path.GetFullPath(AddPaths(RootDirectory, mc.Payload as string));

                    var fileList = Directory.GetFiles(AddPaths(RootDirectory, mc.Payload as string)).ToList();
                    ftsc.SendMessageCarrier(new MessageCarrier(MessageType.ListFilesResponse)
                    {
                        Payload = fileList
                    });
                }
                catch (Exception ex)
                {
                    ftsc.SendMessageCarrier(new MessageCarrier(MessageType.FileNotFound)
                    {
                        Payload = (mc.Payload as string)
                    });
                }
            }

            if (mc.Type == MessageType.ListDirectoriesRequest)
            {
                var ftsc = sender as FileTunnelServerConnection;

                try
                {
                    // Run this just to throw exception if path doesn't exist
                    Path.GetFullPath(AddPaths(RootDirectory, mc.Payload as string));

                    var directoryList = Directory.GetDirectories(AddPaths(RootDirectory, mc.Payload as string)).ToList();

                    for (int i = 0; i < directoryList.Count; i++)
                    {
                        directoryList[i] = directoryList[i] + "\\";
                    }

                    ftsc.SendMessageCarrier(new MessageCarrier(MessageType.ListDirectoriesResponse)
                    {
                        Payload = directoryList
                    });
                }
                catch (Exception ex)
                {
                    ftsc.SendMessageCarrier(new MessageCarrier(MessageType.FileNotFound)
                    {
                        Payload = (mc.Payload as string)
                    });
                }
            }

            if (mc.Type == MessageType.FilePropertiesRequest)
            {
                var ftsc = sender as FileTunnelServerConnection;

                try
                {
                    // Run this just to throw exception if path doesn't exist
                    Path.GetFullPath(AddPaths(RootDirectory, mc.Payload as string));

                    var fileProperties = new FileProperties(AddPaths(RootDirectory, mc.Payload as string));
                    ftsc.SendMessageCarrier(new MessageCarrier(MessageType.FilePropertiesResponse)
                    {
                        Payload = fileProperties
                    });
                }
                catch (Exception ex)
                {
                    ftsc.SendMessageCarrier(new MessageCarrier(MessageType.FileNotFound)
                    {
                        Payload = (mc.Payload as string)
                    });
                }
            }

            if (mc.Type == MessageType.WatchDirectoryRequest)
            {
                var ftsc = sender as FileTunnelServerConnection;

                if (_directoryWatcherSubscribers == null)
                {
                    _directoryWatcherSubscribers = new List <FileTunnelServerConnection>();
                }

                _directoryWatcherSubscribers.Add(ftsc);

                if (_directoryWatcher.EnableRaisingEvents == false)
                {
                    _directoryWatcher.EnableRaisingEvents = true;
                }
            }
        }