Esempio n. 1
0
 private void Login(UdpPacket packet, IPEndPoint clientEP)
 {
     if (!clientCache.ContainsKey(packet.PId))
     {
         clientCache.Add(packet.PId, new SafeDictionary<int, IPEndPoint>());
     }
     clientCache[packet.PId][packet.UserId] = clientEP;
 }
Esempio n. 2
0
        private void Logoff(UdpPacket packet)
        {
            if (clientCache.ContainsKey(packet.PId))
            {
				if (clientCache[packet.PId].ContainsKey(packet.UserId))
				{
					clientCache[packet.PId].Remove(packet.UserId);
				}
            }
        }
 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());
     }
 }
Esempio n. 4
0
 private void PerformBroadcasting(UdpPacket packet, byte[] packageData)
 {
     if (clientCache.ContainsKey(packet.UserId))
     {
         var receivers = clientCache[packet.PId];
         if (receivers.Count > 0)
         {
             Parallel.ForEach(receivers, receiver =>
             {
                 if (receiver.Key != packet.UserId)
                 {
                     AsyncBeginSend(new UdpPacketBuffer(packageData, receiver.Value));
                 }
             });
         }
     }
 }
Esempio n. 5
0
 public static UdpPacket FromBytes(byte[] data)
 {
     UdpPacket packet = new UdpPacket();
     packet.PacketType = (UdpPacketType)Enum.ToObject(typeof(UdpPacketType), data[0]);
     packet.UserId = BitConverter.ToInt32(data, 1);
     packet.PId = BitConverter.ToInt32(data, 5);
     if (data.Length > 9)
     {
         int position = 9;
         while (position < data.Length)
         {
             int length = BitConverter.ToInt32(data, position);
             position += 4;
             byte[] d = new byte[length];
             Array.Copy(data, position, d, 0, length);
             position += length;
             packet.Append(d);
         }
     }
     return packet;
 }
 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;
                            }
                        }
                        if(audioClient != null)
                            audioClient.Close();
                    }
                    catch { }
                    finally
                    {
                        audioClient = null;
                        audioRWLock.ExitWriteLock();
                    }
                }
                
            }
        }