diff options
author | Jose <jose@zeroc.com> | 2017-01-10 14:34:05 +0100 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2017-01-10 14:34:05 +0100 |
commit | 82f314a870b145e81553987100067558498a1be0 (patch) | |
tree | 2b94ca3464ee643c53237021bc7035394fecb4fd /cpp/src/Ice/Network.cpp | |
parent | Fix for static initialization issue with VS 2013 (diff) | |
download | ice-82f314a870b145e81553987100067558498a1be0.tar.bz2 ice-82f314a870b145e81553987100067558498a1be0.tar.xz ice-82f314a870b145e81553987100067558498a1be0.zip |
UWP code simplifications
Diffstat (limited to 'cpp/src/Ice/Network.cpp')
-rwxr-xr-x | cpp/src/Ice/Network.cpp | 123 |
1 files changed, 67 insertions, 56 deletions
diff --git a/cpp/src/Ice/Network.cpp b/cpp/src/Ice/Network.cpp index e53532c48d9..95e9a49249d 100755 --- a/cpp/src/Ice/Network.cpp +++ b/cpp/src/Ice/Network.cpp @@ -32,7 +32,6 @@ #if defined(ICE_OS_UWP) # include <IceUtil/InputUtil.h> -# include <IceUtil/CountDownLatch.h> #elif defined(_WIN32) # include <winsock2.h> # include <ws2tcpip.h> @@ -2057,46 +2056,24 @@ IceInternal::setMcastGroup(SOCKET fd, const Address& group, const string&) // message. We send a valiate connection message that the peers will ignore to workaround // the issue. // - promise<void> p; - create_task(safe_cast<DatagramSocket^>(fd)->GetOutputStreamAsync(group.host, group.port)) + auto out = IceInternal::runSync(safe_cast<DatagramSocket^>(fd)->GetOutputStreamAsync(group.host, group.port)); + auto writer = ref new DataWriter(out); - .then([](IOutputStream^ out) - { + OutputStream os; + os.write(magic[0]); + os.write(magic[1]); + os.write(magic[2]); + os.write(magic[3]); + os.write(currentProtocol); + os.write(currentProtocolEncoding); + os.write(validateConnectionMsg); + os.write(static_cast<Byte>(0)); // Compression status (always zero for validate connection). + os.write(headerSize); // Message size. + os.i = os.b.begin(); - OutputStream os; - os.write(magic[0]); - os.write(magic[1]); - os.write(magic[2]); - os.write(magic[3]); - os.write(currentProtocol); - os.write(currentProtocolEncoding); - os.write(validateConnectionMsg); - os.write(static_cast<Byte>(0)); // Compression status (always zero for validate connection). - os.write(headerSize); // Message size. - os.i = os.b.begin(); - - auto writer = ref new DataWriter(out); - writer->WriteBytes(ref new Array<unsigned char>(&*os.i, static_cast<unsigned int>(headerSize))); - - return writer->StoreAsync(); - }, - task_continuation_context::use_arbitrary()) - - .then([&p](task<unsigned int> t) - { - try - { - t.get(); - p.set_value(); - } - catch(...) - { - p.set_exception(current_exception()); - } - }, - task_continuation_context::use_arbitrary()); + writer->WriteBytes(ref new Array<unsigned char>(&*os.i, static_cast<unsigned int>(headerSize))); - p.get_future().get(); + IceInternal::runSync(writer->StoreAsync()); } catch(Platform::Exception^ pex) { @@ -2191,28 +2168,34 @@ namespace void checkResultAndWait(IAsyncAction^ action) { - if(action->Status == Windows::Foundation::AsyncStatus::Started) + auto status = action->Status; + switch(status) { - IceUtilInternal::CountDownLatch count(1); - HRESULT result = 0; - action->Completed = ref new AsyncActionCompletedHandler( - [&count, &result] (IAsyncAction^ action, Windows::Foundation::AsyncStatus status) - { - if(status != Windows::Foundation::AsyncStatus::Completed) + case Windows::Foundation::AsyncStatus::Started: + { + promise<HRESULT> p; + action->Completed = ref new AsyncActionCompletedHandler( + [&p] (IAsyncAction^ action, Windows::Foundation::AsyncStatus status) { - result = action->ErrorCode.Value; - } - count.countDown(); - }); - count.await(); - if(result) + p.set_value(status != Windows::Foundation::AsyncStatus::Completed ? action->ErrorCode.Value : 0); + }); + + HRESULT result = p.get_future().get(); + if(result) + { + checkErrorCode(__FILE__, __LINE__, result); + } + break; + } + case Windows::Foundation::AsyncStatus::Error: { - checkErrorCode(__FILE__, __LINE__, result); + checkErrorCode(__FILE__, __LINE__, action->ErrorCode.Value); + break; + } + default: + { + break; } - } - else if(action->Status == Windows::Foundation::AsyncStatus::Error) - { - checkErrorCode(__FILE__, __LINE__, action->ErrorCode.Value); } } @@ -2856,6 +2839,34 @@ IceInternal::checkErrorCode(const char* file, int line, HRESULT herr) } } +// +// UWP impose some restriction on operations that block when run from +// STA thread and throws concurrency::invalid_operation. We cannot +// directly call task::get or task::way, this helper method is used to +// workaround this limitation. +// +void +IceInternal::runSync(Windows::Foundation::IAsyncAction^ action) +{ + std::promise<void> p; + + concurrency::create_task(action).then( + [&p](concurrency::task<void> t) + { + try + { + t.get(); + p.set_value(); + } + catch(...) + { + p.set_exception(std::current_exception()); + } + }, + concurrency::task_continuation_context::use_arbitrary()); + + return p.get_future().get(); +} #endif |