Git Product home page Git Product logo

nats_asio's Introduction

nats-asio

Overview

This is nats-io client writen in c++14 with use of boost asio and coroutines libraries.

Requirements

For Library

boost/1.74.0
fmt/6.2.0
spdlog/1.5.0
openssl/1.1.1d
nlohmann_json/3.9.1

For tests

gtest/1.8.1

For nats tool

cxxopts/2.2.1

Usage of library

  • You can just copy interface.hpp and impl.hpp in you project (don't forget to include impl.hpp somewhere)
  • Or you can use with conan package. Add conan remote:
conan remote add antlad-conan https://api.bintray.com/conan/antlad/antlad-conan

And then add nats_asio/0.0.13@_/_ to dependencies.

If you use 17 standard, don't forget to specify it in conan profile or during install, more details here

Example

Please check source code of tool samples/nats_tool.cpp

nats_asio's People

Contributors

antlad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nats_asio's Issues

async_write may overflow

Hello,

Why is the code adding +4 here instead of +2?
Since the calculation is header_size + body_size + \r\n, why are you adding +4?

Thanks beforehand.

Not connecting if deadline_timer not used

In any case, thank you for this great lib, saved me from a lot of headaches.
Sorry if the question is really stupid. Not a c++ programmer at all.

The provided sample did not connect to Nats if we do not use deadline_timer

...
#include <boost/chrono.hpp>
#include <boost/thread/thread.hpp> 
...
void generator::publish(boost::asio::yield_context ctx) {
	const std::string msg("{\"value\": 123}");

	for (;;) { // imagine here we use something like https://github.com/andreiavrammsd/cpp-channel
		auto s = m_conn->publish(m_topic, msg.data(), msg.size(), {}, ctx);

		if (s.failed()) {
			m_log->error("publish failed with error {}", s.error());
		}
		else {
			m_counter++;
		}
		// just to not spam a lot
		boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));
	}
}

Sample output is

C:\projects\cpp\nats_test\nats_test\Release>nats_test.exe
[2023-04-05 09:41:40.839] [console] [info] stats: messages 0 during 1 seconds
[2023-04-05 09:41:40.841] [console] [error] publish failed with error not connected
[2023-04-05 09:41:41.938] [console] [error] publish failed with error not connected
[2023-04-05 09:41:42.986] [console] [error] publish failed with error not connected
[2023-04-05 09:41:43.986] [console] [error] publish failed with error not connected
[2023-04-05 09:41:44.987] [console] [error] publish failed with error not connected
[2023-04-05 09:41:45.987] [console] [error] publish failed with error not connected
[2023-04-05 09:41:46.990] [console] [error] publish failed with error not connected
[2023-04-05 09:41:47.994] [console] [error] publish failed with error not connected

If we don't want to use timer_deadline, instead we want to read messages for publishing from some queue in an infinite loop, we can do this

void generator::publish(boost::asio::yield_context ctx) {
	boost::asio::deadline_timer timer(m_ioc);
	boost::system::error_code error;
	const std::string msg("{\"value\": 123}");

	while (!m_conn->is_connected()) {
		m_log->info("wait for Nats to connect...");
		timer.expires_from_now(boost::posix_time::milliseconds(50));
		timer.async_wait(ctx[error]);

		if (error.failed()) {
			m_log->error(error.message());

			return;
		}
	}

	for (;;) {
		auto s = m_conn->publish(m_topic, msg.data(), msg.size(), {}, ctx);

		if (s.failed()) {
			m_log->error("publish failed with error {}", s.error());
		}
		else {
			m_counter++;
		}

		boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));
	}
}

Sample output is

C:\projects\cpp\nats_test\nats_test\Release>nats_test.exe
[2023-04-05 09:51:18.084] [console] [info] stats: messages 0 during 1 seconds
[2023-04-05 09:51:18.110] [console] [info] wait for Nats to connect...
[2023-04-05 09:51:18.136] [console] [info] on connected
[2023-04-05 09:51:20.162] [console] [info] stats: messages 2 during 1 seconds
[2023-04-05 09:51:22.164] [console] [info] stats: messages 2 during 1 seconds

But I don't understand, why we are forced to use timer_deadline? Why connection to Nats is not established after conn->start(conf); call?

Builded on Windows as x32 application.

One more question - as far as I can see above code will fail to reconnect. This code will reconnect successfully

void generator::publish(boost::asio::yield_context ctx) {
	boost::asio::deadline_timer timer(m_ioc);
	boost::system::error_code error;
	const std::string msg("{\"value\": 123}");

	while (!m_conn->is_connected()) {
		m_log->info("wait for Nats to connect...");
		timer.expires_from_now(boost::posix_time::milliseconds(50));
		timer.async_wait(ctx[error]);

		if (error.failed()) {
			m_log->error(error.message());

			return;
		}
	}

	for (;;) {
		auto s = m_conn->publish(m_topic, msg.data(), msg.size(), {}, ctx);

		if (s.failed()) {
			m_log->error("publish failed with error {}", s.error());
			while (!m_conn->is_connected()) {
				m_log->info("wait for Nats to reconnect...");
				timer.expires_from_now(boost::posix_time::milliseconds(50));
				timer.async_wait(ctx[error]);

				if (error.failed()) {
					m_log->error(error.message());

					return;
				}
			}
		}
		else {
			m_counter++;
		}

		boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));		
	}
}

Is there a better way?

conan install doesn't work

when I execute
conan install --build=missing
get following:
cxxopts/v2.1.2@inexorgame/stable: Not found in local cache, looking in remotes...
cxxopts/v2.1.2@inexorgame/stable: Trying with 'conan-center'...
ERROR: Unable to find 'cxxopts/v2.1.2@inexorgame/stable' in remotes

Tried on both windows 10 and ubunta 18.04
searching for suitable libs, doesn't find any:

conan search cxxopts/* -r=all
There are no packages matching the 'cxxopts/*' pattern

error: no matching function for call to bind

Hi
I am trying to poc a nats sample in C++ but I am struggle with a issue in nats_tool.cpp:

error: no matching function for call to 'bind(, std::__shared_ptr<grubber, (__gnu_cxx::_Lock_policy)2>::element_type*, const std::_Placeholder<1>&, const std::_Placeholder<2>&, const std::_Placeholder<3>&, const std::_Placeholder<4>&, const std::_Placeholder<5>&)'
std::bind(&grubber::on_message, grub_ptr.get(), _1, _2, _3, _4, _5), ctx);

Could you please provide assistance ?

Thanks

-ERR Unknown Protocol Operation

Hi

I am trying to use your sample nats in generator mode.

I launch the client:

C:\HOMEWARE\nats-sample\cmake-build-debug\bin>nats-sample.exe -d --mode gen --address 127.0.0.1 --port 4222 --publish_interval 1000 --topic FOO
[2020-06-26 11:24:29.272] [console] [info] stats: messages 0 during 1 seconds
[2020-06-26 11:24:29.275] [console] [error] publish failed with error not connected
[2020-06-26 11:24:29.278] [console] [debug] got info {"client_id":84,"go":"go1.11.10","host":"0.0.0.0","max_payload":1048576,"port":4222,"proto":1,"server_id":"NDDKGVA5F5LTOQ6GBONFWXENTBAIWSWM5IMCHB6HS4OOZW7QFRVQXY3O","version":"2.0.0"}
[2020-06-26 11:24:29.290] [console] [info] on disconnected
[2020-06-26 11:24:29.291] [console] [error] async handshake failed wrong version number
[2020-06-26 11:24:29.292] [console] [error] connect failed with error wrong version number
[2020-06-26 11:24:29.295] [console] [debug] got info {"client_id":85,"go":"go1.11.10","host":"0.0.0.0","max_payload":1048576,"port":4222,"proto":1,"server_id":"NDDKGVA5F5LTOQ6GBONFWXENTBAIWSWM5IMCHB6HS4OOZW7QFRVQXY3O","version":"2.0.0"}
[2020-06-26 11:24:29.298] [console] [info] on disconnected
[2020-06-26 11:24:29.300] [console] [error] async handshake failed unspecified system error
[2020-06-26 11:24:29.302] [console] [error] connect failed with error unspecified system error
[2020-06-26 11:24:29.312] [console] [debug] got info {"client_id":86,"go":"go1.11.10","host":"0.0.0.0","max_payload":1048576,"port":4222,"proto":1,"server_id":"NDDKGVA5F5LTOQ6GBONFWXENTBAIWSWM5IMCHB6HS4OOZW7QFRVQXY3O","version":"2.0.0"}
[2020-06-26 11:24:29.313] [console] [info] on disconnected
[2020-06-26 11:24:29.315] [console] [error] async handshake failed unspecified system error
[2020-06-26 11:24:29.317] [console] [error] connect failed with error unspecified system error
[2020-06-26 11:24:29.323] [console] [debug] got info {"client_id":87,"go":"go1.11.10","host":"0.0.0.0","max_payload":1048576,"port":4222,"proto":1,"server_id":"NDDKGVA5F5LTOQ6GBONFWXENTBAIWSWM5IMCHB6HS4OOZW7QFRVQXY3O","version":"2.0.0"}

Then I got this issue on the NATS server executable :

C:\HOMEWARE\nats-server-v2.0.0-windows-amd64>nats-server.exe -DVV
[22692] 2020/06/26 11:06:40.333104 [�[32mINF�[0m] Starting nats-server version 2.0.0
[22692] 2020/06/26 11:06:40.334101 [�[36mDBG�[0m] Go build version go1.13.10
[22692] 2020/06/26 11:06:40.335129 [�[32mINF�[0m] Git commit [bf0930e]
[22692] 2020/06/26 11:06:40.337379 [�[32mINF�[0m] Listening for client connections on 0.0.0.0:4222
[22692] 2020/06/26 11:06:40.337379 [�[32mINF�[0m] Server id is NAIF4QISWLDVN4HPMFPMBKWM752Q7FGQZTZJLMNO7BLGOTTR7R3WPF77
[22692] 2020/06/26 11:06:40.337379 [�[32mINF�[0m] Server is ready
[22692] 2020/06/26 11:06:40.337379 [�[36mDBG�[0m] Get non local IPs for "0.0.0.0"
[22692] 2020/06/26 11:06:40.353050 [�[36mDBG�[0m]  ip=172.16.1.68
[22692] 2020/06/26 11:06:40.374994 [�[36mDBG�[0m]  ip=192.168.1.42
[22692] 2020/06/26 11:06:44.655779 [�[36mDBG�[0m] 127.0.0.1:65229 - cid:1 - Client connection created
[22692] 2020/06/26 11:06:44.658859 [�[33mTRC�[0m] 127.0.0.1:65229 - cid:1 - ->> [-ERR Unknown Protocol Operation]
[22692] 2020/06/26 11:06:44.658859 [�[31mERR�[0m] 127.0.0.1:65229 - cid:1 - Client parser ERROR, state=0, i=0: proto='"\x16\x03\x01\x00\xbd\x01\x00\x00\xb9\x03\x03I\x1dts\x9a\xe7?\x02}\xbfkn}\x00\x04\x0f\x1cv̬\xb5"...'
[22692] 2020/06/26 11:06:44.660732 [�[36mDBG�[0m] 127.0.0.1:65229 - cid:1 - Client connection closed: Protocol Violation
[22692] 2020/06/26 11:06:44.669706 [�[36mDBG�[0m] 127.0.0.1:65230 - cid:2 - Client connection created
[22692] 2020/06/26 11:06:44.670704 [�[36mDBG�[0m] 127.0.0.1:65230 - cid:2 - Client connection closed: Client Closed
C:\HOMEWARE\nats-server-v2.0.0-windows-amd64>nats-server.exe -v
nats-server: v2.0.0

Do you have an idea of whats causes this issue ?

Best,
Gilles

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.