Exemplo n.º 1
0
        private void OnReceived(IAsyncResult iAsyncResult)
        {
            try
            {
                VoiceClient client    = iAsyncResult.AsyncState as VoiceClient;
                byte[]      sendBytes = client.GetData(iAsyncResult);

                if (sendBytes.Length == 0)
                {
                    client.ReadOnly.Close();
                    Clients.Remove(client);
                }
                else
                {
                    //foreach (VoiceClient clientSend in Clients)
                    //{
                    //    if (client != clientSend)
                    //    {
                    //        try
                    //        {
                    //            clientSend.ReadOnly.Send(sendBytes);
                    //        }
                    //        catch (Exception e)
                    //        {
                    //            clientSend.ReadOnly.Close();
                    //            Clients.Remove(client);
                    //            return;
                    //        }
                    //    }
                    //}
                    //client.CallBackReceive();

                    // PARALLEL ForEAch experiment:

                    Parallel.ForEach(Clients, (clientToSend, state) =>
                    {
                        if (client != clientToSend)
                        {
                            try
                            {
                                clientToSend.ReadOnly.Send(sendBytes);
                            }
                            catch (Exception e)
                            {
                                clientToSend.ReadOnly.Close();
                                Clients.Remove(client);
                                state.Break();
                                return;
                            }
                        }
                    });
                    client.CallBackReceive();
                }
            }
            catch (ObjectDisposedException e)
            {
                return;
            }
        }
Exemplo n.º 2
0
 private void AcceptClient(Socket clientSocket)
 {
     try
     {
         NewVoiceClient = new VoiceClient(clientSocket, this);
         Clients.Add(NewVoiceClient);
         NewVoiceClient.CallBackReceive();
     }
     catch (ObjectDisposedException e)
     {
         return;
     }
 }