void take_fd(int fd) { ranger::util::scope_guard fd_guard([fd] { ranger::event::tcp_connection::file_descriptor_close(fd); }); ranger::event::tcp_connection conn(m_disp, fd); conn.set_event_handler([this] (ranger::event::tcp_connection& conn, ranger::event::tcp_connection::event_code what) { switch (what) { case ranger::event::tcp_connection::event_code::read: handle_read(conn, conn.read_buffer()); break; case ranger::event::tcp_connection::event_code::timeout: handle_timeout(conn); break; case ranger::event::tcp_connection::event_code::error: handle_error(conn); break; case ranger::event::tcp_connection::event_code::eof: handle_eof(conn); break; default: break; } }); fd_guard.dismiss(); auto remote_ep = conn.remote_endpoint(); std::cout << "thread[" << std::this_thread::get_id() << "] " << "accept connection[" << remote_ep << "]." << std::endl; m_conn_set.emplace(std::move(conn)); }
explicit echo_server(int port) : m_acc(m_disp, ranger::event::endpoint(port)) { m_acc.set_event_handler([this] (ranger::event::tcp_acceptor& acc, int fd) { ranger::event::tcp_connection conn(m_disp, fd); conn.set_event_handler([this] (ranger::event::tcp_connection& conn, ranger::event::tcp_connection::event_code what) { switch (what) { case ranger::event::tcp_connection::event_code::read: handle_read(conn, conn.read_buffer()); break; case ranger::event::tcp_connection::event_code::timeout: handle_timeout(conn); break; case ranger::event::tcp_connection::event_code::error: handle_error(conn); break; case ranger::event::tcp_connection::event_code::eof: handle_eof(conn); break; default: break; } }); auto local_ep = acc.local_endpoint(); auto remote_ep = conn.remote_endpoint(); std::cout << "acceptor[" << local_ep << "]" << " accept connection[" << remote_ep << "]." << std::endl; m_conn_set.emplace(std::move(conn)); return true; }); }
/* * Process read */ void ttread(RP rp, TTY tp, char far* bp, int nb) { USHORT lflag = tp->t_lflag; CQ qp = &tp->inq; int cnt = 0; int eof = 1; int c; /* read chars from queue */ while (nb != 0) { c = getc(qp,NOPEEK); if (c < 0) break; /* simplified ICANON processing */ if (ISSET(lflag,ICANON)) { if (CCEQ(tp->t_cc[VEOF],c)) { eof = 1; break; } } bp[cnt++] = (char)c; nb--; eof = 0; if (ISSET(lflag,ICANON) && CCEQ(tp->t_cc[VEOL],c)) break; } RWCNT(rp) = cnt; if (ISSET(lflag,ICANON) && eof) handle_eof(rp, tp, bp); return; }