make it work
This commit is contained in:
parent
dba185c6aa
commit
19d75eb917
@ -86,13 +86,10 @@ int main(int argc, char * argv[]) {
|
||||
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 << "stopped mainloop" << std::endl;
|
||||
|
||||
} else {
|
||||
std::cerr << "failed to open " << fname << std::endl;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<std::string> & command)
|
||||
{
|
||||
auto cmd = command[0];
|
||||
|
@ -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:
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user