Exemplo n.º 1
0
 public bool Enqueue(FileMetaData data)
 {
     if (data == null || data.Path.Length == 0)
     {
         return(false);
     }
     if (_queueItems.ContainsKey(data.Path))
     {
         return(false);
     }
     _queueItems.Add(data.Path, data);
     _queue.Enqueue(data);
     return(true);
 }
Exemplo n.º 2
0
        private void HandleFileUpdate(Connection activeConnection, FileMetaData metaData, BinaryReader networkReader)
        {
            //find location of file on our file system
            string filePath = Path.Join(activeConnection.LocalSyncPath, metaData.Path);

            ReceiveBegin(this, new ServerEventArgs()
            {
                FileData = metaData, FullLocalPath = filePath, Success = false
            });
            try
            {
                //handle delete and rename operations separately
                if (metaData.OperationType == WatcherChangeTypes.Changed || metaData.OperationType == WatcherChangeTypes.Created)
                {
                    //tell client that we would like to receive this file
                    FileInfo localFile = new FileInfo(filePath);

                    //if our copy is older than theirs, take it
                    if (localFile.Exists == false || localFile.LastWriteTimeUtc < metaData.LastWriteTimeUtc)
                    {
                        Logger.Log("Receiving file \"{0}\" from client.", metaData.Path);

                        using (var fileWriter = new BinaryWriter(new BufferedStream(File.Open(filePath, FileMode.Create))))
                        {
                            long remainingBytes = IPAddress.NetworkToHostOrder(networkReader.ReadInt64());
                            do
                            {
                                //next read will be the smaller of the max buffer size or remaining bytes
                                int    bytesToRequest = (BUFFER_SIZE > remainingBytes) ? (int)remainingBytes : BUFFER_SIZE;
                                byte[] buffer         = networkReader.ReadBytes(bytesToRequest);
                                fileWriter.Write(buffer);
                                remainingBytes -= bytesToRequest;
                            } while (remainingBytes > 0);
                        }

                        //change last write to match client file
                        File.SetLastWriteTimeUtc(filePath, metaData.LastWriteTimeUtc);
                        File.SetLastAccessTimeUtc(filePath, metaData.LastAccessTimeUtc);
                        File.SetCreationTimeUtc(filePath, metaData.CreateTimeUtc);
                    }
                }
                else if (metaData.OperationType == WatcherChangeTypes.Renamed)
                {
                    //no need to send file over network if all we're doing is a rename or delete
                    string oldFilePath = Path.Join(activeConnection.LocalSyncPath, metaData.OldPath);
                    if (File.Exists(oldFilePath))
                    {
                        File.Move(oldFilePath, filePath);
                    }
                }
                else if (metaData.OperationType == WatcherChangeTypes.Deleted)
                {
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                }
                ReceiveEnd(this, new ServerEventArgs()
                {
                    FileData = metaData, FullLocalPath = filePath, Success = true
                });
            }
            catch (Exception ex)
            {
                Logger.Log("Thread #{0} encountered issues with {1}: {2}", ServerId, metaData.Path, ex.Message);
                ReceiveEnd(this, new ServerEventArgs()
                {
                    FileData = metaData, FullLocalPath = filePath, Success = false
                });
            }
            finally
            {
            }
        }
Exemplo n.º 3
0
        public ClientSendResult SendFile(FileMetaData data)
        {
            ClientSendResult args = new ClientSendResult();

            args.FileData = data;

            TcpClient client = new TcpClient(_connection.Address, _connection.Port);

#if DEBUG == false
            //timeouts don't work well when you're debugging
            client.SendTimeout = 5000;
#endif

            BufferedStream stream = new BufferedStream(client.GetStream());
            BinaryReader   reader = new BinaryReader(stream);
            BinaryWriter   writer = new BinaryWriter(stream);
            try
            {
                //introduce ourselves
                if (Handshake(reader, writer) == true)
                {
                    args.WasSuccessful = true;

                    //tell server that we would like to send them a file
                    IMessage message = new FileChangedMessage(data);
                    writer.Write(message.ToBytes());

                    //see if server wants the file
                    message = MessageFactory.FromStream(reader);

                    //server says they want the whole load
                    if (message.MessageId == MessageIdentifier.FileRequest)
                    {
                        message = new FileDataMessage();
                        writer.Write(message.ToBytes());

                        string basePath      = _connection.LocalSyncPath;
                        string localFilePath = Path.Join(basePath, data.Path);
                        if (File.Exists(localFilePath))
                        {
                            FileInfo toSend = new FileInfo(localFilePath);
                            using (var fileReader = new BinaryReader(File.OpenRead(localFilePath)))
                            {
                                byte[] buffer;
                                writer.Write(IPAddress.HostToNetworkOrder(toSend.Length));
                                while ((buffer = fileReader.ReadBytes(BUFFER_SIZE)).Length > 0)
                                {
                                    writer.Write(buffer);
                                }
                                writer.Flush();
                            }
                        }
                    }
                }
            }
            catch (EndOfStreamException ex)
            {
                //end of stream exception doesn't necessairly mean that the transfer was not successful so separate out
                //from generic exception
            }
            catch (Exception ex)
            {
                args.WasSuccessful = false;
                _logger.Log("Error: {0}", ex.Message);
            }
            finally
            {
                client.Close();
            }

            return(args);
        }