/// <summary> /// 实例化 /// </summary> /// <param name="endPoint">此处endPoint是服务器的,用来打洞</param> /// <param name="af"></param> public AudioUtil(MyEndPoint endPoint, AudioForm af) { this.af = af; socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); this.endPoint = new IPEndPoint(IPAddress.Parse(endPoint.Address), endPoint.Port); }
public IAsyncResult BeginSendTCPEndPoint(MyEndPoint endPoint, string guid, AsyncCallback callback, object asyncState) { throw new NotImplementedException(); }
public IAsyncResult BeginSendUDPEndPoint(MyEndPoint endPoint, Member member, bool isFrom, AsyncCallback callback, object asyncState) { throw new NotImplementedException(); }
/// <summary> /// 服务器发来对方endpoint,用来发文件 /// </summary> /// <param name="endPoint"></param> /// <param name="guid"></param> public void SendTCPEndPoint(MyEndPoint endPoint, string guid) { if (DataUtil.HasTransfer()) { if (DataUtil.Transfer.transferTask.ContainsKey(guid)) { DataUtil.Transfer.transferTask[guid].BeginTCPHolePunching(endPoint); } } }
/// <summary> /// 服务器发来对方endpoint,用来语音 /// </summary> /// <param name="endPoint"></param> /// <param name="member"></param> /// <param name="isFrom"></param> public void SendUDPEndPoint(MyEndPoint endPoint, Member member, bool isFrom) { DataUtil.AudioForms[member.id].Start(endPoint); }
/// <summary> /// 该接收的接收,该发送的发送 /// </summary> /// <param name="endPoint"></param> public abstract void Transfer(MyEndPoint endPoint);
/// <summary> /// 开始语音 /// </summary> /// <param name="endPoint">语音对象endPoint</param> public void Start(MyEndPoint endPoint) { this.endPoint = new IPEndPoint(IPAddress.Parse(endPoint.Address), endPoint.Port); sm = new SoundManager(this); sm.StartRecordAndSend(); Thread t = new Thread(() => { ///当Stop时,isAlive设置为false while (isAlive) { try { byte[] data = new byte[500]; EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); int n = socket.ReceiveFrom(data, ref remoteEP); byte[] cmd = new byte[1]; cmd[0] = data[0]; string s = Encoding.UTF8.GetString(cmd); switch (s) { ///语音数据 case "D": { byte[] buffer = new byte[n - 1]; Array.Copy(data, 1, buffer, 0, n - 1); sm.Play(buffer); break; } ///停止命令 case "S": { Stop(); if (af != null) { af.Dispatcher.Invoke(new Action(() => { af.Close(); })); } break; } } } catch (Exception ex) { MyLogger.Logger.Info("接收不到数据,对方可能关闭", ex); if (af != null) { af.Dispatcher.Invoke(new Action(() => { af.Close(); })); } break; } } byte[] stopCmd = Encoding.UTF8.GetBytes("S"); this.Send(stopCmd); }); t.IsBackground = true; t.Start(); }
/// <summary> /// 开始打洞 /// </summary> /// <param name="endPoint"></param> public void BeginTCPHolePunching(MyEndPoint endPoint) { transferFileUtil.Transfer(endPoint); }
/// <summary> /// 初始化socket,并开始发送 /// </summary> /// <param name="endpoint"></param> public void BeginSendFile(MyEndPoint endpoint) { fm.EndPoint = endpoint; transferFileUtil = new SendFileUtil(fm, this); }
public override void Transfer(MyEndPoint endPoint) { this.remoteEP = new IPEndPoint(IPAddress.Parse(endPoint.Address), endPoint.Port); Receive(); }
public FromAudioUtil(MyEndPoint iPEndPoint,AudioForm af=null) : base(iPEndPoint,af) { }
public ToAudioUtil(MyEndPoint iPEndPoint,AudioForm af) : base(iPEndPoint,af) { }
/// <summary> /// 向服务器发送'1',以便udp打洞 /// </summary> /// <param name="myEndPoint"></param> internal void InitSend(MyEndPoint myEndPoint) { fau = new FromAudioUtil(myEndPoint,this); fau.InitWithServerHelp(); }
internal void Start(MyEndPoint endPoint) { isStart = true; timer.Start(); if (isFrom) { fau.Start(endPoint); } else { tau.Start(endPoint); } }