Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="header"></param>
        /// <param name="bytes"></param>
        /// <param name="isUpload"></param>
        private void ResponseFileStreamId(Socket client, HB32Header header, byte[] bytes, bool isUpload)
        {
            SocketDataFlag mask = (SocketDataFlag)((isUpload ? 1 : 0) << 8);
            string         path;

            /// 验证 key
            if (isUpload)
            {
                int    keyLength = Config.KeyLength;
                byte[] key       = new byte[keyLength];
                Array.Copy(bytes, 0, key, 0, keyLength);
                path = BytesParser.ParseString(bytes, ref keyLength);
                if (!CheckKey(key))
                {
                    SendBytes(client, new HB32Header {
                        Flag = SocketDataFlag.DownloadDenied ^ mask
                    }, "key error");
                    return;
                }
            }
            else
            {
                path = Encoding.UTF8.GetString(bytes);
            }
            /// 验证文件是否被占用
            if (IsFileOccupying(path))
            {
                SendBytes(client, new HB32Header {
                    Flag = SocketDataFlag.DownloadDenied ^ mask
                }, "file occupied");
                return;
            }
            /// 生成 FileStreamId 并记录
            try
            {
                FileInfo            fif    = new FileInfo(path);
                FileStream          fs     = new FileStream(path, FileMode.OpenOrCreate, isUpload ? FileAccess.Write : FileAccess.Read);
                ServerFileInfoClass record = new ServerFileInfoClass
                {
                    FStream    = fs,
                    ServerPath = path,
                    Length     = fif.Length,
                };
                int id = GenerateRandomFileStreamId(1 << 16);
                ServerFileSet.Add(id, record);
                SendBytes(client, new HB32Header {
                    Flag = SocketDataFlag.DownloadAllowed ^ mask
                }, id.ToString());
            }
            catch (Exception ex)
            {
                SendBytes(client, new HB32Header {
                    Flag = SocketDataFlag.DownloadDenied ^ mask
                }, ex.Message);
            }
        }
Пример #2
0
        private void ResponseUploadPacket(SocketResponder responder, HB32Header header, byte[] bytes)
        {
            int    i1      = 0;
            string err_msg = "";

            try
            {
                if ((GetIdentity(responder) & SocketIdentity.WriteFile) == 0)
                {
                    throw new Exception("Socket not authenticated.");
                }

                if (!ServerFileSet.ContainsKey(header.I1))
                {
                    i1 = 1;
                    throw new Exception("No fsid key in available server filestream");
                }

                SocketServerFileStreamInfo fs_info = ServerFileSet[header.I1];
                FileStream fs = fs_info.FStream;

                /// I2 == -1 则关闭 FileStream
                if (header.I2 == -1)
                {
                    fs.Close();
                    ServerFileSet.Remove(header.I1);
                    Log("Released file id : " + header.I1.ToString(), LogLevel.Info);
                    return;
                }

                /// FileStream 写入
                long begin = (long)header.I2 * HB32Encoding.DataSize;
                lock (fs)
                {
                    fs.Seek(begin, SeekOrigin.Begin);
                    fs.Write(bytes, 0, header.ValidByteLength);
                }
                fs_info.LastTime = DateTime.Now;
            }
            catch (Exception ex)
            {
                err_msg = ex.Message;
                Log("ResponseUploadPacket exception : " + err_msg, LogLevel.Warn);
            }
            if (string.IsNullOrEmpty(err_msg))
            {
                responder.SendHeader(SocketPacketFlag.UploadPacketResponse);
            }
            else
            {
                responder.SendHeader(SocketPacketFlag.UploadDenied, i1: i1);
            }
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="header"></param>
        /// <param name="bytes"></param>
        /// <param name="isUpload"></param>
        private void ResponseTransferPacket(Socket client, HB32Header header, byte[] bytes, bool isUpload)
        {
            ServerFileInfoClass sfc = ServerFileSet[header.I1];
            FileStream          fs  = sfc.FStream;

            /// I2 == -1 则关闭 FileStream
            if (header.I2 == -1)
            {
                fs.Close();
                ServerFileSet.Remove(header.I1);
                Display.TimeWriteLine("Released file id : " + header.I1.ToString());
                return;
            }
            /// 确定有效 byte 长度
            long begin  = (long)header.I2 * HB32Encoding.DataSize;
            int  length = HB32Encoding.DataSize; // 有效byte长度

            if (begin + HB32Encoding.DataSize > sfc.Length)
            {
                length = (int)(sfc.Length - begin);
            }
            byte[] responseBytes = new byte[0];
            /// 定位 FileStream 读取/写入 bytes
            lock (fs)
            {
                fs.Seek(begin, SeekOrigin.Begin);
                if (isUpload)
                {
                    fs.Write(bytes, 0, header.ValidByteLength);
                    //Display.TimeWriteLine(header.I2.ToString());
                }
                else
                {
                    responseBytes = new byte[length];
                    fs.Read(responseBytes, 0, length);
                }
            }
            sfc.LastTime = DateTime.Now;
            /// response
            if (isUpload)
            {
                SendHeader(client, new HB32Header {
                    Flag = SocketDataFlag.UploadPacketResponse
                });
            }
            else
            {
                SendBytes(client, new HB32Header {
                    Flag = SocketDataFlag.DownloadPacketResponse
                }, responseBytes);
            }
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="header"></param>
        /// <param name="bytes"></param>
        private void ResponseTransferPacket(SocketResponder responder, HB32Header header, byte[] bytes)
        {
            switch (header.Flag)
            {
            case SocketPacketFlag.UploadPacketRequest:
                ResponseUploadPacket(responder, header, bytes);
                break;

            case SocketPacketFlag.DownloadPacketRequest:
                ResponseDownloadPacket(responder, header);
                break;
            }
        }
        /// <summary>
        /// 相应 client 端的 fsid 请求
        /// 文件占用 (文件活跃时间超时被自动清除) 时返回对应 fsid , 否则生成新fsid并返回
        /// 记录在 ServerFileSet 中
        /// </summary>
        /// <param name="client"></param>
        /// <param name="header">依据header判断上传/下载</param>
        /// <param name="bytes"></param>
        private void ResponseFileStreamId(Socket client, HB32Header header, byte[] bytes)
        {
            string           err_msg = "";
            SocketPacketFlag mask    = header.Flag & (SocketPacketFlag)(1 << 8);
            int fsid = -1;

            try
            {
                SocketIdentity required_identity = (mask > 0) ? SocketIdentity.WriteFile : SocketIdentity.ReadFile;
                if ((GetIdentity(client) & required_identity) == 0)
                {
                    throw new Exception("Socket not authenticated.");
                }

                string path = Encoding.UTF8.GetString(bytes);
                if (IsFileOccupying(path, out fsid))
                {
                    ;
                }
                else
                {
                    FileInfo   fif = new FileInfo(path);
                    FileStream fs  = new FileStream(path, FileMode.OpenOrCreate, (mask > 0) ? FileAccess.Write : FileAccess.Read);
                    SocketServerFileStreamInfo record = new SocketServerFileStreamInfo
                    {
                        FStream    = fs,
                        ServerPath = path,
                        Length     = fif.Length,
                    };
                    fsid = GenerateRandomFileStreamId(1 << 16);
                    ServerFileSet.Add(fsid, record);
                }
            }
            catch (Exception ex)
            {
                err_msg = ex.Message;
            }
            if (string.IsNullOrEmpty(err_msg))
            {
                SendBytes(client, new HB32Header {
                    Flag = SocketPacketFlag.DownloadAllowed | mask
                }, fsid.ToString());
            }
            else
            {
                SendBytes(client, new HB32Header {
                    Flag = SocketPacketFlag.DownloadDenied | mask
                }, err_msg);
            }
        }
Пример #6
0
        private void ResponseDownloadPacket(SocketResponder responder, HB32Header header)
        {
            byte[] responseBytes = new byte[1];
            string err_msg       = "";
            int    i1            = 0;

            try
            {
                if ((GetIdentity(responder) & SocketIdentity.ReadFile) == 0)
                {
                    throw new Exception("Socket not authenticated.");
                }

                if (!ServerFileSet.ContainsKey(header.I1))
                {
                    i1 = 1;
                    throw new Exception("No fsid key in available server filestream");
                }

                SocketServerFileStreamInfo fs_info = ServerFileSet[header.I1];
                FileStream fs = fs_info.FStream;

                /// I2 == -1 则关闭 FileStream
                if (header.I2 == -1)
                {
                    fs.Close();
                    ServerFileSet.Remove(header.I1);
                    Log("Released file id : " + header.I1.ToString(), LogLevel.Info);
                    return;
                }

                /// 确定 Server 端 FileStream 读起点和 byte 长度
                long begin  = (long)header.I2 * HB32Encoding.DataSize;
                int  length = HB32Encoding.DataSize; /// <- 读/写 byte长度
                if (begin + HB32Encoding.DataSize > fs_info.Length)
                {
                    length = (int)(fs_info.Length - begin);
                }
                responseBytes = new byte[length];

                /// 读取 FileStream bytes
                lock (fs)
                {
                    fs.Seek(begin, SeekOrigin.Begin);
                    fs.Read(responseBytes, 0, length);
                }
                fs_info.LastTime = DateTime.Now;
            }
            catch (Exception ex)
            {
                err_msg = ex.Message;
                Log("ResponseDownloadPacket exception : " + err_msg, LogLevel.Warn);
            }

            if (string.IsNullOrEmpty(err_msg))
            {
                responder.SendBytes(SocketPacketFlag.DownloadPacketResponse, responseBytes);
            }
            else
            {
                responder.SendBytes(SocketPacketFlag.DownloadDenied, err_msg, i1: i1);
            }
        }
Пример #7
0
 public SocketIdentityCheckEventArgs(HB32Header header, byte[] bytes)
 {
     this.header = header;
     this.bytes  = bytes;
 }
Пример #8
0
 private SocketLib.Enums.SocketIdentity CheckIdentity(HB32Header header, byte[] bytes)
 {
     return(SocketLib.Enums.SocketIdentity.All);
 }