Example #1
0
        /// <summary>
        /// 从套接字接收文件并保存,更新文件列表
        /// </summary>
        /// <param name="socket">套接字</param>
        /// <param name="savename">保存的文件名</param>
        /// <returns>是否成功的结果对象</returns>
        private OperateResult ReceiveFileFromSocketAndUpdateGroup(
            Socket socket,
            string savename
            )
        {
            FileInfo info     = new FileInfo(savename);
            string   guidName = CreateRandomFileName( );
            string   fileName = info.DirectoryName + "\\" + guidName;

            OperateResult <FileBaseInfo> receive = ReceiveFileFromSocket(socket, fileName, null);

            if (!receive.IsSuccess)
            {
                DeleteFileByName(fileName);
                return(receive);
            }

            // 更新操作
            GroupFileContainer fileManagment = GetGroupFromFilePath(info.DirectoryName);
            string             oldName       = fileManagment.UpdateFileMappingName(
                info.Name,
                receive.Content.Size,
                guidName,
                receive.Content.Upload,
                receive.Content.Tag
                );

            // 删除旧的文件
            DeleteExsistingFile(info.DirectoryName, oldName);


            // 回发消息
            return(SendStringAndCheckReceive(socket, 1, StringResources.Language.SuccessText));
        }
Example #2
0
        /// <summary>
        /// 根据文件的显示名称转化为真实存储的名称
        /// </summary>
        /// <param name="factory">第一大类</param>
        /// <param name="group">第二大类</param>
        /// <param name="id">第三大类</param>
        /// <param name="fileName">文件显示名称</param>
        /// <returns>是否成功的结果对象</returns>
        private string TransformFactFileName(string factory, string group, string id, string fileName)
        {
            string             path          = ReturnAbsoluteFilePath(factory, group, id);
            GroupFileContainer fileManagment = GetGroupFromFilePath(path);

            return(fileManagment.GetCurrentFileMappingName(fileName));
        }
Example #3
0
        /// <summary>
        /// 获取当前目录的读写锁,如果没有会自动创建
        /// </summary>
        /// <param name="filePath">相对路径名</param>
        /// <returns>读写锁</returns>
        public GroupFileContainer GetGroupFromFilePath(string filePath)
        {
            GroupFileContainer GroupFile = null;

            hybirdLock.Enter( );

            // lock operator
            if (m_dictionary_group_marks.ContainsKey(filePath))
            {
                GroupFile = m_dictionary_group_marks[filePath];
            }
            else
            {
                GroupFile = new GroupFileContainer(LogNet, filePath);
                m_dictionary_group_marks.Add(filePath, GroupFile);
            }

            hybirdLock.Leave( );
            return(GroupFile);
        }
Example #4
0
        /// <summary>
        /// 当接收到了新的请求的时候执行的操作
        /// </summary>
        /// <param name="socket">异步对象</param>
        /// <param name="endPoint">终结点</param>
        protected override void ThreadPoolLogin(Socket socket, IPEndPoint endPoint)
        {
            OperateResult result = new OperateResult( );

            // 获取ip地址
            string IpAddress = ((IPEndPoint)(socket.RemoteEndPoint)).Address.ToString( );

            // 接收操作信息
            if (!ReceiveInformationHead(
                    socket,
                    out int customer,
                    out string fileName,
                    out string Factory,
                    out string Group,
                    out string Identify).IsSuccess)
            {
                return;
            }

            string relativeName = ReturnRelativeFileName(Factory, Group, Identify, fileName);

            if (customer == HslProtocol.ProtocolFileDownload)
            {
                // 先获取文件的真实名称
                string guidName = TransformFactFileName(Factory, Group, Identify, fileName);
                // 获取文件操作锁
                FileMarkId fileMarkId = GetFileMarksFromDictionaryWithFileName(guidName);
                fileMarkId.EnterReadOperator( );
                // 发送文件数据
                OperateResult send = SendFileAndCheckReceive(socket, ReturnAbsoluteFileName(Factory, Group, Identify, guidName), fileName, "", "", null);
                if (!send.IsSuccess)
                {
                    fileMarkId.LeaveReadOperator( );
                    LogNet?.WriteError(ToString( ), $"{StringResources.Language.FileDownloadFailed} : {send.Message} :{relativeName} ip:{IpAddress}");
                    return;
                }
                else
                {
                    LogNet?.WriteInfo(ToString( ), StringResources.Language.FileDownloadSuccess + ":" + relativeName);
                }

                fileMarkId.LeaveReadOperator( );
                // 关闭连接
                socket?.Close( );
            }
            else if (customer == HslProtocol.ProtocolFileUpload)
            {
                string fullFileName = ReturnAbsoluteFileName(Factory, Group, Identify, fileName);
                // 上传文件
                CheckFolderAndCreate( );
                FileInfo info = new FileInfo(fullFileName);

                try
                {
                    if (!Directory.Exists(info.DirectoryName))
                    {
                        Directory.CreateDirectory(info.DirectoryName);
                    }
                }
                catch (Exception ex)
                {
                    LogNet?.WriteException(ToString( ), StringResources.Language.FilePathCreateFailed + fullFileName, ex);
                    socket?.Close( );
                    return;
                }

                // 接收文件并回发消息
                if (ReceiveFileFromSocketAndUpdateGroup(
                        socket,                // 网络套接字
                        fullFileName).IsSuccess)
                {
                    socket?.Close( );
                    LogNet?.WriteInfo(ToString( ), StringResources.Language.FileUploadSuccess + ":" + relativeName);
                }
                else
                {
                    LogNet?.WriteInfo(ToString( ), StringResources.Language.FileUploadFailed + ":" + relativeName);
                }
            }
            else if (customer == HslProtocol.ProtocolFileDelete)
            {
                string fullFileName = ReturnAbsoluteFileName(Factory, Group, Identify, fileName);

                FileInfo           info          = new FileInfo(fullFileName);
                GroupFileContainer fileManagment = GetGroupFromFilePath(info.DirectoryName);

                // 新增删除的任务
                DeleteExsistingFile(info.DirectoryName, fileManagment.DeleteFile(info.Name));

                // 回发消息
                if (SendStringAndCheckReceive(
                        socket,                                                            // 网络套接字
                        1,                                                                 // 没啥含义
                        "success"                                                          // 没啥含意
                        ).IsSuccess)
                {
                    socket?.Close( );
                }

                LogNet?.WriteInfo(ToString( ), StringResources.Language.FileDeleteSuccess + ":" + relativeName);
            }
            else if (customer == HslProtocol.ProtocolFileDirectoryFiles)
            {
                GroupFileContainer fileManagment = GetGroupFromFilePath(ReturnAbsoluteFilePath(Factory, Group, Identify));

                if (SendStringAndCheckReceive(
                        socket,
                        HslProtocol.ProtocolFileDirectoryFiles,
                        fileManagment.JsonArrayContent).IsSuccess)
                {
                    socket?.Close( );
                }
            }
            else if (customer == HslProtocol.ProtocolFileDirectories)
            {
                List <string> folders = new List <string>( );
                foreach (var m in GetDirectories(Factory, Group, Identify))
                {
                    DirectoryInfo directory = new DirectoryInfo(m);
                    folders.Add(directory.Name);
                }

                Newtonsoft.Json.Linq.JArray jArray = Newtonsoft.Json.Linq.JArray.FromObject(folders.ToArray( ));
                if (SendStringAndCheckReceive(
                        socket,
                        HslProtocol.ProtocolFileDirectoryFiles,
                        jArray.ToString( )).IsSuccess)
                {
                    socket?.Close( );
                }
            }
            else
            {
                // close not supported client
                socket?.Close( );
            }
        }