Ejemplo n.º 1
0
        [InlineData(81925)] // more then one buffer - not dividable by 3
        public void EncodeDecodeLengthsMatchForDifferentLengths(int numberOfBytes)
        {
            string fileName = "Test";
            string mimeType = "application/zip";

            var transFile = new TransportFile(fileName, mimeType);
            var srcData   = new byte[numberOfBytes];

            var rand = new Random();

            rand.NextBytes(srcData);

            using (var srcMs = new MemoryStream(srcData))
                transFile.ReadFrom(srcMs);

            byte[] destData;
            using (var destMs = new MemoryStream())
            {
                transFile.WriteTo(destMs);
                destData = destMs.ToArray();
            }

            Assert.Equal(srcData.Length, destData.Length);
            Assert.Equal(srcData, destData);
        }
Ejemplo n.º 2
0
        public long SendFile(LanUser user, string path, bool bImage = false)
        {
            //发送请求
            UdpPacket packet = new UdpPacket();

            packet.Address = user.IP;
            packet.Port    = user.Port;
            packet.ToMAC   = user.MAC;
            packet.Command = UdpPacket.CMD_SEND_FILE_REQUEST | UdpPacket.CMD_OPTION_NEED_RESPONSE;
            if (bImage)
            {
                packet.Command |= UdpPacket.CMD_OPTION_SEND_FILE_IMAGE;
            }
            packet.FromMAC = this.MAC;

            UdpPacketSendFileRequestExtend extend = new UdpPacketSendFileRequestExtend();

            extend.EncryptKey = user.SecurityKeys.Public;
            extend.File       = new LanFile(path);

            packet.Extend = extend;

            _client.Send(packet);

            //保存要发送文件一览
            TransportFile file = new TransportFile(packet.ID, user.MAC, user.IP, user.Port, user.SecurityKeys.Public, extend.File);

            _fileTransTcpListener.AddTransportFile(file);

            return(file.ID);
        }
Ejemplo n.º 3
0
        public void TransportFileWithoutKnownLengthShouldBeDecodable()
        {
            string fileName = "Test";
            string mimeType = "application/zip";

            var transFile = new TransportFile(fileName, mimeType);
            var srcData   = new byte[81924]; // has to be dividable by 3 without known length
            var rand      = new Random();

            rand.NextBytes(srcData);

            using (var srcMs = new MemoryStream(srcData))
                transFile.ReadFrom(srcMs);

            transFile.OriginalLength = 0; // Reset to default state!

            byte[] destData;
            using (var destMs = new MemoryStream())
            {
                transFile.WriteTo(destMs);
                destData = destMs.ToArray();
            }

            Assert.Equal(srcData.Length, destData.Length);
            Assert.Equal(srcData, destData);
        }
Ejemplo n.º 4
0
        private void _user_FileReceiveError(object sender, FileTransportErrorEventArgs args)
        {
            TransportFile file = args.File;
            LanUser       user = _user[args.File.MAC];

            this.userListBox.SetMessageResult(user.ID, _user.ID, file.ID, false);
        }
Ejemplo n.º 5
0
        public void ReceiveFile(TransportFile file)
        {
            FileTransportTcpClient client = new FileTransportTcpClient(this._context);

            client.ProgressChanged += FileReceiveProgressChanged;
            client.Completed       += FileReceiveCompleted;
            client.Error           += FileReceiveError;
            client.Receive(file);
        }
Ejemplo n.º 6
0
        private void _user_FileTransportProgressChanged(object sender, FileTransportEventArgs args)
        {
            TransportFile file = args.File;

            OutputLog("接收文件[" + file.File.Name + "]," + file.Progress + "%," +
                      LanFile.HumanReadbleLen(file.TransportedLength) + "/" +
                      LanFile.HumanReadbleLen(file.File.Length) + "," +
                      LanFile.HumanReadbleLen(file.TransportedSpeed) + "/s");
        }
Ejemplo n.º 7
0
        private void _user_FileTransportRequested(object sender, FileTransportRequestedEventArgs args)
        {
            TransportFile file = args.File;

            //保存到默认接收文件夹
            file.SavePath = LanConfig.Instance.GetReceivedFilePath(file.File.Name);

            this.userListBox.AddFileReceivingMessage(args.User, file);

            //自动接收文件
            _user.ReceiveFile(file);

            UpdateUnreadMessageUI();
        }
Ejemplo n.º 8
0
        private void ReceiveImage(LanUser user, TransportFile file)
        {
            file.SavePath = LanConfig.Instance.GetTempFileName(".png");

            FileTransportTcpClient client = new FileTransportTcpClient(_context);

            client.ProgressChanged += ImageReceiveProgressChanged;
            client.Completed       += new FileTransportEventHandler((sender, args) =>
            {
                Image image = Image.FromFile(file.SavePath);
                ImageReceivedEventArgs stateArgs = new ImageReceivedEventArgs(user, file.ID, image);
                ImageReceived(this, stateArgs);
            });
            client.Error += ImageReceiveError;
            client.Receive(file);
        }
Ejemplo n.º 9
0
 private void _user_FileTransportRequested(object sender, FileTransportRequestedEventArgs args)
 {
     if (MessageBox.Show("是否接受文件:\r\nFrom:" + args.User + "\r\nFile:" + args.File, "", MessageBoxButtons.YesNo) ==
         DialogResult.Yes)
     {
         SaveFileDialog sfd = new SaveFileDialog();
         if (sfd.ShowDialog() == DialogResult.OK)
         {
             TransportFile file = args.File;
             file.SavePath = sfd.FileName;
             _user.ReceiveFile(file);
         }
     }
     else
     {
         //_user.RejectReceiveFile(args.ID);
     }
 }
Ejemplo n.º 10
0
        private TransportFile CreateTransportFile(string fileName, string mimeType, int chunkNumber, byte[] srcData, int offset, int chunkSize, bool lastChunk, long?transferId)
        {
            TransportFile transFile;

            using (var ms = new MemoryStream(srcData, offset, chunkSize))
            {
                transFile = new TransportFile(fileName, mimeType,
                                              CreateTransFileProperties(lastChunk, chunkNumber, srcData.Length, chunkSize, offset, transferId));
                transFile.ReadFrom(ms);
            }

            using (var ms = new MemoryStream(srcData, offset, chunkSize))
            {
                var propsWithFingerPrint = transFile.ToSerializableProperties();
                transFile = new TransportFile(fileName, mimeType, propsWithFingerPrint);
                transFile.ReadFrom(ms);
            }

            return(transFile);
        }
Ejemplo n.º 11
0
        public void AddFileReceivingMessage(LanUser from, TransportFile file)
        {
            //保存记录
            Store.Models.FileMessage m = new Store.Models.FileMessage();
            m.FromUserId     = from.ID;
            m.ToUserId       = this.OwnerUser.ID;
            m.OriginFilePath = file.SavePath;
            m.FileName       = Path.GetFileName(file.SavePath);
            m.FileLength     = file.File.Length;
            m.Flag           = true; //默认成功,后面按照失败结果设定为false

            MessageListItem item = new MessageListItem();

            item.ID      = file.ID;
            item.Message = m;
            item.User    = from;
            item.State   = MessageState.Receiving;

            //保存到数据库
            item.Save();

            AddMessageListItem(from, item);
        }
Ejemplo n.º 12
0
        public void SetFileTransportProgress(TransportFile file)
        {
            UserListItem    userItem = this[file.MAC];
            UserChatControl chatCtrl = userItem.ChatControl;

            MessageListItem item = null;

            if (chatCtrl == null)
            {
                //如果还没表示则取出来
                item = userItem.GetWaitDisplayMessageItem(file.ID);
            }
            else
            {
                item = chatCtrl.GetMessageItem(file.ID);
            }

            item.FileTransportedLength = file.TransportedLength;
            item.FileTransportedSpeed  = file.TransportedSpeed;
            item.Progress = file.Progress;

            if (file.Progress == 100)
            {
                //传输完毕
                Store.Models.Message m = item.Message;
                m.Flag = true;
                item.Update();

                item.State = MessageState.Received;
            }

            if (chatCtrl != null && file.MAC == this.SelectedUser.MAC)
            {
                //当前表示着,更新
                chatCtrl.RefreshMessageList(item);
            }
        }
Ejemplo n.º 13
0
        private void _user_FileSendCompleted(object sender, FileTransportEventArgs args)
        {
            TransportFile file = args.File;

            this.userListBox.SetFileTransportProgress(file);
        }
Ejemplo n.º 14
0
        private void _user_FileReceiveProgressChanged(object sender, FileTransportEventArgs args)
        {
            TransportFile file = args.File;

            this.userListBox.SetFileTransportProgress(file);
        }
Ejemplo n.º 15
0
 public FileTransportRequestedEventArgs(LanUser user, TransportFile file)
     : base(user)
 {
     this._file = file;
 }
Ejemplo n.º 16
0
        private void ReceivePacketEvent(object sender, UdpClientReceiveEventArgs args)
        {
            UdpPacket packet = args.Packet as UdpPacket;

            if (packet.CheckSendResponse)
            {
                #region 需要回应收到包
                if (packet.Version == Packet.VERSION)
                {
                    UdpPacket packetRsp = new UdpPacket();
                    packetRsp.Address = packet.Address;
                    packetRsp.Port    = packet.Port;
                    packet.ToMAC      = packet.ToMAC;
                    packetRsp.FromMAC = this.MAC;
                    packetRsp.Command = UdpPacket.CMD_RESPONSE;

                    UdpPacketResponseExtend extend = new UdpPacketResponseExtend();
                    extend.ID        = packet.ID;
                    packetRsp.Extend = extend;

                    _client.Send(packetRsp);
                }
                #endregion
            }

            if (packet.CMD == UdpPacket.CMD_NOTHING)
            {
                //do nothing
            }
            else if (packet.CMD == UdpPacket.CMD_ENTRY)
            {
                #region CMD_ENTRY 对方上线
                UpdateState updateState = UpdateContacter(packet);

                //TODO 应该更新所有的,但是跨网的时候好像发送的byte太长,不能正常发送暂且去掉头像
                //SendUpdateStatePacket(LanIM.UpdateState.All,
                //    packet.Address, packet.Port, packet.FromMAC);

                SendUpdateStatePacket(LanIM.UpdateState.NickName | LanIM.UpdateState.PublicKey | LanIM.UpdateState.Status,
                                      packet.Address, packet.Port, packet.FromMAC);

                if (UserEntry != null)
                {
                    LanUser user = this[packet.FromMAC];
                    UserStateChangeEventArgs stateArgs = new UserStateChangeEventArgs(user, updateState);
                    UserEntry(this, stateArgs);
                }
                #endregion
            }
            else if (packet.CMD == UdpPacket.CMD_EXIT)
            {
                #region CMD_EXIT
                if (UserExit != null)
                {
                    LanUser user = this[packet.FromMAC];
                    user.Status = UserStatus.Offline;
                    UserStateChangeEventArgs stateArgs = new UserStateChangeEventArgs(user, LanIM.UpdateState.Status);
                    UserExit(this, stateArgs);
                }
                #endregion
            }
            else if (packet.CMD == UdpPacket.CMD_STATE)
            {
                #region CMD_STATE
                UpdateState updateState = UpdateContacter(packet);
                if (UserStateChange != null)
                {
                    LanUser user = this[packet.FromMAC];
                    UserStateChangeEventArgs stateArgs = new UserStateChangeEventArgs(user, updateState);
                    UserStateChange(this, stateArgs);
                }
                #endregion
            }
            else if (packet.CMD == UdpPacket.CMD_SEND_TEXT)
            {
                #region CMD_SEND_TEXT
                UdpPacketTextExtend extend = packet.Extend as UdpPacketTextExtend;
                if (TextMessageReceived != null)
                {
                    LanUser user = this[packet.FromMAC];
                    TextMessageReceivedEventArgs stateArgs = new TextMessageReceivedEventArgs(user, packet.ID, extend.Text);
                    TextMessageReceived(this, stateArgs);
                }
                #endregion
            }
            else if (packet.CMD == UdpPacket.CMD_SEND_IMAGE)
            {
                #region CMD_SEND_IMAGE
                UdpPacketImageExtend extend = packet.Extend as UdpPacketImageExtend;
                if (ImageReceived != null)
                {
                    LanUser user = this[packet.FromMAC];
                    ImageReceivedEventArgs stateArgs = new ImageReceivedEventArgs(user, packet.ID, extend.Image);
                    ImageReceived(this, stateArgs);
                }
                #endregion
            }
            else if (packet.CMD == UdpPacket.CMD_SEND_FILE_REQUEST)
            {
                #region CMD_SEND_FILE_REQUEST
                LanUser user = this[packet.FromMAC];
                UdpPacketSendFileRequestExtend extend = packet.Extend as UdpPacketSendFileRequestExtend;

                TransportFile file = new TransportFile(packet.ID, user.MAC, user.IP, user.Port, user.SecurityKeys.Public, extend.File);

                if ((packet.Command & UdpPacket.CMD_OPTION_SEND_FILE_IMAGE) != 0)
                {
                    //接收图像
                    if (ImageReceived != null)
                    {
                        this.ReceiveImage(user, file);
                    }
                }
                else
                {
                    if (FileTransportRequested != null)
                    {
                        FileTransportRequestedEventArgs stateArgs = new FileTransportRequestedEventArgs(user, file);
                        FileTransportRequested(this, stateArgs);
                    }
                }
                #endregion
            }
            else if (packet.CMD == UdpPacket.CMD_RESPONSE)
            {
                UdpPacketResponseExtend extend = packet.Extend as UdpPacketResponseExtend;
                _client.NotifySendPacketSuccess(extend.ID);
            }
            else
            {
                throw new Exception("未想定命令");
            }
        }
Ejemplo n.º 17
0
        private void _user_FileSendError(object sender, FileTransportErrorEventArgs args)
        {
            TransportFile file = args.File;

            this.userListBox.SetMessageResult(_user.ID, file.MAC, file.ID, false);
        }