Ejemplo n.º 1
0
 /// <summary>
 /// 处理非法入侵
 /// </summary>
 /// <param name="session"></param>
 private void OnBadCommand(TCPServerSession session)
 {
     session.BadCmd++;
     if (session.BadCmd > 3)//4不过3,如果收到坏消息大于3条,则视为非法攻击,断掉TCP连接
     {
         session.Disconnect();
         session.Dispose();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 文件传送
        /// </summary>
        /// <param name="FileTransmit"></param>
        /// <param name="session"></param>
        private void onFileTransmit(ImageFileMsg FileMsg, TCPServerSession session)
        {
            if (FileMsg.Length > 1024000 || FileMsg.Length < 0) return;//如果所传输的文件大于1M或无大小则为非法请求退出

            ServerImage image = getServerImage(FileMsg.MD5);
            string fullFileName = System.Windows.Forms.Application.StartupPath + "\\Images\\" + FileMsg.MD5 + FileMsg.Extension;//标记文件路径

            #region 在内存中为文件创建空间
            if (FileMsg.type == type.New)//上传文件请求
            {
                if (image == null)//如果服务器内存中无此文件
                {
                    image = new ServerImage(FileMsg.MD5);//创建内存文件
                    Images.Add(image.MD5, image);//将内存文件添加到文件下载服务区
                    if (File.Exists(fullFileName))//如果文件已在服务器硬盘中
                    {
                        image.Data = OpFile.Read(fullFileName);//将文件数据全部读取到内存
                        image.Length = image.Data.Length;//设置准确的文件长度
                        image.uploadLength = image.Length;//标记文件已经全部上传完成
                        FileMsg.type = type.over;//标识文件已经传输完成,通知客户端停止上传
                        sendFileMsg(FileMsg, session);//通知客户端文件已经上传完成
                        session.Disconnect();//断开客户端的TCP连接
                    }
                    else
                    {
                        image.Extension = FileMsg.Extension;
                        image.Length = FileMsg.Length;
                        image.Name = FileMsg.Name;
                        image.userID = FileMsg.from;
                        image.Data = new byte[image.Length];//创建内存空间

                        FileMsg.type = type.set;//通知客户端上传
                        FileMsg.LastLength = 0;//上传位置从零开始
                        sendFileMsg(FileMsg, session);//通知客户端可以上传文件
                    }
                }
                else//如果服务器内存中有此文件
                {
                    FileMsg.type = type.over;//标识文件已经传输完成,通知客户端停止上传
                    sendFileMsg(FileMsg, session);//通知客户端文件已经上传完成
                    session.Disconnect();//断开客户端的TCP连接
                }
            }
            #endregion

            #region 下载文件
            if (FileMsg.type == type.get)//下载文件请求
            {
                if (image == null)  //如果内存中无文件
                    if (File.Exists(fullFileName))//如果文件已在服务器硬盘中
                    {
                        image = new ServerImage(FileMsg.MD5);//创建内存文件 
                        image.MD5 = FileMsg.MD5;
                        image.Data = OpFile.Read(fullFileName);//将文件数据全部读取到内存
                        image.Length = image.Data.Length;//设置准确的文件长度
                        image.uploadLength = image.Length;//标记文件已经全部上传完成
                        Images.Add(image.MD5, image);//将内存文件添加到文件下载服务区
                    }

                if (image != null && FileMsg.LastLength < image.Data.Length) //如果内存中有文件
                {
                    if (FileMsg.LastLength + 10240 > image.Data.Length)
                        FileMsg.fileBlock = new byte[image.Data.Length - FileMsg.LastLength];//要发送的缓冲区
                    else
                        FileMsg.fileBlock = new byte[10240];//要发送的缓冲区

                    Buffer.BlockCopy(image.Data, (int)FileMsg.LastLength, FileMsg.fileBlock, 0, FileMsg.fileBlock.Length);//将其保存于Buffer字节数组

                    FileMsg.type = type.get;//下载标记
                    FileMsg.LastLength += FileMsg.fileBlock.Length;
                    sendFileMsg(FileMsg, session);

                    if (FileMsg.LastLength == image.Data.Length)
                    {
                        FileMsg.type = type.over;//标记下载完成
                        FileMsg.fileBlock = null;
                        sendFileMsg(FileMsg, session);
                        session.Disconnect();
                    }
                }
            }
            #endregion

            #region 上传文件
            if (FileMsg.type == type.set)//上传文件内容
            {
                if (image.uploadLength + FileMsg.fileBlock.Length > image.Length) return;

                Buffer.BlockCopy(FileMsg.fileBlock, 0, image.Data, (int)image.uploadLength, FileMsg.fileBlock.Length);//将收到的数据保存于Buffer字节数组

                image.uploadLength += FileMsg.fileBlock.Length;//设置最后一次上传文件的末尾长度

                FileMsg.LastLength = image.uploadLength;//告诉客户端已上传的文件位置
                FileMsg.fileBlock = null;

                if (image.uploadLength == image.Length)//如果文件上传完成
                {
                    OpFile.Write(image.Data, fullFileName);
                    FileMsg.type = type.over;//标识文件已经传输完成,通知客户端停止上传
                    sendFileMsg(FileMsg, session);//通知客户端文件已经上传完成
                    session.Disconnect();//断开客户端的TCP连接
                }
                else
                    sendFileMsg(FileMsg, session);//通知客户端上传文件下一数据包
            }
            #endregion

        }