Exemple #1
0
 public void SendBytes(SocketPacketFlag flag, byte[] bytes, int i1 = 0, int i2 = 0, int i3 = 0)
 {
     SendBytes(new HB32Header
     {
         Flag = flag,
         I1   = i1,
         I2   = i2,
         I3   = i3
     }, bytes);
 }
Exemple #2
0
 /// <summary>
 /// Send socket 只发送包头
 /// </summary>
 /// <param name="socket"></param>
 /// <param name="flag"></param>
 /// <param name="i1"></param>
 /// <param name="i2"></param>
 /// <param name="i3"></param>
 protected void SendHeader(Socket socket, SocketPacketFlag flag, int i1 = 0, int i2 = 0, int i3 = 0)
 {
     SendHeader(socket, new HB32Header
     {
         Flag = flag,
         I1   = i1,
         I2   = i2,
         I3   = i3
     });
 }
Exemple #3
0
 public void SendHeader(SocketPacketFlag flag, int i1 = 0, int i2 = 0, int i3 = 0)
 {
     SendHeader(new HB32Header
     {
         Flag = flag,
         I1   = i1,
         I2   = i2,
         I3   = i3
     });
 }
Exemple #4
0
 /// <summary>
 /// 发送 Socket 数据包
 /// </summary>
 /// <param name="socket"></param>
 /// <param name="flag"></param>
 /// <param name="bytes"></param>
 /// <param name="i1"></param>
 /// <param name="i2"></param>
 /// <param name="i3"></param>
 protected void SendBytes(Socket socket, SocketPacketFlag flag, byte[] bytes, int i1 = 0, int i2 = 0, int i3 = 0)
 {
     SendBytes(socket, new HB32Header
     {
         Flag = flag,
         I1   = i1,
         I2   = i2,
         I3   = i3
     }, bytes);
 }
        /// <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);
            }
        }
Exemple #6
0
 public void ReceiveBytesWithHeaderFlag(SocketPacketFlag flag, out HB32Header header, out byte[] bytes)
 {
     ReceiveBytes(out header, out bytes);
     if (header.Flag != flag)
     {
         string err_msg = "";
         try
         {
             err_msg = Encoding.UTF8.GetString(bytes);
         }
         catch (Exception) {; }
         throw new ArgumentException(string.Format("[Received not valid header: {0}, required : {1}] -- {2}", header.Flag.ToString(), flag.ToString(), err_msg));
     }
 }
Exemple #7
0
        /// <summary>
        /// 根据 FileTask 向 Server 端请求 FileStreamId (16bit - 0~65535), 如有异常返回-1
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        private int GetFileStreamId(FileTask task)
        {
            SocketPacketFlag mask = (SocketPacketFlag)((task.Type == TransferType.Upload ? 1 : 0) << 8);

            try
            {
                SocketClient client = SocketFactory.GenerateConnectedSocketClient(task, 1);
                client.SendBytes(SocketPacketFlag.DownloadFileStreamIdRequest | mask, task.RemotePath);
                client.ReceiveBytesWithHeaderFlag(SocketPacketFlag.DownloadAllowed ^ mask, out byte[] bytes);
                client.Close();
                string response = Encoding.UTF8.GetString(bytes);
                return(int.Parse(response));
            }
            catch (Exception ex)
            {
                Logger.Log("Cannot get FileStreamID, Exception : " + ex.Message);
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return(-1);
            }
        }
Exemple #8
0
 public void SendBytes(SocketPacketFlag flag, string str, int i1 = 0, int i2 = 0, int i3 = 0)
 {
     SendBytes(flag, Encoding.UTF8.GetBytes(str), i1, i2, i3);
 }
Exemple #9
0
 public void ReceiveBytesWithHeaderFlag(SocketPacketFlag flag, out byte[] bytes)
 {
     ReceiveBytesWithHeaderFlag(flag, out _, out bytes);
 }
Exemple #10
0
 public void ReceiveBytesWithHeaderFlag(SocketPacketFlag flag, out HB32Header header)
 {
     ReceiveBytesWithHeaderFlag(flag, out header, out _);
 }
Exemple #11
0
 /// <summary>
 /// 发送 Socket 数据包, 字符串以 UTF-8 编码后发送
 /// </summary>
 /// <param name="socket"></param>
 /// <param name="flag"></param>
 /// <param name="str"></param>
 /// <param name="i1"></param>
 /// <param name="i2"></param>
 /// <param name="i3"></param>
 protected void SendBytes(Socket socket, SocketPacketFlag flag, string str, int i1 = 0, int i2 = 0, int i3 = 0)
 {
     SendBytes(socket, flag, Encoding.UTF8.GetBytes(str), i1, i2, i3);
 }