Browse Source

Merge pull request #580 from DavidEichmann/fixRtCReceiveMessage

Fix rtcReceiveMessage() in C api consuming messages when buffer is NULL
Paul-Louis Ageneau 3 years ago
parent
commit
de935bd8b3
2 changed files with 32 additions and 26 deletions
  1. 1 1
      DOC.md
  2. 31 25
      src/capi.cpp

+ 1 - 1
DOC.md

@@ -508,7 +508,7 @@ Arguments:
 
 - `id`: the channel identifier
 - `buffer`: a user-supplied buffer where to write the message data
-- `size`: a pointer to a user-supplied int which must be initialized to the size of `buffer`. On success, the function will write the size of the message to it before returning.
+- `size`: a pointer to a user-supplied int which must be initialized to the size of `buffer`. On success, the function will write the size of the message to it before returning (positive size if binary, negative size including terminating 0 if string).
 
 Return value: `RTC_ERR_SUCCESS` or a negative error code (In particular, `RTC_ERR_NOT_AVAIL` is returned when there are no pending messages)
 

+ 31 - 25
src/capi.cpp

@@ -769,31 +769,37 @@ int rtcReceiveMessage(int id, char *buffer, int *size) {
 			return RTC_ERR_NOT_AVAIL;
 
 		return std::visit( //
-		    overloaded{
-		        [&](binary b) {
-			        int ret = copyAndReturn(std::move(b), buffer, *size);
-			        if (ret >= 0) {
-				        channel->receive(); // discard
-				        *size = ret;
-				        return RTC_ERR_SUCCESS;
-			        } else {
-				        *size = int(b.size());
-				        return ret;
-			        }
-		        },
-		        [&](string s) {
-			        int ret = copyAndReturn(std::move(s), buffer, *size);
-			        if (ret >= 0) {
-				        channel->receive(); // discard
-				        *size = -ret;
-				        return RTC_ERR_SUCCESS;
-			        } else {
-				        *size = -int(s.size() + 1);
-				        return ret;
-			        }
-		        },
-		    },
-		    *message);
+		overloaded{
+			[&](binary b) {
+				int ret = copyAndReturn(std::move(b), buffer, *size);
+				if (ret >= 0) {
+					*size = ret;
+					if (buffer) {
+						channel->receive(); // discard
+					}
+					
+					return RTC_ERR_SUCCESS;
+				} else {
+					*size = int(b.size());
+					return ret;
+				}
+			},
+			[&](string s) {
+				int ret = copyAndReturn(std::move(s), buffer, *size);
+				if (ret >= 0) {
+					*size = -ret;
+					if (buffer) {
+						channel->receive(); // discard
+					}
+					
+					return RTC_ERR_SUCCESS;
+				} else {
+					*size = -int(s.size() + 1);
+					return ret;
+				}
+			},
+		},
+		*message);
 	});
 }