/// <summary> /// sockUDP �����ļ����� /// </summary> /// <param name="msg">�ļ���Ϣ</param> private void sendData(CSS.IM.Library.Class.msgFile msg) { try { //if (this.netClass == CSS.IM.Library.Class.NetCommunicationClass.LanUDP)//����Ǿ�����ͨ�� this.sockUDP1.Send(this.OppositeUDPIP,this.OppositeUDPPort, msg.getBytes());//����UDP�������ݵ��Է�������IP��˿� } catch { } }
/// <summary> /// sockUDP 发送文件数据 /// </summary> /// <param name="fInfo">文件信息</param> private void sendData(CSS.IM.Library.Class.msgAV msg) { try { if (this.netClass == CSS.IM.Library.Class.NetCommunicationClass.LanUDP)//如果是局域网通信 this.sockUDP1.Send(this._OppositeUserInfo.LocalIP, this.OppositeUDPPort, msg.getBytes());//采用UDP发送数据到对方局域网IP与端口 else if (this.netClass == CSS.IM.Library.Class.NetCommunicationClass.WanNoProxyUDP)//如果是广域网直接通信 this.sockUDP1.Send(this._OppositeUserInfo.IP, this.OppositeUDPPort, msg.getBytes());//采用UDP发送数据到对方广域网IP与端口 else if (this.netClass == CSS.IM.Library.Class.NetCommunicationClass.WanProxyUDP)//如果是广域网服务器中转通信 this.sockUDP1.Send(this._serverIp, this._serverUDPPort, msg.getBytes());//采用UDP发送数据到服务器中转IP与端口 else if (this.netClass == CSS.IM.Library.Class.NetCommunicationClass.TCP) this.TCPClient1.SendData(msg.getBytes());//采用TCP发送数据 } catch { } }
private void DataArrival(CSS.IM.Library.Class.msgFile msg, CSS.IM.Library.Class.NatClass netClass, System.Net.IPAddress Ip, int Port) { switch (msg.InfoClass) { case (byte)CSS.IM.Library.Class.ProtocolFileTransmit.GetFileTransmitProxyID://����Լ��ӷ������ϻ����ת����ID { this.serverSelfID = msg.SendID; if (this.serverOppositeID != -1)//����Ƿ��ͷ������ת����ID������߶Է���ʼ�ս��ļ� { this.netClass = CSS.IM.Library.Class.NetCommunicationClass.TCP;//��ʶ��ǰͨ��Э��ΪTCP //this._mtu = 1200;//�����������ļ�ʱ����MTUֵ����Ϊ1200ʹ·���������ǽת������ msg.InfoClass = (byte)CSS.IM.Library.Class.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)CSS.IM.Library.Class.ProtocolFileTransmit.FileTransmit://����ļ��������� { //Calculate.WirteLog("�����ļ����Է�"); this.sendFile((int)msg.pSendPos);//�����ļ����Է� } break; case (byte)CSS.IM.Library.Class.ProtocolFileTransmit.GetFileBlock :// ��öԷ�������ļ����ݰ� { //Calculate.WirteLog("�յ��ļ�����"); this.ReceivedFileBlock(msg);//�Է������ļ����ݹ���,�������ݵ��ļ� } break; case (byte)CSS.IM.Library.Class.ProtocolFileTransmit.BeginTransmit://�����Ѿ��������Է�Ҫ��ʼ�����ļ� { this.serverOppositeID = msg.SendID;//��öԷ�ID if (netClass == CSS.IM.Library.Class.NatClass.Tcp)//�����TCPͨ�� { this.netClass = CSS.IM.Library.Class.NetCommunicationClass.TCP;//����TCPЭ�鴫���ļ� } else { this.netClass = CSS.IM.Library.Class.NetCommunicationClass.WanNoProxyUDP;//����UDPЭ�鴫�� } if (this.fileTransmitConnected != null)//����ͨ�ųɹ��¼������˳�ͨ�Ų��� this.fileTransmitConnected(this, this.netClass); if (!this.IsSendState)//����ļ���û�п�ʼ���ͣ����� this.sendRequestGetFileData();//�����ļ����Է� //Calculate.WirteLog("�����Ѿ��������Է�Ҫ��ʼ�����ļ�"); } break; case (byte)CSS.IM.Library.Class.ProtocolFileTransmit.FileTranstmitOver ://�ļ�������� { this.onFileTransmitted(); } break; case (byte)CSS.IM.Library.Class.ProtocolFileTransmit.HandshakeLAN://�յ��Է�������UDP�������� { this.OppositeUDPIP = Ip;//�������öԷ��ľ�����IP this.OppositeUDPPort = Port;//�������öԷ��ľ�����UDP�˿� msg.InfoClass =(byte)CSS.IM.Library.Class.ProtocolFileTransmit.IsOppositeRecSelfLanUDPData;//���߶Է��յ�����������Ϣ this.sockUDP1.Send(this.OppositeUDPIP, this.OppositeUDPPort, msg.getBytes()); //Calculate.WirteLog(this._IsSend.ToString()+ "�յ��Է�������UDP��������:"+ Ip.ToString() +":"+ Port.ToString()); } break; case (byte)CSS.IM.Library.Class.ProtocolFileTransmit.IsOppositeRecSelfLanUDPData://�Է��յ��Լ����͵ľ�����UDP�������� { this.netClass = CSS.IM.Library.Class.NetCommunicationClass.LanUDP;//��ʶ��Է�����������ͨ�ųɹ� if (this._IsSend)//����Ƿ����ļ���һ�����ҶԷ���֮�յ��Լ����������ݣ���ͨ��ͨ����ͨ { //this._mtu = 1400;//�����������ļ�ʱ����MTUֵ����Ϊ5120��5k������ٶ� msg.InfoClass = (byte)CSS.IM.Library.Class.ProtocolFileTransmit.BeginTransmit; msg.SendID = 0; this.sendData(msg);//���߶Է���ʼ�����ļ� } //Calculate.WirteLog(Ip.ToString() + ":" + Port.ToString() + "�Է��յ��Լ����͵ľ�����UDP��������" + this._IsSend.ToString()); } break; case (byte)CSS.IM.Library.Class.ProtocolFileTransmit.HandshakeWAN ://�յ��Է�������UDP�������� { this.OppositeUDPIP = Ip;//�������öԷ��Ĺ�����IP this.OppositeUDPPort = Port;//�������öԷ��Ĺ�����UDP�˿� msg.InfoClass = (byte)CSS.IM.Library.Class.ProtocolFileTransmit.IsOppositeRecSelfWanUDPData ;//���߶Է��յ�����������Ϣ this.sockUDP1.Send(this.OppositeUDPIP, this.OppositeUDPPort, msg.getBytes()); //Calculate.WirteLog(this._IsSend.ToString()+ "�յ��Է�������UDP��������:"+ Ip.ToString() +":"+ Port.ToString()); } break; case (byte)CSS.IM.Library.Class.ProtocolFileTransmit.IsOppositeRecSelfWanUDPData://�Է��յ��Լ����͵ľ�����UDP�������� { this.netClass = CSS.IM.Library.Class.NetCommunicationClass.WanNoProxyUDP;//��ʶ��Է�����������ֱ��ͨ�ųɹ� if (this._IsSend)//����Ƿ����ļ���һ�����ҶԷ���֮�յ��Լ����������ݣ���ͨ��ͨ����ͨ { //this._mtu = 1200;//�����������ļ�ʱ����MTUֵ����Ϊ1200��1k�ٶ� msg.InfoClass = (byte)CSS.IM.Library.Class.ProtocolFileTransmit.BeginTransmit; msg.SendID = 0; this.sendData(msg);//���߶Է���ʼ�����ļ� } //Calculate.WirteLog(Ip.ToString() + ":" + Port.ToString() + "�Է��յ��Լ����͵ľ�����UDP��������" + this._IsSend.ToString()); } break; case (byte)CSS.IM.Library.Class.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 DataArrival(CSS.IM.Library.Class.msgAV msg, CSS.IM.Library.Class.NatClass netClass, System.Net.IPAddress Ip, int Port) { switch (msg.InfoClass) { case (byte)CSS.IM.Library.Class.ProtocolAVTransmit.GetVideoData:// 获得对方传输的视频数据包 { this.OnVideoData(msg.DataBlock); //Calculate.WirteLog("收到v"+ msg.DataBlock.Length.ToString()); //this.ReceivedFileBlock(msg);//对方发送文件数据过来,保存数据到文件 } break; case (byte)CSS.IM.Library.Class.ProtocolAVTransmit.GetAudioData:// 获得对方传输的音频数据包 { this.OnAudioData(msg.DataBlock); //Calculate.WirteLog("收到a"+ msg.DataBlock.Length.ToString()); //this.ReceivedFileBlock(msg);//对方发送文件数据过来,保存数据到文件 } break; case (byte)CSS.IM.Library.Class.ProtocolAVTransmit.GetBITMAPINFOHEADER://获得对方视频图像头信息 { this.OnGetBITMAPINFOHEADER(msg.DataBlock); break; } case (byte)CSS.IM.Library.Class.ProtocolAVTransmit.GetAVTransmitProxyID://获得自己从服务器上获得中转服务ID { this.serverSelfID = msg.SendID; if (this.serverOppositeID != -1)//如果已经获得对方中转服务ID,则告诉对方开始视频传输 { this.netClass = CSS.IM.Library.Class.NetCommunicationClass.TCP;//采用TCP协议传输文件 msg.InfoClass = (byte)CSS.IM.Library.Class.ProtocolAVTransmit.BeginTransmit; msg.SendID = this.serverSelfID; msg.RecID = this.serverOppositeID; this.sendData(msg); } else if (this.AVGetProxyID != null)//如果对方中转服务ID没有获得,则产生事件通知对方自己的ID//如果是接收方则触发获得中转服务ID this.AVGetProxyID(this, this.serverSelfID); } break; case (byte)CSS.IM.Library.Class.ProtocolAVTransmit.BeginTransmit://连接已经建立,对方要求开始接收文件 { this.serverOppositeID = msg.SendID;//获得对方ID if (netClass == CSS.IM.Library.Class.NatClass.Tcp)//如果是TCP通信 { this.netClass = CSS.IM.Library.Class.NetCommunicationClass.TCP;//采用TCP协议传输文件 } else { this.netClass = CSS.IM.Library.Class.NetCommunicationClass.WanNoProxyUDP;//采用UDP协议传输 } if (this.AVConnected != null)//触发通信成功事件,并退出通信测试 this.AVConnected(this, this.netClass); //通信已经建立,开始传输数据 //Calculate.WirteLog("连接已经建立"); } break; case (byte)CSS.IM.Library.Class.ProtocolAVTransmit.HandshakeLAN://收到对方局域网UDP握手数据 { this._OppositeUserInfo.LocalIP = Ip;//重新设置对方的局域网IP this.OppositeUDPPort = Port;//重新设置对方的局域网UDP端口 msg.InfoClass =(byte)CSS.IM.Library.Class.ProtocolAVTransmit.IsOppositeRecSelfLanUDPData;//告诉对方收到握手数据信息 this.sockUDP1.Send(this._OppositeUserInfo.LocalIP, this.OppositeUDPPort, msg.getBytes()); //Calculate.WirteLog(this._IsSend.ToString()+ "收到对方局域网UDP握手数据:"+ Ip.ToString() +":"+ Port.ToString()); } break; case (byte)CSS.IM.Library.Class.ProtocolAVTransmit.IsOppositeRecSelfLanUDPData://对方收到自己发送的局域网UDP握手数据 { this.netClass = CSS.IM.Library.Class.NetCommunicationClass.LanUDP;//标识与对方建立局域网通信成功 msg.InfoClass = (byte)CSS.IM.Library.Class.ProtocolAVTransmit.BeginTransmit; msg.SendID = 0; this.sendData(msg);//告诉对方开始发送文件 //Lan通信已经建立,开始传输数据 } break; case (byte)CSS.IM.Library.Class.ProtocolAVTransmit.HandshakeWAN://收到对方广域网UDP握手数据 { this._OppositeUserInfo.IP = Ip;//重新设置对方的广域网IP this.OppositeUDPPort = Port;//重新设置对方的广域网UDP端口 msg.InfoClass = (byte)CSS.IM.Library.Class.ProtocolAVTransmit.IsOppositeRecSelfWanUDPData;//告诉对方收到握手数据信息 this.sockUDP1.Send(this._OppositeUserInfo.IP, this.OppositeUDPPort, msg.getBytes()); //Calculate.WirteLog(this._IsSend.ToString()+ "收到对方广域网UDP握手数据:"+ Ip.ToString() +":"+ Port.ToString()); } break; case (byte)CSS.IM.Library.Class.ProtocolAVTransmit.IsOppositeRecSelfWanUDPData://对方收到自己发送的局域网UDP握手数据 { this.netClass = CSS.IM.Library.Class.NetCommunicationClass.WanNoProxyUDP;//标识与对方建立广域网直接通信成功 msg.InfoClass = (byte)CSS.IM.Library.Class.ProtocolAVTransmit.BeginTransmit; msg.SendID = 0; this.sendData(msg);//告诉对方开始视频 //Wan通信已经建立,开始传输数据 } break; case (byte)CSS.IM.Library.Class.ProtocolAVTransmit.GetUDPWANInfo://获得服务器返回的文件传输套接字广域网UDP端口 { this.selfUDPPort=msg.SendID;//重新设置对方的广域网UDP端口 if (!this.IsGetWanUDP && this.AVGetUDPPort != null)//如果自己还未与对方套接字建立通信则触发获得端口事件 { this.IsGetWanUDP = true; this.AVGetUDPPort(this, this.selfUDPPort, true); } } break; } }