aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/salticidae/msg.h7
-rw-r--r--include/salticidae/network.h6
-rw-r--r--test/bench_network.cpp20
3 files changed, 22 insertions, 11 deletions
diff --git a/include/salticidae/msg.h b/include/salticidae/msg.h
index bc1a633..80b468a 100644
--- a/include/salticidae/msg.h
+++ b/include/salticidae/msg.h
@@ -61,7 +61,8 @@ class MsgBase {
template<typename MsgType,
typename = typename std::enable_if<
!std::is_same<MsgType, MsgBase>::value &&
- !std::is_same<MsgType, uint8_t *>::value>::type>
+ !std::is_same<MsgType, bytearray_t>::value &&
+ !std::is_same<MsgType, DataStream>::value>::type>
MsgBase(const MsgType &msg): magic(0x0) {
set_opcode(MsgType::opcode);
set_payload(std::move(msg.serialized));
@@ -87,15 +88,13 @@ class MsgBase {
payload(std::move(other.payload)),
no_payload(other.no_payload) {}
- MsgBase(const uint8_t *raw_header): no_payload(true) {
+ MsgBase(DataStream &&s): no_payload(true) {
uint32_t _magic;
opcode_t _opcode;
uint32_t _length;
#ifndef SALTICIDAE_NOCHECKSUM
uint32_t _checksum;
#endif
- DataStream s(raw_header, raw_header + MsgBase::header_size);
-
s >> _magic
>> _opcode
>> _length
diff --git a/include/salticidae/network.h b/include/salticidae/network.h
index f6623b6..c7b7f66 100644
--- a/include/salticidae/network.h
+++ b/include/salticidae/network.h
@@ -377,8 +377,7 @@ void MsgNetwork<OpcodeType>::Conn::on_read() {
{
if (recv_buffer.size() < Msg::header_size) break;
/* new header available */
- bytearray_t data = recv_buffer.pop(Msg::header_size);
- msg = Msg(data.data());
+ msg = Msg(recv_buffer.pop(Msg::header_size));
msg_state = Conn::PAYLOAD;
}
if (msg_state == Conn::PAYLOAD)
@@ -386,8 +385,7 @@ void MsgNetwork<OpcodeType>::Conn::on_read() {
size_t len = msg.get_length();
if (recv_buffer.size() < len) break;
/* new payload available */
- bytearray_t data = recv_buffer.pop(len);
- msg.set_payload(std::move(data));
+ msg.set_payload(recv_buffer.pop(len));
msg_state = Conn::HEADER;
#ifndef SALTICIDAE_NOCHECKSUM
if (!msg.verify_checksum())
diff --git a/test/bench_network.cpp b/test/bench_network.cpp
index 19f0dd9..bd31ee0 100644
--- a/test/bench_network.cpp
+++ b/test/bench_network.cpp
@@ -26,6 +26,7 @@
#include <string>
#include <functional>
#include <thread>
+#include <signal.h>
/* disable SHA256 checksum */
#define SALTICIDAE_NOCHECKSUM
@@ -142,6 +143,10 @@ salticidae::EventContext ec;
NetAddr alice_addr("127.0.0.1:1234");
NetAddr bob_addr("127.0.0.1:1235");
+void signal_handler(int) {
+ throw salticidae::SalticidaeError("got terminal signal");
+}
+
int main() {
/* test two nodes */
MyNet alice(ec, "Alice", bob_addr, 10);
@@ -150,9 +155,18 @@ int main() {
salticidae::EventContext ec;
MyNet bob(ec, "Bob", alice_addr);
bob.connect(alice_addr);
- ec.dispatch();
+ try {
+ ec.dispatch();
+ } catch (std::exception &) {}
});
-
- ec.dispatch();
+ signal(SIGTERM, signal_handler);
+ signal(SIGINT, signal_handler);
+ try {
+ ec.dispatch();
+ } catch (std::exception &e) {
+ pthread_kill(bob_thread.native_handle(), SIGINT);
+ bob_thread.join();
+ SALTICIDAE_LOG_INFO("exception: %s", e.what());
+ }
return 0;
}