/// <summary>
 /// ��Է�UDP�˿ڷ�����������
 /// </summary>
 private void UdpHandshake()
 {
     try
     {
         msgFile msg = new msgFile();
         if (!UdpHandshakeInfoClass)//�������������
         {
             msg.InfoClass = (byte)ProtocolFileTransmit.HandshakeLAN;
             this.sockUDP1.Send(this._OppositeUserInfo.LocalIP, this.OppositeUDPPort, msg.getBytes());
         }
         else if (UdpHandshakeInfoClass)//�������������
         {
             msg.InfoClass = (byte)ProtocolFileTransmit.HandshakeWAN;
             this.sockUDP1.Send(this._OppositeUserInfo.IP, this.OppositeUDPPort, msg.getBytes());
         }
     }
     catch { }
 }
 /// <summary>
 /// sockUDP �����ļ�����
 /// </summary>
 /// <param name="msg">�ļ���Ϣ</param>
 private void sendData(msgFile msg)
 {
     try
     {
         if (this.netClass == NetCommunicationClass.LanUDP)//����Ǿ�����ͨ��
             this.sockUDP1.Send(this._OppositeUserInfo.LocalIP,this.OppositeUDPPort, msg.getBytes());//����UDP�������ݵ��Է�������IP��˿�
         else if (this.netClass == NetCommunicationClass.WanNoProxyUDP)//����ǹ�����ֱ��ͨ��
             this.sockUDP1.Send(this._OppositeUserInfo.IP, this.OppositeUDPPort, msg.getBytes());//����UDP�������ݵ��Է�������IP��˿�
         //else if (this.netClass == NetCommunicationClass.WanProxyUDP)//����ǹ�������������תͨ��
         //    this.sockUDP1.Send(this._serverIp, this._serverUDPPort, msg.getBytes());//����UDP�������ݵ���������תIP��˿�
         else if (this.netClass == NetCommunicationClass.TCP)
             this.asyncTCPClient1.SendData(msg.getBytes());//����TCP��������
     }
     catch
     { }
 }
 /// <summary>
 /// UDP���ݵ����¼�
 /// </summary>
 /// <param name="e">UDP���ݲ���</param>
 private void sockUDP1_DataArrival(object sender,SockEventArgs  e)
 {
     //if (e.Data.Length < 10) return;
     msgFile msg = new msgFile(e.Data);
     this.DataArrival(msg,NatClass.FullCone, e.IP,e.Port);
 }
        //���Է������ļ����ݿ����
        /// <summary>
        /// ����Է������ļ����ݿ�
        /// </summary>
        private void ReceivedFileBlock(msgFile msg)
        {
            if (msg.pSendPos > this.currGetPos)//������͹��������ݴ��ڵ�ǰ��õ�����
            {
                if (this.IsReadWriteFile(this.currGetPos))
                {
                    //�����Ƕ�һ���ļ����ڴ����
                    if (this.currReadCount + 1 == this.readFileCount)//��������һ�ζ�д�ļ����������ļ�β����ȫ�����뵽�ڴ�
                        FileBlock = new byte[this._FileLen - this.currReadCount * this.maxReadWriteFileBlock];
                    else
                        FileBlock = new byte[this.maxReadWriteFileBlock];

                    this.currReadCount++;//�ļ�����������1
                }

                int offSet = this.currGetPos % this.maxReadWriteFileBlock;// ���Ҫ��д�ڴ�ľ���λ��
                Buffer.BlockCopy(msg.FileBlock, 0, this.FileBlock, offSet, msg.FileBlock.Length);//���䱣����Buffer�ֽ�����

                this.currGetPos = (int)msg.pSendPos;

                if (this.fileTransmitting != null)//�����յ������ļ������¼�
                    this.fileTransmitting(this, new fileTransmitEvnetArgs(this._IsSend, this._fullFileName, this._fileName, "", this._FileLen, this.currGetPos, this.FileMD5Value));

                if (this.IsReadWriteFile(this.currGetPos) || this.currGetPos == this._FileLen)
                {
                    ////////////////////////�ļ�����
                    FileStream fw = new FileStream(this._fullFileName, FileMode.Append, FileAccess.Write, FileShare.Read);
                    //fw.Seek(fw.l, SeekOrigin.Begin);//�ϴη��͵�λ��
                    fw.Write(this.FileBlock, 0, this.FileBlock.Length);
                    //�����д�����������첽��ʽ
                    //fw.BeginRead(myData.Buffer, 0, assignSize, new AsyncCallback(AsyncRead), myData);
                    ///ʵ�ֶ��߳�ͬʱ��д�ļ�
                    fw.Close();
                    fw.Dispose();
                    ///////////////////////////
                }

                if (this.currGetPos == this._FileLen)//����ļ�������ɣ�������������¼�
                {
                    msg.InfoClass = (byte)ProtocolFileTransmit.FileTranstmitOver;
                    msg.SendID = this.serverSelfID;
                    msg.RecID = this.serverOppositeID;
                    this.sendData(msg);//���߶Է��ļ��������
                    onFileTransmitted();//�����ļ���������¼�
                    //return;//�ļ�����
                }

                this.sendRequestGetFileData();//���۵�ǰ��ö������ݣ���Ҫ��Է�������һ���ݰ�
            }
        }
        private void DataArrival(msgFile msg, NatClass netClass, System.Net.IPAddress Ip, int Port)
        {
            switch (msg.InfoClass)
            {
                case (byte)ProtocolFileTransmit.GetFileTransmitProxyID://����Լ��ӷ������ϻ����ת����ID
                    {
                        this.serverSelfID = msg.SendID;
                        if (this.serverOppositeID != -1)//����Ƿ��ͷ������ת����ID������߶Է���ʼ�ս��ļ�
                        {
                            this.netClass = NetCommunicationClass.TCP;//��ʶ��ǰͨ��Э��ΪTCP
                            //this._mtu = 1200;//�����������ļ�ʱ����MTUֵ����Ϊ1200ʹ·���������ǽת������
                            msg.InfoClass = (byte)ProtocolFileTransmit.BeginTransmit;
                            msg.SendID = this.serverSelfID;
                            msg.RecID = this.serverOppositeID;
                            this.sendData(msg);
                        }
                        else if (this.getFileProxyID != null)//����ǽ��շ��򴥷������ת����ID��֮�Է�
                            this.getFileProxyID(this, this.serverSelfID);
                    }
                    break;
                case (byte)ProtocolFileTransmit.FileTransmit://����ļ���������
                    {
                        //Calculate.WirteLog("�����ļ����Է�");
                        this.sendFile((int)msg.pSendPos);//�����ļ����Է�
                    }
                    break;
                case (byte)ProtocolFileTransmit.GetFileBlock :// ��öԷ�������ļ����ݰ�
                    {
                        //Calculate.WirteLog("�յ��ļ�����");
                        this.ReceivedFileBlock(msg);//�Է������ļ����ݹ���,�������ݵ��ļ�
                    }
                    break;
                case (byte)ProtocolFileTransmit.BeginTransmit://�����Ѿ��������Է�Ҫ��ʼ�����ļ�
                    {
                        this.serverOppositeID = msg.SendID;//��öԷ�ID
                        if (netClass == NatClass.Tcp)//�����TCPͨ��
                        {
                            this.netClass = NetCommunicationClass.TCP;//����TCPЭ�鴫���ļ�
                        }
                        else
                        {
                            this.netClass = NetCommunicationClass.WanNoProxyUDP;//����UDPЭ�鴫��
                        }

                        if (this.fileTransmitConnected != null)//����ͨ�ųɹ��¼������˳�ͨ�Ų���
                            this.fileTransmitConnected(this, this.netClass);

                        if (!this.IsSendState)//����ļ���û�п�ʼ���ͣ�����
                            this.sendRequestGetFileData();//�����ļ����Է�
                        //Calculate.WirteLog("�����Ѿ��������Է�Ҫ��ʼ�����ļ�");
                    }
                    break;
                case (byte)ProtocolFileTransmit.FileTranstmitOver ://�ļ��������
                    {
                        this.onFileTransmitted();
                    }
                    break;

                case (byte)ProtocolFileTransmit.HandshakeLAN://�յ��Է�������UDP��������
                    {
                        this._OppositeUserInfo.LocalIP = Ip;//�������öԷ��ľ�����IP
                        this.OppositeUDPPort = Port;//�������öԷ��ľ�����UDP�˿�
                        msg.InfoClass =(byte)ProtocolFileTransmit.IsOppositeRecSelfLanUDPData;//���߶Է��յ�����������Ϣ
                        this.sockUDP1.Send(this._OppositeUserInfo.LocalIP, this.OppositeUDPPort, msg.getBytes());
                        //Calculate.WirteLog(this._IsSend.ToString()+ "�յ��Է�������UDP��������:"+ Ip.ToString() +":"+ Port.ToString());
                    }
                    break;
                case (byte)ProtocolFileTransmit.IsOppositeRecSelfLanUDPData://�Է��յ��Լ����͵ľ�����UDP��������
                    {
                        this.netClass = NetCommunicationClass.LanUDP;//��ʶ��Է�����������ͨ�ųɹ�
                       if (this._IsSend)//����Ƿ����ļ���һ�����ҶԷ���֮�յ��Լ����������ݣ���ͨ��ͨ����ͨ
                       {
                           //this._mtu = 1400;//�����������ļ�ʱ����MTUֵ����Ϊ5120��5k������ٶ�
                           msg.InfoClass = (byte)ProtocolFileTransmit.BeginTransmit;
                           msg.SendID = 0;
                           this.sendData(msg);//���߶Է���ʼ�����ļ�
                       }
                       //Calculate.WirteLog(Ip.ToString() + ":" + Port.ToString() + "�Է��յ��Լ����͵ľ�����UDP��������" + this._IsSend.ToString());
                    }
                    break;

                case (byte)ProtocolFileTransmit.HandshakeWAN ://�յ��Է�������UDP��������
                    {
                        this._OppositeUserInfo.IP = Ip;//�������öԷ��Ĺ�����IP
                        this.OppositeUDPPort = Port;//�������öԷ��Ĺ�����UDP�˿�
                        msg.InfoClass = (byte)ProtocolFileTransmit.IsOppositeRecSelfWanUDPData ;//���߶Է��յ�����������Ϣ
                        this.sockUDP1.Send(this._OppositeUserInfo.IP, this.OppositeUDPPort, msg.getBytes());
                        //Calculate.WirteLog(this._IsSend.ToString()+ "�յ��Է�������UDP��������:"+ Ip.ToString() +":"+ Port.ToString());
                    }
                    break;
                case (byte)ProtocolFileTransmit.IsOppositeRecSelfWanUDPData://�Է��յ��Լ����͵ľ�����UDP��������
                    {
                        this.netClass = NetCommunicationClass.WanNoProxyUDP;//��ʶ��Է�����������ֱ��ͨ�ųɹ�
                        if (this._IsSend)//����Ƿ����ļ���һ�����ҶԷ���֮�յ��Լ����������ݣ���ͨ��ͨ����ͨ
                        {
                            //this._mtu = 1200;//�����������ļ�ʱ����MTUֵ����Ϊ1200��1k�ٶ�
                            msg.InfoClass = (byte)ProtocolFileTransmit.BeginTransmit;
                            msg.SendID = 0;
                            this.sendData(msg);//���߶Է���ʼ�����ļ�
                        }
                        //Calculate.WirteLog(Ip.ToString() + ":" + Port.ToString() + "�Է��յ��Լ����͵ľ�����UDP��������" + this._IsSend.ToString());
                    }
                    break;

                case (byte)ProtocolFileTransmit.GetUDPWANInfo://��÷��������ص��ļ������׽��ֹ�����UDP�˿�
                    {
                        this.selfUDPPort=msg.SendID;//�������öԷ��Ĺ�����UDP�˿�
                        if (!this.IsGetWanUDP && this.fileTransmitGetUDPPort != null)
                        {
                            this.IsGetWanUDP = true;//��ʶ�Ѿ�������WAN UDP�˿ڻ�ȡ�¼�
                            this.fileTransmitGetUDPPort(this, this.selfUDPPort, true);
                        }
                        //Calculate.WirteLog(this._IsSend.ToString() + "��÷��������ص��ļ������׽��ֹ�����UDP�˿�:" + this.selfUDPPort);
                    }
                    break;

            }
        }
 private void asyncTCPClient1_OnDataArrival(object sender, SockEventArgs e)
 {
     //if (e.Data.Length < 10) return;
     msgFile msg = new msgFile(e.Data);
     this.DataArrival(msg,NatClass.Tcp ,null,0);
 }
 private void asyncTCPClient1_OnConnected(object sender,  SockEventArgs  e)
 {
     //IMLibrary.Calculate.WirteLog("������");
        msgFile msg = new msgFile((byte)ProtocolFileTransmit.GetFileTransmitProxyID, -1, -1, 0, new byte[1]);
     this.asyncTCPClient1.SendData(msg.getBytes());//�������������ת����ID��
 }
        /// <summary>
        /// ���öԷ��ļ�����UDP���ض˿� 
        /// </summary>
        /// <param name="Port">�ļ�����UDP���ض˿�</param>
        public void setFileTransmitGetUdpLocalPort(int Port, bool udpHandshakeInfoClass)
        {
            this.OppositeUDPPort = Port;//���öԷ�UDP�˿ں�
            this.UdpHandshakeInfoClass = udpHandshakeInfoClass;

            System.Threading.Thread.Sleep(100);

            if (!udpHandshakeInfoClass)//����Լ���δUDP����,���þ�������ʽͨ��
            {
                if (!this.sockUDP1.Listened)//���û������
                    this.UDPListen();//���UDP����
                if (!this.IsGetLanUDP && this.fileTransmitGetUDPPort != null)//��֮�Է����ض˿ڣ�������ö˿��¼�
                {
                    this.IsGetLanUDP = true;//��ʶ�Ѿ�������LAN UDP�¼�
                    this.fileTransmitGetUDPPort(this, this.selfUDPPort, false);
                }
                //Calculate.WirteLog(this._IsSend.ToString() + "����Լ���δUDP����,���þ�������ʽͨ��");
            }
            else if (udpHandshakeInfoClass)//���ù�������ʽͨ��
            {
                if (!this.sockUDP1.Listened)//���û������
                    this.UDPListen();//���UDP����
                msgFile msg = new msgFile();
                msg.InfoClass = (byte)ProtocolFileTransmit.GetUDPWANInfo;//ͨ��Э��
                //this.sockUDP1.Send(_serverIp, _serverUDPPort, msg.getBytes());//����ļ������׽��ֵĹ�����UDP�˿�
                //Calculate.WirteLog(this._IsSend.ToString() + "����ļ������׽��ֵĹ�����UDP�˿�");
            }

            if (!timersUdpPenetrate.Enabled)//���δ���֣���ʼ����
                timersUdpPenetrate.Enabled = true;//��ʼ��Է�UDP�˿�����(��),����ɹ�����ʾ���Խ���UDPͨ��
        }