Пример #1
0
        /// <summary>
        /// 响应发送文件方法
        /// </summary>
        /// <param name="data"></param>
        private void OnResponeSendFile(ResponeTraFransfersFile data)
        {
            SendFileManager sendFileManager;

            if (!SendFileManagerList.TryGetValue(
                    data.MD5,
                    out sendFileManager))
            {
                return;
            }
            if (data.Size > 0)
            {
                OnFileSendBuffer(new FileSendBufferEventArgs(
                                     sendFileManager,
                                     data.Size));
            }
            if (data.Index == 0)
            {
                if (sendFileManager != null)
                {
                    OnFileSendAccept(new FileSendEventArgs(sendFileManager));
                    sendFileManager.Read(data.Index);
                }
            }
            else
            {
                if (data.Index == -1)
                {
                    OnFileSendRefuse(new FileSendEventArgs(sendFileManager));
                }
                SendFileManagerList.Remove(data.MD5);
                sendFileManager.Dispose();
            }
        }
Пример #2
0
        private void ReceiveFileManagerReceiveFileTimeout(
            object sender, EventArgs e)
        {
            ReceiveFileManager receiveFileManager =
                sender as ReceiveFileManager;
            ResponeTraFransfersFile responeTraFransfersFile =
                new ResponeTraFransfersFile(
                    receiveFileManager.MD5,
                    0,
                    receiveFileManager.GetNextReceiveIndex());

            Send(
                (int)Command.ResponeSendFilePack,
                responeTraFransfersFile,
                receiveFileManager.RemoteIP);
        }
Пример #3
0
        /// <summary>
        /// 接收文件事件方法
        /// </summary>
        /// <param name="traFransfersFile"></param>
        /// <param name="remoteEp"></param>
        private void OnRecieveBuffer(
            TraFransfersFile traFransfersFile,
            IPEndPoint remoteEp)
        {
            ReceiveFileManager receiveFileManager;

            if (!ReceiveFileManagerList.TryGetValue(
                    traFransfersFile.MD5,
                    out receiveFileManager))
            {
                return;
            }
            if (receiveFileManager != null)
            {
                ResponeTraFransfersFile responeTraFransfersFile;
                int size = receiveFileManager.ReceiveBuffer(
                    traFransfersFile.Index,
                    traFransfersFile.Buffer);

                if (receiveFileManager.Completed)
                {
                    responeTraFransfersFile = new ResponeTraFransfersFile(
                        traFransfersFile.MD5,
                        size,
                        -2);
                    Send(
                        (int)Command.ResponeSendFilePack,
                        responeTraFransfersFile,
                        remoteEp);
                }
                else
                {
                    responeTraFransfersFile = new ResponeTraFransfersFile(
                        traFransfersFile.MD5,
                        size,
                        receiveFileManager.GetNextReceiveIndex());
                    Send(
                        (int)Command.ResponeSendFilePack,
                        responeTraFransfersFile,
                        remoteEp);
                }
                OnFileReceiveBuffer(
                    new FileReceiveBufferEventArgs(
                        receiveFileManager, traFransfersFile.Buffer.Length));
            }
        }
Пример #4
0
        /// <summary>
        /// 允许接收
        /// </summary>
        /// <param name="e"></param>
        public void AcceptReceive(RequestSendFileEventArgs e)
        {
            TraFransfersFileStart   traFransfersFileStart = e.TraFransfersFileStart;
            IPEndPoint              remoteIP = e.RemoteIP;
            ResponeTraFransfersFile responeTraFransfersFile;

            if (e.Cancel)
            {
                responeTraFransfersFile = new ResponeTraFransfersFile(
                    traFransfersFileStart.MD5, 0, -1);
                Send((int)Command.ResponeSendFile, responeTraFransfersFile, remoteIP);
            }
            else
            {
                ReceiveFileManager receiveFileManager;
                if (!ReceiveFileManagerList.TryGetValue(
                        traFransfersFileStart.MD5,
                        out receiveFileManager))
                {
                    receiveFileManager = new ReceiveFileManager(
                        traFransfersFileStart.MD5,
                        e.Path,
                        traFransfersFileStart.FileName,
                        traFransfersFileStart.PartCount,
                        traFransfersFileStart.PartSize,
                        traFransfersFileStart.Length,
                        remoteIP);
                    receiveFileManager.ReceiveFileComplete +=
                        new FileReceiveCompleteEventHandler(
                            ReceiveFileManagerReceiveFileComplete);
                    receiveFileManager.ReceiveFileTimeout +=
                        new EventHandler(ReceiveFileManagerReceiveFileTimeout);
                    ReceiveFileManagerList.Add(
                        traFransfersFileStart.MD5,
                        receiveFileManager);
                    receiveFileManager.Start();
                }
                responeTraFransfersFile = new ResponeTraFransfersFile(
                    traFransfersFileStart.MD5, 0, 0);
                Send((int)Command.ResponeSendFile, responeTraFransfersFile, remoteIP);
            }
        }