Game Development Reference
In-Depth Information
Here we perform similar steps to the StreamSocket , by creating the socket and
telling it where to connect to. The following line will create a DataWriter that can
be used to send the message:
auto writer = ref new
DataWriter(socket->OutputStream);
Reading and writing data
So we have a socket and we want to communicate using it. For this, we have two
classes that make it easier for us to construct the byte stream that will be sent to the
other machine: the DataReader and DataWriter classes. By using these classes
you also don't need to worry about byte order in the data you send, or how the data
type is formatted.
Side note - Async
Before we look at those two classes, though, we need to take a very quick look at
the Async system in WinRT. Every API call that doesn't always return immediately
(and has Async in the name) will run asynchronously. This is all managed by Win-
dows, but you need to be able to handle it. A call to the ConnectAsync() method
won't return a socket because it immediately returns, allowing you to continue work-
ing while the connection is established in the background. For this reason we need
to use the continuation system provided through the task parallel library in Windows.
With this, we can provide code that will continue after the task has finished, so we
can easily write the code that should run later, and tack it onto the end; the scheduler
will take care of the rest.
This is a great place to use the new lambda feature introduced in C++11. This fea-
ture, similar to lambdas or anonymous delegates in C#, allows you to write a function
within a function, and treat it as an object that can be passed as a parameter. This
lets us avoid creating single line functions for these continuations and cluttering up
our class declaration.
Let's take a look at the PeerFinder::ConnectAsync() method as an example:
Search WWH ::




Custom Search