call the appropriate function with arguments based on key received - c++

In my application I'm calling different functions based on the message key received via network. But my implementation isn't that effective as it consists of exhaustive search for the key in the std::vector<std::string>. I'm thinking of using std::unordered_map<std::string, <fun>> but don't know how to map it to a function and also call that function by passing the required arguments. This is my current implementation:
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> msg_key_list;
msg_key_list.emplace_back("key1");
msg_key_list.emplace_back("key2");
msg_key_list.emplace_back("key3");
msg_key_list.emplace_back("key4");
msg_key_list.emplace_back("key5");
char buff[2048];
ssize_t data_len;
while (true) {
// receive data from network
std::string msg(buff, data_len);
std::string::size_type pos = msg.find_first_of(",");
std::string key = msg.substr(0, pos - 1);
if (key == msg_key_list.at(0)) {
// validate the message received
bool valid;
if (!valid) {
std::cerr << "Invalid data: ";
std::cerr << __FILE__ << ", " << __PRETTY_FUNCTION__ << ", " << __LINE__ << "\n";
continue;
}
std::string val(buff + pos, msg.size() - pos);
// fun1(val);
} else if (key == msg_key_list.at(1)) {
// validate the message received
bool valid;
if (!valid) {
std::cerr << "Invalid data: ";
std::cerr << __FILE__ << ", " << __PRETTY_FUNCTION__ << ", " << __LINE__ << "\n";
continue;
}
std::string val(buff + pos, msg.size() - pos);
// fun2(val);
} else if (key == msg_key_list.at(2)) {
// validate the message received
bool valid;
if (!valid) {
std::cerr << "Invalid data: ";
std::cerr << __FILE__ << ", " << __PRETTY_FUNCTION__ << ", " << __LINE__ << "\n";
continue;
}
std::string val(buff + pos, msg.size() - pos);
// fun3(val);
} else if (key == msg_key_list.at(3)) {
// validate the message received
bool valid;
if (!valid) {
std::cerr << "Invalid data: ";
std::cerr << __FILE__ << ", " << __PRETTY_FUNCTION__ << ", " << __LINE__ << "\n";
continue;
}
std::string val(buff + pos, msg.size() - pos);
// fun4(val);
} else if (key == msg_key_list.at(4)) {
// validate the message received
bool valid;
if (!valid) {
std::cerr << "Invalid data: ";
std::cerr << __FILE__ << ", " << __PRETTY_FUNCTION__ << ", " << __LINE__ << "\n";
continue;
}
std::string val(buff + pos, msg.size() - pos);
// fun5(val);
}
}
}

I made an example of how I whould approach this (if all handlers would share the same function signature). I also avoid the string copying in the message handler because that would slow things done unecessarily.
#include <iostream>
#include <stdexcept>
#include <vector>
#include <map>
#include <string>
//-----------------------------------------------------------------------------
// function signature of your message handlers
using handler_t = void(*)(const std::string_view&);
//-----------------------------------------------------------------------------
// handler functions
void handler_1(const std::string_view& msg)
{
// validate data here
std::cout << "handler 1 : " << msg << std::endl;
};
void handler_2(const std::string_view& msg)
{
std::cout << "handler 2 : " << msg << std::endl;
};
//-----------------------------------------------------------------------------
// handler selection
void handle_message(const std::string& buffer)
{
static const std::map<std::string_view, handler_t> handler_map
{
{"key1",handler_1},
{"key2",handler_2}
};
try
{
// find location of first ',' (if any)
// also ensure the ',' is not too far back for splitting
auto index = buffer.find_first_of(',');
if ((index != std::string::npos) && (index < (buffer.length() - 1)))
{
auto pos = buffer.begin() + index;
// No need to copy data to seperate strings
// string_views will do.
std::string_view key{ buffer.begin(), pos };
std::string_view data{ pos + 1, buffer.end() };
// lookup handler, not if no handler is found
// a std::out_of_range exception will be thrown
auto handler = handler_map.at(key);
// call the handler
handler(data);
}
}
catch (const std::out_of_range&)
{
// todo decide what you want to do with incorrect messages here
}
}
//-----------------------------------------------------------------------------
// and handle a test message in main
int main()
{
const std::string data{ "key1,Hello" };
handle_message(data);
}

Related

Google Pub/Sub C++ C++ Pub/Sub stops receiving

Using the online documentation and the code in the sample repo, I've implemented PubSub for a single incoming and multiple outgoing. The pull PubSub is an ordering subscription (would much prefer an ordered and deliever once, but that doesn't seem allowed). I have dead letter configured for each PubSub. Repeatably, the code will stop pulling messages. Sometimes in 5 minutes, sometimes after a couple of days. This morning, there were 55k messages waiting to be pulled. No messages in the dead letter. Based on the website, I expected something from the then() handler in the subscription, but there is no error messages. Here is the relevant parts my code.
/* These are based on https://github.com/googleapis/google-cloud-cpp/blob/HEAD/google/cloud/pubsub/samples/samples.cc */
/* https://cloud.google.com/pubsub/docs/handling-failures#dead-letter_topic */
/* https://cloud.google.com/blog/products/application-development/running-cplusplus-apps-with-the-help-of-pubsub-and-gke */
/* https://cloud.google.com/pubsub/docs/samples/pubsub-subscriber-error-listener#code-sample */
#include <google/cloud/pubsub/publisher.h>
#include <google/cloud/storage/client.h>
#include <google/cloud/pubsub/subscriber.h>
#include <google/cloud/pubsub/subscription_admin_client.h>
#include <google/cloud/pubsub/topic_admin_client.h>
#include <nlohmann/json.hpp>
void createTopic(const std::string& topicId, const std::string& dlTopicId, google::cloud::pubsub::TopicAdminClient& client)
{
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string project_id, std::string topic_id, std::string dl_topic_id)
{
auto topic = client.CreateTopic(pubsub::TopicBuilder(pubsub::Topic(project_id, topic_id)));
// Note that kAlreadyExists is a possible error when the library retries.
if (topic.status().code() == google::cloud::StatusCode::kAlreadyExists)
{
;
}
else if (!topic)
{
throw std::runtime_error(topic.status().message());
}
else
{
LOGn << "The topic " << topic_id << " was successfully created: " << topic->DebugString();
}
auto dl_topic = client.CreateTopic(pubsub::TopicBuilder(pubsub::Topic(project_id, dl_topic_id)));
// Note that kAlreadyExists is a possible error when the library retries.
if (dl_topic.status().code() == google::cloud::StatusCode::kAlreadyExists)
{
;
}
else if (!dl_topic)
{
throw std::runtime_error(dl_topic.status().message());
}
else
{
LOGn << "The deadletter topic " << dl_topic_id << " was successfully created: " << dl_topic->DebugString();
}
}
(client, pubSubProjectId, topicId, dlTopicId);
}
void createOrderingSubscription(const std::string& topicId, const std::string& subscriptionId, const std::string& dlTopicId, const std::string& dlSubscriptionId, google::cloud::pubsub::SubscriptionAdminClient& client)
{
namespace pubsub = ::google::cloud::pubsub;
return [](pubsub::SubscriptionAdminClient client, std::string const& project_id, std::string const& topic_id, std::string const& subscription_id, std::string const& dl_topic_id, std::string const& dl_subscription_id)
{
static const constexpr int DEAD_LETTER_DELIVERY_ATTEMPTS = 5;
auto sub = client.CreateSubscription(
pubsub::Topic(project_id, std::move(topic_id)),
pubsub::Subscription(project_id, subscription_id),
pubsub::SubscriptionBuilder{}
.enable_message_ordering(true)
.set_ack_deadline(std::chrono::seconds(60))
.set_dead_letter_policy(
pubsub::SubscriptionBuilder::MakeDeadLetterPolicy(
pubsub::Topic(project_id, dl_topic_id),
DEAD_LETTER_DELIVERY_ATTEMPTS))
);
if (sub.status().code() == google::cloud::StatusCode::kAlreadyExists)
{
;
}
else if (!sub)
{
throw std::runtime_error(sub.status().message());
}
else
{
LOGd << "The subscription " << subscription_id << " was successfully created: " << sub->DebugString();
}
auto dl_sub = client.CreateSubscription(
pubsub::Topic(project_id, std::move(dl_topic_id)),
pubsub::Subscription(project_id, dl_subscription_id)
);
if (dl_sub.status().code() == google::cloud::StatusCode::kAlreadyExists)
{
;
}
else if (! dl_sub)
{
throw std::runtime_error(sub.status().message());
}
else
{
LOGd << "The deadletter subscription " << dl_subscription_id << " was successfully created: " << dl_sub->DebugString();
}
return;
}
(client, pubSubProjectId, topicId, subscriptionId, dlTopicId, dlSubscriptionId);
}
void run()
{
while (running)
{
try
{
namespace pubsub = ::google::cloud::pubsub;;
int concurrency = 1;
pubsub::TopicAdminClient topicClient(pubsub::MakeTopicAdminConnection());
pubsub::SubscriptionAdminClient subscriptionClient(pubsub::MakeSubscriptionAdminConnection());
createTopicSubscriptionId(eId, fId, "", fTopicId, fSubscriptionId, fDLTopicId, fDLSubscriptionId);
createTopic(fTopicId, fDLTopicId, topicClient);
createOrderingSubscription(fTopicId, fSubscriptionId, fDLTopicId, fDLSubscriptionId, subscriptionClient);
fSubscriptionProblem = false;
auto const subscription = pubsub::Subscription(pubSubProjectId, fSubscriptionId);
auto subscriber = pubsub::Subscriber(pubsub::MakeSubscriberConnection(
subscription,
pubsub::SubscriberOptions{}
.set_max_outstanding_messages(concurrency)
.set_max_outstanding_bytes(concurrency * 1024)
.set_max_concurrency(concurrency),
pubsub::ConnectionOptions{}.set_background_thread_pool_size(concurrency))
);
auto session = subscriber.Subscribe([](pubsub::Message const& m, pubsub::AckHandler h) {
ProcessMsg::CommTopic topic = static_cast<ProcessMsg::CommTopic>(std::stoi(m.attributes()["topic"]));
bool g = true;
std::string msg = m.data();
LOGi << "Received message, id " << m.message_id() << ", attempt " << h.delivery_attempt() <<", ordering key '" << m.ordering_key() << "', topic " << ProcessMsg::commTopic(topic) << " (" << topic << ")";
messageCallback(msg);
std::move(h).ack();
})
.then([](::google::cloud::future<google::cloud::Status> f) {
LOGe << "Subscription problem : " << f.get();
fSubscriptionProblem = true;
});
if (! session.valid())
{
LOGe << "Subscribe session invalid";
break;
}
while (running && ! fSubscriptionProblem)
{
if (outgoingQueue.empty())
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
continue;
}
std::string gTopicId = "";
std::string gSubscriptionId = "";
std::string gDLTopicId = "";
std::string gDLSubscriptionId = "";
const OutgoingMsg omsg = outgoingQueue.front();
createTopicSubscriptionId(0, 0, omsg.identifier, gTopicId, gSubscriptionId, gDLTopicId, gDLSubscriptionId);
createTopic(gTopicId, gDLTopicId, topicClient);
createOrderingSubscription(gTopicId, gSubscriptionId, gDLTopicId, gDLSubscriptionId, subscriptionClient);
try
{
auto publisher = pubsub::Publisher(pubsub::MakePublisherConnection(pubsub::Topic(pubSubProjectId, gTopicId)));
auto id = publisher.Publish(pubsub::MessageBuilder{}
.SetData(omsg.msg)
.SetAttribute("timestamp", std::to_string(time(NULL)))
.Build()).get();
if (! id)
{
LOGe << "Failed to publish message " << id.status().message();
}
else
{
LOGd << "Published message, id : " << *id << " (" << omsg.msg << ") to " << pubSubProjectId << "/" << gTopicId;
outgoingQueue.pop_front();
}
}
catch (std::exception const& e)
{
LOGe << "Failed to publish message " << e.what();
}
}
if (running)
{
LOGn << "GCP problem, cancelling session";
session.cancel();
LOGd << "Waiting 15 seconds";
std::this_thread::sleep_for(std::chrono::seconds(15));
}
}
catch (std::runtime_error &e)
{
LOGe << "RuntimeError exception : " << e.what();
}
catch(std::exception& e)
{
LOGe << "Exception : " << e.what();
}
}
}

Unknown error in inserting values in multimap

I have the following code, where I try to insert values into a multimap of 2 strings, but I keep getting an error that I cannot understand. I've been trying to solve this for hours.
The whole point of the program is to sort the lines of a dictionary based on the automatic sorting of the multimap insertion.
// sort_entries_of_multiple_dictionaries.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <iomanip>
#include <sstream>
// Prototypes
int indexDict(std::multimap<std::string, std::string>& dict);
int main()
{
std::multimap<std::string, std::string> dict;
if(indexDict(dict) == 0)
return 0;
}
int indexDict(std::multimap<std::string, std::string>& dict)
{
std::ifstream inputFile{ "output.txt", std::ios::in };
std::string currentDictEntry{};
size_t currentLine{};
if (!inputFile)
{
std::cerr << "input.txt FILE NOT FOUND in the current directory" << std::endl;
system("pause");
return 0;
}
while (std::getline(inputFile, currentDictEntry))
{
//std::cout << currentDictEntry << std::endl; // TO DELETE
std::string currentWord{};
size_t delimiterPos = currentDictEntry.find('\t', 0);
if (delimiterPos == std::string::npos)
std::cerr << "ERROR. Delimiter \"<b>\" not found in line " << currentLine << std::endl;
else
{
//std::cout << "pos of \\t = " << delimiterPos << std::endl; // TO DELETE
for (char& ch : currentDictEntry)
{
if (ch != '\t')
{
currentWord += ch;
}
else
break;
}
std::cout << currentWord /* << '|' */ << std::endl; // TO DELETE
auto value = currentDictEntry.substr(delimiterPos, std::string::npos);
std::cout << "size= " << value.size() << '|' << value << std::endl;
dict.insert( currentWord, currentWord/*, value*/ );
}
if (currentLine == 50) return 0; // TO DELETE
currentLine++;
}
return 1;
}
if (currentLine == 50) return 0; // TO DELETE
currentLine++;
}
return 1;
}
The error I keep getting is:
unary '++': '_Iter' does not define this operator or a conversion to a type acceptable to the predefined operator
illegal indirection
as #Evg said, it accepts a std::pair
dict.insert(std::make_pair(currentWord, value));
if I understand your intention correctly, you don't want to save the \t into your result, so add 1 after delimiterPos to get the correct value:
auto value = currentDictEntry.substr(delimiterPos + 1, std::string::npos);
test run. output.txt:
4 d
1 a
2 b
3 c
0
output:
"0" - ""
"1" - "a"
"2" - "b"
"3" - "c"
"4" - "d"
full code:
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <iomanip>
#include <sstream>
// Prototypes
int indexDict(std::multimap<std::string, std::string>& dict);
int main()
{
std::multimap<std::string, std::string> dict;
if (indexDict(dict) == 0)
return 0;
for (auto& i : dict) {
std::cout << "\"" << i.first << "\" - \"" << i.second << "\"\n";
}
}
int indexDict(std::multimap<std::string, std::string>& dict)
{
std::ifstream inputFile{ "output.txt", std::ios::in };
std::string currentDictEntry{};
size_t currentLine{};
if (!inputFile)
{
std::cerr << "output.txt FILE NOT FOUND in the current directory" << std::endl;
system("pause");
return 0;
}
while (std::getline(inputFile, currentDictEntry))
{
//std::cout << currentDictEntry << std::endl; // TO DELETE
std::string currentWord{};
size_t delimiterPos = currentDictEntry.find('\t', 0);
if (delimiterPos == std::string::npos)
std::cerr << "ERROR. Delimiter \"<b>\" not found in line " << currentLine << std::endl;
else
{
//std::cout << "pos of \\t = " << delimiterPos << std::endl; // TO DELETE
for (char& ch : currentDictEntry)
{
if (ch != '\t')
{
currentWord += ch;
}
else
break;
}
std::cout << currentWord /* << '|' */ << std::endl; // TO DELETE
auto value = currentDictEntry.substr(delimiterPos + 1, std::string::npos);
std::cout << "size= " << value.size() << '|' << value << std::endl;
dict.insert(std::make_pair(currentWord, value));
}
if (currentLine == 50) return 0; // TO DELETE
currentLine++;
}
return 1;
}
small mistakes in your code: you don't need <algorithm> and <vector>. also your error message said input.txt instead of output.txt.
I Change dict.insert( currentWord, currentWord/*, value*/ ); To dict.insert({ currentWord,currentWord }); and error Has solved

Boost ASIO - Unable to read packet header from body

I use Boost ASIO and I would like to separate header from body message.
Here is my opcode enum:
enum ServerOpcode : uint16_t{
SMSG_AUTH_CONNECTION_RESPONSE = 0x001,
SMSG_LOGIN_REQUEST_RESPONSE = 0x002,
SMSG_LOGIN_REQUEST_RESPONSE_TEST = 0x1A6,
};
Here is how I send message:
boost::asio::io_context io_context;
std::vector<tcp::socket> sockets;
for (int i = 0; i < connectionsNumber; ++i) {
try
{
sockets.emplace_back(io_context);
tcp::socket& s{sockets.back()};
tcp::resolver resolver(io_context);
boost::asio::connect(s, resolver.resolve( defaultIP,config.GetConfigValue("AuthServerPort", defaultPort)));
enum { body_length = 1024 };
enum { header_length = 8 };
enum { max_length = body_length + header_length};
char header_[header_length];
char body_[body_length];
char data_[header_length + body_length];
ServerOpcode opc;
opc = ServerOpcode::SMSG_LOGIN_REQUEST_RESPONSE_TEST;
std::string message = "I am testing here!!!";
snprintf(header_,header_length,"%x",opc);
strcpy(body_, message.c_str());
sprintf(data_, "%s %s", header_, body_);
size_t request_length = sizeof(data_)/sizeof(*data_);
std::cout << "Header: " << header_ << std::endl;
std::cout << "body: " << body_ << std::endl;
std::cout << "whole message is: " << data_ << std::endl;
std::cout << "size: " << request_length << std::endl;
std::cout << "max size: " << max_length << std::endl;
boost::asio::write(s, boost::asio::buffer(data_, request_length));
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
}
Here is how I read it on the server:
void Vibranium::Client::read_header() {
auto self(shared_from_this());
boost::asio::async_read(socket,
boost::asio::buffer(_packet.data_, _packet.header_length),
[this, self](boost::system::error_code ec, std::size_t /*length*/)
{
if (!ec)
{
std::cout << "Header: " << std::endl;
std::cout.write(_packet.data_, _packet.header_length);
std::cout << "\n";
//read_body();
}
else
{
std::cerr << "Invalid header sent!" << std::endl;
}
});
}
Packet.h:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "string"
#include "Protocol/ServerOpcode.h"
class Packet {
public:
Packet() : body_length_(0)
{
}
enum { max_body_length = 1024 };
enum { header_length = 8 };
char header_[header_length];
char body_[max_body_length];
char data_[header_length + max_body_length];
char * body();
void PreparePacket(ServerOpcode serverOpcode, std::string message);
std::size_t body_length() const;
private:
std::size_t body_length_;
};
#endif //VIBRANIUM_CORE_PACKET_H
However I see output as:
New Connection (ID: 4)
Header:
1a6 I am
That is not correct! Why Header: is showing 1a6 I am? Where is my mistake?
I am expecting it to be just 1a6. How can I fix it?
In this line after reading:
std::cout.write(_packet.data_, _packet.header_length);
header_length is 8 = length of '1a6 I am'
Your header length should be 3 (not 8), if you want to have '1a6' only.
This line: snprintf(header_,header_length,"%x",opc); should have a newline character at the end of it if you're going to print it. If that's the case, you should extend your buffers by one to allow for a newline. Or better yet, print the expected characters and specify the specific length whenever they need to be printed (as you're doing on the server):
std::cout << "Header: " << std::endl;
std::cout.write(_packet.data_, _packet.header_length);
std::cout << "\n";
As the code is written now, the output continues (in contiguous memory), until the next newline character is hit.
Additionally, I'd recommend using vector<uint_8> instead of arrays as they allow for bounds checking and allow for default moveable/copyable semantics for the Packet type. You may also consider using the boost::asio::buffer type.

Boost ASIO async_read is not reading data from the server

I have a server/client application which works for a write from client to a read at server.
After the sever is done receiving the data in the function read_async_1, it writes a simple string "Response" at the end.
Now, this is not received at the client. In the client code, StartHandlingServer is what does an async read.. Now, the handler inside this is not getting called..
Can someone please have a look at this ? Appreciate your feedback.
Server Code
#include <boost/asio.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/bind.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/tuple/tuple.hpp>
#include <thread>
#include <atomic>
#include <memory>
#include <iostream>
#include "../stocks.hpp"
using namespace boost;
class Service {
public:
Service(){}
void StartHandligClient(boost::shared_ptr<asio::ip::tcp::socket> sock)
{
std::cout << "StartHandligClient : sock.use_count : " << sock.use_count() << "\n";
read_async_1(sock);
return;
}
private:
void read_async_1(boost::shared_ptr<asio::ip::tcp::socket> sock)
{
if(!(*sock.get()).is_open())
{
std::cout << getpid() << " : Socket closed in sync_read \n" << std::flush;
return ;
}
std::cout << "haha_1\n" << std::flush;
boost::asio::async_read( (*sock.get()), boost::asio::buffer(inbound_header_),
[this, sock](boost::system::error_code ec,
size_t bytesRead)
{
int headerBytesReceived = bytesRead;
std::cout << "\n\n headerBytesReceived : " << headerBytesReceived << "\n" << std::flush ;
if (!ec)
{
// Determine the length of the serialized data.
std::istringstream is(std::string(inbound_header_, header_length));
std::cout << "is : +" << is.str() << "+, inbound_header_ : +" << inbound_header_ << "+\n";
std::size_t inbound_data_size = 0;
if (!(is >> std::hex >> inbound_data_size))
{
// Header doesn't seem to be valid. Inform the caller.
// boost::system::error_code error(boost::asio::error::invalid_argument);
// boost::get<0>(handler)(error);
std::cout << "RET-1 \n";
return;
}
std::cout << "inbound_data_size : " << inbound_data_size << "\n" << std::flush;
// Start an asynchronous call to receive the data.
inbound_data_.resize(inbound_data_size);
std::cout << "inbound_data_.size() : " << inbound_data_.size() << "\n" << std::flush;
int bytesReceived = asio::read( *sock.get(), boost::asio::buffer(inbound_data_) );
std::string archive_data(&inbound_data_[0], inbound_data_.size());
std::istringstream archive_stream(archive_data);
boost::archive::text_iarchive archive(archive_stream);
archive >> stocks_;
std::cout << "bytesReceived : " << bytesReceived << " , stocks_.size() : " << stocks_.size() << "\n";
// Print out the data that was received.
for (std::size_t i = 0; i < stocks_.size(); ++i)
{
std::cout << "Stock number " << i << "\n";
std::cout << " code: " << stocks_[i].code << "\n";
std::cout << " name: " << stocks_[i].name << "\n";
std::cout << " open_price: " << stocks_[i].open_price << "\n";
std::cout << " high_price: " << stocks_[i].high_price << "\n";
std::cout << " low_price: " << stocks_[i].low_price << "\n";
std::cout << " last_price: " << stocks_[i].last_price << "\n";
std::cout << " buy_price: " << stocks_[i].buy_price << "\n";
std::cout << " buy_quantity: " << stocks_[i].buy_quantity << "\n";
std::cout << " sell_price: " << stocks_[i].sell_price << "\n";
std::cout << " sell_quantity: " << stocks_[i].sell_quantity << "\n";
}
sleep(1);
// Sending response.
std::string response = "Response\n";
asio::write(*sock.get(), asio::buffer(response));
this->read_async_1(sock);
}
else
{
// Terminate connection ?
if(ec == boost::asio::error::eof)
{
std::cout << getpid() << " : ** sync_read : Connection lost : boost::asio::error::eof ** \n";
}
std::cout << "Error occured in async_read! Error code = " << ec.value() << ". Message: " << ec.message() << "\n" << std::flush;
return ;
}
return ;
}
);
std::cout << getpid() << " : final return from async_read \n" << std::flush;
return ;
}
/// The size of a fixed length header.
enum { header_length = 8 };
/// Holds an outbound header.
std::string outbound_header_;
/// Holds the outbound data.
std::string outbound_data_;
/// Holds an inbound header.
char inbound_header_[header_length];
/// Holds the inbound data.
std::vector<char> inbound_data_;
std::vector<stock> stocks_;
};
class Acceptor {
public:
Acceptor(asio::io_service& ios, unsigned short port_num) :
m_ios(ios),
m_acceptor(m_ios,
asio::ip::tcp::endpoint(
asio::ip::address_v4::any(),
port_num))
{
m_acceptor.listen();
}
void Accept() {
std::cout << "Server Accept() \n" << std::flush;
boost::shared_ptr<asio::ip::tcp::socket> sock(new asio::ip::tcp::socket(m_ios));
m_acceptor.accept(*sock.get());
(new Service)->StartHandligClient(sock);
std::cout << "Accept : sock.use_count : " << sock.use_count() << "\n";
}
private:
asio::io_service& m_ios;
asio::ip::tcp::acceptor m_acceptor;
};
class Server {
public:
Server() : m_stop(false) {}
void Start(unsigned short port_num) {
m_thread.reset(new std::thread([this, port_num]() {
Run(port_num);
}));
}
void Stop() {
std::cout << "STOPPING \n";
m_stop.store(true);
m_thread->join();
}
private:
void Run(unsigned short port_num) {
Acceptor acc(m_ios, port_num);
while (!m_stop.load())
{
std::cout << "Server accept\n" << std::flush;
acc.Accept();
m_ios.run();
}
}
std::unique_ptr<std::thread> m_thread;
std::atomic<bool> m_stop;
asio::io_service m_ios;
};
int main()
{
unsigned short port_num = 3333;
try {
Server srv;
srv.Start(port_num);
std::this_thread::sleep_for(std::chrono::seconds(100));
std::cout << "Stopping server \n";
srv.Stop();
}
catch (system::system_error &e) {
std::cout << "Error occured! Error code = "
<< e.code() << ". Message: "
<< e.what();
}
return 0;
}
Client Code
#include <boost/asio.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/bind.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include "../stocks.hpp"
using namespace boost;
class mysock : public boost::asio::ip::tcp::socket
{
public:
mysock(asio::io_service& serv) : boost::asio::ip::tcp::socket(serv)
{
}
~mysock()
{
std::cout << "Inside destructor for mysock \n";
}
};
class SyncTCPClient {
public:
SyncTCPClient(const std::string& raw_ip_address,
unsigned short port_num) :
socket_((new mysock(m_ios))),
m_ep(asio::ip::address::from_string(raw_ip_address), port_num)
{
(*socket_.get()).open(m_ep.protocol());
connect();
StartHandlingServer(socket_);
}
mysock& socket()
{
return *socket_.get();
}
void connect() {
(*socket_.get()).connect(m_ep);
m_ios.run();
}
void StartHandlingServer(boost::shared_ptr<mysock> sock)
{
if(!(*sock.get()).is_open())
{
std::cout << getpid() << " : Socket closed in sync_read \n" << std::flush;
return ;
}
std::cout << "Start StartHandlingServer\n" << std::flush;
char inbound_header_[4];;
try
{
boost::asio::async_read( (*sock.get()), boost::asio::buffer(inbound_header_),
[this, sock](boost::system::error_code ec,
size_t bytesRead)
{
int headerBytesReceived = bytesRead;
std::cout << "\n\n headerBytesReceived : " << headerBytesReceived << "\n" << std::flush ;
if (!ec)
{
}
else
{
if(ec == boost::asio::error::eof)
{
std::cout << getpid() << " : ** sync_read : Connection lost : boost::asio::error::eof ** \n";
}
std::cout << "Error occured in async_read! Error code = " << ec.value() << ". Message: " << ec.message() << "\n" << std::flush;
return ;
}
}
);
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
std::cout << "Done StartHandlingServer\n" << std::flush;
}
void close() {
(*socket_.get()).shutdown(
boost::asio::ip::tcp::socket::shutdown_both);
(*socket_.get()).close();
}
std::string emulateLongComputationOp(
unsigned int duration_sec) {
std::string request = "EMULATE_LONG_COMP_OP "
+ std::to_string(duration_sec)
+ "\n";
sendRequest(request);
/*
sleep(2);
sendRequest(request);
sleep(2);
sendRequest(request);
*/
return receiveResponse();
};
private:
void sendRequest(const std::string& request) {
std::vector<stock> stocks_;
// Create the data to be sent to each client.
stock s;
s.code = "ABC";
s.name = "A Big Company";
s.open_price = 4.56;
s.high_price = 5.12;
s.low_price = 4.33;
s.last_price = 4.98;
s.buy_price = 4.96;
s.buy_quantity = 1000;
s.sell_price = 4.99;
s.sell_quantity = 2000;
stocks_.push_back(s);
// Serialize the data first so we know how large it is.
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);
archive << stocks_;
outbound_data_ = archive_stream.str();
std::cout << "outbound_data_ : " << outbound_data_ << "\n" << std::flush;
std::cout << "outbound_data_.size() : " << outbound_data_.size() << "\n" << std::flush;
// Format the header.
std::ostringstream header_stream;
header_stream << std::setw(header_length) << std::hex << outbound_data_.size();
std::cout << "header_stream.str() : " << header_stream.str() << "\n" << std::flush;
std::cout << "header_stream.str().size() : " << header_stream.str().size() << "\n" << std::flush;
if (!header_stream || header_stream.str().size() != header_length)
{
// Something went wrong, inform the caller.
// boost::system::error_code error(boost::asio::error::invalid_argument);
// socket_.get_io_service().post(boost::bind(handler, error));
return;
}
outbound_header_ = header_stream.str();
std::cout << "outbound_header_ : +" << outbound_header_ << "+\n" << std::flush;
// Write the serialized data to the socket. We use "gather-write" to send
// both the header and the data in a single write operation.
/*
std::vector<boost::asio::const_buffer> buffers;
buffers.push_back(boost::asio::buffer(outbound_header_));
buffers.push_back(boost::asio::buffer(outbound_data_));
*/
std::size_t headerSize = asio::write(*socket_.get(), boost::asio::buffer(outbound_header_));
std::size_t dataSize = asio::write(*socket_.get(), boost::asio::buffer(outbound_data_));
std::cout << "headerSize : " << headerSize << " , dataSize : " << dataSize;
}
std::string receiveResponse() {
std::string response;
/*
asio::streambuf buf;
asio::read_until(*socket_.get(), buf, '\n');
std::istream input(&buf);
std::getline(input, response);
*/
return response;
}
private:
asio::io_service m_ios;
boost::shared_ptr<mysock> socket_;
asio::ip::tcp::endpoint m_ep;
enum { header_length = 8 };
std::string outbound_data_;
std::string outbound_header_;
};
int main()
{
const std::string raw_ip_address = "127.0.0.1";
const unsigned short port_num = 3333;
try {
SyncTCPClient client(raw_ip_address, port_num);
std::cout << "Sending request to the server... \n"<< std::endl;
std::string response = client.emulateLongComputationOp(10);
std::cout << "\nResponse received: " << response << std::endl;
sleep(10);
std::cout << "\n\n Closing client connection \n\n";
// Close the connection and free resources.
client.close();
}
catch (system::system_error &e) {
std::cout << "Client Error occured! Error code = " << e.code()
<< ". Message: " << e.what();
return e.code().value();
}
return 0;
}
Included file stocks.hpp
#ifndef _STOCKS_HPP_
#define _STOCKS_HPP_
struct stock
{
std::string code;
std::string name;
double open_price;
double high_price;
double low_price;
double last_price;
double buy_price;
int buy_quantity;
double sell_price;
int sell_quantity;
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & code;
ar & name;
ar & open_price;
ar & high_price;
ar & low_price;
ar & last_price;
ar & buy_price;
ar & buy_quantity;
ar & sell_price;
ar & sell_quantity;
}
};
#endif
The io_service::run function runs until there are no more events.
In the client you run it once, when there are no active events to be handled, which means it will return immediately.
Because the io_server isn't "running" it will not handle any events.
You need to call run (or poll) in a loop, like you do in the server.

Boost ASIO async_read is not reading data from the client

I have a server/client application which works for a write from client to a read at server.
Inside the startHandlig function in the server code, if I comment async_connect_1 and the return after it, then it works fine which involves sync write function.
I added async_connect_1 function inside Service() class to asynchronously read from the socket.
This function is called when a client connects to the server and this function returns immediately.
I expect the callback function corresponding to async_read to be called, but that is not happening...
I'm stuck at this since a long time.. Appreciate help on this...
Server Code
#include <boost/asio.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/bind.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/tuple/tuple.hpp>
#include <thread>
#include <atomic>
#include <memory>
#include <iostream>
#include "../stocks.hpp"
using namespace boost;
class Service {
public:
Service(){}
void StartHandligClient(
std::shared_ptr<asio::ip::tcp::socket> sock) {
read_async_1(sock);
return;
std::thread th(([this, sock]() {
HandleClient(sock);
}));
std::cout << "Detached \n";
th.detach();
}
private:
void read_async_1(std::shared_ptr<asio::ip::tcp::socket> sock)
{
if(!(*sock.get()).is_open())
{
std::cout << getpid() << " : Socket closed in sync_read \n" << std::flush;
return ;
}
std::cout << "haha_1\n" << std::flush;
boost::asio::async_read( (*sock.get()), boost::asio::buffer(inbound_header_),
[this](boost::system::error_code ec,
size_t bytesRead)
{
std::cout << "haha_2\n" << std::flush;
if (!ec)
{
int headerBytesReceived = bytesRead;
std::cout << "\n\n headerBytesReceived : " << headerBytesReceived << "\n" << std::flush ;
// this->async_read(sock);
}
else
{
// Terminate connection ?
if(ec == boost::asio::error::eof)
{
std::cout << getpid() << " : ** sync_read : Connection lost : boost::asio::error::eof ** \n";
}
std::cout << "Error occured in sync_read! Error code = " << ec.value() << ". Message: " << ec.message() << "\n" << std::flush;
return ;
}
return ;
}
);
std::cout << getpid() << " : final return from async_read \n" << std::flush;
return ;
}
void HandleClient(std::shared_ptr<asio::ip::tcp::socket> sock) {
while(1)
{
try {
// asio::streambuf request;
// asio::read_until(*sock.get(), request, '\n');
int headerBytesReceived = asio::read( *sock.get(), boost::asio::buffer(inbound_header_) );
std::cout << "headerBytesReceived : " << headerBytesReceived << "\n" << std::flush;
// Determine the length of the serialized data.
std::istringstream is(std::string(inbound_header_, header_length));
std::cout << "is : " << is.str() << ", inbound_header_ : " << inbound_header_ << "\n";
std::size_t inbound_data_size = 0;
if (!(is >> std::hex >> inbound_data_size))
{
// Header doesn't seem to be valid. Inform the caller.
// boost::system::error_code error(boost::asio::error::invalid_argument);
// boost::get<0>(handler)(error);
std::cout << "RET-1 \n";
return;
}
std::cout << "inbound_data_size : " << inbound_data_size << "\n" << std::flush;
// Start an asynchronous call to receive the data.
inbound_data_.resize(inbound_data_size);
std::cout << "inbound_data_.size() : " << inbound_data_.size() << "\n" << std::flush;
int bytesReceived = asio::read( *sock.get(), boost::asio::buffer(inbound_data_) );
std::string archive_data(&inbound_data_[0], inbound_data_.size());
std::istringstream archive_stream(archive_data);
boost::archive::text_iarchive archive(archive_stream);
archive >> stocks_;
std::cout << "bytesReceived : " << bytesReceived << " , stocks_.size() : " << stocks_.size() << "\n";
// Print out the data that was received.
for (std::size_t i = 0; i < stocks_.size(); ++i)
{
std::cout << "Stock number " << i << "\n";
std::cout << " code: " << stocks_[i].code << "\n";
std::cout << " name: " << stocks_[i].name << "\n";
std::cout << " open_price: " << stocks_[i].open_price << "\n";
std::cout << " high_price: " << stocks_[i].high_price << "\n";
std::cout << " low_price: " << stocks_[i].low_price << "\n";
std::cout << " last_price: " << stocks_[i].last_price << "\n";
std::cout << " buy_price: " << stocks_[i].buy_price << "\n";
std::cout << " buy_quantity: " << stocks_[i].buy_quantity << "\n";
std::cout << " sell_price: " << stocks_[i].sell_price << "\n";
std::cout << " sell_quantity: " << stocks_[i].sell_quantity << "\n";
}
}
catch (system::system_error &e)
{
boost::system::error_code ec = e.code();
if(ec == boost::asio::error::eof)
{
std::cout << "EOF Error \n";
}
std::cout << "Server Error occured! Error code = "
<< e.code() << ". Message: "
<< e.what() << "\n";
break;
}
}
// Clean-up.
delete this;
}
/// The size of a fixed length header.
enum { header_length = 8 };
/// Holds an outbound header.
std::string outbound_header_;
/// Holds the outbound data.
std::string outbound_data_;
/// Holds an inbound header.
char inbound_header_[header_length];
/// Holds the inbound data.
std::vector<char> inbound_data_;
std::vector<stock> stocks_;
};
class Acceptor {
public:
Acceptor(asio::io_service& ios, unsigned short port_num) :
m_ios(ios),
m_acceptor(m_ios,
asio::ip::tcp::endpoint(
asio::ip::address_v4::any(),
port_num))
{
m_acceptor.listen();
}
void Accept() {
std::cout << "Server Accept() \n" << std::flush;
std::shared_ptr<asio::ip::tcp::socket>
sock(new asio::ip::tcp::socket(m_ios));
m_acceptor.accept(*sock.get());
(new Service)->StartHandligClient(sock);
}
private:
asio::io_service& m_ios;
asio::ip::tcp::acceptor m_acceptor;
};
class Server {
public:
Server() : m_stop(false) {}
void Start(unsigned short port_num) {
m_thread.reset(new std::thread([this, port_num]() {
Run(port_num);
}));
}
void Stop() {
m_stop.store(true);
m_thread->join();
}
private:
void Run(unsigned short port_num) {
Acceptor acc(m_ios, port_num);
while (!m_stop.load()) {
std::cout << "Server accept\n" << std::flush;
acc.Accept();
}
}
std::unique_ptr<std::thread> m_thread;
std::atomic<bool> m_stop;
asio::io_service m_ios;
};
int main()
{
unsigned short port_num = 3333;
try {
Server srv;
srv.Start(port_num);
std::this_thread::sleep_for(std::chrono::seconds(100));
std::cout << "Stopping server \n";
srv.Stop();
}
catch (system::system_error &e) {
std::cout << "Error occured! Error code = "
<< e.code() << ". Message: "
<< e.what();
}
return 0;
}
Client Code
#include <boost/asio.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/bind.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include "../stocks.hpp"
using namespace boost;
class SyncTCPClient {
public:
SyncTCPClient(const std::string& raw_ip_address,
unsigned short port_num) :
m_ep(asio::ip::address::from_string(raw_ip_address),
port_num),
m_sock(m_ios) {
m_sock.open(m_ep.protocol());
}
void connect() {
m_sock.connect(m_ep);
}
void close() {
m_sock.shutdown(
boost::asio::ip::tcp::socket::shutdown_both);
m_sock.close();
}
std::string emulateLongComputationOp(
unsigned int duration_sec) {
std::string request = "EMULATE_LONG_COMP_OP "
+ std::to_string(duration_sec)
+ "\n";
sendRequest(request);
return receiveResponse();
};
private:
void sendRequest(const std::string& request) {
std::vector<stock> stocks_;
// Create the data to be sent to each client.
stock s;
s.code = "ABC";
s.name = "A Big Company";
s.open_price = 4.56;
s.high_price = 5.12;
s.low_price = 4.33;
s.last_price = 4.98;
s.buy_price = 4.96;
s.buy_quantity = 1000;
s.sell_price = 4.99;
s.sell_quantity = 2000;
stocks_.push_back(s);
// Serialize the data first so we know how large it is.
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);
archive << stocks_;
outbound_data_ = archive_stream.str();
std::cout << "outbound_data_ : " << outbound_data_ << "\n" << std::flush;
std::cout << "outbound_data_.size() : " << outbound_data_.size() << "\n" << std::flush;
// Format the header.
std::ostringstream header_stream;
header_stream << std::setw(header_length) << std::hex << outbound_data_.size();
std::cout << "header_stream.str() : " << header_stream.str() << "\n" << std::flush;
std::cout << "header_stream.str().size() : " << header_stream.str().size() << "\n" << std::flush;
if (!header_stream || header_stream.str().size() != header_length)
{
// Something went wrong, inform the caller.
// boost::system::error_code error(boost::asio::error::invalid_argument);
// socket_.get_io_service().post(boost::bind(handler, error));
return;
}
outbound_header_ = header_stream.str();
std::cout << "outbound_header_ : " << outbound_header_ << "\n" << std::flush;
// Write the serialized data to the socket. We use "gather-write" to send
// both the header and the data in a single write operation.
std::vector<boost::asio::const_buffer> buffers;
buffers.push_back(boost::asio::buffer(outbound_header_));
buffers.push_back(boost::asio::buffer(outbound_data_));
std::size_t sizeSent = asio::write(m_sock, buffers);
std::cout << "sizeSent : " << sizeSent << "\n" << std::flush;
}
std::string receiveResponse() {
std::string response;
/*
asio::streambuf buf;
asio::read_until(m_sock, buf, '\n');
std::istream input(&buf);
std::getline(input, response);
*/
return response;
}
private:
asio::io_service m_ios;
asio::ip::tcp::endpoint m_ep;
asio::ip::tcp::socket m_sock;
enum { header_length = 8 };
std::string outbound_data_;
std::string outbound_header_;
};
int main()
{
const std::string raw_ip_address = "127.0.0.1";
const unsigned short port_num = 3333;
try {
SyncTCPClient client(raw_ip_address, port_num);
// Sync connect.
client.connect();
sleep(1);
std::cout << "Sending request to the server... "
<< std::endl;
std::string response = client.emulateLongComputationOp(10);
std::cout << "Response received: " << response << std::endl;
sleep(100);
std::cout << "\n\n Closing client connection \n\n";
// Close the connection and free resources.
client.close();
}
catch (system::system_error &e) {
std::cout << "Client Error occured! Error code = " << e.code()
<< ". Message: " << e.what();
return e.code().value();
}
return 0;
}
Included File (stocks.hpp)
#ifndef _STOCKS_HPP_
#define _STOCKS_HPP_
struct stock
{
std::string code;
std::string name;
double open_price;
double high_price;
double low_price;
double last_price;
double buy_price;
int buy_quantity;
double sell_price;
int sell_quantity;
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & code;
ar & name;
ar & open_price;
ar & high_price;
ar & low_price;
ar & last_price;
ar & buy_price;
ar & buy_quantity;
ar & sell_price;
ar & sell_quantity;
}
};
#endif
You have written Error code = 125. Message: Operation canceled as comment in previous response, i think that socket may be closed before async operation will be done.
What is lifetime of your socket ?
[1] socket is created in Accept method
std::shared_ptr<asio::ip::tcp::socket>
sock(new asio::ip::tcp::socket(m_ios)); // ref count +1
//...
(new Service)->StartHandligClient(sock); // this function returns immediately
// so socket's ref count -1
[2] in StartHandligClient()
sock is passed by value, so ref count of socket +1, but
void StartHandligClient(
std::shared_ptr<asio::ip::tcp::socket> sock) { // +1 ref count
read_async_1(sock); // this function returns immediately
return; // -1 ref count of socket
}
[3] in read_async_1 socket is passed by value, +1 on ref count of socket, but this function returns immediately, when function ends, ref count is decreased and socket object is deleted.
You created lambda object to execute asynchronus operation, but socket object may be closed before doing it.
You did apparently use a asio::io_service, but you forgot to run it.
m_ios.run();
Run the io_context object's event processing loop.
Fix this and your handler[s] will be called.
You can either create a thread for this, or call it in your main function in your 'main-thread'.
std::thread([this]() { m_ios.run(); } );
Note: Don't forget to stop (1) it later and join the thread (2) if you created one.