当前位置: 首页>>代码示例>>C#>>正文


C# DataReader.ReadInt16方法代码示例

本文整理汇总了C#中Windows.Storage.Streams.DataReader.ReadInt16方法的典型用法代码示例。如果您正苦于以下问题:C# DataReader.ReadInt16方法的具体用法?C# DataReader.ReadInt16怎么用?C# DataReader.ReadInt16使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Windows.Storage.Streams.DataReader的用法示例。


在下文中一共展示了DataReader.ReadInt16方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Send

        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());
            }
        }
开发者ID:CHS982,项目名称:Coding4Fun,代码行数:80,代码来源:Publisher.cs

示例2: ConnectAsync

        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;
            }
        }
开发者ID:CHS982,项目名称:Coding4Fun,代码行数:91,代码来源:Subscriber.cs


注:本文中的Windows.Storage.Streams.DataReader.ReadInt16方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。