Skip to content

Commit b80f4f8

Browse files
fix: change requestId type for method invocation
the unsigned type suites better the request id type, id should not be negative. Fixes the overflow issue, as unsigned int behavior is defined.
1 parent 32a5c35 commit b80f4f8

9 files changed

+22
-26
lines changed

src/olink/clientnode.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace ApiGear { namespace ObjectLink {
99
ClientNode::ClientNode(ClientRegistry& registry)
1010
: BaseNode()
1111
, m_registry(registry)
12-
, m_nextRequestId(0)
1312
{
1413
}
1514

@@ -50,7 +49,7 @@ void ClientNode::invokeRemote(const std::string& methodId, const nlohmann::json&
5049
static const std::string invokeRemoteLog = "ClientNode.invokeRemote: ";
5150
emitLog(LogLevel::Info, invokeRemoteLog, methodId);
5251
std::unique_lock<std::mutex> lock(m_pendingInvokesMutex);
53-
int requestId = nextRequestId();
52+
auto requestId = nextRequestId();
5453
m_invokesPending[requestId] = func;
5554
lock.unlock();
5655
nlohmann::json msg = Protocol::invokeMessage(requestId, methodId, args);
@@ -113,7 +112,7 @@ void ClientNode::handlePropertyChange(const std::string& propertyId, const nlohm
113112
}
114113
}
115114

116-
void ClientNode::handleInvokeReply(int requestId, const std::string& methodId, const nlohmann::json& value)
115+
void ClientNode::handleInvokeReply(unsigned int requestId, const std::string& methodId, const nlohmann::json& value)
117116
{
118117
static const std::string handleInvokeLog = "ClientNode.handleInvokeReply: ";
119118
emitLogWithPayload(LogLevel::Info, value, handleInvokeLog, methodId);
@@ -154,12 +153,9 @@ void ClientNode::handleError(int msgType, int requestId, const std::string& erro
154153
emitLog(LogLevel::Info, errorLog, std::to_string(msgType), std::to_string(requestId), error);
155154
}
156155

157-
int ClientNode::nextRequestId()
156+
unsigned int ClientNode::nextRequestId()
158157
{
159158
m_nextRequestId++;
160-
if (m_nextRequestId < 0){
161-
m_nextRequestId = 0;
162-
}
163159
return m_nextRequestId;
164160
}
165161

src/olink/clientnode.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class OLINK_EXPORT ClientNode : public BaseNode, public IClientNode, public std:
7474
/** IProtocolListener::handlePropertyChange implementation */
7575
void handlePropertyChange(const std::string& propertyId, const nlohmann::json& value) override;
7676
/** IProtocolListener::handleInvokeReply implementation */
77-
void handleInvokeReply(int requestId, const std::string& methodId, const nlohmann::json& value) override;
77+
void handleInvokeReply(unsigned int requestId, const std::string& methodId, const nlohmann::json& value) override;
7878
/** IProtocolListener::handleSignal implementation */
7979
void handleSignal(const std::string& signalId, const nlohmann::json& args) override;
8080
/** IProtocolListener::handleError implementation */
@@ -84,17 +84,17 @@ class OLINK_EXPORT ClientNode : public BaseNode, public IClientNode, public std:
8484
* Returns a request id for outgoing messages.
8585
* @return a unique, non negative id.
8686
*/
87-
int nextRequestId();
87+
unsigned int nextRequestId();
8888
private:
8989
/* The registry in which client is registered and which provides sinks connected with this node*/
9090
ClientRegistry& m_registry;
9191
/* Id of this node in registry.*/
9292
unsigned long m_nodeId;
9393

9494
/* Value of last request id.*/
95-
std::atomic<int> m_nextRequestId;
95+
std::atomic<unsigned int> m_nextRequestId = {0};
9696
/** Collection of callbacks for method replies that client is waiting for associated with the id for invocation request message.*/
97-
std::map<int,InvokeReplyFunc> m_invokesPending;
97+
std::map<unsigned int,InvokeReplyFunc> m_invokesPending;
9898
std::mutex m_pendingInvokesMutex;
9999
};
100100

src/olink/core/basenode.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void BaseNode::handleUnlink(const std::string& objectId)
4444
emitLog(LogLevel::Warning, notImplementedLog, std::string(__func__), objectId);
4545
}
4646

47-
void BaseNode::handleInvoke(int, const std::string& methodId, const nlohmann::json& args)
47+
void BaseNode::handleInvoke(unsigned int, const std::string& methodId, const nlohmann::json& args)
4848
{
4949
emitLogWithPayload(LogLevel::Warning, args, notImplementedLog, std::string(__func__), methodId, " args ");
5050
}
@@ -59,7 +59,7 @@ void BaseNode::handleInit(const std::string& objectId, const nlohmann::json& pro
5959
emitLogWithPayload(LogLevel::Warning, props, notImplementedLog, std::string(__func__), objectId, " props ");
6060
}
6161

62-
void BaseNode::handleInvokeReply(int requestId, const std::string& methodId, const nlohmann::json& value)
62+
void BaseNode::handleInvokeReply(unsigned int requestId, const std::string& methodId, const nlohmann::json& value)
6363
{
6464
emitLog(LogLevel::Warning, notImplementedLog, std::string(__func__), methodId, " requestId ", std::to_string(requestId), " value ", value);
6565
}

src/olink/core/basenode.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ class OLINK_EXPORT BaseNode: public LoggerBase,
4242
// Empty, logging only implementation of IProtocolListener::handleUnlink, should be overwritten on server side.
4343
void handleUnlink(const std::string& objectId) override;
4444
// Empty, logging only implementation of IProtocolListener::handleInvoke, should be overwritten on server side.
45-
void handleInvoke(int requestId, const std::string& methodId, const nlohmann::json& args) override;
45+
void handleInvoke(unsigned int requestId, const std::string& methodId, const nlohmann::json& args) override;
4646
// Empty, logging only implementation of IProtocolListener::handleSetProperty, should be overwritten on server side.
4747
void handleSetProperty(const std::string& propertyId, const nlohmann::json& value) override;
4848
// Empty, logging only implementation of IProtocolListener::handleInit, should be overwritten on client side.
4949
void handleInit(const std::string& objectId, const nlohmann::json& props) override;
5050
// Empty, logging only implementation of IProtocolListener::handleInvokeReply, should be overwritten on client side.
51-
void handleInvokeReply(int requestId, const std::string& methodId, const nlohmann::json& value) override;
51+
void handleInvokeReply(unsigned int requestId, const std::string& methodId, const nlohmann::json& value) override;
5252
// Empty, logging only implementation of IProtocolListener::handleSignal, should be overwritten on client side.
5353
void handleSignal(const std::string& signalId, const nlohmann::json& args) override;
5454
// Empty, logging only implementation of IProtocolListener::handlePropertyChange, should be overwritten on client side.

src/olink/core/protocol.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ nlohmann::json Protocol::propertyChangeMessage(const std::string& propertyId, co
6565
);
6666
}
6767

68-
nlohmann::json Protocol::invokeMessage(int requestId, const std::string& methodId, const nlohmann::json& args)
68+
nlohmann::json Protocol::invokeMessage(unsigned int requestId, const std::string& methodId, const nlohmann::json& args)
6969
{
7070
return nlohmann::json::array(
7171
{ MsgType::Invoke, requestId, methodId, args }
7272
);
7373
}
7474

75-
nlohmann::json Protocol::invokeReplyMessage(int requestId, const std::string& methodId, const nlohmann::json& value)
75+
nlohmann::json Protocol::invokeReplyMessage(unsigned int requestId, const std::string& methodId, const nlohmann::json& value)
7676
{
7777
return nlohmann::json::array(
7878
{ MsgType::InvokeReply, requestId, methodId, value }
@@ -131,14 +131,14 @@ bool Protocol::handleMessage(const nlohmann::json& msg, IProtocolListener& liste
131131
break;
132132
}
133133
case int(MsgType::Invoke): {
134-
const auto& id = msg[1].get<int>();
134+
const auto& id = msg[1].get<unsigned int>();
135135
const auto& methodId = msg[2].get<std::string>();
136136
const auto& args = msg[3].get<nlohmann::json>();
137137
listener.handleInvoke(id, methodId, args);
138138
break;
139139
}
140140
case int(MsgType::InvokeReply): {
141-
const auto& id = msg[1].get<int>();
141+
const auto& id = msg[1].get<unsigned int>();
142142
const auto& methodId = msg[2].get<std::string>();
143143
const auto& value = msg[3].get<nlohmann::json>();
144144
listener.handleInvokeReply(id, methodId, value);

src/olink/core/protocol.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ class OLINK_EXPORT IProtocolListener
7979
* @param methodId Unambiguously describes method in object for which invoke message was received.
8080
* @param args Arguments with which method should be invoked.
8181
*/
82-
virtual void handleInvoke(int requestId, const std::string& methodId, const nlohmann::json& args) = 0;
82+
virtual void handleInvoke(unsigned int requestId, const std::string& methodId, const nlohmann::json& args) = 0;
8383
/**
8484
* Client side handler, handles invokeReply message.
8585
* @param requestId Identifier of a request with which the client requested method invocation.
8686
* should be used to deliver the result to a caller.
8787
* @param methodId Unambiguously describes method in object for which invokeReply message was received.
8888
* @param value Method's result value.
8989
*/
90-
virtual void handleInvokeReply(int requestId, const std::string& methodId, const nlohmann::json& value) = 0;
90+
virtual void handleInvokeReply(unsigned int requestId, const std::string& methodId, const nlohmann::json& value) = 0;
9191
/**
9292
* Client side handler, handles signal message.
9393
* @param signalId Unambiguously describes signal in object for which signal message was received.
@@ -165,7 +165,7 @@ class OLINK_EXPORT Protocol : public LoggerBase
165165
* @param args Arguments with which method should be invoked.
166166
* @return Composed invokeMessage in json format.
167167
*/
168-
static nlohmann::json invokeMessage(int requestId, const std::string& methodId, const nlohmann::json& args);
168+
static nlohmann::json invokeMessage(unsigned int requestId, const std::string& methodId, const nlohmann::json& args);
169169
/**
170170
* Method message.
171171
* Composes a response to a method invocation message for a methodId.
@@ -175,7 +175,7 @@ class OLINK_EXPORT Protocol : public LoggerBase
175175
* @param value Value that is an outcome of method invocation.
176176
* @return Composed invokeReplyMessage in json format.
177177
*/
178-
static nlohmann::json invokeReplyMessage(int requestId, const std::string& methodId, const nlohmann::json& value);
178+
static nlohmann::json invokeReplyMessage(unsigned int requestId, const std::string& methodId, const nlohmann::json& value);
179179
/**
180180
* Signal message.
181181
* Composes a notification message for signal emitted for signalId.

src/olink/remotenode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void RemoteNode::handleSetProperty(const std::string& propertyId, const nlohmann
8585
}
8686
}
8787

88-
void RemoteNode::handleInvoke(int requestId, const std::string& methodId, const nlohmann::json& args)
88+
void RemoteNode::handleInvoke(unsigned int requestId, const std::string& methodId, const nlohmann::json& args)
8989
{
9090
auto objectId = ApiGear::ObjectLink::Name::getObjectId(methodId);
9191
auto source = m_registry.getSource(objectId).lock();

src/olink/remotenode.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class OLINK_EXPORT RemoteNode: public BaseNode, public IRemoteNode, public std::
8181
/** IProtocolListener::handleSetProperty implementation. */
8282
void handleSetProperty(const std::string& propertyId, const nlohmann::json& value) override;
8383
/** IProtocolListener::handleInvoke implementation. */
84-
void handleInvoke(int requestId, const std::string& methodId, const nlohmann::json& args) override;
84+
void handleInvoke(unsigned int requestId, const std::string& methodId, const nlohmann::json& args) override;
8585

8686
/** IRemoteNode::notifyPropertyChange implementation. */
8787
void notifyPropertyChange(const std::string& propertyId, const nlohmann::json& value) override;

tests/test_protocol.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ TEST_CASE("protocol")
1616
json props = {{ "count", 0 }};
1717
int value = 1;
1818
json args = {1, 2};
19-
int requestId = 1;
19+
unsigned int requestId = 1;
2020
MsgType msgType = MsgType::Invoke;
2121
std::string error = "failed";
2222

0 commit comments

Comments
 (0)