diff options
author | Determinant <[email protected]> | 2018-07-19 14:43:15 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2018-07-19 14:43:15 -0400 |
commit | b4bf23c07601560d708fbdd7c20aca20b630c983 (patch) | |
tree | 021e31927feed92dc61df6b51c3e840471715650 /src | |
parent | a75778995a4e0742f244670e9cc02a56611ccfe0 (diff) |
clean up code; fix mem leak in util
Diffstat (limited to 'src')
-rw-r--r-- | src/conn.cpp | 24 | ||||
-rw-r--r-- | src/util.cpp | 5 |
2 files changed, 14 insertions, 15 deletions
diff --git a/src/conn.cpp b/src/conn.cpp index 052f2ad..e600ec9 100644 --- a/src/conn.cpp +++ b/src/conn.cpp @@ -36,10 +36,12 @@ namespace salticidae { ConnPool::Conn::operator std::string() const { - return "<Conn fd=" + std::to_string(fd) + " " + - "addr=" + std::string(addr).c_str() + " " + - "mode=" + ((mode == Conn::ACTIVE) ? "active" : "passive") + - ">"; + DataStream s; + s << "<Conn " + << "fd=" << std::to_string(fd) << " " + << "addr=" << std::string(addr) << " " + << "mode=" << ((mode == Conn::ACTIVE) ? "active" : "passive") << ">"; + return std::move(s); } void ConnPool::Conn::send_data(evutil_socket_t fd, short events) { @@ -150,7 +152,7 @@ void ConnPool::accept_client(evutil_socket_t fd, short) { conn->ev_write.add(); conn->ready_send = false; add_conn(conn); - SALTICIDAE_LOG_INFO("created connection %s", std::string(*conn).c_str()); + SALTICIDAE_LOG_INFO("created %s", std::string(*conn).c_str()); conn->on_setup(); } ev_listen.add(); @@ -222,7 +224,7 @@ void ConnPool::Conn::terminate() { } } -void ConnPool::Conn::try_conn(evutil_socket_t, short) { +void ConnPool::Conn::try_conn() { auto conn = self(); /* pin the connection */ struct sockaddr_in sockin; memset(&sockin, 0, sizeof(struct sockaddr_in)); @@ -230,7 +232,7 @@ void ConnPool::Conn::try_conn(evutil_socket_t, short) { sockin.sin_addr.s_addr = addr.ip; sockin.sin_port = addr.port; - if (connect(fd, (struct sockaddr *)&sockin, + if (::connect(fd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) < 0 && errno != EINPROGRESS) { SALTICIDAE_LOG_INFO("cannot connect to %s", std::string(addr).c_str()); @@ -242,7 +244,7 @@ void ConnPool::Conn::try_conn(evutil_socket_t, short) { ev_connect.add_with_timeout(cpool->conn_server_timeout); } -ConnPool::conn_t ConnPool::create_conn(const NetAddr &addr) { +ConnPool::conn_t ConnPool::connect(const NetAddr &addr) { int fd; int one = 1; if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) @@ -253,16 +255,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->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_conn_timeout()); + conn->try_conn(); add_conn(conn); - SALTICIDAE_LOG_INFO("created connection %s", std::string(*conn).c_str()); + SALTICIDAE_LOG_INFO("created %s", std::string(*conn).c_str()); return conn; } diff --git a/src/util.cpp b/src/util.cpp index 7125598..7ef01a9 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -220,8 +220,7 @@ size_t Config::parse(int argc, char **argv) { SALTICIDAE_LOG_INFO("loaded configuration from %s", conf_fname.c_str()); size_t nopts = opts.size(); - struct option *longopts = (struct option *)malloc( - sizeof(struct option) * (nopts + 1)); + auto longopts = BoxObj<struct option[]>(new struct option[nopts + 1]); int ind; std::string shortopts; for (size_t i = 0; i < nopts; i++) @@ -238,7 +237,7 @@ size_t Config::parse(int argc, char **argv) { longopts[nopts] = {0, 0, 0, 0}; for (;;) { - int id = getopt_long(argc, argv, shortopts.c_str(), longopts, &ind); + int id = getopt_long(argc, argv, shortopts.c_str(), longopts.get(), &ind); if (id == -1) break; if (id == '?') |