static void CreateTrans(string[] args) { IsRuning = true; IPAddress ip = IPAddress.Any; int videoPort = 3000; int audioPort = videoPort + 2; if (args.Length > 1) { ip = IPAddress.Parse(args[1]); } if (args.Length > 2) { videoPort = int.Parse(args[2]); } if (args.Length > 2) { videoPort = int.Parse(args[2]); } if (args.Length > 3) { audioPort = int.Parse(args[3]); } Socket videoSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); videoSocket.Bind(new IPEndPoint(ip, videoPort)); Socket audioSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); audioSocket.Bind(new IPEndPoint(ip, audioPort)); VideoObj = new SSObj(videoSocket); VideoObj.type = "Video"; AudioObj = new SSObj(audioSocket); AudioObj.type = "Audio"; IPAddress ip1 = null; int port1 = 0; for (int i = 0; i < args.Length; i++) { if (args[i].StartsWith("ep=")) { string[] eps = args[i].Substring(3).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); foreach (var ep in eps) { IPAddress.TryParse(ep.Substring(0, ep.IndexOf(":")), out ip1); int.TryParse(ep.Substring(ep.IndexOf(":") + 1), out port1); VideoObj.DistEPList.Add(new IPEndPoint(ip1, port1)); AudioObj.DistEPList.Add(new IPEndPoint(ip1, port1 + 2)); } } } VideoThread = new Thread(new ParameterizedThreadStart(StartTran)); VideoThread.Start(VideoObj); AudioThread = new Thread(new ParameterizedThreadStart(StartTran)); AudioThread.Start(AudioObj); Console.WriteLine("CreateTrans: "); Console.WriteLine("Video: " + ip.ToString() + ":" + videoPort.ToString()); Console.WriteLine("Audio: " + ip.ToString() + ":" + audioPort.ToString()); Console.WriteLine(""); for (int i = 0; i < VideoObj.DistEPList.Count; i++) { Console.WriteLine(String.Format("{0} 第{1}个分发客户端 {2}:{3}", "video", i + 1, VideoObj.DistEPList[i].Address, VideoObj.DistEPList[i].Port)); Console.WriteLine(String.Format("{0} 第{1}个分发客户端 {2}:{3}", "audio", i + 1, AudioObj.DistEPList[i].Address, AudioObj.DistEPList[i].Port)); } }
static void StartTran(Object obj) { SSObj ssObj = (SSObj)obj; IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint remote = (EndPoint)sender; Queue <byte[]> epTmpQueue = new Queue <byte[]>(100 * 1024); // 避免第一个socket发了一堆数据,第二个socket还没上来早造成丢包 byte[] buffer = new byte[512 * 1024]; while (IsRuning) { try { if (ssObj.Socket.Available == 0) { Thread.Sleep(20); continue; } int size = ssObj.Socket.ReceiveFrom(buffer, ref remote); IPEndPoint remoteEP = (IPEndPoint)remote; if (ssObj.EP1 == null) // 第一个socket第一次发数据 { ssObj.EP1 = remoteEP; Console.WriteLine(String.Format("{0} 第1个转发客户端 {1}:{2}", ssObj.type, remoteEP.Address, remoteEP.Port)); } if (ssObj.EP2 == null && SameEP(ssObj.EP1, remoteEP)) // 第二个socket还没发过数据 { byte[] tmpBuffer = new byte[size]; Array.Copy(buffer, tmpBuffer, size); epTmpQueue.Enqueue(tmpBuffer); // 中转缓存 DistData(ssObj.Socket, ssObj.EP1, remoteEP, ssObj.DistEPList, buffer, size); // 分发 continue; } if (ssObj.EP1 != null && ssObj.EP2 == null) // 第二个socket第一次发 { ssObj.EP2 = remoteEP; Console.WriteLine(String.Format("{0} 第2个转发客户端 {1}:{2}", ssObj.type, remoteEP.Address, remoteEP.Port)); while (true) // 发送中转缓存 { if (epTmpQueue.Count == 0) { break; } else { byte[] tmpBuffer = epTmpQueue.Dequeue(); ssObj.EP1Count += tmpBuffer.Length; ssObj.Socket.SendTo(tmpBuffer, 0, tmpBuffer.Length, SocketFlags.None, ssObj.EP2); Thread.Sleep(10); } } } EndPoint targetEP = null; if (SameEP(ssObj.EP1, remoteEP)) { targetEP = ssObj.EP2; ssObj.EP1Count += size; } else if (SameEP(ssObj.EP2, remoteEP)) { targetEP = ssObj.EP1; ssObj.EP2Count += size; } if (targetEP != null) { ssObj.Socket.SendTo(buffer, 0, size, SocketFlags.None, targetEP); } if (SameEP(ssObj.EP1, remoteEP)) { for (int i = 0; i < VideoObj.DistEPList.Count; i++) { ssObj.Socket.SendTo(buffer, 0, size, SocketFlags.None, ssObj.DistEPList[i]); } } DistData(ssObj.Socket, ssObj.EP1, remoteEP, ssObj.DistEPList, buffer, size); // 分发 } catch (Exception ex) { Console.WriteLine(ssObj.type + " StartTran Error:" + ex.Message); Restart(); break; } } }