ReadInt16() public méthode

public ReadInt16 ( ) : short
Résultat short
Exemple #1
0
        public async void Send(byte[] fileBytes)
        {
            try
            {
                if (Speakers != null && Speakers.Count > 0 && fileBytes != null)
                {
                    //iterate through the speakers and send out the media file to each speaker
                    foreach (Speaker speaker in Speakers)
                    {
                        StreamSocket socket = speaker.Socket;

                        if (socket != null)
                        {
                            IOutputStream outStream = socket.OutputStream;
                            using (DataWriter dataWriter = new DataWriter(outStream))
                            {
                                //write header bytes to indicate to the subscriber 
                                //information about the file to be sent
                                dataWriter.WriteInt16((short)MessageType.Media);
                                dataWriter.WriteInt32(fileBytes.Length);
                                await dataWriter.StoreAsync();
                                //start from 0 and increase by packet size
                                int partNumber = 0;
                                int sourceIndex = 0;
                                int bytesToWrite = fileBytes.Length;
                                while (bytesToWrite > 0)
                                {
                                    dataWriter.WriteInt32(partNumber);
                                    int packetSize = bytesToWrite;
                                    if (packetSize > MAX_PACKET_SIZE)
                                    {
                                        packetSize = MAX_PACKET_SIZE;
                                    }
                                    byte[] fragmentedPixels = new byte[packetSize];
                                    Array.Copy(fileBytes, sourceIndex, fragmentedPixels, 0, packetSize);
                                    dataWriter.WriteBytes(fragmentedPixels);
                                    Debug.WriteLine("sent byte packet length " + packetSize);
                                    await dataWriter.StoreAsync();
                                    sourceIndex += packetSize;
                                    bytesToWrite -= packetSize;
                                    partNumber++;
                                    Debug.WriteLine("sent total bytes " + (fileBytes.Length - bytesToWrite));
                                }
                                //Finally DetachStream
                                dataWriter.DetachStream();
                            }
                        }
                    }


                    //check the speakers have all received the file
                    foreach (Speaker speaker in Speakers)
                    {
                        StreamSocket socket = speaker.Socket;
                        if (socket != null)
                        {
                            //wait for the 'I got it' message
                            DataReader reader = new DataReader(socket.InputStream);
                            uint x = await reader.LoadAsync(sizeof(short));
                            MessageType t = (MessageType)reader.ReadInt16();
                            if (MessageType.Ready == t)
                            {
                                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                () =>
                                {
                                    Speakers.Remove(speaker);
                                    speaker.Status = "Ready";
                                    Speakers.Add(speaker);
                                });
                            }
                            reader.DetachStream();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
Exemple #2
0
        public async void ConnectAsync(string host, string name)
        {
            HostName hostName;
            try
            {
                hostName = new HostName(host);
            }
            catch (ArgumentException)
            {
                Debug.WriteLine("Error: Invalid host name {0}.", host);
                return;
            }

            _socket = new StreamSocket();
            _socket.Control.KeepAlive = true;
            _socket.Control.QualityOfService = SocketQualityOfService.LowLatency;

            //hard coded port - can be user-specified but to keep this sample simple it is 21121
            await _socket.ConnectAsync(hostName, "21121");
            Debug.WriteLine("Connected");

            //first message to send is the name of this virtual speaker
            _writer = new DataWriter(_socket.OutputStream);
            _writer.WriteUInt32(_writer.MeasureString(name));
            _writer.WriteString(name);

            try
            {
                await _writer.StoreAsync();
                Debug.WriteLine("{0} registered successfully.", name);
            }
            catch (Exception exception)
            {
                Debug.WriteLine("Send failed with error: " + exception.Message);
                // If this is an unknown status it means that the error if fatal and retry will likely fail.
                if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                {
                    throw;
                }
            }

            //set no current message type
            MessageType currentMessageType = MessageType.Unknown;
            // then wait for the audio to be sent to us 
            DataReader reader = new DataReader(_socket.InputStream);

            while (true)
            {
                uint x = await reader.LoadAsync(sizeof(short));
                short t = reader.ReadInt16();
                currentMessageType = (MessageType)t;

                switch (currentMessageType)
                {
                    case MessageType.Media:
                        await ReadMediaFileAsync(reader);
                        break;
                    case MessageType.Play:
                        {
                            MediaPlayer mediaPlayer = BackgroundMediaPlayer.Current;
                            if (mediaPlayer != null)
                            {
                                if (mediaPlayer.CurrentState != MediaPlayerState.Playing)
                                {
                                    mediaPlayer.SetStreamSource(_playingStream);
                                    mediaPlayer.Play();
                                    Debug.WriteLine("Player playing. TotalDuration = " +
                                        mediaPlayer.NaturalDuration.Minutes + ':' + mediaPlayer.NaturalDuration.Seconds);
                                }
                            }
                        }
                        break;
                    case MessageType.Stop:
                        {
                            MediaPlayer mediaPlayer = BackgroundMediaPlayer.Current;
                            if (mediaPlayer != null)
                            {
                                if (mediaPlayer.CurrentState == MediaPlayerState.Playing)
                                {
                                    mediaPlayer.Pause();
                                    Debug.WriteLine("Player paused");
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }
                currentMessageType = MessageType.Unknown;
            }
        }