Exemplo n.º 1
0
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="in_message"></param>
        /// <param name="newClientInfo"></param>
        /// <returns></returns>
        public static Boolean AddPermission(NetworkStream dataStream, Message in_message, ClientInfo newClientInfo)
        {
            Message out_message = new Message();

            //登陆确认命令
            out_message.Command = Message.CommandHeader.AddPermission;

            //从输入信息中获取用户名、密码
            string name = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
            string projectid = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];
            int permissionlevel = Convert.ToInt32(Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[2]);

            //用户名密码校验
            Boolean isUserExisted = UserBussinessManager.UserExisted(name);
            if (!isUserExisted)//消息体需要根据数据库检索结果//同时初始化permission
            {//用户不存在
                Console.WriteLine("添加权限:User doesn't existed!");
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_NOTEXISTED);
                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return false;
            }
            else
            {
                Boolean isPermissionExisted = PermissionExisted(name, projectid, permissionlevel);
                if (isPermissionExisted)
                {//权限已经存在
                    out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_EXISTED);
                    Console.WriteLine("添加权限:权限已经存在!");
                    //打包输出信息,将输出信息写入输出流
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                    return false;
                }
                else
                {
                    Console.WriteLine("添加权限:准备添加权限");
                    if (Database.insertPermission(name, projectid, permissionlevel))
                    {
                        out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_SUCCEED);
                        Console.WriteLine("添加权限:" + name + " " + projectid + " " + permissionlevel.ToString() + "权限添加成功!");
                        //打包输出信息,将输出信息写入输出流
                        dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);

                        return true;
                    }
                    return false;
                }
            }
        }
Exemplo n.º 2
0
        public void Chating(Message in_message)
        {
            Message out_message = new Message();
            Console.Write("Client @");
            Console.Write(client.Client.RemoteEndPoint);
            Console.WriteLine(" say:");
            Console.WriteLine(Encoding.Unicode.GetString(in_message.MessageBody));

            out_message = new Message();
            out_message.Command = Message.CommandHeader.Chat;
            Console.WriteLine("Server to Client:");
            out_message.MessageBody = Encoding.Unicode.GetBytes(Console.ReadLine());
            //打包输出信息,并将输出信息写入输出流
            dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 删除工程
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="in_message"></param>
        /// <param name="newClientInfo"></param>
        /// <returns></returns>
        public static Boolean deleteProject(NetworkStream dataStream, Message in_message, ClientInfo newClientInfo)
        {
            Message out_message = new Message();

            //登陆删除命令
            out_message.Command = Message.CommandHeader.DeleteProject;

            Console.WriteLine("执行删除工程aaaa");

            //从输入信息中获取工程名和项目名
            string UserName = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
            string ProjectID = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];
            string ProgramID = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[2];

            Console.WriteLine("dadfadfada" + UserName + ProjectID + ProgramID);
            //权限检测
            if (true)
            { }
            else
            { }
            //工程是否存在
            Boolean isProjectExisted = ProjectExisted(ProjectID, ProgramID);

            Console.WriteLine(isProjectExisted);
            if (isProjectExisted == false)//消息体需要根据数据库检索结果
            {//用户不存在
                out_message.MessageBody = Encoding.Unicode.GetBytes("false");

                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return false;
            }
            else
            {  //工程存在
                if ((Database.deleteProject(UserName, ProjectID, ProgramID)))
                {

                    out_message.MessageBody = Encoding.Unicode.GetBytes("succeed");
                    //打包输出信息,将输出信息写入输出流
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                    return true;
                }
            }
            return false;
        }
Exemplo n.º 4
0
        /// <summary>
        /// 管理客户端添加工程
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="in_message"></param>
        /// <param name="newClientInfo"></param>
        /// <returns></returns>
        public static Boolean AddProject(NetworkStream dataStream, Message in_message, ClientInfo newClientInfo)
        {
            Message out_message = new Message();

            //登陆确认命令
            out_message.Command = Message.CommandHeader.AddProject;

            //从输入信息中获取用ProjectID,ProgramID,ProjectDescription,ProjectType
            string ProjectID = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
            string ProgramID = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];
            string ProjectDescription = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[2];
            //int ProjectType = Int32.Parse(Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[3]);
            string ProjectType = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[3];

            //工程校验
            Boolean isProjectExisted = ProjectExisted(ProjectID, ProgramID);
            if (!isProjectExisted)//消息体需要根据数据库检索结果
            {//验证通过

                Database.insertSolutionProject(ProgramID, ProjectID, ProjectDescription, ProjectType);

                out_message.MessageBody = Encoding.Unicode.GetBytes("succeed");

                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);

                return true;
            }
            else
            {  //验证未通过
                Console.WriteLine("project has existed!");
                out_message.MessageBody = Encoding.Unicode.GetBytes("existed");
                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return false;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 管理客户端添加用户
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="in_message"></param>
        /// <param name="newClientInfo"></param>
        /// <returns></returns>
        public static Boolean AddUser(NetworkStream dataStream, Message in_message, ClientInfo newClientInfo)
        {
            Message out_message = new Message();

            //登陆确认命令
            out_message.Command = Message.CommandHeader.AddUser;

            //从输入信息中获取用户名、密码
            string name = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
            string passwd = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];
            //int groupid = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[2];
            int groupid = Int32.Parse(Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[2]);

            //用户名密码校验
            Boolean isUserExisted = UserExisted(name);
            if (!isUserExisted)//消息体需要根据数据库检索结果//同时初始化permission
            {//验证通过

                Database.insertUser(name, passwd, groupid);

                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_SUCCEED);

                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);

                return true;
            }
            else
            {  //验证未通过
                Console.WriteLine("User has existed!");
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_EXISTED);
                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return false;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 搜索工程信息
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="in_message">工程与项目名称</param>
        /// <param name="newClientInfo"></param>
        /// <returns></returns>
        public static Boolean SearchProject(NetworkStream dataStream, Message in_message, ClientInfo newClientInfo)
        {
            Message out_message = new Message();

            //登陆确认命令
            out_message.Command = Message.CommandHeader.SearchProject;

            //从输入信息中获取工程名和项目名
            string ProjectID = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
            string ProgramID = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];

            //工程是否存在
            Boolean isProjectExisted = ProjectExisted(ProjectID, ProgramID);
            if (isProjectExisted == false)//消息体需要根据数据库检索结果//同时初始化permission
            {//用户不存在
                out_message.MessageBody = Encoding.Unicode.GetBytes("notexisted");

                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return false;
            }
            else
            {  //工程存在
                Projectinfo tempproject = Database.queryProjectInfo(ProjectID, ProgramID);

                if (tempproject == null)
                {
                    out_message.MessageBody = Encoding.Unicode.GetBytes("null" + ":" + "null" + ":" + "null" + ":" + "-100");
                    return false;
                }

                out_message.MessageBody = Encoding.Unicode.GetBytes(tempproject.projectID + ":" + tempproject.programID + ":" + tempproject.projectDescription + ":" + tempproject.projectType.ToString());
                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return true;
            }
        }
Exemplo n.º 7
0
        public static Boolean SearchPermission(NetworkStream dataStream, Message in_message, ClientInfo newClientInfo)
        {
            Message out_message = new Message();

            //登陆确认命令
            out_message.Command = Message.CommandHeader.SearchPermission;

            //从输入信息中获取用户名
            string name = Encoding.Unicode.GetString(in_message.MessageBody);

            //用户名是否存在
            Boolean isUserExisted = UserBussinessManager.UserExisted(name);
            if (!isUserExisted)//消息体需要根据数据库检索结果//同时初始化permission
            {//用户不存在
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_NOTEXISTED);

                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return false;
            }
            else
            {  //用户存在
                List<Permission> permissions = Database.queryPermission(name);
                if (permissions == null)
                {
                    out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_NOTEXISTED);

                    //打包输出信息,将输出信息写入输出流
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                    return false;
                }
                else
                {
                    int permissionnum = permissions.Count;
                    string code = permissionnum.ToString();
                    foreach (Permission p in permissions)
                    {
                        code += ":" + p.projectName + ":" + p.permissionlevel.ToString();
                    }
                    out_message.MessageBody = Encoding.Unicode.GetBytes(code);
                    //打包输出信息,将输出信息写入输出流
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                    return true;
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// 搜索用户信息
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="in_message"></param>
        /// <param name="newClientInfo"></param>
        /// <returns></returns>
        public static Boolean SearchUser(NetworkStream dataStream, Message in_message, ClientInfo newClientInfo)
        {
            Message out_message = new Message();

            //登陆确认命令
            out_message.Command = Message.CommandHeader.SearchUser;

            //从输入信息中获取用户名
            string name = Encoding.Unicode.GetString(in_message.MessageBody);

            //用户名是否存在
            Boolean isUserExisted = UserExisted(name);
            if (!isUserExisted)//消息体需要根据数据库检索结果//同时初始化permission
            {//用户不存在
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_NOTEXISTED);

                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return false;
            }
            else
            {  //用户存在
                User tempuser = Database.queryUser(name);

                out_message.MessageBody = Encoding.Unicode.GetBytes(tempuser.name + ":" + tempuser.passwd + ":" + tempuser.groupId.ToString());
                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return true;
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 用户名密码认证,权限分配读取
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="in_message"></param>
        /// <param name="newClientInfo"></param>
        /// <returns></returns>
        public static Boolean Authenticate(NetworkStream dataStream,Message in_message,ClientInfo newClientInfo)
        {
            Message out_message = new Message();

            //登陆确认命令
            out_message.Command = Message.CommandHeader.LoginAck;

            //从输入信息中获取用户名、密码、工具类型、工程id
            string name = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
            string passwd = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];
            string tooltype = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[2];
            string prjid = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[3];

            //先进行用户名密码校验
            Boolean isAuthSucceed = UserBussinessManager.CheckUser(name, passwd);
            if (isAuthSucceed)//消息体需要根据数据库检索结果//同时初始化permission
            {//验证通过

                //初始化ClientInfo中name、passwd相应属性
                newClientInfo.name = name;
                newClientInfo.passwd = passwd;
                newClientInfo.projectid = prjid;

                //工程id为空,说明工具刚启动,则校验是否用户是否有访问该工具的权限
                if (prjid.Equals(string.Empty))
                {
                    newClientInfo.permissionList = Database.queryPermissionbyPrjtype(name, tooltype);
                }
                else
                {
                    //初始化ClientInfo中permission相应属性
                    //newClientInfo.permissionList = Database.queryPermission(name);
                    newClientInfo.permissionList = Database.queryPermissionbyPrjID(name, prjid);
                }

                if (newClientInfo.permissionList != null)
                {
                    Console.WriteLine("权限存在");
                    string allowSolutionsAndProjects = "allow";
                    out_message.MessageBody = Encoding.Unicode.GetBytes(allowSolutionsAndProjects);

                    //打包输出信息,将输出信息写入输出流
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);

                    //确认之后发一个含有服务器所有项目工程名称的xml描述文件
                    //xml描述文件由服务器动态生成
                    /*String mess_xml = GenerateXml();
                    if (mess_xml.CompareTo("") != 0)
                    {
                        out_message.MessageBody = Encoding.Unicode.GetBytes(mess_xml);
                        dataStream.Write(out_message.ToBytes(),0,out_message.MessageLength);
                        return true;
                        //非空
                    }
                    else
                    {
                        out_message.MessageBody = Encoding.Unicode.GetBytes("无工程信息");
                        dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                        return false;
                        //空文件
                    }*/
                    /*
                     * 注释(bhs)
                     */

                    //阻塞读取数据,等待客户端反馈信息
                    while (!dataStream.CanRead)
                    {
                        Thread.Sleep(5);//5ms
                    }

                    while (!dataStream.DataAvailable)
                    {
                        Thread.Sleep(5);//5ms
                    }

                    in_message = Message.Parse(dataStream);
                    string s = Encoding.Unicode.GetString(in_message.MessageBody);
                    byte[] b = new byte[45];

                    //客户端发送ready信息,则向客户端传送服务器的项目列表xml文件
                    if (s.CompareTo("ready") == 0)
                    {
                        //ClientBusinessManager.SendSolutionProjectListXml(dataStream);
                        return true;
                    }
                    else

                        return false;
                }
                else
                {
                    Console.WriteLine("权限为空");

                    string allowSolutionsAndProjects = "deny";
                    out_message.MessageBody = Encoding.Unicode.GetBytes(allowSolutionsAndProjects);
                    Console.WriteLine(Encoding.Unicode.GetString(out_message.MessageBody));

                    //打包输出信息,将输出信息写入输出流
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                    return false;
                }
            }
            else
            {  //验证未通过
                Console.WriteLine("验证未通过");
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_DENY_PW);
                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return false;
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// 搜索项目下全部信息
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="in_message"></param>
        /// <param name="newClientInfo"></param>
        /// <returns></returns>
        public static Boolean SearchSolution(NetworkStream dataStream, Message in_message, ClientInfo newClientInfo)
        {
            Message out_message = new Message();

            //登陆确认命令
            out_message.Command = Message.CommandHeader.SearchSolution;

            //从输入信息中获取项目名称
            string name = Encoding.Unicode.GetString(in_message.MessageBody);

            //用户名是否存在
            Boolean isSolutionExisted = true;// UserExisted(name);
            if (!isSolutionExisted)//消息体需要根据数据库检索结果//同时初始化permission
            {//用户不存在
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_NOTEXISTED);

                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return false;
            }
            else
            {  //用户存在
                List<ProjectInfo> solutions = Database.querySolution(name);
                if (solutions == null)
                {
                    out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_NOTEXISTED);

                    //打包输出信息,将输出信息写入输出流
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                    return false;
                }
                else
                {
                    int projectnum = solutions.Count;
                    string code = projectnum.ToString();
                    List<string> activeclients = ClientThreadManager.GetActiveClients();
                    foreach (ProjectInfo p in solutions)
                    {
                        if(activeclients.Contains(p.ProjectID))
                            code += ":" + p.ProjectType + ":" + p.ProjectID.ToString()+":在线";
                        else
                            code += ":" + p.ProjectType + ":" + p.ProjectID.ToString() + ":脱机";
                    }
                    out_message.MessageBody = Encoding.Unicode.GetBytes(code);
                    //打包输出信息,将输出信息写入输出流
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                    return true;
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// 客户端请求获取任意文档,服务器给予确认
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="in_message"></param>
        /// <returns></returns>
        public static Boolean SendAckToClientGetDocumentRequest(NetworkStream dataStream, Message in_message)
        {
            string solutionName = string.Empty;
            string projectName = string.Empty;
            string documentName = string.Empty;
            string solutionProjectDirectory = string.Empty;

            Boolean isAck;
            Message out_message = new Message();
            out_message.Command = Message.CommandHeader.GetDocumentAck;

            solutionName = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
            projectName = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];
            documentName = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[2];
            solutionProjectDirectory = ServerManager.serverConfInfo.RootDirectory + "\\" + solutionName + "\\" + projectName;
            //默认在当前目录下搜索项目工程xml描述文件

            //还需要添加权限

            //判断文件存在性
            if (Directory.GetFiles(solutionProjectDirectory).Count() == 0
                || !File.Exists(solutionProjectDirectory + "\\" + documentName))//客户端需要的当前服务器无法提供
            {
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_DENY);
                Console.WriteLine(documentName + " is not exist.");
                isAck = false;
            }
            else
            {
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_ALLOW);
                isAck = true;
            }
            dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);

            return isAck;
        }
Exemplo n.º 12
0
        public static Boolean AutoPushXML(Message in_message)
        {
            Console.Write("Auto pushing XML from ");
            string fileName = string.Empty;

            //从输入信息中获取源、目的工程id与项目id
            string sourceprjid = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
            string destinationprjid = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];
            string solutionname = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[2];
            Console.WriteLine(sourceprjid + " to " + destinationprjid);
            string solutionProjectDirectory = solutionname + "\\" + sourceprjid;  //获取源工具数据
            //找一个xml描述文件,暂定为GetFiles函数的第一个文件,有待于数据库协作判断,待加
            //foreach (var file in Directory.GetFiles(solutionProjectDirectory))
            //{

            //}
            // fileName = Directory.GetFiles(solutionProjectDirectory).First();
            fileName = Directory.GetFiles(solutionProjectDirectory).Last();

            if (fileName == null)
            {
                return false;
            }
            Console.WriteLine("File found:" + fileName);
            //打开本地文件,读取内容,分行写到message中,连续发送
            //第一个数据包消息体:文件名
            //最后一个数据包消息体:#
            try
            {
                ClientInfo destinationclient = ClientThreadManager.GetClient(destinationprjid);
                if (destinationclient == null)   //检测目标客户端是否在线
                {
                    Console.WriteLine("Destination client is offline!");
                    return false;
                }
                else
                {
                    Console.WriteLine("Destination client is online! Ready to push......");
                    NetworkStream pushdataStream = destinationclient.client.GetStream();
                    Message push_message;
                    using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
                    {
                        //传送文件开始的第一个数据包
                        push_message = new Message();
                        push_message.MessageBody = Encoding.Unicode.GetBytes(solutionname + ":" + destinationprjid);//项目名+工程名
                        push_message.Command = Message.CommandHeader.PushXML;
                        push_message.MessageFlag = Message.MessageFlagHeader.FileBegin;
                        pushdataStream.Write(push_message.ToBytes(), 0, push_message.MessageLength);
                        //下面传送文件内容
                        string line = string.Empty;
                        int packetNum = 1;
                        while ((line = sr.ReadLine()) != null)
                        {
                            push_message = new Message();
                            push_message.MessageBody = Encoding.Unicode.GetBytes(line);
                            push_message.Command = Message.CommandHeader.PushXML;
                            push_message.FilePacketNumber = packetNum++;//数据包添加序号
                            push_message.MessageFlag = Message.MessageFlagHeader.FileMiddle;
                            pushdataStream.Write(push_message.ToBytes(), 0, push_message.MessageLength);
                        }

                        //文件读取结束
                        push_message = new Message();
                        push_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_SHARP);//该消息体最终被舍弃
                        push_message.Command = Message.CommandHeader.PushXML;
                        push_message.MessageFlag = Message.MessageFlagHeader.FileEnd;
                        pushdataStream.Write(push_message.ToBytes(), 0, push_message.MessageLength);
                        Console.WriteLine("Sending......Waiting for callback......");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("PushXML: " + ex.Message);
            }
            return false;
        }
Exemplo n.º 13
0
        /// <summary>
        /// 权限范围内的发送项目及工程列表,该文件动态生成
        /// </summary>
        /// <param name="dataStream"></param>
        /// <returns></returns>
        public static Boolean SendSolutionProjectListXml(NetworkStream dataStream)
        {
            //暂定于当前程序运行主目录下 待改
            string fileName = ServerManager.serverConfInfo.RootDirectory + "\\SolutionProjectList";

            //打开本地文件,读取内容,分行写到message中,连续发送
            //第一个数据包消息体:文件名
            //最后一个数据包消息体:#
            try
            {

                Message out_message;
                using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
                {
                    //传送文件开始的第一个数据包
                    out_message = new Message();
                    out_message.MessageBody = Encoding.Unicode.GetBytes(fileName);
                    out_message.Command = Message.CommandHeader.SendXml;
                    out_message.MessageFlag = Message.MessageFlagHeader.FileBegin;
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);

                    //下面传送文件内容
                    string line = string.Empty;
                    int packetNum = 1;
                    while ((line = sr.ReadLine()) != null)
                    {
                        out_message = new Message();
                        out_message.MessageBody = Encoding.Unicode.GetBytes(line);
                        out_message.Command = Message.CommandHeader.SendXml;
                        out_message.FilePacketNumber = packetNum++;//数据包添加序号
                        out_message.MessageFlag = Message.MessageFlagHeader.FileMiddle;
                        dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                        // Console.WriteLine(line);
                    }

                    //文件读取结束
                    out_message = new Message();
                    out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_SHARP);//该消息体最终被舍弃 "#"
                    out_message.Command = Message.CommandHeader.SendXml;
                    out_message.MessageFlag = Message.MessageFlagHeader.FileEnd;
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                }

                //读取客户端反馈,以判断客户端是否正确接收文件
                Message in_message = Message.Parse(dataStream);
                if (in_message.Command == Message.CommandHeader.ReceivedXml
                    && Encoding.Unicode.GetString(in_message.MessageBody).CompareTo(Constants.M_YES) == 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("SendXml: " + ex.Message);
                return false;
            }

            return true;
        }
Exemplo n.º 14
0
        /// <summary>
        /// 服务器发送任意格式文档给客户端
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="documentName"></param>
        public static Boolean SendDocument(NetworkStream dataStream, string solutionName, string projectName, string documentName)
        {
            string solutionProjectDirectory = ServerManager.serverConfInfo.RootDirectory + "\\" + solutionName + "\\" + projectName;
            string filenameWithRelativePath = solutionProjectDirectory + "\\" + documentName;

            try
            {
                Message out_message;

                //传送文件开始的第一个数据包
                out_message = new Message();
                out_message.MessageBody = Encoding.Unicode.GetBytes(solutionName + ":" + projectName + ":" + documentName);
                out_message.Command = Message.CommandHeader.SendDocument;
                out_message.MessageFlag = Message.MessageFlagHeader.FileBegin;
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);

                //传送文件内容
                using (FileStream fs = new FileStream(filenameWithRelativePath, FileMode.Open, FileAccess.Read))
                {
                    BinaryReader br = new BinaryReader(fs);
                    byte[] buffer = new byte[8192];//2^13
                    try
                    {
                        int checksize;
                        do
                        {
                            checksize = br.Read(buffer, 0, 8192);
                            if (checksize > 0)
                            {
                                out_message = new Message();
                                out_message.Command = Message.CommandHeader.SendDocument;
                                out_message.MessageFlag = Message.MessageFlagHeader.FileMiddle;
                                out_message.MessageBody = new byte[checksize];
                                if (checksize < 8192)//最后一个数据包字节数不够
                                {
                                    for (int i = 0; i < checksize; i++)
                                        out_message.MessageBody[i] = buffer[i];
                                }
                                else
                                {
                                    buffer.CopyTo(out_message.MessageBody, 0);
                                }

                                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                            }
                        } while (checksize > 0);

                        //最后一个数据包
                        out_message = new Message();
                        out_message.Command = Message.CommandHeader.SendDocument;
                        out_message.MessageFlag = Message.MessageFlagHeader.FileEnd;
                        //out_message.MessageBody = new byte[1];
                        out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_SHARP);//该消息体最终被舍弃
                        dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);

                        //读取客户端反馈,以判断客户端是否正确接收文件
                        Message in_message = Message.Parse(dataStream);
                        if (in_message.Command == Message.CommandHeader.ReceivedDocument
                            && Encoding.Unicode.GetString(in_message.MessageBody).CompareTo(Constants.M_YES) == 0)
                        {
                            return true;
                        }
                    }
                    catch (EndOfStreamException ex)
                    {
                        Console.WriteLine("SendDocument: " + ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("SendDocument: " + ex.Message);
            }
            return false;
        }
Exemplo n.º 15
0
        /// <summary>
        /// 客户端发送项目工程XML描述文件给服务器请求,服务器加以确认,并将确认信息发送给客户端
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="in_message"></param>
        /// <returns></returns>
        public static Boolean SendAckToClientSendXmlRequest(NetworkStream dataStream, Message in_message)
        {
            //判断客户端上传项目工程文件的权限

            Boolean isAck;
            Message out_message = new Message();
            out_message.Command = Message.CommandHeader.SendXmlAck;

            if (true)//检查permission
            {
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_ALLOW);
                isAck = true;
            }
            else
            {
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_DENY);
                isAck = false;
            }
            dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
            return isAck;
        }
Exemplo n.º 16
0
        /// <summary>
        /// 针对客户端获取xml文件的请求GetXmlRequest,服务器首先要确认客户端请求的合理性,然后决定是否发xml文件
        /// 判断文件的存在性,判断客户端的权限是否足够
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="in_message"></param>
        /// <returns></returns>
        public static Boolean SendAckToClientGetXmlRequest(NetworkStream dataStream, Message in_message)
        {
            string solutionName = string.Empty;
            string projectName = string.Empty;
            string solutionProjectDirectory = string.Empty;

            Boolean isAck;
            Message out_message = new Message();
            out_message.Command = Message.CommandHeader.GetXmlAck;

            solutionName = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
            projectName = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];

            //默认在当前目录下搜索项目及工程文件夹
            solutionProjectDirectory = ServerManager.serverConfInfo.RootDirectory + "\\" + solutionName + "\\" + projectName;

            //还需要添加权限判断
            Console.WriteLine("file path is " + solutionProjectDirectory);

            //判断文件夹及存在性
            if (Directory.Exists(solutionProjectDirectory) == true // 文件夹存在性
                && Directory.GetFiles(solutionProjectDirectory).Count() != 0)//客户端需要的xml描述文件存在性
            {
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_ALLOW);
                isAck = true;
            }
            else
            {
                Console.WriteLine(projectName + " is not exist.");
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_DENY);
                isAck = false;
            }
            dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);

            return isAck;
        }
Exemplo n.º 17
0
        /// <summary>
        /// 服务器接受客户端发送过来的任意格式的文档
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="?"></param>
        public static Boolean GetDocument(NetworkStream dataStream)
        {
            //整个文件接收工作需要在这全部完成,阻塞方式
            try
            {
                FileStream fs;
                Message in_message;
                Message out_message;

                string solutionName = string.Empty;
                string projectName = string.Empty;
                string documentName = string.Empty;
                string documentNameAddTimestampWithRelativePath = string.Empty;

                string solutionProjectDirectory = ServerManager.serverConfInfo.RootDirectory + "\\" + solutionName + "\\" + projectName;
                do
                {
                    in_message = Message.Parse(dataStream);
                    switch (in_message.MessageFlag)
                    {
                        case Message.MessageFlagHeader.FileBegin://得到文件名,新建一个以该文件名命名的文件
                            solutionName = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
                            projectName = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];
                            documentName = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[2];

                            //判断当前目录下是否存在该项目及工程文件夹
                            if (!Directory.Exists(solutionProjectDirectory))
                            {
                                //在当前路径下创建项目文件夹
                                Directory.CreateDirectory(solutionProjectDirectory);
                            }
                            //生成时间戳 格式:__2011_12_2_19_48_37
                            string timestamp = "__" + DateTime.Now.ToString().Replace(':', '_').Replace(' ', '_').Replace('/', '_');
                            documentNameAddTimestampWithRelativePath = solutionProjectDirectory + "\\" + documentName + timestamp;

                            if (!File.Exists(documentNameAddTimestampWithRelativePath))
                            {
                                fs = File.Create(documentNameAddTimestampWithRelativePath);
                                fs.Close();
                            }
                            break;
                        case Message.MessageFlagHeader.FileMiddle:
                            if (!File.Exists(documentNameAddTimestampWithRelativePath))
                            {
                                Console.WriteLine(documentName + " not exist.");
                            }
                            using (fs = File.Open(documentNameAddTimestampWithRelativePath, FileMode.Append))
                            {
                                BinaryWriter bw = new BinaryWriter(fs);
                                bw.Write(in_message.MessageBody, 0, in_message.MessageBody.Length);
                                bw.Close();
                                fs.Close();
                            }
                            break;
                        case Message.MessageFlagHeader.FileEnd:
                            //发送成功接收的确认数据包
                            out_message = new Message();
                            out_message.Command = Message.CommandHeader.ReceivedDocument;
                            out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_YES);
                            dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                            return true;
                        default:
                            //发送接收失败的确认数据包
                            out_message = new Message();
                            out_message.Command = Message.CommandHeader.ReceivedDocument;
                            out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_NO);
                            dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                            Console.WriteLine("The document file trasfer failed...");
                            //若存在接受不全的文件,删除
                            if (File.Exists(documentNameAddTimestampWithRelativePath))
                            {
                                File.Delete(documentNameAddTimestampWithRelativePath);
                            }
                            break;
                    }
                } while (in_message.MessageFlag == Message.MessageFlagHeader.FileBegin
                    || in_message.MessageFlag == Message.MessageFlagHeader.FileMiddle);

            }
            catch (Exception ex)
            {
                Console.WriteLine("GetDocument: " + ex.Message);
            }

            return false;
        }
Exemplo n.º 18
0
        //更新推送
        public static void ReminderPush(ClientInfo clientInfo, String message)
        {
            try
            {
                NetworkStream dataStream = clientInfo.client.GetStream();
                Message push_message = new Message();

                //生成推送消息
                push_message.Command = Message.CommandHeader.ReminderAutoPush;
                push_message.MessageBody = Encoding.Unicode.GetBytes(message);

                //推送
                dataStream.Write(push_message.ToBytes(), 0, push_message.MessageLength);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// 发送工程服务器端项目工程xml描述文件projectNameXml给客户端,整个文件的发送均在此方法内完成
        /// </summary>
        /// <param name="dataStream"></param>
        /// <param name="solutionName"></param>
        /// <param name="projectName"></param>
        /// <returns></returns>
        public static Boolean SendXml(NetworkStream dataStream, string solutionName, string projectName)
        {
            string solutionProjectDirectory = ServerManager.serverConfInfo.RootDirectory + "\\" + solutionName + "\\" + projectName;
            string fileName = string.Empty;

            //找一个xml描述文件,暂定为GetFiles函数的第一个文件,有待于数据库协作判断,待加
            //foreach (var file in Directory.GetFiles(solutionProjectDirectory))
            //{

            //}
            // fileName = Directory.GetFiles(solutionProjectDirectory).First();
            fileName = Directory.GetFiles(solutionProjectDirectory).Last();

            if (fileName == null)
            {
                return false;
            }

            //打开本地文件,读取内容,分行写到message中,连续发送
            //第一个数据包消息体:文件名
            //最后一个数据包消息体:#
            try
            {
                Message out_message;
                using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
                {
                    //传送文件开始的第一个数据包
                    out_message = new Message();
                    out_message.MessageBody = Encoding.Unicode.GetBytes(solutionName + ":" + projectName);//项目名+工程名
                    out_message.Command = Message.CommandHeader.SendXml;
                    out_message.MessageFlag = Message.MessageFlagHeader.FileBegin;
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);

                    //下面传送文件内容
                    string line = string.Empty;
                    int packetNum = 1;
                    while ((line = sr.ReadLine()) != null)
                    {
                        out_message = new Message();
                        out_message.MessageBody = Encoding.Unicode.GetBytes(line);
                        out_message.Command = Message.CommandHeader.SendXml;
                        out_message.FilePacketNumber = packetNum++;//数据包添加序号
                        out_message.MessageFlag = Message.MessageFlagHeader.FileMiddle;
                        dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                    }

                    //文件读取结束
                    out_message = new Message();
                    out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_SHARP);//该消息体最终被舍弃
                    out_message.Command = Message.CommandHeader.SendXml;
                    out_message.MessageFlag = Message.MessageFlagHeader.FileEnd;
                    dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                }

                //读取客户端反馈,以判断客户端是否正确接收文件
                Message in_message = Message.Parse(dataStream);
                if (in_message.Command == Message.CommandHeader.ReceivedXml
                    && Encoding.Unicode.GetString(in_message.MessageBody).CompareTo(Constants.M_YES) == 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("SendXml: " + ex.Message);
            }
            return false;
        }
Exemplo n.º 20
0
        /// <summary>
        /// 服务器接受客户端传来的项目工程xml描述文件
        /// </summary>
        /// <param name="dataStream"></param>
        public static Boolean GetXml(NetworkStream dataStream)
        {
            //整个文件接收工作需要在这全部完成,阻塞方式
            FileStream fs;
            Message in_message;
            Message out_message;
            string solutionName = string.Empty;
            string projectName = string.Empty;
            string solutionProjectDirectory = ServerManager.serverConfInfo.RootDirectory;
            //格式@主目录\\子目录\\工程描述文件
            string projectNameAddTimestampWithRelativePath = string.Empty;

            do
            {
                in_message = Message.Parse(dataStream);
                switch (in_message.MessageFlag)
                {
                    case Message.MessageFlagHeader.FileBegin://得到文件名,新建一个以该文件名命名的文件
                        solutionName = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
                        projectName = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];

                        //项目工程文件夹
                        solutionProjectDirectory += "\\" + solutionName + "\\" + projectName;

                        //判断当前目录下是否存在该项目及工程文件夹
                        if (!Directory.Exists(solutionProjectDirectory))
                        {
                            //在当前路径下创建项目文件夹
                            Directory.CreateDirectory(solutionProjectDirectory);
                        }

                        //生成时间戳 格式:__2011_12_2_19_48_37
                        string timestamp = "__" + DateTime.Now.ToString().Replace(':', '_').Replace(' ', '_').Replace('/', '_');
                        projectNameAddTimestampWithRelativePath = solutionName + "\\" + projectName + "\\" + projectName + timestamp + ".xml";
                        if (!File.Exists(projectNameAddTimestampWithRelativePath))
                        {
                            fs = File.Create(projectNameAddTimestampWithRelativePath);
                            fs.Close();//需要关闭该文件,否则下面无法获取文件锁,进行文件读写
                        }
                        break;
                    case Message.MessageFlagHeader.FileMiddle://得到文件内容,采用追加方式写入文件
                        if (!File.Exists(projectNameAddTimestampWithRelativePath))
                        {
                            Console.WriteLine(projectName + " not exist.");
                        }
                        fs = File.Open(projectNameAddTimestampWithRelativePath, FileMode.Append);
                        StreamWriter sw = new StreamWriter(fs, Encoding.Unicode, in_message.MessageBody.Length);
                        sw.WriteLine(Encoding.Unicode.GetString(in_message.MessageBody));
                        sw.Flush();
                        sw.Close();
                        fs.Close();
                        break;
                    case Message.MessageFlagHeader.FileEnd://文件传送结束
                        //发送成功接收的确认数据包
                        out_message = new Message();
                        out_message.Command = Message.CommandHeader.ReceivedXml;
                        out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_YES);
                        dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                        return true;
                    default:
                        //发送接收失败的确认数据包
                        out_message = new Message();
                        out_message.Command = Message.CommandHeader.ReceivedXml;
                        out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_NO);
                        dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                        Console.WriteLine("The xml file trasfer failed...");
                        //若存在接受不全的文件,删除
                        if (File.Exists(projectNameAddTimestampWithRelativePath))
                        {
                            File.Delete(projectNameAddTimestampWithRelativePath);
                        }
                        break;
                }
            } while (in_message.MessageFlag == Message.MessageFlagHeader.FileBegin ||
                in_message.MessageFlag == Message.MessageFlagHeader.FileMiddle);
            return false;
        }
Exemplo n.º 21
0
        public static void PushXMLAck(NetworkStream dataStream, Message in_message, ClientInfo newClientInfo)
        {
            ClientInfo adminclient = ClientThreadManager.GetClient("ADMIN");
            if (adminclient == null)
            {
                if (in_message.Command == Message.CommandHeader.PushXMLAck)
                    if (Encoding.Unicode.GetString(in_message.MessageBody).CompareTo(Constants.M_YES) == 0)
                    {
                        Console.WriteLine("Pushing successfully!");
                    }
                    else
                    {
                        Console.WriteLine("Pushing failed!");
                    }
            }
            else
            {
                NetworkStream pushackStream = adminclient.client.GetStream();
                Message out_message = new Message();
                out_message.Command = Message.CommandHeader.PushXMLAck;

                if (in_message.Command == Message.CommandHeader.PushXMLAck)
                    if (Encoding.Unicode.GetString(in_message.MessageBody).CompareTo(Constants.M_YES) == 0)
                    {
                        Console.WriteLine("Pushing successfully!");
                        out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_SUCCEED);  //发送成功,将消息送回调度者
                        //打包输出信息,将输出信息写入输出流
                        pushackStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                    }
                    else
                    {
                        out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_FAIL);
                        //打包输出信息,将输出信息写入输出流
                        pushackStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                        Console.WriteLine("Pushing failed!");
                    }
            }
        }
Exemplo n.º 22
0
        public static Message Parse(NetworkStream connection)
        {
            Message message = new Message();
            byte[] buffer = new byte[4];

            //先读出前4个字节,即Message的长度
            if(ReadMessagePartial(connection, buffer,4)){
                message.MessageLength = BitConverter.ToInt32(buffer, 0);
            }

            if (message.MessageLength > 3)
            {
                buffer = new byte[message.MessageLength - 4];

                //读出消息的其它字节
                if (ReadMessagePartial(connection, buffer, message.MessageLength - 4))
                {
                    //读出一个字节的文件信息标志
                    message.MessageFlag = (MessageFlagHeader)buffer[0];

                    //读出数据包序号,4个字节
                    message.FilePacketNumber = BitConverter.ToInt32(buffer, 1);

                    //读出命令
                    message.Command = (CommandHeader)buffer[5];

                    //读出消息体
                    int i = 0;
                    message.MessageBody = new byte[message.MessageLength - 10];
                    while (i < message.MessageLength - 10)
                    {
                        message.MessageBody[i] = buffer[6 + i];
                        i++;
                    }
                }
            }
            return message;
        }
Exemplo n.º 23
0
        public static Boolean PushXML(NetworkStream dataStream, Message in_message, ClientInfo newClientInfo)
        {
            Console.Write("Start pushing XML from ");
            string fileName = string.Empty;
            Message out_message = new Message();

            //推送命令
            out_message.Command = Message.CommandHeader.PushXML;

            //从输入信息中获取源、目的工程id以及项目id
            string sourceprjid = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[0];
            string destinationprjid = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[1];
            string solutionname = Encoding.Unicode.GetString(in_message.MessageBody).Split(':')[2];
            Console.WriteLine(sourceprjid + " to " + destinationprjid);

            List<ProjectInfo> solutions = Database.querySolution(solutionname); //检测项目是否存在
            if (solutions == null)
            {
                out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_NOTEXISTED);
                Console.WriteLine("Solution not existed!");
                //打包输出信息,将输出信息写入输出流
                dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                return false;
            }
            else //项目存在
            {
                string solutionProjectDirectory = solutionname + "\\" + sourceprjid;  //获取源工具数据
                //找一个xml描述文件,暂定为GetFiles函数的第一个文件,有待于数据库协作判断,待加
                //foreach (var file in Directory.GetFiles(solutionProjectDirectory))
                //{

                //}
                // fileName = Directory.GetFiles(solutionProjectDirectory).First();
                fileName = Directory.GetFiles(solutionProjectDirectory).Last();

                if (fileName == null)
                {
                    return false;
                }
                Console.WriteLine("File found:" + fileName);
                //打开本地文件,读取内容,分行写到message中,连续发送
                //第一个数据包消息体:文件名
                //最后一个数据包消息体:#
                try
                {
                    ClientInfo destinationclient = ClientThreadManager.GetClient(destinationprjid);
                    if (destinationclient == null)   //检测目标客户端是否在线
                    {
                        out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_OFFLINE);
                        Console.WriteLine("Destination client is offline!");
                        //打包输出信息,将输出信息写入输出流
                        dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                        return false;
                    }
                    else
                    {
                        Console.WriteLine("Destination client is online! Ready to push......");
                        NetworkStream pushdataStream = destinationclient.client.GetStream();
                        Message push_message;
                        using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
                        {
                            //传送文件开始的第一个数据包
                            push_message = new Message();
                            push_message.MessageBody = Encoding.Unicode.GetBytes(solutionname + ":" + destinationprjid);//项目名+工程名
                            push_message.Command = Message.CommandHeader.PushXML;
                            push_message.MessageFlag = Message.MessageFlagHeader.FileBegin;
                            pushdataStream.Write(push_message.ToBytes(), 0, push_message.MessageLength);
                            //下面传送文件内容
                            string line = string.Empty;
                            int packetNum = 1;
                            while ((line = sr.ReadLine()) != null)
                            {
                                push_message = new Message();
                                push_message.MessageBody = Encoding.Unicode.GetBytes(line);
                                push_message.Command = Message.CommandHeader.PushXML;
                                push_message.FilePacketNumber = packetNum++;//数据包添加序号
                                push_message.MessageFlag = Message.MessageFlagHeader.FileMiddle;
                                pushdataStream.Write(push_message.ToBytes(), 0, push_message.MessageLength);
                            }

                            //文件读取结束
                            push_message = new Message();
                            push_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_SHARP);//该消息体最终被舍弃
                            push_message.Command = Message.CommandHeader.PushXML;
                            push_message.MessageFlag = Message.MessageFlagHeader.FileEnd;
                            pushdataStream.Write(push_message.ToBytes(), 0, push_message.MessageLength);
                            Console.WriteLine("Sending......Waiting for callback......");
                        }
                        out_message.MessageBody = Encoding.Unicode.GetBytes(Constants.M_WAITING);  //发送成功,但是尚未接到客户端回馈,将消息送回调度者
                        //打包输出信息,将输出信息写入输出流
                        dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);

                        //读取客户端反馈,以判断客户端是否正确接收文件
                        //    Message push_back_message = Message.Parse(pushdataStream);

                        //if (push_back_message.Command == Message.CommandHeader.PushXMLAck)
                        //    if (Encoding.Unicode.GetString(push_back_message.MessageBody).CompareTo("yes") == 0)
                        //    {
                        //        Console.WriteLine("Pushing successfully!");
                        //        out_message.MessageBody = Encoding.Unicode.GetBytes("succeed");  //发送成功,将消息送回调度者
                        //        //打包输出信息,将输出信息写入输出流
                        //        dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                        //        return true;
                        //    }
                        //    else
                        //    {
                        //        out_message.MessageBody = Encoding.Unicode.GetBytes("fail");
                        //        //打包输出信息,将输出信息写入输出流
                        //        dataStream.Write(out_message.ToBytes(), 0, out_message.MessageLength);
                        //        Console.WriteLine("Pushing failed!");
                        //        return false;
                        //    }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("PushXML: " + ex.Message);
                }
                return false;
            }
        }
Exemplo n.º 24
0
        //负责向指定客户端推送信息
        public static Boolean PushMessage(ClientInfo clientInfo , string message)
        {
            try
            {
                NetworkStream dataStream = clientInfo.client.GetStream();
                Message push_message = new Message();

                //生成推送消息
                push_message.Command = Message.CommandHeader.Push;
                push_message.MessageBody = Encoding.Unicode.GetBytes(message);

                //推送
                dataStream.Write(push_message.ToBytes(), 0, push_message.MessageLength);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }