Esempio n. 1
0
        private void handleRequestAppTransferMessage(RequestAppTransferMessage msg, IPAddress sender)
        {
            // see if we have the app
            AppInfo theApp = Library.Apps[msg.appId];

            if (theApp == null)
            {
                Debug.WriteLine(String.Format("{1} requested AppId {0} but I don't have it", msg.appId, sender.ToString()));
                return;
            }

            // find the peer
            SyncPeer peer = _peers.Find((aPeer) => { return(aPeer.Address.Equals(sender)); });

            Debug.WriteLine(String.Format("I will transfer {0} to {1}", msg.appId, peer.Hostname));

            bool isUpdateRequest = msg.existingFiles != null;

            // build manifest
            AppManifest manifest = AppManifest.FromAppInfo(theApp, Library, isUpdateRequest); // todo - async/threaded

            // if peer provided a list of existing files, check our own
            if (isUpdateRequest)
            {
                manifest.RemoveMatchingFiles(msg.existingFiles);
            }

            // verify manifest is valid
            if (manifest == null)
            {
                Debug.WriteLine("Could not build manifest for AppId [" + msg.appId + "], aborting.");
                CancelAppTransferMessage cancelMsg = new CancelAppTransferMessage();
                cancelMsg.transferId = msg.transferId;
                cancelMsg.reason     = "Unable to build manifest";
                _tcpAgent.SendMessage(cancelMsg, sender);
                return;
            }

            // send "start transfer" message with manifest
            StartAppTransferMessage newMsg = new StartAppTransferMessage();

            newMsg.manifest   = manifest;
            newMsg.transferId = msg.transferId;

            _tcpAgent.SendMessage(newMsg, sender);

            // create transfer
            TransferInfo transfer = new TransferInfo(newMsg.transferId);

            transfer.Manifest  = newMsg.manifest;
            transfer.App       = theApp;
            transfer.IsSending = true;
            transfer.Peer      = peer;
            transfer.Port      = msg.listenPort;

            // determine install dir and write
            string manifestRoot = Library.Path;

            Utility.EnsureEndsWithSlash(ref manifestRoot);
            manifestRoot += AppManifest.STEAM_COMMON_DIR + theApp.InstallDir; // todo ensure no path chars in installdir
            Utility.EnsureEndsWithSlash(ref manifestRoot);
            DirectoryInfo diManifest = new DirectoryInfo(manifestRoot);

            if (!diManifest.Exists)
            {
                diManifest.Create();
            }
            transfer.ManifestRoot = diManifest;

            // subscribe to state change notifications (so we can update our status message)
            subscribeTransferEvents(transfer);

            // create agent to send the files
            TransferAgent agent = new TransferAgent();

            agent.StartSend(transfer);

            // fire event to notify listeners a transfer was created
            TransferCreatedEventHandler handler = OnTransferCreated;

            if (handler != null)
            {
                handler(this, new TransferEventArgs(transfer));
            }
        }
Esempio n. 2
0
        public void RequestApp(AppInfo app)
        {
            if (app == null)
            {
                throw new ArgumentNullException("Cannot request null app");
            }

            if (app.InstallDir.Length == 0 || app.InstallDir.Contains(Path.DirectorySeparatorChar) || app.InstallDir.Contains(Path.AltDirectorySeparatorChar))
            {
                throw new ArgumentOutOfRangeException("Cannot request app with invalid install dir [" + app.InstallDir + "]");
            }

            // determine who has a copy of the app
            SyncPeer whoHasIt = null;

            foreach (SyncPeer peer in _peers)
            {
                if (peer.Apps.Contains(app))
                {
                    whoHasIt = peer;
                    break;
                }
            }

            if (whoHasIt == null)
            {
                throw new AppNotAvailableException();
            }

            // determine install dir
            string manifestRoot = Library.Path;

            Utility.EnsureEndsWithSlash(ref manifestRoot);
            manifestRoot += AppManifest.STEAM_COMMON_DIR + app.InstallDir;
            Utility.EnsureEndsWithSlash(ref manifestRoot);
            DirectoryInfo diManifest    = new DirectoryInfo(manifestRoot);
            AppManifest   existingFiles = null;

            if (!diManifest.Exists)
            {
                diManifest.Create();
            }
            else
            {
                // determine which files we already have for this app,
                // and send that as part of the request message
                // todo - build manifest async
                existingFiles = AppManifest.FromDirectory(diManifest.FullName, true);
            }

            // create a transfer
            TransferInfo transfer = new TransferInfo(); // generates a transferid

            transfer.IsSending    = false;
            transfer.Peer         = whoHasIt;
            transfer.App          = app;
            transfer.Port         = Properties.Settings.Default.ListenPort;
            transfer.ManifestRoot = diManifest;

            if (getReceiveTransfersInProgress() == 0)
            { // start immediately if we're not receiving anything else
                Debug.WriteLine("Starting transfer agent to receive " + transfer.App.Name);
                startReceiveApp(transfer, existingFiles);
            }
            else
            { // otherwise add to the queue
                Debug.WriteLine("Queuing transfer of " + transfer.App.Name);
                TransferQueue.Enqueue(transfer);
            }
        }
Esempio n. 3
0
 public static AppManifest FromAppInfo(AppInfo app, AppLibrary library)
 {
     return(FromAppInfo(app, library, false));
 }