public void Rpc_OtherClients() { var receivers = new RpcReceiver[ClientsNum]; var clients = new IguagileClient[ClientsNum]; var poolOpen = new Semaphore(0, ClientsNum); var poolClose = new Semaphore(0, ClientsNum); for (var i = 0; i < ClientsNum; i++) { var client = new IguagileClient(); client.Open += () => poolOpen.Release(1); client.Close += () => poolClose.Release(1); client.OnError += e => Assert.Fail(e.Message); var receiver = new RpcReceiver(client, ClientsNum - 1); client.AddRpc(nameof(RpcReceiver.RpcMethod), receiver); client.Connect(ServerAddress, PortTcp, Protocol.Tcp); clients[i] = client; receivers[i] = receiver; } for (var i = 0; i < ClientsNum; i++) { poolOpen.WaitOne(); } for (var i = 0; i < ClientsNum; i++) { clients[i].Rpc(nameof(RpcReceiver.RpcMethod), RpcTargets.OtherClients, clients[i].UserId); } for (var i = 0; i < ClientsNum; i++) { poolClose.WaitOne(); } }
static void Main(string[] args) { Console.Write("Enter server address: "); var address = Console.ReadLine(); string name; do { Console.Write("Your name: "); name = Console.ReadLine().Trim(); } while (name == ""); var receiver = new RpcMessageReceiver(); client.AddRpc(nameof(receiver.WriteMessage), receiver); client.OnConnected += () => client.Rpc(nameof(receiver.WriteMessage), RpcTargets.AllClients, $"* {name} joined."); client.OnError += Console.WriteLine; var players = new Dictionary <int, AudioPlayer>(); client.OnBinaryReceived += (id, data) => { if (!players.ContainsKey(id)) { var player = new AudioPlayer(waveFormat); player.AddSamples(data); player.Play(); players[id] = player; } else { players[id].AddSamples(data); } }; client.StartAsync(address, port, Protocol.Tcp); var waveIn = new WaveInEvent { WaveFormat = waveFormat, BufferMilliseconds = 100 }; waveIn.DataAvailable += (s, e) => { var sum = 0.0; for (int i = 0; i < e.BytesRecorded; i += waveIn.WaveFormat.BlockAlign) { short sample = (short)((e.Buffer[i + 1] << 8) | e.Buffer[i + 0]); float normalized = sample / 32768f; var square = normalized * normalized; sum += square; } var average = sum / e.BytesRecorded; const double threshold = 25e-6; if (average > threshold) { client.SendBinaryAsync(e.Buffer, RpcTargets.OtherClients); } }; waveIn.StartRecording(); while (true) { var message = Console.ReadLine().Trim(); if (!client.IsConnected) { break; } client.Rpc(nameof(receiver.WriteMessage), RpcTargets.AllClients, $"{name}: {message}"); } foreach (var(_, player) in players) { player.Dispose(); } receiver.WriteMessage("Connection closed. Press enter to exit."); Console.ReadLine(); }