Commit 0c6d1256 authored by Martin Quinson's avatar Martin Quinson
Browse files

Some sonar fixes

- explicit to a constructor
- prefer automatic memory to explicit new/delete
- some const methods
parent f7864d1c
Pipeline #400626 passed with stages
in 4 minutes and 53 seconds
......@@ -23,14 +23,13 @@ void write_message(const Message & message, rsg::TcpSocket & socket)
// Generate message buffer (header + content).
const uint32_t content_size = message.ByteSizeLong();
const uint32_t message_size = content_size + 4;
auto message_content = new uint8_t[message_size]();
uint8_t message_content[message_size];
memcpy(message_content, &content_size, 4); // TODO: set endianness
bool serialize_ok = message.SerializeToArray(message_content + 4, content_size);
RSG_ENFORCE(serialize_ok, "Could not serialize Protobuf message");
// Send message on socket.
socket.send_all(message_content, message_size);
delete[] message_content;
}
template <typename Message>
......
......@@ -47,20 +47,18 @@ void rsg::TcpSocket::connect(const std::string & server_address, uint16_t port)
_connected = true;
}
void rsg::TcpSocket::send_all(const uint8_t * buffer, size_t size)
{
RSG_ASSERT(_fd != -1, "Wrong TcpSocket::send_all call: Invalid socket");
RSG_ASSERT(_connected, "Wrong TcpSocket::send_all call: Not connected");
void rsg::TcpSocket::send_all(const uint8_t *buffer, size_t size) const {
RSG_ASSERT(_fd != -1, "Wrong TcpSocket::send_all call: Invalid socket");
RSG_ASSERT(_connected, "Wrong TcpSocket::send_all call: Not connected");
for (size_t sent = 0; sent < size; )
{
errno = 0;
size_t remaining_size = size - sent;
ssize_t bytes_sent = ::send(_fd, buffer + sent, remaining_size, 0);
sent += bytes_sent;
for (size_t sent = 0; sent < size;) {
errno = 0;
size_t remaining_size = size - sent;
ssize_t bytes_sent = ::send(_fd, buffer + sent, remaining_size, 0);
sent += bytes_sent;
RSG_ENFORCE(bytes_sent != -1, "Could not send: %s", strerror(errno));
}
RSG_ENFORCE(bytes_sent != -1, "Could not send: %s", strerror(errno));
}
}
void rsg::TcpSocket::recv_all(uint8_t * buffer, size_t size)
......
......@@ -13,7 +13,7 @@ class TcpSocket
{
public:
TcpSocket();
TcpSocket(int fd);
explicit TcpSocket(int fd);
~TcpSocket();
TcpSocket(const TcpSocket &) = delete;
TcpSocket(const TcpSocket &&) = delete;
......@@ -23,7 +23,7 @@ public:
void connect(const std::string & server_address, uint16_t port);
void disconnect();
void send_all(const uint8_t * buffer, size_t size);
void send_all(const uint8_t *buffer, size_t size) const;
void recv_all(uint8_t * buffer, size_t size);
void recv(uint8_t * buffer, size_t size, size_t & bytes_read);
......
......@@ -68,21 +68,22 @@ rsg::ActorPtr rsg::Actor::create(const std::string & name, const rsg::HostPtr &
// This thread should NEVER run concurrently with the new actor's thread. To do so:
// - A semaphore ensures that this thread waits until the new actor is connected to the server.
// - Once connected, the new actor yields so it is stopped at least until this actor triggers a simulation event.
sem_t * semaphore = new sem_t;
int sem_return = sem_init(semaphore, 0, 0);
sem_t semaphore;
int sem_return = sem_init(&semaphore, 0, 0);
RSG_ENFORCE(sem_return == 0, "Cannot sem_init: %s\n", strerror(errno));
// Create a thread for the newly-created actor.
std::thread * child_thread = new std::thread(actor_wrapper, ack.actorcreate().id(), semaphore, code, code_arg,
before_connect_code, before_connect_code_arg, after_connect_code, after_connect_code_arg);
std::thread *child_thread =
new std::thread(actor_wrapper, ack.actorcreate().id(), &semaphore, code,
code_arg, before_connect_code, before_connect_code_arg,
after_connect_code, after_connect_code_arg);
rsg::connection->add_child_thread(child_thread);
// Wait until the child has connected.
sem_return = sem_wait(semaphore);
sem_return = sem_wait(&semaphore);
RSG_ENFORCE(sem_return == 0, "Cannot sem_wait: %s\n", strerror(errno));
sem_return = sem_destroy(semaphore);
sem_return = sem_destroy(&semaphore);
RSG_ENFORCE(sem_return == 0, "Cannot sem_destroy: %s\n", strerror(errno));
delete semaphore;
return rsg::ActorPtr(new Actor(ack.actorcreate().id()));
}
......@@ -154,17 +155,17 @@ rsg::HostPtr rsg::Actor::get_host()
return rsg::HostPtr(new Host(ack.actorgethost().name()));
}
std::string rsg::Actor::get_name()
{
rsg::pb::Decision decision;
auto actor = new rsg::pb::Actor();
actor->set_id(_id);
decision.set_allocated_actorgetname(actor);
std::string rsg::Actor::get_name() const {
rsg::pb::Decision decision;
auto actor = new rsg::pb::Actor();
actor->set_id(_id);
decision.set_allocated_actorgetname(actor);
rsg::pb::DecisionAck ack;
rsg::connection->send_decision(decision, ack);
RSG_ENFORCE(ack.success(), "Actor(id=%d) does not exist in the simulation", _id);
return ack.actorgetname();
rsg::pb::DecisionAck ack;
rsg::connection->send_decision(decision, ack);
RSG_ENFORCE(ack.success(), "Actor(id=%d) does not exist in the simulation",
_id);
return ack.actorgetname();
}
int rsg::Actor::get_pid() const
......
......@@ -29,7 +29,7 @@ public:
void join(double timeout);
HostPtr get_host();
std::string get_name();
std::string get_name() const;
int get_pid() const;
private:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment