Beispiel #1
0
        /// <summary>
        /// 解析客户端发过来的消息包
        /// </summary>
        /// <param name="bag"></param>
        private void AnalyzeClientMessage(NetworkStream stream, MessageBag bag)
        {
            LogService.WriteServerRunLog(LogLevel.Info, "解析来自" + _publisherIP + "的不同类型的消息包");

            MessageType type = (MessageType)bag.Head.Type;

            switch (type)
            {
            case MessageType.User:
                if (bag.Head.State == 1)     //处理客户端传来的第一个消息包
                {
                    FirstMessageBag firstBag = (FirstMessageBag)bag;
                    DealWithUserMessage(stream, firstBag);
                }
                else if (bag.Head.State == 2)      //用户退出,用户切换
                {
                    string getUserId = Encoding.UTF8.GetString(bag.BytesBody);

                    if (CommonService.RecordUserDic.ContainsKey(getUserId))
                    {
                        CommonService.RecordUserDic.Remove(getUserId);
                    }
                }
                else if (bag.Head.State == 3)      //由CGI发送的UserID
                {
                    DealWithCGIMessage(stream, bag);
                }
                break;

            case MessageType.File:
                FileMessageBag fileBag = (FileMessageBag)bag;
                DealWithFileMessage(stream, fileBag);
                break;

            default:
                Debug.Fail("未知在MessageType:" + type);
                break;
            }
        }
Beispiel #2
0
        /// <summary>
        /// 处理文件类型消息
        /// </summary>
        private void DealWithFileMessage(NetworkStream stream, FileMessageBag fileMsgBag)
        {
            //处理直接发送file包的情况(未经过用户验证)
            if (!_isRealUser)
            {
                return;
            }

            ///将当前上传文件加入待显示的字典
            string fileUrl = fileMsgBag.FileUrl;

            DisposeUserMethod.SetUserCurrentFile(fileUrl);

            ///接收文件
            byte[] fileContent = fileMsgBag.FileBody;

            if (FileSave(fileContent, fileUrl))
            {
                /// 向客户端发送文件url,表示此文件接收成功
                SendResponseMessage(stream, MessageType.FileReceive, fileUrl, 2);

                /// 还没接收完,继续接收
                if (_fileCount != _Total)
                {
                    GetMessage();
                }
                /// 接收完全,返回OK
                else
                {
                    //此信息写入日志
                    _receivedTime = DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString();


                    ///给客户端发送OK消息,表示所有文件接收完成
                    SendResponseMessage(stream, MessageType.FileReceive, string.Empty, 1);

                    LogService.WriteServerRunLog(LogLevel.Info, "来自" + _publisherIP + "的所有文件接收完成,开始整理接收的网站");

                    //将接收到的文件复制到相应的位置
                    DisposeSdsite disposeSdsite = new DisposeSdsite();

                    if (!disposeSdsite.ExecuteSdsite(_userFilePath, _projectName))
                    {
                        SendResponseMessage(stream, MessageType.FileTidy, string.Empty, 0);
                        return;
                    }
                    LogService.WriteServerRunLog(LogLevel.Info, "来自" + _publisherIP + "的sdsite文件已整理完毕,开始生成可浏览的网站");

                    //接收完毕,开始生成
                    //try
                    //{
                    //    //初始一下当前网站的根路径
                    //    string initPath = AnyFilePath.GetWebAbsolutePath(_userFilePath);
                    //    ToHtmlDirectoryService.InitializePath(initPath);

                    //    BuildSite buildSite = new BuildSite();

                    //    if (!buildSite.StartBuild(_userFilePath, _projectName))
                    //    {
                    //        SendResponseMessage(stream, MessageType.SiteBuild, string.Empty, 0);
                    //        return;
                    //    }
                    //}
                    //catch (Exception e)
                    //{
                    //    ExceptionService.WriteExceptionLog(e);
                    //    SendResponseMessage(stream, MessageType.SiteBuild, string.Empty, 0);
                    //    return;
                    //}

                    LogService.WriteServerRunLog(LogLevel.Info, "来自" + _publisherIP + "的sdsite文件,其网站生成完毕");

                    //此信息写入日志
                    _buildedTime = DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString();


                    //生成成功   返回消息;不管网站是否有同名,都将返回一个值(空,或是新网站名,或是原本的网站名)
                    SendResponseMessage(stream, MessageType.SiteBuild, _changedProjName, 1);

                    DisposeUserMethod.DeleteUser();
                    Client.Client.Close();

                    RecordLogToTable();

                    RecordInfoToTable(disposeSdsite);
                }
            }
        }