/// <summary>
        /// 发送打包好的消息
        /// </summary>
        /// <param name="cm"></param>
        /// <returns>返回发出的消息包编号</returns>
        public ulong Send(Message cm)
        {
            if (!Client.IsInitialized)
            {
                return(0ul);
            }

            cm.Options |= (ulong)Define.Consts.Cmd_All_Option.EnableNewDataContract;

            MessageEventArgs mea = new MessageEventArgs(cm)
            {
                Message = cm, IsHandled = false, Host = cm.Host
            };

            OnMessageSending(mea);
            if (mea.IsHandled)
            {
                return(mea.Message.PackageNo);
            }

            //判断远程主机是否支持这个模式
            if (!Config.ForceOldContract && cm.Host != null && cm.Host.IsEnhancedContractEnabled)
            {
                Entity.PackedNetworkMessage[] pnm = MessagePackerV2.BuildNetworkMessage(cm);
                PackageEventArgs pea = new PackageEventArgs(pnm.Length > 1, pnm[0], pnm);
                OnPckageSending(pea);
                if (!pea.IsHandled)
                {
                    Array.ForEach(pnm, s => { Client.Send(s); });
                    OnPackageSended(pea);
                }
            }
            else
            {
                Entity.PackedNetworkMessage pn = MessagePacker.BuildNetworkMessage(cm);
                PackageEventArgs            pe = new PackageEventArgs(false, pn, null);
                OnPckageSending(pe);
                if (!pe.IsHandled)
                {
                    Client.Send(pn);
                    OnPackageSended(pe);
                }
            }
            OnMessageSended(mea);

            return(cm.PackageNo);
        }
Beispiel #2
0
        /// <summary>
        /// 接收文件夹
        /// </summary>
        /// <param name="client"></param>
        /// <param name="task"></param>
        /// <param name="item"></param>
        void PerformFileReceive_Folder(TcpClient client, FileTaskInfo task, FileTaskItem item)
        {
            TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Initializing);
            //创建网络流
            NetworkStream netStream = null;

            try
            {
                netStream = client.GetStream();
            }
            catch (Exception)
            {
                OnNetworkError(new EventArgs());
                TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Failure);
                return;
            }

            bool updateTotal = item.TotalSize == 0;             //是否在更新进度的时候需要更新总数

            using (netStream)
            {
                //发送文件请求
                PackedNetworkMessage pnm = MessagePacker.BuildNetworkMessage(Message.Create(task.RemoteHost, task.RemoteHost.HostSub.Ipv4Address, Config.GetRandomTick(), Config.HostName, Config.HostUserName, Consts.Commands.GetDirFiles, 0ul, string.Format("{0:x}:{1:x}:0:", task.PackageID, item.Index), ""));
                if (!SendDataInBuffer(netStream, pnm.Data, pnm.Data.Length))
                {
                    return;                                                                             //发送请求
                }
                TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Processing);
                item.StartTime = DateTime.Now;

                string         filename;
                ulong          size;
                bool           isFolder, isRet;
                Stack <string> pathStack   = new Stack <string>();
                string         currentPath = item.FullPath;
                while (client.Connected && item.State != FileTaskItemState.Canceling && ReadCmdHeader(netStream, task.RemoteHost, out filename, out size, out isFolder, out isRet))
                {
                    if (isRet)
                    {
                        if (pathStack.Count > 0)
                        {
                            currentPath = pathStack.Pop();
                            if (pathStack.Count == 0)
                            {
                                break;                                                   //Fixed: 当当前队列不再有信息时,其实接收已经完成了。因为当前队列中最后一个肯定是文件夹自己。
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    else if (isFolder)
                    {
                        //建立文件夹
                        if (!ProcessDirCmd(item, ref currentPath, pathStack, filename, updateTotal))
                        {
                            TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Failure);
                            return;
                        }
                    }
                    else
                    {
                        //传文件
                        if (!ProcessFileTransfer(client, netStream, task, item, currentPath, filename, size, updateTotal))
                        {
                            TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Failure);
                            return;
                        }
                    }
                }

                netStream.Close();
            }

            if (item.State == FileTaskItemState.Canceling)
            {
                TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Canceled);
            }
            else if (item.State != FileTaskItemState.Failure)
            {
                TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Finished);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 接收文件
        /// </summary>
        /// <param name="client"></param>
        /// <param name="task"></param>
        /// <param name="item"></param>
        void PerformFileReceive_File(TcpClient client, FileTaskInfo task, FileTaskItem item)
        {
            TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Initializing);
            //创建文件
            System.IO.FileInfo fileinfo = new FileInfo(item.FullPath);
            if (fileinfo.Exists)
            {
                item.CurrentFileTransfered = (ulong)fileinfo.Length;
            }
            if (item.CurrentFileSize <= item.CurrentFileTransfered)
            {
                TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Finished);
                return;
            }
            //创建文件
            System.IO.FileStream writer = null;
            try
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(item.FullPath));
                writer = fileinfo.OpenWrite();
            }
            catch (Exception)
            {
                OnFileSystemOperationError(new FileSystemOperationErrorEventArgs(FileSystemOperationType.CreateFile, item.FullPath, task.RemoteHost));
                TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Failure);
                return;
            }
            //创建网络流
            NetworkStream netStream = null;

            try
            {
                netStream = client.GetStream();
            }
            catch (Exception)
            {
                OnNetworkError(new EventArgs());
                TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Failure);
                return;
            }
            using (writer)
            {
                using (netStream)
                {
                    //发送文件请求
                    PackedNetworkMessage pnm = MessagePacker.BuildNetworkMessage(Message.Create(task.RemoteHost, task.RemoteHost.HostSub.Ipv4Address, Config.GetRandomTick(), Config.HostName, Config.HostUserName, Consts.Commands.GetFileData, 0ul, string.Format("{0:x}:{1:x}:{2}", task.PackageID, item.Index, item.CurrentFileTransfered), ""));
                    if (!SendDataInBuffer(netStream, pnm.Data, pnm.Data.Length))
                    {
                        return;                                                                                 //发送请求
                    }
                    TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Processing);
                    item.StartTime = DateTime.Now;

                    byte[] buffer = new byte[SendBuffer];
                    int    length = buffer.Length;

                    while (item.State != FileTaskItemState.Canceling && client.Connected && (length = ReceiveByBuffer(netStream, buffer, length)) > 0)
                    {
                        if (!WriteBufferToFile(task.RemoteHost, writer, item.FullPath, buffer, length))
                        {
                            return;
                        }
                        item.CurrentFileTransfered += (ulong)length;
                        item.FinishedSize          += (ulong)length;

                        //判断还有多少
                        length = buffer.Length;
                        ulong restLength = item.CurrentFileSize - item.CurrentFileTransfered;
                        if (restLength < (ulong)length)
                        {
                            length = (int)restLength;
                        }
                    }
                }
            }

            //判断是否完成
            if (item.State == FileTaskItemState.Canceling)
            {
                TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Canceled);
            }
            else if (item.CurrentFileTransfered == item.CurrentFileSize)
            {
                TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Finished);
            }
            else
            {
                TaskManager.MarkReceiveTaskItemState(item, FileTaskItemState.Failure);
            }
        }