Example #1
0
        /// <summary>
        /// Sends command to socket server.
        /// </summary>
        /// <param name="command"></param>
        /// <param name="connectionTimeout"></param>
        /// <returns></returns>
        public async Task <SocketResponse> SendCommand(SocketCommand command, int connectionTimeout = 2000)
        {
            // Wait while previous command is proccessed
            await _semaphoreSlim.WaitAsync();

            try
            {
                // Connect to socket if not connected
                if (socket == null)
                {
                    await Connect(connectionTimeout);
                }

                // Serialize command to json and send
                string request = JsonHelper.ToJson <SocketCommand>(command);
                // Write first the length of the string as UINT32 value followed up by the string.
                // Writing data to the writer will just store data in memory.
                writer.WriteUInt32(writer.MeasureString(request));
                writer.WriteString(request);
                // Write the locally buffered data to the network.
                await writer.StoreAsync();

                // Read first 4 bytes (length of the subsequent string).
                uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));

                if (sizeFieldCount != sizeof(uint))
                {
                    // The underlying socket was closed before we were able to read the whole data.
                    return(new SocketResponse(SocketResponse.StatusCode.EXCEPTION, "The underlying socket was closed before we were able to read the whole data."));
                }
                // Read the string.
                uint stringLength       = reader.ReadUInt32();
                uint actualStringLength = await reader.LoadAsync(stringLength);

                if (stringLength != actualStringLength)
                {
                    // The underlying socket was closed before we were able to read the whole data.
                    return(new SocketResponse(SocketResponse.StatusCode.EXCEPTION, "The underlying socket was closed before we were able to read the whole data."));
                }
                string responseString = reader.ReadString(actualStringLength);
                return(JsonHelper.FromJson <SocketResponse>(responseString));
            }
            catch (Exception ex)
            {
                Disconnect();
                throw ex;
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
        /// <summary>
        /// Custom void for recognizing and processing command.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private SocketResponse ProcessRequest(string request)
        {
            SocketCommand command = JsonHelper.FromJson <SocketCommand>(request);

            Debug.WriteLine("Recieved command: " + command.Type.ToString());

            switch (command.Type)
            {
                #region BackgroundAudioManipulation

            case SocketCommand.CommandType.PLAY:
                Radio currentRadio = internetRadioHelper.GetRadio(command.Parameters[0].ToString());
                internetRadioHelper.CurrentRadio = currentRadio;
                return(BackgroundAudioHelper.PlayRadio(currentRadio));

            case SocketCommand.CommandType.STOP:
                return(BackgroundAudioHelper.Stop());

            case SocketCommand.CommandType.SET_VOLUME:
                return(BackgroundAudioHelper.SetVolume(Double.Parse(command.Parameters[0].ToString())));

            case SocketCommand.CommandType.GET_VOLUME:
                return(BackgroundAudioHelper.GetVolume());

                #endregion

                #region InternetRadioManipulation

            case SocketCommand.CommandType.GET_STATUS:
                return(InternetRadioStatusHelper.GetStatus(internetRadioHelper.CurrentRadio));

            case SocketCommand.CommandType.GET_RADIOS:
                return(internetRadioHelper.GetRadios());

            case SocketCommand.CommandType.ADD_RADIO:
                return(internetRadioHelper.AddRadio(JsonHelper.FromJson <Radio>(command.Parameters[0].ToString())));

            case SocketCommand.CommandType.DELETE_RADIO:
                return(internetRadioHelper.DeleteRadio(command.Parameters[0].ToString()));

                #endregion

            default:
                return(new SocketResponse(SocketResponse.StatusCode.BAD_REQUEST));
            }
        }