aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt23
-rw-r--r--include/salticidae/conn.h33
-rw-r--r--include/salticidae/util.h2
-rw-r--r--src/conn.cpp24
-rw-r--r--src/util.cpp4
-rw-r--r--test/CMakeLists.txt4
6 files changed, 57 insertions, 33 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b358842..cc6ce86 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -30,9 +30,20 @@ find_package(OpenSSL REQUIRED)
include_directories(include)
add_library(salticidae
+ OBJECT
src/util.cpp
src/conn.cpp)
-target_link_libraries(salticidae event crypto)
+
+option(BUILD_SHARED "build shared library." OFF)
+if(BUILD_SHARED)
+ set_property(TARGET salticidae PROPERTY POSITION_INDEPENDENT_CODE 1)
+ add_library(salticidae_shared SHARED $<TARGET_OBJECTS:salticidae>)
+ set_target_properties(salticidae_shared PROPERTIES OUTPUT_NAME "salticidae")
+ target_link_libraries(salticidae_shared event crypto)
+endif()
+add_library(salticidae_static STATIC $<TARGET_OBJECTS:salticidae>)
+set_target_properties(salticidae_static PROPERTIES OUTPUT_NAME "salticidae")
+target_link_libraries(salticidae_static event crypto)
add_subdirectory(test)
@@ -46,11 +57,5 @@ elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
add_definitions(-DSALTICIDAE_NORMAL_LOG)
endif()
-#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -no-pie -pg")
-set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -W -Wall -Wextra -pedantic")
-
-macro(remove_cxx_flag flag)
- string(REPLACE "${flag}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
-endmacro()
-
-remove_cxx_flag("-DNDEBUG")
+set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -W -Wall -Wextra -pedantic -Wsuggest-override")
+set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -W -Wall -Wextra -pedantic -Wsuggest-override")
diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h
index f91e30e..4db5f40 100644
--- a/include/salticidae/conn.h
+++ b/include/salticidae/conn.h
@@ -44,18 +44,8 @@
#include "salticidae/netaddr.h"
#include "salticidae/msg.h"
-const int MAX_LISTEN_BACKLOG = 10;
-const size_t BUFF_SEG_SIZE = 4096;
-const size_t MAX_MSG_HANDLER = 64;
-const double TRY_CONN_DELAY = 2;
-const double CONN_SERVER_TIMEOUT = 2;
-
namespace salticidae {
-inline double gen_rand_timeout(double base_timeout) {
- return base_timeout + rand() / (double)RAND_MAX * 0.5 * base_timeout;
-}
-
class RingBuffer {
struct buffer_entry_t {
bytearray_t data;
@@ -163,6 +153,7 @@ class ConnPool {
};
private:
+ size_t seg_buff_size;
conn_t self_ref;
int fd;
ConnPool *cpool;
@@ -197,6 +188,7 @@ class ConnPool {
const NetAddr &get_addr() const { return addr; }
ConnMode get_mode() const { return mode; }
RingBuffer &read() { return recv_buffer; }
+ void set_seg_buff_size(size_t size) { seg_buff_size = size; }
void write(bytearray_t &&data) {
send_buffer.push(std::move(data));
@@ -226,6 +218,10 @@ class ConnPool {
};
private:
+ int max_listen_backlog;
+ double try_conn_delay;
+ double conn_server_timeout;
+ size_t seg_buff_size;
std::unordered_map<int, conn_t> pool;
int listen_fd;
Event ev_listen;
@@ -236,10 +232,22 @@ class ConnPool {
protected:
EventContext eb;
virtual conn_t create_conn() = 0;
+ virtual double gen_conn_timeout() {
+ return gen_rand_timeout(try_conn_delay);
+ }
public:
friend Conn;
- ConnPool(const EventContext &eb): eb(eb) {}
+ ConnPool(const EventContext &eb,
+ int max_listen_backlog = 10,
+ double try_conn_delay = 2,
+ double conn_server_timeout = 2,
+ size_t seg_buff_size = 4096):
+ max_listen_backlog(max_listen_backlog),
+ try_conn_delay(try_conn_delay),
+ conn_server_timeout(conn_server_timeout),
+ seg_buff_size(seg_buff_size),
+ eb(eb) {}
~ConnPool() {
for (auto it: pool)
@@ -249,6 +257,9 @@ class ConnPool {
}
}
+ ConnPool(const ConnPool &) = delete;
+ ConnPool(ConnPool &&) = delete;
+
/** create an active mode connection to addr */
conn_t create_conn(const NetAddr &addr);
/** setup and start listening */
diff --git a/include/salticidae/util.h b/include/salticidae/util.h
index d0fd984..505f014 100644
--- a/include/salticidae/util.h
+++ b/include/salticidae/util.h
@@ -40,6 +40,8 @@ namespace salticidae {
void sec2tv(double t, struct timeval &tv);
void event_add_with_timeout(struct event *ev, double timeout);
+double gen_rand_timeout(double base_timeout, double alpha = 0.5);
+
std::string trim(const std::string &s,
const std::string &space = "\t\r\n ");
std::vector<std::string> split(const std::string &s, const std::string &delim);
diff --git a/src/conn.cpp b/src/conn.cpp
index 0b42853..62cba8a 100644
--- a/src/conn.cpp
+++ b/src/conn.cpp
@@ -45,12 +45,12 @@ ConnPool::Conn::operator std::string() const {
void ConnPool::Conn::send_data(evutil_socket_t fd, short events) {
if (!(events & EV_WRITE)) return;
auto conn = self(); /* pin the connection */
- ssize_t ret = BUFF_SEG_SIZE;
- while (ret == BUFF_SEG_SIZE)
+ ssize_t ret = seg_buff_size;
+ while (ret == (ssize_t)seg_buff_size)
{
if (!send_buffer.size()) /* nothing to write */
break;
- bytearray_t buff_seg = send_buffer.pop(BUFF_SEG_SIZE);
+ bytearray_t buff_seg = send_buffer.pop(seg_buff_size);
ssize_t size = buff_seg.size();
ret = send(fd, buff_seg.data(), size, MSG_NOSIGNAL);
SALTICIDAE_LOG_DEBUG("socket sent %d bytes", ret);
@@ -88,12 +88,12 @@ void ConnPool::Conn::send_data(evutil_socket_t fd, short events) {
void ConnPool::Conn::recv_data(evutil_socket_t fd, short events) {
if (!(events & EV_READ)) return;
auto conn = self(); /* pin the connection */
- ssize_t ret = BUFF_SEG_SIZE;
- while (ret == BUFF_SEG_SIZE)
+ ssize_t ret = seg_buff_size;
+ while (ret == (ssize_t)seg_buff_size)
{
bytearray_t buff_seg;
- buff_seg.resize(BUFF_SEG_SIZE);
- ret = recv(fd, buff_seg.data(), BUFF_SEG_SIZE, 0);
+ buff_seg.resize(seg_buff_size);
+ ret = recv(fd, buff_seg.data(), seg_buff_size, 0);
SALTICIDAE_LOG_DEBUG("socket read %d bytes", ret);
if (ret <= 0)
{
@@ -137,6 +137,7 @@ void ConnPool::accept_client(evutil_socket_t fd, short) {
NetAddr addr((struct sockaddr_in *)&client_addr);
conn_t conn = create_conn();
Conn *conn_ptr = conn.get();
+ conn->seg_buff_size = seg_buff_size;
conn->fd = client_fd;
conn->cpool = this;
conn->mode = Conn::PASSIVE;
@@ -197,7 +198,7 @@ void ConnPool::init(NetAddr listen_addr) {
if (bind(listen_fd, (struct sockaddr *)&sockin, sizeof(sockin)) < 0)
throw ConnPoolError(std::string("binding error"));
- if (::listen(listen_fd, MAX_LISTEN_BACKLOG) < 0)
+ if (::listen(listen_fd, max_listen_backlog) < 0)
throw ConnPoolError(std::string("listen error"));
ev_listen = Event(eb, listen_fd, EV_READ,
std::bind(&ConnPool::accept_client, this, _1, _2));
@@ -238,7 +239,7 @@ void ConnPool::Conn::try_conn(evutil_socket_t, short) {
}
ev_connect = Event(cpool->eb, fd, EV_WRITE,
std::bind(&Conn::conn_server, this, _1, _2));
- ev_connect.add_with_timeout(CONN_SERVER_TIMEOUT);
+ ev_connect.add_with_timeout(cpool->conn_server_timeout);
}
ConnPool::conn_t ConnPool::create_conn(const NetAddr &addr) {
@@ -252,13 +253,14 @@ ConnPool::conn_t ConnPool::create_conn(const NetAddr &addr) {
if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1)
throw ConnPoolError(std::string("unable to set nonblocking socket"));
conn_t conn = create_conn();
- Conn * conn_ptr = conn.get();
+ Conn *conn_ptr = conn.get();
+ conn->seg_buff_size = seg_buff_size;
conn->fd = fd;
conn->cpool = this;
conn->mode = Conn::ACTIVE;
conn->addr = addr;
conn->ev_connect = Event(eb, -1, 0, std::bind(&Conn::try_conn, conn_ptr, _1, _2));
- conn->ev_connect.add_with_timeout(gen_rand_timeout(TRY_CONN_DELAY));
+ conn->ev_connect.add_with_timeout(gen_conn_timeout());
add_conn(conn);
SALTICIDAE_LOG_INFO("created connection %s", std::string(*conn).c_str());
return conn;
diff --git a/src/util.cpp b/src/util.cpp
index 2adb997..7125598 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -47,6 +47,10 @@ void event_add_with_timeout(struct event *ev, double timeout) {
event_add(ev, &tv);
}
+double gen_rand_timeout(double base_timeout, double alpha) {
+ return base_timeout + rand() / (double)RAND_MAX * alpha * base_timeout;
+}
+
const std::string get_current_datetime() {
/* credit: http://stackoverflow.com/a/41381479/544806 */
char fmt[64], buf[64];
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index bedaaa3..7af1f87 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -21,7 +21,7 @@
# SOFTWARE.
add_executable(test_msg test_msg.cpp)
-target_link_libraries(test_msg salticidae)
+target_link_libraries(test_msg salticidae_static)
add_executable(test_stream test_stream.cpp)
-target_link_libraries(test_stream salticidae)
+target_link_libraries(test_stream salticidae_static)