Game Development Reference
In-Depth Information
auto op = PeerFinder::ConnectAsync(peer);
concurrency::task<StreamSocket^>
connectTask(op);
connectTask.then([this](concurrency::task<StreamSocket^>
resultTask)
{
auto socket = resultTask.get();
// Use socket to communicate
}
In here the ConnectAsync method returns a token, which we can use to check
if the task is done at any point in time. However, a nicer way to handle this is to
schedule some code to run right after the connection is established (or fails). To
do that, we need to use the concurrency::task class, which will let us call the
task.then() method. As you can see in the sample earlier, the then() method
takes a function pointer, which we define as a lambda in this case. However, if you
prefer to define the function separately, you can pass a reference to the function us-
ing legacy C-style function pointers.
The function signature needs to take the previous task as a parameter, which we
can use to get the returned value using the task.get() method. If any exceptions
have occurred asynchronously, this is where they will be rethrown so you can handle
them. An example of this might be a failure during the socket connection. An ex-
ception is thrown but because the code is running asynchronously and you haven't
written the code that is running, you can't catch the exception. The operating system
will aggregate any exceptions and throw them when you call the get() method, al-
lowing you to handle them then.
The DataReader
So we have a DataReader and we're ready to start retrieving messages or inform-
ation from the stream. DataReader allows us to get the information we need while
handling the type conversion for us by allowing us to call a method corresponding
to the data type. For example, if you want to read a 32-bit signed integer from the
stream, you can call the reader->ReadInt32() method and get an int back.
Search WWH ::




Custom Search