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);
            }
        }
Esempio n. 2
0
        public bool SendBroadcastObject <T>(T objectToSend)
        {
            var results = new ConcurrentBag <bool>();
            var mc      = new MessageCarrier(MessageType.Object)
            {
                Payload = objectToSend
            };

            Parallel.ForEach(Connections, c =>
            {
                if (c.IsReady)
                {
                    results.Add(c.SendMessageCarrier(mc));
                }
            });

            return(results.ToArray().All(r => r));
        }
        private void OnDirectoryWatcherChanged(object source, FileSystemEventArgs e)
        {
            var clientSidePathAndName = e.FullPath.Replace(RootDirectory, "");

            if (clientSidePathAndName.Contains(".") == true)    // This is to ignore changes in subdirectories
            {
                if (e.ChangeType == WatcherChangeTypes.Changed || e.ChangeType == WatcherChangeTypes.Created || e.ChangeType == WatcherChangeTypes.Renamed)
                {
                    var mc = new MessageCarrier(MessageType.WatcherFileModified)
                    {
                        Payload = clientSidePathAndName
                    };

                    if (_directoryWatcherPending.Any(p => (string)p.Payload == clientSidePathAndName) == false)
                    {
                        _directoryWatcherPending.Push(mc);
                    }
                }
            }
        }
        public bool SendMessageCarrier <T>(T messageCarrier)
        {
            MessageCarrier mc = messageCarrier as MessageCarrier;

            return(SendMessage(mc));
        }
        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;
                }
            }
        }
Esempio n. 6
0
 private void R_ObjectRecieved(object sender, MessageCarrier e)
 {
     MessageCarrierReceived?.Invoke(sender, e);
 }