Example #1
0
        private void ServerReceivedData(byte[] data, IPEndPoint sender)
        {
            // GET CLIENT
            Client c = GetClient(sender);

            if (Encoding.ASCII.GetString(data).Substring(0, 3) == "HLO")             // HELLO
            {
                // CLIENT CONNECTED
                string client_guid = Encoding.ASCII.GetString(data).Substring(3);

                Logger.WriteLine("SERVER RECEIVED: " + Encoding.ASCII.GetString(data));
                udpServer.Reply(Encoding.ASCII.GetBytes("HELLO CLIENT!"), sender);

                lock (clientsLock)
                {
                    if (clients.ContainsKey(client_guid))
                    {
                        clients[client_guid].SetConnected(true);
                        clients[client_guid].endpoint = sender;

                        // RESEND ALL CLIENT LOOP LIGHTS STATUSES
                        InitializeClientLoopButtons(client_guid);

                        // RESEND ALL LOOP NAMES
                        SendAllLoopNames(sender);
                    }
                    else
                    {
                        clients.Add(client_guid, new Client(sender, serverSampleRate, serverChannels, client_guid));

                        // SEND ALL CLIENT LOOP LIGHTS STATUSES (Just in case)
                        InitializeClientLoopButtons(client_guid);

                        // SEND ALL LOOP NAMES
                        SendAllLoopNames(sender);
                    }
                    // SET LAST PACKET TIME
                    clients[client_guid].lastPacket = DateTime.Now;
                }
            }
            else if (c != null)
            {
                // SET LAST PACKET TIME
                clients[c.guid].lastPacket = DateTime.Now;

                if (!c.IsConnected())
                {
                    // CLIENT WOKE TO LIFE AGAIN
                    c.SetConnected(true);
                    clients[c.guid].endpoint = sender;
                    InitializeClientLoopButtons(c.guid);
                }

                if (Encoding.ASCII.GetString(data) == "BYE SERVER!")
                {
                    // CLIENT CONNECTED
                    Logger.WriteLine("SERVER RECEIVED: " + Encoding.ASCII.GetString(data));

                    lock (clientsLock)
                    {
                        // MARK AS DISCONNECTED
                        clients[c.guid].SetConnected(false);

                        // TURN OFF ALL CLIENTS LOOP LIGHTS
                        for (int i = 0; i < numLoops; i++)
                        {
                            udpServer.Reply(CMD.GetCmdBytes(CMD.CMDS.TurnOffLoopListenLight, (byte)i), sender);
                            udpServer.Reply(CMD.GetCmdBytes(CMD.CMDS.TurnOffLoopTalkLight, (byte)i), sender);
                            udpServer.Reply(CMD.GetCmdBytes(CMD.CMDS.TurnOffLoopTalkFlash, (byte)i), sender);
                        }
                    }

                    udpServer.Reply(Encoding.ASCII.GetBytes("BYE CLIENT!"), sender);
                }
                else if (data.Length == 3)
                {
                    // RECEIVED COMMAND
                    Logger.WriteLine("SERVER RECEIVED COMMAND: " + data[0] + " " + data[1] + " " + data[2]);
                    CMD.ExecuteCmd(data, c.guid);
                }
                else
                {
                    // ASSUME SAMPLE RECEIVED
                    lock (clientsLock)
                    {
                        processIncomingAudioData(data, clients[c.guid]);
                    }
                }
            }
        }
Example #2
0
        static public void RunCommand(CMDS command, byte argument, string sender)
        {
            switch (command)
            {
            case CMDS.StartListeningToLoop:
                if (!clients[sender].GetSources().Contains((int)argument))
                {
                    clients[sender].AddSource((int)argument);
                    loops[(int)argument].addListenerToLoop(sender);
                    UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOnLoopListenLight, argument), clients[sender].endpoint);
                }
                break;

            case CMDS.StopListeningToLoop:
                if (clients[sender].GetSources().Contains((int)argument))
                {
                    clients[sender].RemoveSource((int)argument);
                    loops[(int)argument].removeListenerFromLoop(sender);
                    UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOffLoopListenLight, argument), clients[sender].endpoint);
                }
                break;

            case CMDS.StartTalkingToLoop:
                // REMOVE TALKER FROM ALL LOOPS
                for (int i = 0; i < loops.Count; i++)
                {
                    if (loops[i].talkers.Contains(sender))
                    {
                        loops[i].removeTalkerFromLoop(sender);
                        clients[sender].destinations.Remove(i);
                        UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOffLoopTalkFlash, (byte)i), clients[sender].endpoint);
                    }
                }

                if (!clients[sender].destinations.Contains((int)argument))
                {
                    clients[sender].destinations.Add((int)argument);
                    loops[(int)argument].addTalkerToLoop(sender);
                    UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOnLoopTalkFlash, argument), clients[sender].endpoint);

                    // ACTIVATE ACTIVE-LIGHT FOR ALL USERS
                    foreach (KeyValuePair <string, Client> kvp in clients)
                    {
                        Client c = kvp.Value;

                        if (c.IsConnected() && kvp.Key != sender)
                        {
                            UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOnLoopTalkLight, argument), c.endpoint);
                        }
                    }
                }

                // Check if loop is left without talkers
                for (int i = 0; i < loops.Count; i++)
                {
                    if (loops[i].talkers.Count == 0)
                    {
                        // No more talkers in this loop, turn of active talker light for all users
                        foreach (KeyValuePair <string, Client> kvp in clients)
                        {
                            Client c = kvp.Value;

                            if (c.IsConnected())
                            {
                                UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOffLoopTalkLight, (byte)i), c.endpoint);
                            }
                        }
                    }
                }
                break;

            case CMDS.StopTalkingToLoop:
                if (clients[sender].destinations.Contains((int)argument))
                {
                    clients[sender].destinations.Remove((int)argument);
                    loops[(int)argument].removeTalkerFromLoop(sender);
                    UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOffLoopTalkFlash, argument), clients[sender].endpoint);

                    // IF LOOP IS EMPTY BLANK TALK BUTTONS
                    if (loops[(int)argument].talkers.Count == 0)
                    {
                        foreach (KeyValuePair <string, Client> kvp in clients)
                        {
                            Client c = kvp.Value;

                            if (c.IsConnected())
                            {
                                UdpServer.Reply(CMD.GetCmdBytes(CMDS.TurnOffLoopTalkLight, argument), c.endpoint);
                            }
                        }
                    }
                }
                break;

            case CMDS.TurnOnLoopListenLight:
                if (lButtons.ContainsKey((int)argument))
                {
                    lButtons[(int)argument].Invoke(new Action(() => { lButtons[(int)argument].setLitState(true); }));
                }
                break;

            case CMDS.TurnOffLoopListenLight:
                if (lButtons.ContainsKey((int)argument))
                {
                    lButtons[(int)argument].Invoke(new Action(() => { lButtons[(int)argument].setLitState(false); }));
                }
                break;

            case CMDS.TurnOnLoopTalkLight:
                if (tButtons.ContainsKey((int)argument))
                {
                    tButtons[(int)argument].Invoke(new Action(() => { tButtons[(int)argument].setLitState(true); }));
                }
                break;

            case CMDS.TurnOffLoopTalkLight:
                if (tButtons.ContainsKey((int)argument))
                {
                    tButtons[(int)argument].Invoke(new Action(() => { tButtons[(int)argument].setLitState(false); }));
                }
                break;

            case CMDS.SetClientAudioFormat:
                clients[sender].audioFormat = (int)argument;
                clients[sender].decoder.SetFormat((int)argument);
                clients[sender].encoder.SetFormat((int)argument);
                break;


            case CMDS.Ping:
                UdpServer.Reply(CMD.GetCmdBytes(CMDS.Pong, (byte)0), clients[sender].endpoint);
                break;
            }
        }