コード例 #1
0
ファイル: FileSocket.cs プロジェクト: ushiop/GamePluginsHub
        /// <summary>
        /// 发送文件,会阻塞线程,发完返回TRUE,没发完返回FALSE
        /// </summary>
        /// <param name="file">需要发送的文件的文件描述对象</param>
        public void SendFile(FileDescription file)
        {
            Sending = true;
            try{
                FileStream f = new FileStream(file.getFullPath(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                byte[] data = GetDataHead('d', f.Length);
                MaxSize  = data.Length + f.Length;
                SendSize = 0;
                MemoryStream tmp     = new MemoryStream(data);
                byte[]       tmpsend = new byte[15];
                tmp.Read(tmpsend, 0, 15);
                client.GetStream().Write(tmpsend, 0, tmpsend.Length);
                SendSize = SendSize + tmpsend.Length;
                tmp.Dispose();
                tmp.Close();
                while (SendSize < MaxSize)
                {
                    byte[] send = new byte[MaxSize - SendSize < SendSizeBag ? (MaxSize - SendSize) : SendSizeBag];
                    f.Read(send, 0, send.Length);
                    client.GetStream().Write(send, 0, send.Length);
                    SendSize = SendSize + send.Length;
                    ChangeState("正在发送文件.." + File.getName() + "..." + SendSize + "/" + MaxSize);

                    onFileTransferMissionAcc?.Invoke(this, 100.0 * (Convert.ToDouble(SendSize) / MaxSize));
                }
                ChangeState("文件发送完成");
                MaxSize  = -1;
                SendSize = 0;
            }
            catch
            {
                Stop();
            }
            Sending = false;
        }
コード例 #2
0
ファイル: FileSocket.cs プロジェクト: ushiop/GamePluginsHub
        private void RecvMessage()
        {
            while (FileSocketState != "关闭")
            {
                try
                {
                    NetworkStream netS = client.GetStream();
                    if (netS.DataAvailable == true && Sending == false)
                    {
                        if (MaxSize == -1)
                        {
                            byte[] head  = new byte[15];
                            int    heads = 0;
                            while (heads < 15)
                            {
                                byte[] f = new byte[1];
                                netS.Read(f, 0, 1);
                                if (f[0] == 0 && head[0] == 0)
                                {
                                    continue;
                                }
                                head[heads] = f[0];
                                heads++;
                            }
                            MemoryStream tmpMs       = new MemoryStream(head);
                            BinaryReader tmpMsReader = new BinaryReader(tmpMs);
                            string       msg         = Encoding.GetEncoding("GBK").GetString(tmpMsReader.ReadBytes(6));

                            if (msg == "USHIOP")
                            {
                                //合法数据包头
                                //进行设置
                                NowDataType = (char)tmpMsReader.ReadByte(); //当前传输类型
                                MaxSize     = tmpMsReader.ReadInt64();      //读取数据流总大小
                                tmpStream   = new MemoryStream();
                            }
                            tmpMsReader.Close();
                            tmpMs.Dispose();
                            tmpMs.Close();
                        }
                        if (MaxSize != -1 && tmpStream != null)
                        {
                            byte[] result = new byte[MaxSize - GetSize < SendSizeBag ? (MaxSize - GetSize) : SendSizeBag];
                            GetSize = GetSize + netS.Read(result, 0, result.Length);
                            tmpStream.Write(result, 0, result.Length);
                            ChangeState("正在接受数据..." + GetSize + "/" + MaxSize);
                            onFileTransferMissionGetAcc?.Invoke(this, NowDataType, 100.0 * (Convert.ToDouble(GetSize) / MaxSize));
                            if (GetSize >= MaxSize)
                            {
                                ChangeState("接受数据完成");
                                MaxSize  = -1;
                                GetSize  = 0;
                                SendSize = 0;
                                if (NowDataType == 's')
                                {
                                    byte[] tmp = new byte[tmpStream.Length];
                                    tmpStream.Position = 0;
                                    tmpStream.Read(tmp, 0, Convert.ToInt32(tmpStream.Length));
                                    tmpStream.Dispose();
                                    tmpStream.Close();
                                    tmpStream = null;
                                    GetStringMessage(Encoding.GetEncoding("GBK").GetString(tmp));
                                }
                                if (NowDataType == 'd')
                                {
                                    ChangeState("正在整合文件" + File.getName() + "..");
                                    MaxSize  = -1;
                                    GetSize  = 0;
                                    SendSize = 0;
                                    long readsize = 0;
                                    tmpStream.Position = 0;
                                    FileStream save = new FileStream(File.getFullPath(), FileMode.Create);
                                    while (readsize < tmpStream.Length)
                                    {
                                        long   bytesize = tmpStream.Length - readsize > SendSizeBag ? SendSizeBag : tmpStream.Length - readsize;
                                        byte[] tmp      = new byte[bytesize];

                                        readsize = readsize + tmpStream.Read(tmp, 0, tmp.Length);
                                        save.Write(tmp, 0, tmp.Length);
                                        ChangeState("正在整合文件" + File.getName() + ".." + readsize.ToString() + "/" + tmpStream.Length);
                                    }
                                    tmpStream.Dispose();
                                    tmpStream.Close();
                                    tmpStream = null;
                                    save.Dispose();
                                    save.Close();
                                    save = null;
                                    onFileTransferMissionComplete?.Invoke(this, File);
                                }
                                NowDataType = 'p';
                            }
                        }
                    }
                }
                catch
                {
                    Stop();
                }
            }
        }