diff --git a/contrib/backends/nntpchan-daemon/nntpd.cpp b/contrib/backends/nntpchan-daemon/nntpd.cpp index a338c24..3d8660e 100644 --- a/contrib/backends/nntpchan-daemon/nntpd.cpp +++ b/contrib/backends/nntpchan-daemon/nntpd.cpp @@ -16,20 +16,20 @@ int main(int argc, char * argv[]) { } nntpchan::Mainloop loop; - + nntpchan::NNTPServer nntp(loop); - + std::string fname(argv[1]); std::ifstream i(fname); if(i.is_open()) { INI::Parser conf(i); - + std::vector requiredSections = {"nntp", "storage"}; - + auto & level = conf.top(); - + for ( const auto & section : requiredSections ) { if(level.sections.find(section) == level.sections.end()) { std::cerr << "config file " << fname << " does not have required section: "; @@ -46,7 +46,7 @@ int main(int argc, char * argv[]) { } nntp.SetStoragePath(storeconf["path"]); - + auto & nntpconf = level.sections["nntp"].values; if (nntpconf.find("bind") == nntpconf.end()) { @@ -75,29 +75,26 @@ int main(int argc, char * argv[]) { } else { std::cerr << "unknown frontend type '" << ftype << "'" << std::endl; } - + } - + auto & a = nntpconf["bind"]; try { - nntp.Bind(a); + nntp.Bind(a); } catch ( std::exception & ex ) { std::cerr << "failed to bind: " << ex.what() << std::endl; return 1; } - try { - std::cerr << "run mainloop" << std::endl; - loop.Run(); - } catch ( std::exception & ex ) { - std::cerr << "exception in mainloop: " << ex.what() << std::endl; - return 2; - } - + + std::cerr << "run mainloop" << std::endl; + loop.Run(); + std::cerr << "stopped mainloop" << std::endl; + } else { std::cerr << "failed to open " << fname << std::endl; return 1; } - - + + } diff --git a/contrib/backends/nntpchan-daemon/src/event.cpp b/contrib/backends/nntpchan-daemon/src/event.cpp index b269816..40dca28 100644 --- a/contrib/backends/nntpchan-daemon/src/event.cpp +++ b/contrib/backends/nntpchan-daemon/src/event.cpp @@ -21,7 +21,7 @@ namespace nntpchan void Mainloop::Run(uv_run_mode mode) { - uv_run(m_loop, mode); + assert(uv_run(m_loop, mode) == 0); } - + } diff --git a/contrib/backends/nntpchan-daemon/src/line.cpp b/contrib/backends/nntpchan-daemon/src/line.cpp index ea87bd9..9ccd3fb 100644 --- a/contrib/backends/nntpchan-daemon/src/line.cpp +++ b/contrib/backends/nntpchan-daemon/src/line.cpp @@ -5,7 +5,7 @@ namespace nntpchan { LineReader::LineReader(size_t limit) : m_close(false), lineLimit(limit) {} - void LineReader::OnData(const char * d, ssize_t l) + void LineReader::Data(const char * d, ssize_t l) { if(l <= 0) return; // process leftovers @@ -32,6 +32,8 @@ namespace nntpchan { // leftovers m_leftovers = std::string(data, begin-idx); } + else + m_leftovers = ""; } void LineReader::OnLine(const char *d, const size_t l) diff --git a/contrib/backends/nntpchan-daemon/src/line.hpp b/contrib/backends/nntpchan-daemon/src/line.hpp index 825deac..4ce978d 100644 --- a/contrib/backends/nntpchan-daemon/src/line.hpp +++ b/contrib/backends/nntpchan-daemon/src/line.hpp @@ -13,7 +13,7 @@ namespace nntpchan LineReader(size_t lineLimit); /** @brief queue inbound data from connection */ - virtual void OnData(const char * data, ssize_t s); + void Data(const char * data, ssize_t s); /** implements IConnHandler */ virtual bool ShouldClose(); diff --git a/contrib/backends/nntpchan-daemon/src/nntp_auth.cpp b/contrib/backends/nntpchan-daemon/src/nntp_auth.cpp index 1f9f6d7..fa09e57 100644 --- a/contrib/backends/nntpchan-daemon/src/nntp_auth.cpp +++ b/contrib/backends/nntpchan-daemon/src/nntp_auth.cpp @@ -21,7 +21,7 @@ namespace nntpchan char * buff = new char[l]; // read file m_instream->read(buff, l); - OnData(buff, l); + Data(buff, l); delete [] buff; return m_found; } diff --git a/contrib/backends/nntpchan-daemon/src/nntp_handler.cpp b/contrib/backends/nntpchan-daemon/src/nntp_handler.cpp index d49a7a1..54651d6 100644 --- a/contrib/backends/nntpchan-daemon/src/nntp_handler.cpp +++ b/contrib/backends/nntpchan-daemon/src/nntp_handler.cpp @@ -21,11 +21,6 @@ namespace nntpchan if(m_auth) delete m_auth; } - void NNTPServerHandler::OnData(const char * data, ssize_t sz) - { - LineReader::OnData(data, sz); - } - void NNTPServerHandler::HandleLine(const std::string &line) { if(m_state == eStateReadCommand) { @@ -42,6 +37,11 @@ namespace nntpchan } } + void NNTPServerHandler::OnData(const char * data, ssize_t l) + { + Data(data, l); + } + void NNTPServerHandler::HandleCommand(const std::deque & command) { auto cmd = command[0]; diff --git a/contrib/backends/nntpchan-daemon/src/nntp_handler.hpp b/contrib/backends/nntpchan-daemon/src/nntp_handler.hpp index edfa68e..8d9a649 100644 --- a/contrib/backends/nntpchan-daemon/src/nntp_handler.hpp +++ b/contrib/backends/nntpchan-daemon/src/nntp_handler.hpp @@ -16,10 +16,10 @@ namespace nntpchan virtual bool ShouldClose(); - virtual void OnData(const char * data, ssize_t sz); - void SetAuth(NNTPCredentialDB * creds); + virtual void OnData(const char *, ssize_t); + void Greet(); protected: diff --git a/contrib/backends/nntpchan-daemon/src/nntp_server.cpp b/contrib/backends/nntpchan-daemon/src/nntp_server.cpp index 86e4d1e..b191115 100644 --- a/contrib/backends/nntpchan-daemon/src/nntp_server.cpp +++ b/contrib/backends/nntpchan-daemon/src/nntp_server.cpp @@ -58,7 +58,7 @@ namespace nntpchan void NNTPServerConn::SendNextReply() { IConnHandler * handler = GetHandler(); - if(handler->HasNextLine()) { + while(handler->HasNextLine()) { auto line = handler->GetNextLine(); SendString(line + "\n"); } diff --git a/contrib/backends/nntpchan-daemon/src/server.cpp b/contrib/backends/nntpchan-daemon/src/server.cpp index 0c55d5f..9570bc6 100644 --- a/contrib/backends/nntpchan-daemon/src/server.cpp +++ b/contrib/backends/nntpchan-daemon/src/server.cpp @@ -15,6 +15,7 @@ namespace nntpchan void Server::Close() { + std::cout << "Close server" << std::endl; uv_close((uv_handle_t*)&m_server, [](uv_handle_t * s) { Server * self = (Server*)s->data; if (self) delete self; @@ -39,8 +40,10 @@ namespace nntpchan OnAcceptError(status); return; } + std::cout << "new conn" << std::endl; IServerConn * conn = CreateConn(s); assert(conn); + m_conns.push_back(conn); conn->Greet(); } @@ -73,15 +76,14 @@ namespace nntpchan return line; } - IServerConn::IServerConn(uv_loop_t * l, uv_stream_t * s, Server * parent, IConnHandler * h) + IServerConn::IServerConn(uv_loop_t * l, uv_stream_t * st, Server * parent, IConnHandler * h) { m_loop = l; - m_stream = s; m_parent = parent; m_handler = h; uv_tcp_init(l, &m_conn); m_conn.data = this; - uv_accept(s, (uv_stream_t*) &m_conn); + uv_accept(st, (uv_stream_t*) &m_conn); uv_read_start((uv_stream_t*) &m_conn, [] (uv_handle_t * h, size_t s, uv_buf_t * b) { IServerConn * self = (IServerConn*) h->data; if(self == nullptr) return; @@ -94,6 +96,7 @@ namespace nntpchan IServerConn * self = (IServerConn*) s->data; if(self == nullptr) return; if(nread > 0) { + std::cout << "read " << nread << std::endl; self->m_handler->OnData(b->base, nread); self->SendNextReply(); if(self->m_handler->ShouldClose()) @@ -118,7 +121,7 @@ namespace nntpchan void IServerConn::SendString(const std::string & str) { WriteBuffer * b = new WriteBuffer(str); - uv_write(&b->w, *this, &b->b, 1, [](uv_write_t * w, int status) { + uv_write(&b->w, (uv_stream_t*)&m_conn, &b->b, 1, [](uv_write_t * w, int status) { (void) status; WriteBuffer * wb = (WriteBuffer *) w->data; if(wb) diff --git a/contrib/backends/nntpchan-daemon/src/server.hpp b/contrib/backends/nntpchan-daemon/src/server.hpp index 8bb3c1b..34e20ef 100644 --- a/contrib/backends/nntpchan-daemon/src/server.hpp +++ b/contrib/backends/nntpchan-daemon/src/server.hpp @@ -50,11 +50,9 @@ namespace nntpchan void SendString(const std::string & str); Server * Parent() { return m_parent; }; IConnHandler * GetHandler() { return m_handler; }; - operator uv_stream_t * () { return m_stream; }; uv_loop_t * GetLoop() { return m_loop; }; private: uv_tcp_t m_conn; - uv_stream_t * m_stream; uv_loop_t * m_loop; Server * m_parent; IConnHandler * m_handler;