private void ConnectAudio() { audioClient.Connect(RoomVM.ServiceIp, ApplicationVM.LocalCache.RoomAudioServicePort); UdpPacket packet = new UdpPacket(Me.Id, RoomVM.Id, UdpPacketType.Login); var loginBytes = packet.ToBytes(); try { audioRWLock.EnterWriteLock(); while (true) { try { audioClient.Send(loginBytes, loginBytes.Length); IPEndPoint serverEndPoint = null; var bytes = audioClient.Receive(ref serverEndPoint); UdpPacket p = UdpPacket.FromBytes(bytes); if (p.PacketType == UdpPacketType.LoginSucceed) { break; } } catch { continue; } } } catch { } finally { audioRWLock.ExitWriteLock(); } AsyncBeginAudioReceive(); }
private void DisconnectAudio() { UdpPacket packet = new UdpPacket(Me.Id, RoomVM.Id, UdpPacketType.Logoff); var logoffBytes = packet.ToBytes(); if (audioClient != null) { if (audioClient.Client.Connected) { try { audioRWLock.EnterWriteLock(); while (true) { try { if (audioClient != null) { audioClient.Send(logoffBytes, logoffBytes.Length); IPEndPoint serverEndPoint = null; var bytes = audioClient.Receive(ref serverEndPoint); UdpPacket p = UdpPacket.FromBytes(bytes); if (p.PacketType == UdpPacketType.LogoffSuccedd) { break; } } else { break; } } catch { continue; } } audioClient.Close(); } catch { } finally { audioClient = null; audioRWLock.ExitWriteLock(); } } } }
protected override void PacketReceived(UdpPacketBuffer buffer) { EndPoint client = buffer.RemoteEndPoint; byte[] receivedData = buffer.Data.Take(buffer.DataLength).ToArray(); UdpPacket packet = UdpPacket.FromBytes(receivedData); switch (packet.PacketType) { case UdpPacketType.Login: Console.WriteLine("User Login -- " + packet.UserId); Login(packet, buffer.RemoteEndPoint); packet.PacketType = UdpPacketType.LoginSucceed; var bytes = packet.ToBytes(); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); Console.WriteLine("User Login Succeed-- " + packet.UserId); break; case UdpPacketType.Data: PerformBroadcasting(packet, receivedData); break; case UdpPacketType.Logoff: Console.WriteLine("User Logoff -- " + packet.UserId); Logoff(packet); packet.PacketType = UdpPacketType.LogoffSuccedd; bytes = packet.ToBytes(); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); udpSocket.Send(bytes, bytes.Length, buffer.RemoteEndPoint); Console.WriteLine("User Logoff Succeed -- " + packet.UserId); break; default: break; } }
private void AsyncEndAudioReceive(IAsyncResult ar) { if (audioClient != null) { audioRWLock.EnterReadLock(); if (audioClient != null) { try { IPEndPoint remoteEP = new IPEndPoint(System.Net.IPAddress.Any, 0); byte[] receivedData = audioClient.EndReceive(ar, ref remoteEP); if (((Me.MicStatus & MicStatusMessage.MicStatus_Audio) == MicStatusMessage.MicStatus_Off) || !ApplicationVM.ProfileVM.AudioConfigurationVM.LoopbackRecording) { UdpPacket packet = UdpPacket.FromBytes(receivedData); if (packet.PacketType == UdpPacketType.Data) { if (!soundPlayers.ContainsKey(packet.UserId) && playAll) { StartAudioPlaying(packet.UserId); } if (soundPlayers.ContainsKey(packet.UserId)) { foreach (var d in packet.Data) { soundPlayers[packet.UserId].AddSample(d, 0, d.Length); } } } } } catch { } } audioRWLock.ExitReadLock(); AsyncBeginAudioReceive(); } }