本文整理汇总了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());
}
}
示例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;
}
}