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(); }
void SampleAvailableEventHandler(List <byte[]> datas) { if (datas != null && datas.Count > 0) { UdpPacket packet = new UdpPacket(Me.Id, RoomVM.Id, UdpPacketType.Data); foreach (var d in datas) { packet.Append(d); } AsyncBeginAudioSend(packet.ToBytes()); } }
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 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(); } } } }