Game Development Reference
In-Depth Information
ate the DataWriter using an IOutputStream taken from one of your sockets, as
shown:
auto writer = ref new
DataWriter(socket->OutputStream);
Once you have the writer you can then use the appropriate Write command to con-
vert your data to bytes and save it to an internal buffer.
Note
At this point, your data has not been transmitted yet. We need to call another
asynchronous method later on to do this.
Sending the data in a batched block of bytes is better than sending out a couple of
bytes at a time, so by using the DataWriter you're also optimizing your throughput
by allowing it to handle batching for you. This is done as follows:
writer->WriteInt32(10); // int
writer->WriteSingle(5.4f); // float
writer->WriteByte('a'); // unsigned char
These are some examples of the available Write methods that you can use to send
your data. Once you're done writing out your message, you need to just make one
more method call to finish up, as shown in the following piece of code:
writer->StoreAsync();
This asynchronous method will take the buffer that you've built up and write it to the
stream, which in turn will send the data out on the socket to the other endpoint.
At this point the data has been sent and the other side will receive it at some point.
Remember that these are asynchronous methods, so you should be careful to not
try and write out more data until the StoreAsync() method is complete. If you're
sending data in each frame, then you might want to only call the StoreAsync()
Search WWH ::




Custom Search