git subrepo clone (merge) --branch=v3.6.1 https://github.com/nats-io/nats.c.git deps/nats.c

subrepo:
  subdir:   "deps/nats.c"
  merged:   "66cec7f"
upstream:
  origin:   "https://github.com/nats-io/nats.c.git"
  branch:   "v3.6.1"
  commit:   "66cec7f"
git-subrepo:
  version:  "0.4.6"
  commit:   "b8b46501e"
This commit is contained in:
2023-08-15 00:19:51 -07:00
parent 5bc936a09f
commit 79a45fd2e3
558 changed files with 151795 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
if(NOT NATS_BUILD_EXAMPLES)
return()
endif()
# We need this directory to build the examples
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/examples/getstarted)
# Get all the .c files in the examples directory
file(GLOB EXAMPLES_SOURCES RELATIVE ${PROJECT_SOURCE_DIR}/examples/getstarted *.c)
if(NOT NATS_BUILD_STATIC_EXAMPLES)
add_definitions(-Dnats_IMPORTS)
endif()
# For each file...
foreach(examples_src ${EXAMPLES_SOURCES})
# Remove the suffix so that it becomes the executable name
string(REPLACE ".c" "" examplename ${examples_src})
set(exampleexe "${examplename}")
# Build the executable
add_executable(${exampleexe} ${PROJECT_SOURCE_DIR}/examples/getstarted/${examples_src})
# Link
if(NATS_BUILD_STATIC_EXAMPLES)
target_link_libraries(${exampleexe} nats_static ${NATS_EXTRA_LIB})
else()
target_link_libraries(${exampleexe} nats ${NATS_EXTRA_LIB})
endif()
endforeach()

View File

@@ -0,0 +1,69 @@
// Copyright 2017-2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <nats.h>
static void
onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure)
{
printf("Received msg: %s - %.*s\n",
natsMsg_GetSubject(msg),
natsMsg_GetDataLength(msg),
natsMsg_GetData(msg));
// Need to destroy the message!
natsMsg_Destroy(msg);
// Notify the main thread that we are done.
*(bool *)(closure) = true;
}
int main(int argc, char **argv)
{
natsConnection *conn = NULL;
natsSubscription *sub = NULL;
natsStatus s;
volatile bool done = false;
printf("Listening on subject 'foo'\n");
// Creates a connection to the default NATS URL
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
if (s == NATS_OK)
{
// Creates an asynchronous subscription on subject "foo".
// When a message is sent on subject "foo", the callback
// onMsg() will be invoked by the client library.
// You can pass a closure as the last argument.
s = natsConnection_Subscribe(&sub, conn, "foo", onMsg, (void*) &done);
}
if (s == NATS_OK)
{
for (;!done;) {
nats_Sleep(100);
}
}
// Anything that is created need to be destroyed
natsSubscription_Destroy(sub);
natsConnection_Destroy(conn);
// If there was an error, print a stack trace and exit
if (s != NATS_OK)
{
nats_PrintLastErrorStack(stderr);
exit(2);
}
return 0;
}

View File

@@ -0,0 +1,135 @@
// Copyright 2020-2021 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <nats.h>
int main(int argc, char **argv)
{
natsConnection *conn = NULL;
natsSubscription *sub = NULL;
natsMsg *msg = NULL;
natsMsg *rmsg = NULL;
natsStatus s;
// Creates a connection to the default NATS URL
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
// Create a message
if (s == NATS_OK)
s = natsMsg_Create(&msg, "foo", NULL, "body", 4);
// Create a header by setting a key/value
if (s == NATS_OK)
s = natsMsgHeader_Set(msg, "My-Key1", "value1");
// Let's set a new key
if (s == NATS_OK)
s = natsMsgHeader_Set(msg, "My-Key2", "value2");
// Here we add a value to the first key
if (s == NATS_OK)
s = natsMsgHeader_Add(msg, "My-Key1", "value3");
// Adding yet another key
if (s == NATS_OK)
s = natsMsgHeader_Set(msg, "My-Key3", "value4");
// Remove a key
if (s == NATS_OK)
s = natsMsgHeader_Delete(msg, "My-Key3");
// Let's print all the keys that are currently set
if (s == NATS_OK)
{
const char* *keys = NULL;
int nkeys = 0;
int i;
s = natsMsgHeader_Keys(msg, &keys, &nkeys);
for (i=0; (s == NATS_OK) && (i<nkeys); i++)
{
// To get all values that are associated with a key
const char* *values = NULL;
int nvalues = 0;
int j;
s = natsMsgHeader_Values(msg, keys[i], &values, &nvalues);
for (j=0; (s == NATS_OK) && (j < nvalues); j++)
printf("Key: '%s' Value: '%s'\n", keys[i], values[j]);
// We need to free the returned array of pointers.
free((void*) values);
}
// We need to free the returned array of pointers.
free((void*) keys);
}
// Create a subscription that we will use to receive this message
if (s == NATS_OK)
s = natsConnection_SubscribeSync(&sub, conn, "foo");
// Now publish the message
if (s == NATS_OK)
s = natsConnection_PublishMsg(conn, msg);
// We should receive it
if (s == NATS_OK)
s = natsSubscription_NextMsg(&rmsg, sub, 1000);
// Now let's check some headers from the received message
if (s == NATS_OK)
{
const char *val = NULL;
// Notice that calling natsMsgHeader_Get() on a key that has
// several values will return the first entry.
s = natsMsgHeader_Get(rmsg, "My-Key1", &val);
if (s == NATS_OK)
printf("For key 'My-Key1', got value: '%s'\n", val);
// If we lookup for a key that does not exist, we get NATS_NOT_FOUND.
if (s == NATS_OK)
{
s = natsMsgHeader_Get(rmsg, "key-does-not-exist", &val);
if (s == NATS_NOT_FOUND)
s = NATS_OK;
else
printf("Should not have found that key!\n");
}
// Same for delete
if (s == NATS_OK)
{
s = natsMsgHeader_Delete(rmsg, "key-does-not-exist");
if (s == NATS_NOT_FOUND)
s = NATS_OK;
else
printf("Should not have found that key!\n");
}
}
// Anything that is created need to be destroyed
natsMsg_Destroy(msg);
natsMsg_Destroy(rmsg);
natsSubscription_Destroy(sub);
natsConnection_Destroy(conn);
// If there was an error, print a stack trace and exit
if (s != NATS_OK)
{
nats_PrintLastErrorStack(stderr);
exit(2);
}
return 0;
}

View File

@@ -0,0 +1,48 @@
// Copyright 2017-2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <nats.h>
int main(int argc, char **argv)
{
natsConnection *conn = NULL;
natsStatus s;
printf("Publishes a message on subject 'foo'\n");
// Creates a connection to the default NATS URL
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
if (s == NATS_OK)
{
const char data[] = {104, 101, 108, 108, 111, 33};
// Publishes the sequence of bytes on subject "foo".
s = natsConnection_Publish(conn, "foo", data, sizeof(data));
}
// Anything that is created need to be destroyed
natsConnection_Destroy(conn);
// If there was an error, print a stack trace and exit
if (s != NATS_OK)
{
nats_PrintLastErrorStack(stderr);
exit(2);
}
return 0;
}

View File

@@ -0,0 +1,56 @@
// Copyright 2017-2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <nats.h>
int main(int argc, char **argv)
{
natsConnection *conn = NULL;
natsMsg *msg = NULL;
natsStatus s;
printf("Publishes a message on subject 'foo'\n");
// Creates a connection to the default NATS URL
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
if (s == NATS_OK)
{
const char data[] = {104, 101, 108, 108, 111, 33};
// Creates a message for subject "foo", no reply, and
// with the given payload.
s = natsMsg_Create(&msg, "foo", NULL, data, sizeof(data));
}
if (s == NATS_OK)
{
// Publishes the message on subject "foo".
s = natsConnection_PublishMsg(conn, msg);
}
// Anything that is created need to be destroyed
natsMsg_Destroy(msg);
natsConnection_Destroy(conn);
// If there was an error, print a stack trace and exit
if (s != NATS_OK)
{
nats_PrintLastErrorStack(stderr);
exit(2);
}
return 0;
}

View File

@@ -0,0 +1,47 @@
// Copyright 2017-2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <nats.h>
int main(int argc, char **argv)
{
natsConnection *conn = NULL;
natsStatus s;
printf("Publishes a message on subject 'foo'\n");
// Creates a connection to the default NATS URL
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
if (s == NATS_OK)
{
// This is a convenient function to send a message on "foo"
// as a string.
s = natsConnection_PublishString(conn, "foo", "hello!");
}
// Anything that is created need to be destroyed
natsConnection_Destroy(conn);
// If there was an error, print a stack trace and exit
if (s != NATS_OK)
{
nats_PrintLastErrorStack(stderr);
exit(2);
}
return 0;
}

View File

@@ -0,0 +1,76 @@
// Copyright 2017-2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <nats.h>
static void
onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure)
{
printf("Received msg: %s - %.*s\n",
natsMsg_GetSubject(msg),
natsMsg_GetDataLength(msg),
natsMsg_GetData(msg));
// Sends a reply
if (natsMsg_GetReply(msg) != NULL)
{
natsConnection_PublishString(nc, natsMsg_GetReply(msg),
"here's some help");
}
// Need to destroy the message!
natsMsg_Destroy(msg);
// Notify the main thread that we are done.
*(bool *)(closure) = true;
}
int main(int argc, char **argv)
{
natsConnection *conn = NULL;
natsSubscription *sub = NULL;
natsStatus s;
volatile bool done = false;
printf("Listening for requests on subject 'help'\n");
// Creates a connection to the default NATS URL
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
if (s == NATS_OK)
{
// Creates an asynchronous subscription on subject "help",
// waiting for a request. If a message arrives on this
// subject, the callback onMsg() will be invoked and it
// will send a reply.
s = natsConnection_Subscribe(&sub, conn, "help", onMsg, (void*) &done);
}
if (s == NATS_OK)
{
for (;!done;) {
nats_Sleep(100);
}
}
// Anything that is created need to be destroyed
natsSubscription_Destroy(sub);
natsConnection_Destroy(conn);
// If there was an error, print a stack trace and exit
if (s != NATS_OK)
{
nats_PrintLastErrorStack(stderr);
exit(2);
}
return 0;
}

View File

@@ -0,0 +1,60 @@
// Copyright 2017-2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <nats.h>
int main(int argc, char **argv)
{
natsConnection *conn = NULL;
natsMsg *reply= NULL;
natsStatus s;
printf("Publishes a message on subject 'help'\n");
// Creates a connection to the default NATS URL
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
if (s == NATS_OK)
{
// Sends a request on "help" and expects a reply.
// Will wait only for a given number of milliseconds,
// say for 5 seconds in this example.
s = natsConnection_RequestString(&reply, conn, "help",
"really need some", 5000);
}
if (s == NATS_OK)
{
// If we are here, we should have received the reply
printf("Received reply: %.*s\n",
natsMsg_GetDataLength(reply),
natsMsg_GetData(reply));
// Need to destroy the message!
natsMsg_Destroy(reply);
}
// Anything that is created need to be destroyed
natsConnection_Destroy(conn);
// If there was an error, print a stack trace and exit
if (s != NATS_OK)
{
nats_PrintLastErrorStack(stderr);
exit(2);
}
return 0;
}

View File

@@ -0,0 +1,64 @@
// Copyright 2017-2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <nats.h>
int main(int argc, char **argv)
{
natsConnection *conn = NULL;
natsSubscription *sub = NULL;
natsMsg *msg = NULL;
natsStatus s;
printf("Listening on subject 'foo'\n");
// Creates a connection to the default NATS URL
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
if (s == NATS_OK)
{
// Creates an synchronous subscription on subject "foo".
s = natsConnection_SubscribeSync(&sub, conn, "foo");
}
if (s == NATS_OK)
{
// With synchronous subscriptions, one need to poll
// using this function. A timeout is used to instruct
// how long we are willing to wait. The wait is in milliseconds.
// So here, we are going to wait for 5 seconds.
s = natsSubscription_NextMsg(&msg, sub, 5000);
}
if (s == NATS_OK)
{
// If we are here, we should have received a message.
printf("Received msg: %s - %.*s\n",
natsMsg_GetSubject(msg),
natsMsg_GetDataLength(msg),
natsMsg_GetData(msg));
// Need to destroy the message!
natsMsg_Destroy(msg);
}
// Anything that is created need to be destroyed
natsSubscription_Destroy(sub);
natsConnection_Destroy(conn);
// If there was an error, print a stack trace and exit
if (s != NATS_OK)
{
nats_PrintLastErrorStack(stderr);
exit(2);
}
return 0;
}