unsigned int mMaxNumberOfPeers;
template<typename SentDataType, typename ReceivedDataType>
+ MessageID callAsyncInternal(const MethodID methodID,
+ const PeerID peerID,
+ const std::shared_ptr<SentDataType>& data,
+ const typename ResultHandler<ReceivedDataType>::type& process);
+
+ template<typename SentDataType, typename ReceivedDataType>
void setMethodHandlerInternal(const MethodID methodID,
const typename MethodHandler<SentDataType, ReceivedDataType>::type& process);
const typename ResultHandler<ReceivedDataType>::type& process)
{
Lock lock(mStateMutex);
+ return callAsyncInternal<SentDataType, ReceivedDataType>(methodID, peerID, data, process);
+}
+
+template<typename SentDataType, typename ReceivedDataType>
+MessageID Processor::callAsyncInternal(const MethodID methodID,
+ const PeerID peerID,
+ const std::shared_ptr<SentDataType>& data,
+ const typename ResultHandler<ReceivedDataType>::type& process)
+{
auto request = MethodRequest::create<SentDataType, ReceivedDataType>(methodID, peerID, data, process);
mRequestQueue.pushBack(Event::METHOD, request);
return request->messageID;
cv.notify_all();
};
- MessageID messageID = callAsync<SentDataType, ReceivedDataType>(methodID,
- peerID,
- data,
- process);
+ Lock lock(mStateMutex);
+ MessageID messageID = callAsyncInternal<SentDataType, ReceivedDataType>(methodID,
+ peerID,
+ data,
+ process);
auto isResultInitialized = [&result]() {
return result.isValid();
};
- Lock lock(mStateMutex);
LOGT(mLogPrefix + "Waiting for the response...");
+ //In the case of too large sending time response can be received far after timeoutMS but
+ //before this thread wakes up and before predicate check (there will by no timeout exception)
if (!cv.wait_for(lock, std::chrono::milliseconds(timeoutMS), isResultInitialized)) {
LOGW(mLogPrefix + "Probably a timeout in callSync. Checking...");
methodResult->set(returnData);
}
+void shortEchoCallback(const PeerID,
+ std::shared_ptr<RecvData>& data,
+ MethodResult::Pointer methodResult)
+{
+ std::this_thread::sleep_for(std::chrono::milliseconds(SHORT_OPERATION_TIME));
+ auto returnData = std::make_shared<SendData>(data->intVal);
+ methodResult->set(returnData);
+}
+
PeerID connect(Service& s, Client& c)
{
// Connects the Client to the Service and returns Clients PeerID
MULTI_FIXTURE_TEST_CASE(WriteTimeout, F, ThreadedFixture, GlibFixture)
{
Service s(F::getPoll(), SOCKET_PATH);
- s.setMethodHandler<SendData, RecvData>(1, echoCallback);
+ s.setMethodHandler<SendData, RecvData>(1, shortEchoCallback);
s.start();
Client c(F::getPoll(), SOCKET_PATH);