コード例 #1
0
        public void SaveLoadSvFileTransferResult()
        {
            // arrange
            const FileTransferRequestResult refResult = FileTransferRequestResult.Accepted;
            var         refSid       = new FileTransferId(81289);
            const ulong refToken     = 120982;
            const int   refBlockSize = 8192;
            var         msg          = new SvFileTransferResult
            {
                Result    = refResult,
                SessionId = refSid,
                Token     = refToken,
                BlockSize = refBlockSize,
            };

            // act
            MultiprotocolSaveLoad(msg, () =>
            {
                // assert
                Assert.AreEqual(msg.Result, refResult);
                Assert.AreEqual(msg.SessionId, refSid);
                Assert.AreEqual(msg.Token, refToken);
                Assert.AreEqual(msg.BlockSize, refBlockSize);
            });
        }
コード例 #2
0
        private void OnClFileTransferRequest(ClFileTransferRequest msg, ClientId id)
        {
            var sender = clients[id];
            SocketClientBase receiver;

            if (!nameToClient.TryGetValue(msg.Username, out receiver))
            {
                Root.Log(LogLevel.Warning, "Server: client {0} requested file transfer " +
                         "to unknown client {1}", id, msg.Username);
                var result = new SvFileTransferResult
                {
                    Result    = FileTransferRequestResult.Rejected,
                    SessionId = FileTransferId.InvalidId,
                    Token     = msg.Token
                };
                SendTo(id, result);
                return;
            }
            var ft = ftsContainer.CreateSession(msg.Token, sender.Login, receiver.Login, msg.FileHash, msg.FileSize);

            ft.SrcName   = msg.FileName;
            ft.BlockSize = SelectBlockSize(msg.BlockSize);
            var request = new SvFileTransferRequest
            {
                Username  = sender.Login,
                FileName  = Path.GetFileName(msg.FileName),
                FileHash  = msg.FileHash,
                FileSize  = msg.FileSize,
                BlockSize = ft.BlockSize,
                SessionId = ft.Id
            };

            SendTo(receiver.Id, request);
        }
コード例 #3
0
        private void OnSvFileTransferResult(SvFileTransferResult msg)
        {
            OutcomingFileTransfer oft;

            if (!pendingFts.TryGetValue(msg.Token, out oft))
            {
                Root.Log(LogLevel.Error, "FileTransferManager: session not found [token={0}]", msg.Token);
                return;
            }
            switch (msg.Result)
            {
            case FileTransferRequestResult.Accepted:
                pendingFts.Remove(msg.Token);
                oft.SetId(msg.SessionId);
                oft.State     = FileTransferState.Working;
                oft.BlockSize = msg.BlockSize;
                oft.Reader    = new BlockFileReader(oft.Name, oft.BlockSize);
                assignedFts.Add(oft.Id, oft);
                break;

            default: // rejected or ...
                oft.State = FileTransferState.Cancelled;
                oft.Delete();
                break;
            }
            OnTransferStateChanged(oft);
        }
コード例 #4
0
        private void OnClFileTransferRespond(ClFileTransferRespond msg, ClientId id)
        {
            var cl = clients[id];
            var ft = ftsContainer.GetSessionById(msg.SessionId);

            if (ft == null)
            {
                Root.Log(LogLevel.Warning, "Server: bad file transfer session id ({0}) received from client {1} ",
                         msg.SessionId, cl.Login);
                // XXX: send result
                return;
            }
            SocketClientBase fileSender;

            if (!nameToClient.TryGetValue(ft.Sender, out fileSender))
            {
                Root.Log(LogLevel.Warning, "Server: client {0} attempted to send file transfer verification result " +
                         "to disconnected client {1}", cl.Login, ft.Receiver);
                // XXX: suspend session (cl is offline)
                return;
            }
            var result = new SvFileTransferResult
            {
                Result = msg.Result,
                Token  = ft.Token
            };

            if (msg.Result == FileTransferRequestResult.Accepted)
            {
                var newBlockSize = Math.Min(SelectBlockSize(msg.BlockSize), ft.BlockSize);
                ft.BlockSize     = newBlockSize;
                ft.State         = FileTransferState.Working;
                result.BlockSize = newBlockSize;
                result.SessionId = msg.SessionId;
            }
            else
            {
                ftsContainer.DeleteSession(ft.Id);
                result.SessionId = FileTransferId.InvalidId;
            }
            SendTo(fileSender.Id, result);
        }